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"}, }, } }