1package provider
2
3import (
4 "context"
5 "io"
6 "net/http"
7 "net/http/httptest"
8 "testing"
9)
10
11func TestDiscoverOpenAICodex(t *testing.T) {
12 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13 if r.URL.Path != "/models" {
14 t.Errorf("path = %q, want /models", r.URL.Path)
15 }
16 if got := r.URL.Query().Get("client_version"); got != openaiCodexClientVersion {
17 t.Errorf("client_version = %q, want %q", got, openaiCodexClientVersion)
18 }
19 if got := r.Header.Get("authorization"); got != "Bearer token" {
20 t.Errorf("authorization = %q", got)
21 }
22 if got := r.Header.Get("chatgpt-account-id"); got != "account" {
23 t.Errorf("chatgpt-account-id = %q", got)
24 }
25 if got := r.Header.Get("originator"); got != "codex_cli_rs" {
26 t.Errorf("originator = %q", got)
27 }
28 _, _ = io.WriteString(w, `{"models":[
29 {"slug":"gpt-5.6-luna","display_name":"GPT-5.6 Luna","supported_in_api":true,"context_window":272000,"max_context_window":400000,"supported_reasoning_levels":["low","medium","high"]},
30 {"slug":"gpt-hidden","display_name":"Hidden","supported_in_api":true,"visibility":"hide"},
31 {"slug":"gpt-unsupported","display_name":"Unsupported","supported_in_api":false},
32 {"slug":"gpt-custom","supported_in_api":true,"max_context_window":100000}
33 ]}`)
34 }))
35 defer srv.Close()
36
37 got, err := DiscoverOpenAICodex(context.Background(), "token", "account", srv.URL)
38 if err != nil {
39 t.Fatal(err)
40 }
41 if len(got) != 2 {
42 t.Fatalf("models = %+v, want 2 models", got)
43 }
44 if got[0].Provider != "openai-codex" || got[0].ID != "gpt-5.6-luna" || got[0].DisplayName != "GPT-5.6 Luna" || got[0].ContextWindow != 272000 || !got[0].Reasoning || got[0].Source != "live" {
45 t.Errorf("first model = %+v", got[0])
46 }
47 if got[1].ID != "gpt-custom" || got[1].DisplayName != "gpt-custom" || got[1].ContextWindow != 100000 {
48 t.Errorf("second model = %+v", got[1])
49 }
50}
51
52func TestDiscoverOpenAICodexRejectsHTTPError(t *testing.T) {
53 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54 http.Error(w, "denied", http.StatusUnauthorized)
55 }))
56 defer srv.Close()
57
58 if _, err := DiscoverOpenAICodex(context.Background(), "token", "", srv.URL); err == nil {
59 t.Fatal("DiscoverOpenAICodex error = nil, want HTTP error")
60 }
61}