Files
sub2api/backend/internal/service/openai_codex_models_fallback_test.go
shaw b55a546fa5 fix(openai): expand wildcard-only fallback allowlists against servable models
Use the configured-manifest candidate set (mapping keys, plus the default
catalog only when an account serves unmapped models) and tag the fallback
log with the group.
2026-12-03 16:01:17 +08:00

219 lines
7.6 KiB
Go

package service
import (
"context"
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
type codexFallbackAccountRepo struct {
AccountRepository
accounts []Account
}
func (r codexFallbackAccountRepo) ListSchedulableByGroupID(context.Context, int64) ([]Account, error) {
return append([]Account(nil), r.accounts...), nil
}
func (r codexFallbackAccountRepo) ListModelAvailabilityCandidates(context.Context, *int64, []string, bool) ([]Account, error) {
return append([]Account(nil), r.accounts...), nil
}
func newCodexFallbackTestService(accounts []Account) *OpenAIGatewayService {
return NewOpenAIGatewayService(
codexFallbackAccountRepo{accounts: accounts},
nil, nil, nil, nil, nil, nil,
nil, // cfg
nil, nil, nil, nil, nil,
nil, // httpUpstream
nil, nil, nil, nil, nil, nil, nil, nil,
)
}
func codexFallbackManifestSlugs(t *testing.T, manifest *OpenAIModelsResponse) []string {
t.Helper()
require.NotNil(t, manifest)
var envelope struct {
Models []struct {
Slug string `json:"slug"`
} `json:"models"`
}
require.NoError(t, json.Unmarshal(manifest.Body, &envelope))
slugs := make([]string, 0, len(envelope.Models))
for _, model := range envelope.Models {
slugs = append(slugs, model.Slug)
}
return slugs
}
func newCodexFallbackAPIKeyAccount(mapping map[string]any) Account {
credentials := map[string]any{
"api_key": "sk-fallback",
"base_url": "https://upstream.example/v1",
}
if len(mapping) > 0 {
credentials["model_mapping"] = mapping
}
return Account{
ID: 1,
Name: "fallback-upstream",
Platform: PlatformOpenAI,
Type: AccountTypeAPIKey,
Status: StatusActive,
Schedulable: true,
Concurrency: 1,
Credentials: credentials,
}
}
func TestBuildLocalCodexModelsFallbackManifestPrefersGroupModelsListConfig(t *testing.T) {
accounts := []Account{
newCodexFallbackAPIKeyAccount(map[string]any{"mapped-alias": "upstream-model"}),
}
group := &Group{ID: 7, Platform: PlatformOpenAI}
group.ModelAllowlist = GroupModelAllowlist{
Enabled: true,
Models: []string{"gpt-5.6-luna", "*", "gpt-6.2-sol", "gpt-5.6-luna"},
}
manifest, configured, err := newCodexFallbackTestService(accounts).
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
require.NoError(t, err)
require.True(t, configured)
require.Equal(t, []string{"gpt-5.6-luna", "gpt-6.2-sol"}, codexFallbackManifestSlugs(t, manifest))
}
func TestBuildLocalCodexModelsFallbackManifestWildcardOnlyAllowlistStaysWithinAllowlist(t *testing.T) {
accounts := []Account{
newCodexFallbackAPIKeyAccount(map[string]any{"gpt-5.5-custom": "upstream-model", "other-alias": "upstream-model"}),
}
group := &Group{ID: 7, Platform: PlatformOpenAI}
group.ModelAllowlist = GroupModelAllowlist{Enabled: true, Models: []string{"gpt-5.5*"}}
manifest, configured, err := newCodexFallbackTestService(accounts).
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
require.NoError(t, err)
require.True(t, configured)
// The only account is restricted to its mapping, so the default catalog is
// not a candidate: advertising gpt-5.5 would offer a model it cannot serve.
require.Equal(t, []string{"gpt-5.5-custom"}, codexFallbackManifestSlugs(t, manifest))
}
func TestBuildLocalCodexModelsFallbackManifestWildcardOnlyAllowlistExpandsDefaultCatalog(t *testing.T) {
accounts := []Account{newCodexFallbackAPIKeyAccount(nil)}
group := &Group{ID: 7, Platform: PlatformOpenAI}
group.ModelAllowlist = GroupModelAllowlist{Enabled: true, Models: []string{"gpt-5.5*"}}
manifest, configured, err := newCodexFallbackTestService(accounts).
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
require.NoError(t, err)
require.True(t, configured)
slugs := codexFallbackManifestSlugs(t, manifest)
require.Contains(t, slugs, "gpt-5.5")
for _, slug := range slugs {
require.True(t, group.ModelAllowlist.Allows(slug), "fallback must not advertise %q outside the allowlist", slug)
}
require.NotContains(t, slugs, "gpt-5.6-sol")
}
func TestBuildLocalCodexModelsFallbackManifestWildcardOnlyAllowlistWithoutMatchKeepsUpstreamError(t *testing.T) {
accounts := []Account{newCodexFallbackAPIKeyAccount(map[string]any{"alias-a": "target-a"})}
group := &Group{ID: 7, Platform: PlatformOpenAI}
group.ModelAllowlist = GroupModelAllowlist{Enabled: true, Models: []string{"no-such-family-*"}}
manifest, configured, err := newCodexFallbackTestService(accounts).
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
require.NoError(t, err)
require.False(t, configured)
require.Nil(t, manifest)
}
func TestBuildLocalCodexModelsFallbackManifestUsesAccountModelMapping(t *testing.T) {
accounts := []Account{
newCodexFallbackAPIKeyAccount(map[string]any{"alias-b": "target-b", "alias-a": "target-a"}),
}
group := &Group{ID: 7, Platform: PlatformOpenAI}
manifest, configured, err := newCodexFallbackTestService(accounts).
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
require.NoError(t, err)
require.True(t, configured)
require.Equal(t, []string{"alias-a", "alias-b"}, codexFallbackManifestSlugs(t, manifest))
}
func TestBuildLocalCodexModelsFallbackManifestUsesDefaultModels(t *testing.T) {
accounts := []Account{newCodexFallbackAPIKeyAccount(nil)}
group := &Group{ID: 7, Platform: PlatformOpenAI}
manifest, configured, err := newCodexFallbackTestService(accounts).
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
require.NoError(t, err)
require.True(t, configured)
slugs := codexFallbackManifestSlugs(t, manifest)
require.Contains(t, slugs, "gpt-5.6-sol")
// Dedicated media models cannot be advertised in a Codex manifest.
require.NotContains(t, slugs, "gpt-image-2")
}
func TestBuildLocalCodexModelsFallbackManifestFiltersRetiredModels(t *testing.T) {
accounts := []Account{{
ID: 1,
Name: "fallback-oauth",
Platform: PlatformOpenAI,
Type: AccountTypeOAuth,
Status: StatusActive,
Schedulable: true,
Concurrency: 1,
Credentials: map[string]any{"access_token": "token"},
}}
group := &Group{ID: 7, Platform: PlatformOpenAI}
manifest, configured, err := newCodexFallbackTestService(accounts).
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
require.NoError(t, err)
require.True(t, configured)
slugs := codexFallbackManifestSlugs(t, manifest)
require.Contains(t, slugs, "gpt-5.6-sol")
require.NotContains(t, slugs, "gpt-5.5")
}
func TestBuildLocalCodexModelsFallbackManifestEmptyLocalList(t *testing.T) {
accounts := []Account{newCodexFallbackAPIKeyAccount(nil)}
group := &Group{ID: 7, Platform: PlatformOpenAI}
group.ModelAllowlist = GroupModelAllowlist{Enabled: true, Models: []string{"gpt-image-2"}}
manifest, configured, err := newCodexFallbackTestService(accounts).
BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
require.NoError(t, err)
require.False(t, configured)
require.Nil(t, manifest)
}
func TestBuildLocalCodexModelsFallbackManifestSupportsETagRevalidation(t *testing.T) {
accounts := []Account{newCodexFallbackAPIKeyAccount(nil)}
group := &Group{ID: 7, Platform: PlatformOpenAI}
service := newCodexFallbackTestService(accounts)
first, configured, err := service.BuildLocalCodexModelsFallbackManifest(context.Background(), group, "")
require.NoError(t, err)
require.True(t, configured)
require.NotEmpty(t, first.ETag)
second, configured, err := service.BuildLocalCodexModelsFallbackManifest(context.Background(), group, first.ETag)
require.NoError(t, err)
require.True(t, configured)
require.True(t, second.NotModified)
require.Nil(t, second.Body)
require.Equal(t, first.ETag, second.ETag)
}