package service import ( "context" "net/http" "net/http/httptest" "testing" ) func newCodexModelsTestAccount() *Account { return &Account{ ID: 1, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Credentials: map[string]any{ "access_token": "test-access-token", "chatgpt_account_id": "acc-123", }, } } func TestFetchCodexModelsManifestPassthrough(t *testing.T) { manifestBody := `{"models":[{"slug":"gpt-5.5","display_name":"GPT-5.5"}]}` var gotAuth, gotAccountID, gotOriginator, gotClientVersion string server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotAuth = r.Header.Get("Authorization") gotAccountID = r.Header.Get("chatgpt-account-id") gotOriginator = r.Header.Get("Originator") gotClientVersion = r.URL.Query().Get("client_version") w.Header().Set("ETag", `W/"abc123"`) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(manifestBody)) })) defer server.Close() original := chatgptCodexModelsURL chatgptCodexModelsURL = server.URL defer func() { chatgptCodexModelsURL = original }() s := &OpenAIGatewayService{} manifest, err := s.FetchCodexModelsManifest(context.Background(), newCodexModelsTestAccount(), "0.137.0", "") if err != nil { t.Fatalf("FetchCodexModelsManifest returned error: %v", err) } if string(manifest.Body) != manifestBody { t.Errorf("body not passed through verbatim: got %q", manifest.Body) } if manifest.ETag != `W/"abc123"` { t.Errorf("etag not passed through: got %q", manifest.ETag) } if gotAuth != "Bearer test-access-token" { t.Errorf("authorization header: got %q", gotAuth) } if gotAccountID != "acc-123" { t.Errorf("chatgpt-account-id header: got %q", gotAccountID) } if gotOriginator != "codex_cli_rs" { t.Errorf("originator header: got %q", gotOriginator) } if gotClientVersion != "0.137.0" { t.Errorf("client_version query: got %q", gotClientVersion) } } func TestFetchCodexModelsManifestDefaultClientVersion(t *testing.T) { var gotClientVersion string server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotClientVersion = r.URL.Query().Get("client_version") _, _ = w.Write([]byte(`{"models":[]}`)) })) defer server.Close() original := chatgptCodexModelsURL chatgptCodexModelsURL = server.URL defer func() { chatgptCodexModelsURL = original }() s := &OpenAIGatewayService{} if _, err := s.FetchCodexModelsManifest(context.Background(), newCodexModelsTestAccount(), "", ""); err != nil { t.Fatalf("FetchCodexModelsManifest returned error: %v", err) } if gotClientVersion != openAICodexProbeVersion { t.Errorf("default client_version: got %q, want %q", gotClientVersion, openAICodexProbeVersion) } } func TestFetchCodexModelsManifestNotModified(t *testing.T) { var gotIfNoneMatch string server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotIfNoneMatch = r.Header.Get("If-None-Match") w.Header().Set("ETag", `W/"abc123"`) w.WriteHeader(http.StatusNotModified) })) defer server.Close() original := chatgptCodexModelsURL chatgptCodexModelsURL = server.URL defer func() { chatgptCodexModelsURL = original }() s := &OpenAIGatewayService{} manifest, err := s.FetchCodexModelsManifest(context.Background(), newCodexModelsTestAccount(), "0.137.0", `W/"abc123"`) if err != nil { t.Fatalf("FetchCodexModelsManifest returned error: %v", err) } if !manifest.NotModified { t.Error("expected NotModified to be true") } if gotIfNoneMatch != `W/"abc123"` { t.Errorf("if-none-match header: got %q", gotIfNoneMatch) } } func TestFetchCodexModelsManifestUpstreamError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Error(w, `{"detail":"boom"}`, http.StatusInternalServerError) })) defer server.Close() original := chatgptCodexModelsURL chatgptCodexModelsURL = server.URL defer func() { chatgptCodexModelsURL = original }() s := &OpenAIGatewayService{} if _, err := s.FetchCodexModelsManifest(context.Background(), newCodexModelsTestAccount(), "0.137.0", ""); err == nil { t.Fatal("expected error for upstream 500, got nil") } } func TestFetchCodexModelsManifestMissingToken(t *testing.T) { account := newCodexModelsTestAccount() delete(account.Credentials, "access_token") s := &OpenAIGatewayService{} if _, err := s.FetchCodexModelsManifest(context.Background(), account, "0.137.0", ""); err == nil { t.Fatal("expected error for missing access token, got nil") } }