|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/clovapi/switcher/internal/profile" |
| 10 | + "github.com/clovapi/switcher/internal/provider" |
| 11 | +) |
| 12 | + |
| 13 | +func TestClaudeDesktopGatewayModelsListFormat(t *testing.T) { |
| 14 | + store := &profile.Store{ |
| 15 | + Version: profile.StoreVersion, |
| 16 | + List: []profile.Profile{{ |
| 17 | + Name: provider.CodexVendorName, |
| 18 | + Kind: "subscription", |
| 19 | + SubscriptionProviderID: provider.CodexProviderID, |
| 20 | + Models: []profile.Model{ |
| 21 | + {ID: "gpt-5.5", Model: "gpt-5.5"}, |
| 22 | + {ID: "gpt-5.4", Model: "gpt-5.4"}, |
| 23 | + }, |
| 24 | + }}, |
| 25 | + } |
| 26 | + s := NewServer(profile.ProxyConfig{Host: "127.0.0.1", Port: 27483}) |
| 27 | + s.ProfileLoader = func() (*profile.Store, error) { return store, nil } |
| 28 | + ts := httptest.NewServer(s.Server.Handler) |
| 29 | + defer ts.Close() |
| 30 | + |
| 31 | + req, err := http.NewRequest(http.MethodGet, ts.URL+"/codex/v1/models", nil) |
| 32 | + if err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + req.Header.Set("Authorization", "Bearer clovapi-local") |
| 36 | + resp, err := http.DefaultClient.Do(req) |
| 37 | + if err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + defer resp.Body.Close() |
| 41 | + if resp.StatusCode != http.StatusOK { |
| 42 | + t.Fatalf("status = %d", resp.StatusCode) |
| 43 | + } |
| 44 | + var body map[string]any |
| 45 | + if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + if body["has_more"] != false { |
| 49 | + t.Fatalf("has_more = %#v", body["has_more"]) |
| 50 | + } |
| 51 | + if body["first_id"] != "gpt-5.5" || body["last_id"] != "gpt-5.4" { |
| 52 | + t.Fatalf("first/last id = %#v / %#v", body["first_id"], body["last_id"]) |
| 53 | + } |
| 54 | + data, ok := body["data"].([]any) |
| 55 | + if !ok || len(data) != 2 { |
| 56 | + t.Fatalf("data = %#v", body["data"]) |
| 57 | + } |
| 58 | + item, _ := data[0].(map[string]any) |
| 59 | + if item["type"] != "model" || item["id"] != "gpt-5.5" { |
| 60 | + t.Fatalf("item = %#v", item) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestCodexModelsListWithoutDesktopBearerStaysOpenAIFormat(t *testing.T) { |
| 65 | + store := &profile.Store{ |
| 66 | + Version: profile.StoreVersion, |
| 67 | + List: []profile.Profile{{ |
| 68 | + Name: provider.CodexVendorName, |
| 69 | + Kind: "subscription", |
| 70 | + Models: []profile.Model{ |
| 71 | + {ID: "gpt-5.5", Model: "gpt-5.5"}, |
| 72 | + }, |
| 73 | + }}, |
| 74 | + } |
| 75 | + s := NewServer(profile.ProxyConfig{Host: "127.0.0.1", Port: 27483}) |
| 76 | + s.ProfileLoader = func() (*profile.Store, error) { return store, nil } |
| 77 | + ts := httptest.NewServer(s.Server.Handler) |
| 78 | + defer ts.Close() |
| 79 | + |
| 80 | + resp, err := http.Get(ts.URL + "/codex/v1/models") |
| 81 | + if err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + defer resp.Body.Close() |
| 85 | + var body map[string]any |
| 86 | + if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + if body["object"] != "list" { |
| 90 | + t.Fatalf("object = %#v", body["object"]) |
| 91 | + } |
| 92 | + if _, ok := body["has_more"]; ok { |
| 93 | + t.Fatalf("unexpected Claude Desktop fields: %#v", body) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestIsClaudeDesktopGatewayRequest(t *testing.T) { |
| 98 | + req, _ := http.NewRequest(http.MethodGet, "http://127.0.0.1:27483/codex/v1/models", nil) |
| 99 | + req.Header.Set("Authorization", "Bearer clovapi-local") |
| 100 | + if !isClaudeDesktopGatewayRequest(req) { |
| 101 | + t.Fatal("expected desktop gateway request") |
| 102 | + } |
| 103 | + req.Header.Set("Authorization", "Bearer sk-other") |
| 104 | + if isClaudeDesktopGatewayRequest(req) { |
| 105 | + t.Fatal("unexpected desktop gateway match") |
| 106 | + } |
| 107 | +} |
0 commit comments