1package provider
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "io"
8 "net/http"
9 "strings"
10 "testing"
11)
12
13// An image-only tool result must not serialize to an empty
14// function_call_output (the Responses API may reject it) and a
15// following user-message image must serialize as input_image so the
16// model actually receives the bytes.
17func TestCodexImageToolResultMirror(t *testing.T) {
18 c := NewOpenAICodex("token", "acct", "").(*codexClient)
19
20 wire, err := c.buildRequest(Request{
21 Model: "gpt-5.5",
22 Messages: []Message{
23 {Role: RoleUser, Content: []Content{TextBlock{Text: "look at this"}}},
24 {Role: RoleAssistant, Content: []Content{
25 ToolCallBlock{ID: "call_1", Name: "read", Arguments: []byte(`{"path":"x.png"}`)},
26 }},
27 {Role: RoleTool, Content: []Content{
28 ToolResultBlock{CallID: "call_1", Content: []Content{
29 ImageBlock{MimeType: "image/png", Data: []byte("png-bytes")},
30 }},
31 }},
32 // The agent loop appends this mirror after an image tool result.
33 {Role: RoleUser, Content: []Content{
34 TextBlock{Text: "Tool output included the following image content:"},
35 ImageBlock{MimeType: "image/png", Data: []byte("png-bytes")},
36 }},
37 },
38 })
39 if err != nil {
40 t.Fatal(err)
41 }
42
43 var sawFnOutput, sawInputImage bool
44 for _, item := range wire.Input {
45 switch v := item.(type) {
46 case codexFunctionCallOutput:
47 sawFnOutput = true
48 if strings.TrimSpace(v.Output) == "" {
49 t.Fatalf("image-only tool result produced empty function_call_output")
50 }
51 if !strings.Contains(strings.ToLower(v.Output), "image") {
52 t.Fatalf("placeholder should mention image, got %q", v.Output)
53 }
54 case codexInputMessage:
55 for _, ct := range v.Content {
56 if img, ok := ct.(codexInputImage); ok && img.Type == "input_image" {
57 sawInputImage = true
58 }
59 }
60 }
61 }
62 if !sawFnOutput {
63 t.Fatalf("no function_call_output emitted")
64 }
65 if !sawInputImage {
66 t.Fatalf("mirrored user image was not serialized as input_image")
67 }
68}
69
70type roundTripFunc func(*http.Request) (*http.Response, error)
71
72func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
73
74func TestCodexPreviewModelUsesCodexCLIShape(t *testing.T) {
75 c := NewOpenAICodex("token", "acct", "https://example.test/backend-api/codex/responses").(*codexClient)
76 var gotReq *http.Request
77 var gotBody codexRequest
78 c.http.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
79 gotReq = r
80 if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
81 t.Fatal(err)
82 }
83 return &http.Response{
84 StatusCode: http.StatusOK,
85 Header: http.Header{"Content-Type": []string{"text/event-stream"}},
86 Body: io.NopCloser(strings.NewReader("")),
87 }, nil
88 })
89
90 events, err := c.Stream(context.Background(), Request{
91 Model: "gpt-5.6-terra",
92 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
93 })
94 if err != nil {
95 t.Fatal(err)
96 }
97 for range events {
98 }
99
100 if gotReq == nil {
101 t.Fatal("request was not sent")
102 }
103 if gotReq.Header.Get("originator") != "codex_cli_rs" {
104 t.Fatalf("originator = %q", gotReq.Header.Get("originator"))
105 }
106 if gotReq.Header.Get("user-agent") != "codex_cli_rs/0.0.0" {
107 t.Fatalf("user-agent = %q", gotReq.Header.Get("user-agent"))
108 }
109 if gotBody.PromptCacheKey == "" {
110 t.Fatal("prompt_cache_key was not set")
111 }
112 if gotReq.Header.Get("session-id") != gotBody.PromptCacheKey {
113 t.Fatalf("session-id = %q, prompt_cache_key = %q", gotReq.Header.Get("session-id"), gotBody.PromptCacheKey)
114 }
115}
116
117func TestGPT56UsesNativeMaxReasoningEffort(t *testing.T) {
118 c := NewOpenAICodex("token", "acct", "").(*codexClient)
119 wire, err := c.buildRequest(Request{Model: "gpt-5.6-sol", Reasoning: "max"})
120 if err != nil {
121 t.Fatal(err)
122 }
123 if wire.Reasoning == nil || wire.Reasoning.Effort != "max" {
124 t.Fatalf("reasoning = %+v", wire.Reasoning)
125 }
126}
127
128func TestOpenAIGPT56DoesNotUseCodexCLIRouting(t *testing.T) {
129 named := NewOpenAIResponsesNamed("token", "https://example.test/v1/responses", "openai").(*renamedClient)
130 c := named.inner.(*codexClient)
131 var gotReq *http.Request
132 var gotBody codexRequest
133 c.http.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
134 gotReq = r
135 if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
136 t.Fatal(err)
137 }
138 return &http.Response{
139 StatusCode: http.StatusOK,
140 Header: http.Header{"Content-Type": []string{"text/event-stream"}},
141 Body: io.NopCloser(strings.NewReader("")),
142 }, nil
143 })
144
145 events, err := c.Stream(context.Background(), Request{
146 Model: "gpt-5.6-sol",
147 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
148 })
149 if err != nil {
150 t.Fatal(err)
151 }
152 for range events {
153 }
154
155 if gotReq == nil {
156 t.Fatal("request was not sent")
157 }
158 if gotReq.Header.Get("session-id") != "" {
159 t.Fatalf("session-id = %q", gotReq.Header.Get("session-id"))
160 }
161 if gotBody.PromptCacheKey != "" {
162 t.Fatalf("prompt_cache_key = %q", gotBody.PromptCacheKey)
163 }
164}
165
166func TestCodexNestedStreamError(t *testing.T) {
167 c := NewOpenAICodex("token", "acct", "").(*codexClient)
168 resp := &http.Response{
169 Body: io.NopCloser(strings.NewReader("data: {\"type\":\"error\",\"error\":{\"code\":\"model_not_available\",\"message\":\"limited preview\"}}\n\n")),
170 }
171 out := make(chan Event, 16)
172 go c.runStream(context.Background(), resp, Request{Model: "gpt-5.6-sol"}, out)
173
174 var got error
175 for ev := range out {
176 if done, ok := ev.(EventDone); ok {
177 got = done.Err
178 }
179 }
180 if got == nil || got.Error() != "codex error: limited preview" {
181 t.Fatalf("error = %v", got)
182 }
183}
184
185func TestCodexSolKeepsZotShape(t *testing.T) {
186 c := NewOpenAICodex("token", "acct", "https://example.test/backend-api/codex/responses").(*codexClient)
187 var gotReq *http.Request
188 var body bytes.Buffer
189 c.http.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
190 gotReq = r
191 _, _ = body.ReadFrom(r.Body)
192 return &http.Response{
193 StatusCode: http.StatusOK,
194 Header: http.Header{"Content-Type": []string{"text/event-stream"}},
195 Body: io.NopCloser(strings.NewReader("")),
196 }, nil
197 })
198
199 events, err := c.Stream(context.Background(), Request{
200 Model: "gpt-5.6-sol",
201 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
202 })
203 if err != nil {
204 t.Fatal(err)
205 }
206 for range events {
207 }
208
209 if gotReq == nil {
210 t.Fatal("request was not sent")
211 }
212 if gotReq.Header.Get("originator") != "zot" {
213 t.Fatalf("originator = %q", gotReq.Header.Get("originator"))
214 }
215 if gotReq.Header.Get("session-id") != "" {
216 t.Fatalf("session-id = %q", gotReq.Header.Get("session-id"))
217 }
218 if strings.Contains(body.String(), "prompt_cache_key") {
219 t.Fatalf("Sol request unexpectedly included prompt_cache_key: %s", body.String())
220 }
221}