mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
Root cause: Codex /models requests always fetch the manifest from a scheduled upstream. Custom API-key upstreams that do not implement /v1/models return 5xx, an empty catalog, or a non-standard body, so the gateway exhausts account failover and returns 502 even when the group can describe its models locally. Change: generate a local Codex manifest when every candidate fails (or the account switch budget is exhausted) and when an upstream returns an empty manifest. Sources follow the group's models_list_config, then the group accounts' model_mapping keys, then the default OpenAI catalog, with the same retired-model filter as /v1/models. The local list only replaces the upstream error when it is non-empty, keeps ETag/304 revalidation, and leaves successful upstream manifests untouched. Pinned discovery mode keeps its explicit FallbackToScheduler contract. Tests: cover 5xx/empty/non-JSON upstream fallback, all-candidates-failed fallback, source priority, upstream success regression, ETag revalidation, and preserving the original error when the local list is empty.
120 lines
4.6 KiB
Go
120 lines
4.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCodexModelsFallsBackToLocalManifestWhenUpstreamCannotServeModels(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
firstStatus int
|
|
firstBody string
|
|
}{
|
|
{name: "upstream 5xx", firstStatus: http.StatusServiceUnavailable},
|
|
{name: "empty manifest", firstStatus: http.StatusOK, firstBody: `{"models":[]}`},
|
|
{name: "non JSON body", firstStatus: http.StatusOK, firstBody: `not-json`},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
handler, upstream, groupID := newCodexModelsFailoverTestHandlerWithAccountCount(tc.firstStatus, 1, 3)
|
|
upstream.firstBody = tc.firstBody
|
|
|
|
recorder := performCodexModelsRequest(t, handler, groupID)
|
|
|
|
require.Equal(t, http.StatusOK, recorder.Code, recorder.Body.String())
|
|
require.NotEmpty(t, recorder.Header().Get("ETag"))
|
|
slugs := codexHandlerManifestSlugs(t, recorder)
|
|
require.Contains(t, slugs, "gpt-5.6-sol")
|
|
require.NotContains(t, slugs, "gpt-image-2")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCodexModelsFallsBackToLocalManifestAfterAllCandidatesFail(t *testing.T) {
|
|
handler, upstream, groupID := newCodexModelsFailoverTestHandler(http.StatusServiceUnavailable)
|
|
upstream.statuses = map[int64]int{
|
|
1: http.StatusServiceUnavailable,
|
|
2: http.StatusServiceUnavailable,
|
|
}
|
|
|
|
recorder := performCodexModelsRequest(t, handler, groupID)
|
|
|
|
require.Equal(t, []int64{1, 2}, upstream.calls())
|
|
require.Equal(t, http.StatusOK, recorder.Code, recorder.Body.String())
|
|
require.Contains(t, codexHandlerManifestSlugs(t, recorder), "gpt-5.6-sol")
|
|
}
|
|
|
|
func TestCodexModelsFallbackPrefersGroupModelsListConfig(t *testing.T) {
|
|
handler, upstream, groupID := newCodexModelsFailoverTestHandler(http.StatusServiceUnavailable)
|
|
upstream.statuses = map[int64]int{
|
|
1: http.StatusServiceUnavailable,
|
|
2: http.StatusServiceUnavailable,
|
|
}
|
|
group := &service.Group{ID: groupID, Platform: service.PlatformOpenAI}
|
|
group.ModelAllowlist = service.GroupModelAllowlist{
|
|
Enabled: true,
|
|
Models: []string{"gpt-5.6-luna", "gpt-6.2-sol"},
|
|
}
|
|
|
|
recorder := performCodexModelsRequestForGroup(t, handler, group, "")
|
|
|
|
require.Equal(t, http.StatusOK, recorder.Code, recorder.Body.String())
|
|
require.ElementsMatch(t, []string{"gpt-5.6-luna", "gpt-6.2-sol"}, codexHandlerManifestSlugs(t, recorder))
|
|
}
|
|
|
|
func TestCodexModelsUpstreamManifestWinsOverLocalFallback(t *testing.T) {
|
|
handler, upstream, groupID := newCodexModelsFailoverTestHandlerWithAccountCount(http.StatusOK, 1, 3)
|
|
upstream.firstBody = `{"models":[{"slug":"upstream-only-model"}]}`
|
|
|
|
recorder := performCodexModelsRequest(t, handler, groupID)
|
|
|
|
require.Equal(t, http.StatusOK, recorder.Code, recorder.Body.String())
|
|
require.Equal(t, []string{"upstream-only-model"}, codexHandlerManifestSlugs(t, recorder))
|
|
}
|
|
|
|
func TestCodexModelsFallbackSupportsETagRevalidation(t *testing.T) {
|
|
handler, upstream, groupID := newCodexModelsFailoverTestHandlerWithAccountCount(http.StatusServiceUnavailable, 1, 3)
|
|
group := &service.Group{ID: groupID, Platform: service.PlatformOpenAI}
|
|
|
|
first := performCodexModelsRequestForGroup(t, handler, group, "")
|
|
require.Equal(t, http.StatusOK, first.Code, first.Body.String())
|
|
etag := first.Header().Get("ETag")
|
|
require.NotEmpty(t, etag)
|
|
require.NotEmpty(t, upstream.calls())
|
|
|
|
second := performCodexModelsRequestForGroup(t, handler, group, etag)
|
|
require.Equal(t, http.StatusNotModified, second.Code, second.Body.String())
|
|
require.Empty(t, second.Body.Bytes())
|
|
}
|
|
|
|
func TestCodexModelsKeepsUpstreamErrorWhenLocalFallbackIsEmpty(t *testing.T) {
|
|
handler, upstream, groupID := newCodexModelsFailoverTestHandlerWithAccountCount(http.StatusServiceUnavailable, 1, 3)
|
|
group := &service.Group{ID: groupID, Platform: service.PlatformOpenAI}
|
|
// Dedicated image models are dropped from Codex manifests, so the local
|
|
// list is empty and the original upstream error must be preserved.
|
|
group.ModelAllowlist = service.GroupModelAllowlist{Enabled: true, Models: []string{"gpt-image-2"}}
|
|
|
|
recorder := performCodexModelsRequestForGroup(t, handler, group, "")
|
|
|
|
require.Equal(t, http.StatusBadGateway, recorder.Code, recorder.Body.String())
|
|
require.NotEmpty(t, upstream.calls())
|
|
}
|
|
|
|
// codexModelsGroupWithoutLocalFallback configures a model list that Codex
|
|
// cannot advertise (image-generation only). The #5252 local fallback therefore
|
|
// stays empty and tests can still observe the original upstream error.
|
|
func codexModelsGroupWithoutLocalFallback(groupID int64) *service.Group {
|
|
return &service.Group{
|
|
ID: groupID,
|
|
Platform: service.PlatformOpenAI,
|
|
ModelAllowlist: service.GroupModelAllowlist{
|
|
Enabled: true,
|
|
Models: []string{"gpt-image-2"},
|
|
},
|
|
}
|
|
}
|