v0.3.4 fc39bd9

openai_codex_test.go RawHistoryFolder

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 TestXAIResponsesUsesNamedProviderCatalogAndEndpoint(t *testing.T) {
129	named := NewOpenAIResponsesNamed("token", "https://api.x.ai/v1", "xai").(*renamedClient)
130	c := named.inner.(*codexClient)
131
132	if c.baseURL != "https://api.x.ai/v1/responses" {
133		t.Fatalf("base URL = %q", c.baseURL)
134	}
135	for _, model := range []string{"grok-4.5", "grok-build-0.1"} {
136		if _, err := c.buildRequest(Request{Model: model}); err != nil {
137			t.Errorf("build request for %s: %v", model, err)
138		}
139	}
140}
141
142func TestOpenAIGPT56DoesNotUseCodexCLIRouting(t *testing.T) {
143	named := NewOpenAIResponsesNamed("token", "https://example.test/v1/responses", "openai").(*renamedClient)
144	c := named.inner.(*codexClient)
145	var gotReq *http.Request
146	var gotBody codexRequest
147	c.http.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
148		gotReq = r
149		if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
150			t.Fatal(err)
151		}
152		return &http.Response{
153			StatusCode: http.StatusOK,
154			Header:     http.Header{"Content-Type": []string{"text/event-stream"}},
155			Body:       io.NopCloser(strings.NewReader("")),
156		}, nil
157	})
158
159	events, err := c.Stream(context.Background(), Request{
160		Model:    "gpt-5.6-sol",
161		Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
162	})
163	if err != nil {
164		t.Fatal(err)
165	}
166	for range events {
167	}
168
169	if gotReq == nil {
170		t.Fatal("request was not sent")
171	}
172	if gotReq.Header.Get("session-id") != "" {
173		t.Fatalf("session-id = %q", gotReq.Header.Get("session-id"))
174	}
175	if gotBody.PromptCacheKey != "" {
176		t.Fatalf("prompt_cache_key = %q", gotBody.PromptCacheKey)
177	}
178}
179
180func TestCodexNestedStreamError(t *testing.T) {
181	c := NewOpenAICodex("token", "acct", "").(*codexClient)
182	resp := &http.Response{
183		Body: io.NopCloser(strings.NewReader("data: {\"type\":\"error\",\"error\":{\"code\":\"model_not_available\",\"message\":\"limited preview\"}}\n\n")),
184	}
185	out := make(chan Event, 16)
186	go c.runStream(context.Background(), resp, Request{Model: "gpt-5.6-sol"}, out)
187
188	var got error
189	for ev := range out {
190		if done, ok := ev.(EventDone); ok {
191			got = done.Err
192		}
193	}
194	if got == nil || got.Error() != "codex error: limited preview" {
195		t.Fatalf("error = %v", got)
196	}
197}
198
199func TestCodexSolKeepsZotShape(t *testing.T) {
200	c := NewOpenAICodex("token", "acct", "https://example.test/backend-api/codex/responses").(*codexClient)
201	var gotReq *http.Request
202	var body bytes.Buffer
203	c.http.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
204		gotReq = r
205		_, _ = body.ReadFrom(r.Body)
206		return &http.Response{
207			StatusCode: http.StatusOK,
208			Header:     http.Header{"Content-Type": []string{"text/event-stream"}},
209			Body:       io.NopCloser(strings.NewReader("")),
210		}, nil
211	})
212
213	events, err := c.Stream(context.Background(), Request{
214		Model:    "gpt-5.6-sol",
215		Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}},
216	})
217	if err != nil {
218		t.Fatal(err)
219	}
220	for range events {
221	}
222
223	if gotReq == nil {
224		t.Fatal("request was not sent")
225	}
226	if gotReq.Header.Get("originator") != "zot" {
227		t.Fatalf("originator = %q", gotReq.Header.Get("originator"))
228	}
229	if gotReq.Header.Get("session-id") != "" {
230		t.Fatalf("session-id = %q", gotReq.Header.Get("session-id"))
231	}
232	if strings.Contains(body.String(), "prompt_cache_key") {
233		t.Fatalf("Sol request unexpectedly included prompt_cache_key: %s", body.String())
234	}
235}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close