package executor import ( "bytes" "context" "io" "net/http" "net/http/httptest" "strings" "testing" "time" "github.com/router-for-me/CLIProxyAPI/v7/internal/config" _ "github.com/router-for-me/CLIProxyAPI/v7/internal/translator" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" "github.com/tidwall/gjson") func TestCodexExecutorExecute_NonEmptyCompletionOutputHydratesMissingItemID(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte(`data: {"type":"response.output_item.done","item":{"id":"fc_123","type":"function_call","call_id":"call_123","name":"weather","arguments":"{}"},"output_index":0}` + "\n\n")) _, _ = w.Write([]byte(`data: {"type":"response.output_item.done","item":{"id":"fc_done_existing","type":"function_call","call_id":"call_existing","name":"other","arguments":"{}"},"output_index":1}` + "\n\n")) _, _ = w.Write([]byte(`data: {"type":"response.completed","response":{"id":"resp_1","object":"response","status":"completed","output":[{"id":null,"type":"function_call","call_id":"call_123","name":"weather-terminal","arguments":"{}"},{"id":"fc_existing","type":"function_call","call_id":"call_existing","name":"preserved","arguments":"{}"}]}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.4", Payload: []byte(`{"model":"gpt-5.4","input":"What is the weather?"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err != nil { t.Fatalf("Execute error: %v", err) } if got := gjson.GetBytes(resp.Payload, "output.0.id").String(); got != "fc_123" { t.Fatalf("output[0].id = %q, want %q; payload=%s", got, "fc_123", resp.Payload) } if got := gjson.GetBytes(resp.Payload, "output.0.name").String(); got != "weather-terminal" { t.Fatalf("output[0].name = %q, want terminal value; payload=%s", got, resp.Payload) } if got := gjson.GetBytes(resp.Payload, "output.1.id").String(); got != "fc_existing" { t.Fatalf("output[1].id = %q, want existing value; payload=%s", got, resp.Payload) }} func TestCodexExecutorExecute_EmptyStreamCompletionOutputUsesOutputItemDone(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"ok\"}]},\"output_index\":0}\n")) _, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"created_at\":1775555723,\"status\":\"completed\",\"model\":\"gpt-5.4-mini-2026-03-17\",\"output\":[],\"usage\":{\"input_tokens\":8,\"output_tokens\":28,\"total_tokens\":36}}}\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.4-mini", Payload: []byte(`{"model":"gpt-5.4-mini","messages":[{"role":"user","content":"Say ok"}]}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai"), Stream: false, }) if err != nil { t.Fatalf("Execute error: %v", err) } gotContent := gjson.GetBytes(resp.Payload, "choices.0.message.content").String() if gotContent != "ok" { t.Fatalf("choices.0.message.content = %q, want %q; payload=%s", gotContent, "ok", string(resp.Payload)) }} func TestCodexExecutorExecuteSurfacesTerminalStreamError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("event: response.created\n")) _, _ = w.Write([]byte(`data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-5.5"}}` + "\n\n")) _, _ = w.Write([]byte("event: error\n")) _, _ = w.Write([]byte(`data: {"type":"error","error":{"type":"invalid_request_error","code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try again.","param":"input"},"sequence_number":2}` + "\n\n")) _, _ = w.Write([]byte("event: response.failed\n")) _, _ = w.Write([]byte(`data: {"type":"response.failed","response":{"id":"resp_1","status":"failed","error":{"code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try again."}}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} _, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err == nil { t.Fatal("expected terminal stream error, got nil") } if got := statusCodeFromTestError(t, err); got != http.StatusBadRequest { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusBadRequest, err) } assertCodexErrorCode(t, err.Error(), "invalid_request_error", "context_too_large") if !strings.Contains(err.Error(), "Your input exceeds the context window") { t.Fatalf("error message missing upstream context text: %v", err) }} func TestCodexExecutorExecuteIncompleteResponseIsSuccessful(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte(`data: {"type":"response.incomplete","response":{"id":"resp_1","model":"gpt-5.5","status":"incomplete","incomplete_details":{"reason":"max_output_tokens"},"output":[],"usage":{"input_tokens":10,"output_tokens":5,"total_tokens":15}}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","messages":[{"role":"user","content":"hello"}]}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("claude"), Stream: false, }) if err != nil { t.Fatalf("Execute error: %v", err) } if got := gjson.GetBytes(resp.Payload, "stop_reason").String(); got != "max_tokens" { t.Fatalf("stop_reason = %q, want %q; payload=%s", got, "max_tokens", resp.Payload) }} func TestCodexExecutorExecuteExplicitTerminalFailureIsNotRequestScoped(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte(`data: {"type":"error","error":{"type":"invalid_request_error","code":"invalid_value","message":"Invalid input."}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} _, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err == nil { t.Fatal("expected explicit terminal failure, got nil") } if got := statusCodeFromTestError(t, err); got != http.StatusBadRequest { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusBadRequest, err) } assertNotRequestScopedTestError(t, err)} func TestCodexExecutorExecuteMissingCompletionIsRequestScoped(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\"}}\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} _, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err == nil { t.Fatal("expected missing-completion error, got nil") } if got := statusCodeFromTestError(t, err); got != http.StatusRequestTimeout { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusRequestTimeout, err) } assertRequestScopedTestError(t, err)} func TestCodexExecutorExecuteStreamMissingCompletionIsRequestScoped(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\"}}\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} result, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var streamErr error for chunk := range result.Chunks { if chunk.Err != nil { streamErr = chunk.Err } } if streamErr == nil { t.Fatal("expected missing-completion stream error, got nil") } if got := statusCodeFromTestError(t, streamErr); got != http.StatusRequestTimeout { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusRequestTimeout, streamErr) } assertRequestScopedTestError(t, streamErr)} func TestCodexExecutorExecuteStreamExplicitTerminalFailureIsNotSuccessful(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\"}}\n\n")) _, _ = w.Write([]byte(`data: {"type":"error","error":{"type":"invalid_request_error","code":"invalid_value","message":"Invalid input."}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} result, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var streamErr error for chunk := range result.Chunks { if chunk.Err != nil { streamErr = chunk.Err } } if streamErr == nil { t.Fatal("expected explicit terminal stream error, got nil") } if got := statusCodeFromTestError(t, streamErr); got != http.StatusBadRequest { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusBadRequest, streamErr) } assertNotRequestScopedTestError(t, streamErr)} func TestCodexAutoExecutorHTTPFallbackForwardsSequentialCutoffReasoningSummaryDelivery(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body, errRead := io.ReadAll(r.Body) if errRead != nil { t.Errorf("read request body: %v", errRead) return } if gjson.GetBytes(body, "stream_options.include_usage").Exists() { t.Errorf("unsupported stream option was forwarded: %s", body) } w.Header().Set("Content-Type", "text/event-stream") if delivery := gjson.GetBytes(body, "stream_options.reasoning_summary_delivery").String(); delivery == "sequential_cutoff" { _, _ = w.Write([]byte(`data: {"type":"response.reasoning_summary_text.done","item_id":"rs_1","summary_index":0,"text":"Checking"}` + "\n\n")) } else { _, _ = w.Write([]byte(`data: {"type":"response.reasoning_summary_text.delta","item_id":"rs_1","summary_index":0,"delta":"Checking"}` + "\n\n")) } _, _ = w.Write([]byte(`data: {"type":"response.completed","response":{"id":"resp_1","output":[],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}}` + "\n\n")) })) defer server.Close() executor := NewCodexAutoExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} result, err := executor.ExecuteStream(cliproxyexecutor.WithDownstreamWebsocket(context.Background()), auth, cliproxyexecutor.Request{ Model: "gpt-5.6-sol", Payload: []byte(`{"model":"gpt-5.6-sol","input":"hello","reasoning":{"summary":"detailed"},"stream_options":{"reasoning_summary_delivery":"sequential_cutoff","include_usage":true}}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), ResponseFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var output bytes.Buffer for chunk := range result.Chunks { if chunk.Err != nil { t.Fatalf("stream error: %v", chunk.Err) } output.Write(chunk.Payload) } if !strings.Contains(output.String(), `"type":"response.reasoning_summary_text.done"`) { t.Fatalf("missing sequential-cutoff summary event; output=%s", output.String()) }} func TestCodexExecutorTransportFailureBeforeTerminalIsRequestScoped(t *testing.T) { tests := []struct { name string stream bool }{ {name: "non-streaming"}, {name: "streaming", stream: true}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { created := []byte("data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\"}}\n\n") ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", roundTripperFunc(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"text/event-stream"}}, Body: io.NopCloser(io.MultiReader(bytes.NewReader(created), unexpectedEOFReader{})), Request: req, }, nil })) executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": "http://codex.test", "api_key": "test", }} req := cliproxyexecutor.Request{Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`)} opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FromString("openai-response"), Stream: tc.stream} var terminalErr error if tc.stream { result, errStream := executor.ExecuteStream(ctx, auth, req, opts) if errStream != nil { t.Fatalf("ExecuteStream error: %v", errStream) } for chunk := range result.Chunks { if chunk.Err != nil { terminalErr = chunk.Err } } } else { _, terminalErr = executor.Execute(ctx, auth, req, opts) } if terminalErr == nil { t.Fatal("expected transport failure before terminal event") } if got := statusCodeFromTestError(t, terminalErr); got != http.StatusRequestTimeout { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusRequestTimeout, terminalErr) } assertRequestScopedTestError(t, terminalErr) }) }} func TestCodexExecutorExecuteIgnoresTransportErrorAfterCompletion(t *testing.T) { completed := []byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}\n\n") ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", roundTripperFunc(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"text/event-stream"}}, Body: io.NopCloser(io.MultiReader(bytes.NewReader(completed), unexpectedEOFReader{})), Request: req, }, nil })) executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": "http://codex.test", "api_key": "test", }} resp, err := executor.Execute(ctx, auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err != nil { t.Fatalf("unexpected error after response.completed: %v", err) } if got := gjson.GetBytes(resp.Payload, "id").String(); got != "resp_1" { t.Fatalf("response id = %q, want resp_1; payload=%s", got, resp.Payload) }} func TestCodexExecutorExecuteStreamIgnoresTransportErrorAfterCompletion(t *testing.T) { completed := []byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\",\"output\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}\n\n") ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", roundTripperFunc(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"text/event-stream"}}, Body: io.NopCloser(io.MultiReader(bytes.NewReader(completed), unexpectedEOFReader{})), Request: req, }, nil })) executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": "http://codex.test", "api_key": "test", }} result, err := executor.ExecuteStream(ctx, auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var streamErr error for chunk := range result.Chunks { if chunk.Err != nil { streamErr = chunk.Err } } if streamErr != nil { t.Fatalf("unexpected error after response.completed: %v", streamErr) }} func TestCodexExecutorExecuteStreamSurfacesTerminalStreamError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("event: response.created\n")) _, _ = w.Write([]byte(`data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-5.5"}}` + "\n\n")) _, _ = w.Write([]byte("event: error\n")) _, _ = w.Write([]byte(`data: {"type":"error","error":{"type":"invalid_request_error","code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try again.","param":"input"},"sequence_number":2}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} result, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var streamErr error for chunk := range result.Chunks { if chunk.Err != nil { streamErr = chunk.Err break } } if streamErr == nil {Lines 1–500 of 721. Load more
package executor import ( "bytes" "context" "io" "net/http" "net/http/httptest" "strings" "testing" "time" "github.com/router-for-me/CLIProxyAPI/v7/internal/config" _ "github.com/router-for-me/CLIProxyAPI/v7/internal/translator" cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" "github.com/tidwall/gjson" ) func TestCodexExecutorExecute_NonEmptyCompletionOutputHydratesMissingItemID(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte(`data: {"type":"response.output_item.done","item":{"id":"fc_123","type":"function_call","call_id":"call_123","name":"weather","arguments":"{}"},"output_index":0}` + "\n\n")) _, _ = w.Write([]byte(`data: {"type":"response.output_item.done","item":{"id":"fc_done_existing","type":"function_call","call_id":"call_existing","name":"other","arguments":"{}"},"output_index":1}` + "\n\n")) _, _ = w.Write([]byte(`data: {"type":"response.completed","response":{"id":"resp_1","object":"response","status":"completed","output":[{"id":null,"type":"function_call","call_id":"call_123","name":"weather-terminal","arguments":"{}"},{"id":"fc_existing","type":"function_call","call_id":"call_existing","name":"preserved","arguments":"{}"}]}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.4", Payload: []byte(`{"model":"gpt-5.4","input":"What is the weather?"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err != nil { t.Fatalf("Execute error: %v", err) } if got := gjson.GetBytes(resp.Payload, "output.0.id").String(); got != "fc_123" { t.Fatalf("output[0].id = %q, want %q; payload=%s", got, "fc_123", resp.Payload) } if got := gjson.GetBytes(resp.Payload, "output.0.name").String(); got != "weather-terminal" { t.Fatalf("output[0].name = %q, want terminal value; payload=%s", got, resp.Payload) } if got := gjson.GetBytes(resp.Payload, "output.1.id").String(); got != "fc_existing" { t.Fatalf("output[1].id = %q, want existing value; payload=%s", got, resp.Payload) } } func TestCodexExecutorExecute_EmptyStreamCompletionOutputUsesOutputItemDone(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"ok\"}]},\"output_index\":0}\n")) _, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"created_at\":1775555723,\"status\":\"completed\",\"model\":\"gpt-5.4-mini-2026-03-17\",\"output\":[],\"usage\":{\"input_tokens\":8,\"output_tokens\":28,\"total_tokens\":36}}}\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.4-mini", Payload: []byte(`{"model":"gpt-5.4-mini","messages":[{"role":"user","content":"Say ok"}]}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai"), Stream: false, }) if err != nil { t.Fatalf("Execute error: %v", err) } gotContent := gjson.GetBytes(resp.Payload, "choices.0.message.content").String() if gotContent != "ok" { t.Fatalf("choices.0.message.content = %q, want %q; payload=%s", gotContent, "ok", string(resp.Payload)) } } func TestCodexExecutorExecuteSurfacesTerminalStreamError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("event: response.created\n")) _, _ = w.Write([]byte(`data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-5.5"}}` + "\n\n")) _, _ = w.Write([]byte("event: error\n")) _, _ = w.Write([]byte(`data: {"type":"error","error":{"type":"invalid_request_error","code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try again.","param":"input"},"sequence_number":2}` + "\n\n")) _, _ = w.Write([]byte("event: response.failed\n")) _, _ = w.Write([]byte(`data: {"type":"response.failed","response":{"id":"resp_1","status":"failed","error":{"code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try again."}}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} _, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err == nil { t.Fatal("expected terminal stream error, got nil") } if got := statusCodeFromTestError(t, err); got != http.StatusBadRequest { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusBadRequest, err) } assertCodexErrorCode(t, err.Error(), "invalid_request_error", "context_too_large") if !strings.Contains(err.Error(), "Your input exceeds the context window") { t.Fatalf("error message missing upstream context text: %v", err) } } func TestCodexExecutorExecuteIncompleteResponseIsSuccessful(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte(`data: {"type":"response.incomplete","response":{"id":"resp_1","model":"gpt-5.5","status":"incomplete","incomplete_details":{"reason":"max_output_tokens"},"output":[],"usage":{"input_tokens":10,"output_tokens":5,"total_tokens":15}}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} resp, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","messages":[{"role":"user","content":"hello"}]}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("claude"), Stream: false, }) if err != nil { t.Fatalf("Execute error: %v", err) } if got := gjson.GetBytes(resp.Payload, "stop_reason").String(); got != "max_tokens" { t.Fatalf("stop_reason = %q, want %q; payload=%s", got, "max_tokens", resp.Payload) } } func TestCodexExecutorExecuteExplicitTerminalFailureIsNotRequestScoped(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte(`data: {"type":"error","error":{"type":"invalid_request_error","code":"invalid_value","message":"Invalid input."}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} _, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err == nil { t.Fatal("expected explicit terminal failure, got nil") } if got := statusCodeFromTestError(t, err); got != http.StatusBadRequest { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusBadRequest, err) } assertNotRequestScopedTestError(t, err) } func TestCodexExecutorExecuteMissingCompletionIsRequestScoped(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\"}}\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} _, err := executor.Execute(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err == nil { t.Fatal("expected missing-completion error, got nil") } if got := statusCodeFromTestError(t, err); got != http.StatusRequestTimeout { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusRequestTimeout, err) } assertRequestScopedTestError(t, err) } func TestCodexExecutorExecuteStreamMissingCompletionIsRequestScoped(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\"}}\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} result, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var streamErr error for chunk := range result.Chunks { if chunk.Err != nil { streamErr = chunk.Err } } if streamErr == nil { t.Fatal("expected missing-completion stream error, got nil") } if got := statusCodeFromTestError(t, streamErr); got != http.StatusRequestTimeout { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusRequestTimeout, streamErr) } assertRequestScopedTestError(t, streamErr) } func TestCodexExecutorExecuteStreamExplicitTerminalFailureIsNotSuccessful(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\"}}\n\n")) _, _ = w.Write([]byte(`data: {"type":"error","error":{"type":"invalid_request_error","code":"invalid_value","message":"Invalid input."}}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} result, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var streamErr error for chunk := range result.Chunks { if chunk.Err != nil { streamErr = chunk.Err } } if streamErr == nil { t.Fatal("expected explicit terminal stream error, got nil") } if got := statusCodeFromTestError(t, streamErr); got != http.StatusBadRequest { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusBadRequest, streamErr) } assertNotRequestScopedTestError(t, streamErr) } func TestCodexAutoExecutorHTTPFallbackForwardsSequentialCutoffReasoningSummaryDelivery(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body, errRead := io.ReadAll(r.Body) if errRead != nil { t.Errorf("read request body: %v", errRead) return } if gjson.GetBytes(body, "stream_options.include_usage").Exists() { t.Errorf("unsupported stream option was forwarded: %s", body) } w.Header().Set("Content-Type", "text/event-stream") if delivery := gjson.GetBytes(body, "stream_options.reasoning_summary_delivery").String(); delivery == "sequential_cutoff" { _, _ = w.Write([]byte(`data: {"type":"response.reasoning_summary_text.done","item_id":"rs_1","summary_index":0,"text":"Checking"}` + "\n\n")) } else { _, _ = w.Write([]byte(`data: {"type":"response.reasoning_summary_text.delta","item_id":"rs_1","summary_index":0,"delta":"Checking"}` + "\n\n")) } _, _ = w.Write([]byte(`data: {"type":"response.completed","response":{"id":"resp_1","output":[],"usage":{"input_tokens":1,"output_tokens":1,"total_tokens":2}}}` + "\n\n")) })) defer server.Close() executor := NewCodexAutoExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} result, err := executor.ExecuteStream(cliproxyexecutor.WithDownstreamWebsocket(context.Background()), auth, cliproxyexecutor.Request{ Model: "gpt-5.6-sol", Payload: []byte(`{"model":"gpt-5.6-sol","input":"hello","reasoning":{"summary":"detailed"},"stream_options":{"reasoning_summary_delivery":"sequential_cutoff","include_usage":true}}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), ResponseFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var output bytes.Buffer for chunk := range result.Chunks { if chunk.Err != nil { t.Fatalf("stream error: %v", chunk.Err) } output.Write(chunk.Payload) } if !strings.Contains(output.String(), `"type":"response.reasoning_summary_text.done"`) { t.Fatalf("missing sequential-cutoff summary event; output=%s", output.String()) } } func TestCodexExecutorTransportFailureBeforeTerminalIsRequestScoped(t *testing.T) { tests := []struct { name string stream bool }{ {name: "non-streaming"}, {name: "streaming", stream: true}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { created := []byte("data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\"}}\n\n") ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", roundTripperFunc(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"text/event-stream"}}, Body: io.NopCloser(io.MultiReader(bytes.NewReader(created), unexpectedEOFReader{})), Request: req, }, nil })) executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": "http://codex.test", "api_key": "test", }} req := cliproxyexecutor.Request{Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`)} opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FromString("openai-response"), Stream: tc.stream} var terminalErr error if tc.stream { result, errStream := executor.ExecuteStream(ctx, auth, req, opts) if errStream != nil { t.Fatalf("ExecuteStream error: %v", errStream) } for chunk := range result.Chunks { if chunk.Err != nil { terminalErr = chunk.Err } } } else { _, terminalErr = executor.Execute(ctx, auth, req, opts) } if terminalErr == nil { t.Fatal("expected transport failure before terminal event") } if got := statusCodeFromTestError(t, terminalErr); got != http.StatusRequestTimeout { t.Fatalf("status code = %d, want %d; err=%v", got, http.StatusRequestTimeout, terminalErr) } assertRequestScopedTestError(t, terminalErr) }) } } func TestCodexExecutorExecuteIgnoresTransportErrorAfterCompletion(t *testing.T) { completed := []byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\",\"status\":\"completed\",\"output\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}\n\n") ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", roundTripperFunc(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"text/event-stream"}}, Body: io.NopCloser(io.MultiReader(bytes.NewReader(completed), unexpectedEOFReader{})), Request: req, }, nil })) executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": "http://codex.test", "api_key": "test", }} resp, err := executor.Execute(ctx, auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: false, }) if err != nil { t.Fatalf("unexpected error after response.completed: %v", err) } if got := gjson.GetBytes(resp.Payload, "id").String(); got != "resp_1" { t.Fatalf("response id = %q, want resp_1; payload=%s", got, resp.Payload) } } func TestCodexExecutorExecuteStreamIgnoresTransportErrorAfterCompletion(t *testing.T) { completed := []byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"model\":\"gpt-5.5\",\"output\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}\n\n") ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", roundTripperFunc(func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"text/event-stream"}}, Body: io.NopCloser(io.MultiReader(bytes.NewReader(completed), unexpectedEOFReader{})), Request: req, }, nil })) executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": "http://codex.test", "api_key": "test", }} result, err := executor.ExecuteStream(ctx, auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var streamErr error for chunk := range result.Chunks { if chunk.Err != nil { streamErr = chunk.Err } } if streamErr != nil { t.Fatalf("unexpected error after response.completed: %v", streamErr) } } func TestCodexExecutorExecuteStreamSurfacesTerminalStreamError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write([]byte("event: response.created\n")) _, _ = w.Write([]byte(`data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-5.5"}}` + "\n\n")) _, _ = w.Write([]byte("event: error\n")) _, _ = w.Write([]byte(`data: {"type":"error","error":{"type":"invalid_request_error","code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try again.","param":"input"},"sequence_number":2}` + "\n\n")) })) defer server.Close() executor := NewCodexExecutor(&config.Config{}) auth := &cliproxyauth.Auth{Attributes: map[string]string{ "base_url": server.URL, "api_key": "test", }} result, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{ Model: "gpt-5.5", Payload: []byte(`{"model":"gpt-5.5","input":"hello"}`), }, cliproxyexecutor.Options{ SourceFormat: sdktranslator.FromString("openai-response"), Stream: true, }) if err != nil { t.Fatalf("ExecuteStream error: %v", err) } var streamErr error for chunk := range result.Chunks { if chunk.Err != nil { streamErr = chunk.Err break } } if streamErr == nil {