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 TestCodexSolKeepsZotShape(t *testing.T) {
118 c := NewOpenAICodex("token", "acct", "https://example.test/backend-api/codex/responses").(*codexClient)
119 var gotReq *http.Request
120 var body bytes.Buffer
121 c.http.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
122 gotReq = r
123 _, _ = body.ReadFrom(r.Body)
124 return &http.Response{
125 StatusCode: http.StatusOK,
126 Header: http.Header{"Content-Type": []string{"text/event-stream"}},
127 Body: io.NopCloser(strings.NewReader("")),
128 }, nil
129 })
130
131 events, err := c.Stream(context.Background(), Request{
132 Model: "gpt-5.6-sol",
133 Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
134 })
135 if err != nil {
136 t.Fatal(err)
137 }
138 for range events {
139 }
140
141 if gotReq == nil {
142 t.Fatal("request was not sent")
143 }
144 if gotReq.Header.Get("originator") != "zot" {
145 t.Fatalf("originator = %q", gotReq.Header.Get("originator"))
146 }
147 if gotReq.Header.Get("session-id") != "" {
148 t.Fatalf("session-id = %q", gotReq.Header.Get("session-id"))
149 }
150 if strings.Contains(body.String(), "prompt_cache_key") {
151 t.Fatalf("Sol request unexpectedly included prompt_cache_key: %s", body.String())
152 }
153}