mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-30 16:23:31 +00:00
fix(openai): filter account test models by model mapping
This commit is contained in:
@@ -72,6 +72,37 @@ func TestFetchOpenAIAccountModelsAPIKeyPopulatesPickerFields(t *testing.T) {
|
||||
require.Equal(t, "named-model", models[2].ID)
|
||||
}
|
||||
|
||||
func TestFetchOpenAIAccountModelsAPIKeyAppliesAccountModelMapping(t *testing.T) {
|
||||
gateway := newCodexModelsAPIKeyTestService(&codexModelsHTTPUpstreamStub{do: func(_ *http.Request, _ string, _ int64, _ int) (*http.Response, error) {
|
||||
return ordinaryModelsUpstreamResponse(`{"data":[
|
||||
{"id":"upstream-target","owned_by":"provider","created":123},
|
||||
{"id":"direct-model","owned_by":"provider"},
|
||||
{"id":"unconfigured-model","owned_by":"provider"}
|
||||
]}`), nil
|
||||
}})
|
||||
svc := &AccountTestService{openaiGatewayService: gateway}
|
||||
account := newCodexModelsAPIKeyTestAccount("https://models.example/v1")
|
||||
account.Credentials["model_mapping"] = map[string]any{
|
||||
"configured-alias": "upstream-target",
|
||||
"direct-model": "direct-model",
|
||||
}
|
||||
|
||||
models, err := svc.FetchOpenAIAccountModels(context.Background(), account)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, models, 2)
|
||||
ids := []string{models[0].ID, models[1].ID}
|
||||
require.ElementsMatch(t, []string{"configured-alias", "direct-model"}, ids)
|
||||
require.NotContains(t, ids, "unconfigured-model")
|
||||
alias := models[0]
|
||||
if alias.ID != "configured-alias" {
|
||||
alias = models[1]
|
||||
}
|
||||
require.Equal(t, "configured-alias", alias.ID)
|
||||
require.Equal(t, "configured-alias", alias.DisplayName)
|
||||
require.Equal(t, "provider", alias.OwnedBy)
|
||||
require.EqualValues(t, 123, alias.Created)
|
||||
}
|
||||
|
||||
func TestFetchOpenAIAccountModelsPreservesEmptyCatalog(t *testing.T) {
|
||||
gateway := newCodexModelsAPIKeyTestService(&codexModelsHTTPUpstreamStub{do: func(_ *http.Request, _ string, _ int64, _ int) (*http.Response, error) {
|
||||
return ordinaryModelsUpstreamResponse(`{"data":[]}`), nil
|
||||
|
||||
@@ -188,10 +188,17 @@ func (s *AccountTestService) FetchOpenAIAccountModels(ctx context.Context, accou
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// The shared discovery response is the raw upstream catalog. Project it
|
||||
// through the account mapping before exposing it in the admin picker so
|
||||
// configured aliases remain public names and unconfigured models stay out.
|
||||
projectedBody, err := projectAccountModelsBody(response.Body, account, nil, false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("project OpenAI account models: %w", err)
|
||||
}
|
||||
var payload struct {
|
||||
Data []openai.Model `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(response.Body, &payload); err != nil {
|
||||
if err := json.Unmarshal(projectedBody, &payload); err != nil {
|
||||
return nil, fmt.Errorf("decode OpenAI account models: %w", err)
|
||||
}
|
||||
// Every entry in the picker is labelled by the same rule: the upstream display
|
||||
|
||||
Reference in New Issue
Block a user