Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

72.2 KB
Raw Blame
package gateway

import (
	"bytes"
	"context"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/yetone/magpie/internal/catalog"
	"github.com/yetone/magpie/internal/provider"
)

// fake is an upstream that records what it got and replies with a script.
type fake struct {
	t     *testing.T
	got   []byte
	path  string
	head  http.Header
	reply string // SSE body
	ctype string // "" means text/event-stream; "none" sends no header at all
	code  int
	// refuse, when set, answers a request it gives a code for with it
	refuse func(body []byte) (code int, reply string)
	calls  int
}

func (f *fake) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	f.got, _ = io.ReadAll(r.Body)
	f.path, f.head = r.URL.Path, r.Header
	f.calls++
	if f.refuse != nil {
		if code, reply := f.refuse(f.got); code != 0 {
			w.Header().Set("Content-Type", "application/json")
			w.WriteHeader(code)
			io.WriteString(w, reply)
			return
		}
	}
	ct := f.ctype
	if ct == "" {
		ct = "text/event-stream"
	}
	if ct == "none" {
		w.Header()["Content-Type"] = nil // like the ChatGPT backend: the body alone says it streams
	} else {
		w.Header().Set("Content-Type", ct)
	}
	if f.code != 0 {
		w.WriteHeader(f.code)
	}
	io.WriteString(w, f.reply)
}

func sse(lines ...string) string {
	var b strings.Builder
	for _, l := range lines {
		b.WriteString(l + "\n\n")
	}
	return b.String()
}

// setup points magpie's provider file at a temp dir and adds one provider
// speaking only the given protocol, backed by the fake. Rests are kept
// for the process, by provider id, and every test's provider is "fake":
// one a test rested (out of its allowance, held without asking since
// #1772) is lifted before and after it, or a later test's request would be
// answered from it, its fake never asked (#1840).
func setup(t *testing.T, proto provider.Protocol, f *fake) *httptest.Server {
	t.Helper()
	noRests()
	t.Cleanup(noRests)
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	t.Setenv("XDG_CACHE_HOME", t.TempDir())
	up := httptest.NewServer(f)
	t.Cleanup(up.Close)
	p := provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}}
	switch proto {
	case provider.Chat:
		p.Chat = up.URL + "/v1"
	case provider.Responses:
		p.Responses = up.URL + "/v1"
	case provider.Anthropic:
		p.Anthropic = up.URL
	}
	if err := provider.Save(p); err != nil {
		t.Fatal(err)
	}
	return up
}

// noRests lifts every rest the package's tests have recorded.
func noRests() {
	restingUntil.Lock()
	restingUntil.m = map[string]time.Time{}
	restingUntil.note = map[string]Rest{}
	restingUntil.Unlock()
}

func post(t *testing.T, path, body string) (int, string) {
	t.Helper()
	rec := httptest.NewRecorder()
	req := httptest.NewRequest("POST", path, strings.NewReader(body))
	New().Handler().ServeHTTP(rec, req)
	return rec.Code, rec.Body.String()
}

func events(body string) []map[string]any {
	var out []map[string]any
	readSSE(strings.NewReader(body), func(_, data string) error {
		var m map[string]any
		if json.Unmarshal([]byte(data), &m) == nil {
			out = append(out, m)
		}
		return nil
	})
	return out
}

type fallbackFake struct {
	refusal       string // chat's, an English one when empty
	chatBody      []byte
	responsesBody []byte
	chatCalls     int
	responseCalls int
}

func (f *fallbackFake) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	body, _ := io.ReadAll(r.Body)
	switch r.URL.Path {
	case "/v1/chat/completions":
		f.chatCalls++
		f.chatBody = body
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusBadRequest)
		if f.refusal != "" {
			io.WriteString(w, f.refusal)
			return
		}
		io.WriteString(w, `{"error":{"message":"This model is not supported in the v1/chat/completions endpoint. Use v1/responses."}}`)
	case "/v1/responses":
		f.responseCalls++
		f.responsesBody = body
		w.Header().Set("Content-Type", "text/event-stream")
		io.WriteString(w, sse(
			`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1","model":"m1","usage":{"input_tokens":0,"output_tokens":0}}}`,
			`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"fallback ok"}`,
			`event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r1","model":"m1","status":"completed","usage":{"input_tokens":4,"output_tokens":2}}}`,
		))
	default:
		http.NotFound(w, r)
	}
}

func TestAnthropicClientFallsBackFromChatToResponses(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	t.Setenv("XDG_CACHE_HOME", t.TempDir())
	f := &fallbackFake{}
	up := httptest.NewServer(f)
	t.Cleanup(up.Close)
	if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"},
		Chat: up.URL + "/v1", Responses: up.URL + "/v1"}); err != nil {
		t.Fatal(err)
	}

	handler := New().Handler()
	call := func(body string) (int, string) {
		rec := httptest.NewRecorder()
		req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
		handler.ServeHTTP(rec, req)
		return rec.Code, rec.Body.String()
	}
	code, body := call(`{"model":"m1","max_tokens":100,"messages":[{"role":"user","content":"hello"}]}`)
	if code != 200 {
		t.Fatalf("status %d: %s", code, body)
	}
	if f.chatCalls != 1 || f.responseCalls != 1 {
		t.Fatalf("calls: chat=%d responses=%d", f.chatCalls, f.responseCalls)
	}
	var chat, responses map[string]any
	if json.Unmarshal(f.chatBody, &chat) != nil || json.Unmarshal(f.responsesBody, &responses) != nil {
		t.Fatalf("bad translated requests: chat=%s responses=%s", f.chatBody, f.responsesBody)
	}
	if chat["stream"] != true || responses["stream"] != true || responses["model"] != "m1" {
		t.Errorf("requests: chat=%v responses=%v", chat, responses)
	}
	var out struct {
		Content []struct {
			Type string `json:"type"`
			Text string `json:"text"`
		} `json:"content"`
		Usage aUsage `json:"usage"`
	}
	if err := json.Unmarshal([]byte(body), &out); err != nil {
		t.Fatal(err)
	}
	if len(out.Content) != 1 || out.Content[0].Text != "fallback ok" || out.Usage.InputTokens != 4 || out.Usage.OutputTokens != 2 {
		t.Errorf("reply: %s", body)
	}
	code, body = call(`{"model":"m1","max_tokens":100,"messages":[{"role":"user","content":"again"}]}`)
	if code != 200 {
		t.Fatalf("second status %d: %s", code, body)
	}
	if f.chatCalls != 1 || f.responseCalls != 2 {
		t.Fatalf("cached calls: chat=%d responses=%d", f.chatCalls, f.responseCalls)
	}
}

// A model its provider serves only on /responses, asked for by a Chat
// Completions client (Pi through Copilot): the relay is turned away, the
// request is spoken as Responses instead, and later turns go there at once.
func TestChatClientFallsBackToResponses(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	t.Setenv("XDG_CACHE_HOME", t.TempDir())
	f := &fallbackFake{}
	up := httptest.NewServer(f)
	t.Cleanup(up.Close)
	if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"},
		Chat: up.URL + "/v1", Responses: up.URL + "/v1"}); err != nil {
		t.Fatal(err)
	}
	handler := New().Handler()
	call := func(body string) (int, string) {
		rec := httptest.NewRecorder()
		handler.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body)))
		return rec.Code, rec.Body.String()
	}
	for i, want := range []int{1, 2} {
		code, body := call(`{"model":"m1","messages":[{"role":"user","content":"hello"}]}`)
		if code != 200 || !strings.Contains(body, "fallback ok") {
			t.Fatalf("call %d: status %d: %s", i, code, body)
		}
		if f.chatCalls != 1 || f.responseCalls != want {
			t.Fatalf("call %d: chat=%d responses=%d", i, f.chatCalls, f.responseCalls)
		}
	}
}

// agentRouterSaid is AgentRouter's word that a model is served on
// /v1/responses, as #3766 quotes it.
const agentRouterSaid = "模型 gpt-5.6-sol 不支持 /v1/chat/completions 接口,请改用 /v1/responses(OpenAI Responses API)。"

// agentRouterRefusal is that word in an error body. The report has the
// message only, so the JSON around it is a guess, and so is how it is
// encoded: agentRouterEscaped has what isn't ASCII escaped, as Python's
// json.dumps writes it (#1669).
var (
	agentRouterRefusal = `{"error":{"message":"` + agentRouterSaid + `","type":"invalid_request_error"}}`
	agentRouterEscaped = `{"error":{"message":` + strconv.QuoteToASCII(agentRouterSaid) + `,"type":"invalid_request_error"}}`
)

// #3766 (char1eslu): AgentRouter turns its Responses-only models away on
// /v1/chat/completions in Chinese, which wrongEndpoint didn't read, so a
// provider with both URLs answered a Chat or a Messages client 400 though
// /v1/responses serves the model. Both now get its reply from there.
func TestChineseRefusalFallsBackToResponses(t *testing.T) {
	for _, c := range []struct{ name, path, body, refusal string }{
		{"chat", "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hello"}]}`, agentRouterRefusal},
		{"messages", "/v1/messages", `{"model":"m1","max_tokens":100,"messages":[{"role":"user","content":"hello"}]}`, agentRouterRefusal},
		{"chat, escaped", "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hello"}]}`, agentRouterEscaped},
		{"messages, escaped", "/v1/messages", `{"model":"m1","max_tokens":100,"messages":[{"role":"user","content":"hello"}]}`, agentRouterEscaped},
	} {
		t.Run(c.name, func(t *testing.T) {
			t.Setenv("XDG_CONFIG_HOME", t.TempDir())
			t.Setenv("XDG_CACHE_HOME", t.TempDir())
			f := &fallbackFake{refusal: c.refusal}
			up := httptest.NewServer(f)
			t.Cleanup(up.Close)
			if err := provider.Save(provider.Provider{ID: "agentrouter", Name: "AgentRouter", Key: "k", Models: []string{"m1"},
				Chat: up.URL + "/v1", Responses: up.URL + "/v1"}); err != nil {
				t.Fatal(err)
			}
			rec := httptest.NewRecorder()
			New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", c.path, strings.NewReader(c.body)))
			if rec.Code != 200 || !strings.Contains(rec.Body.String(), "fallback ok") {
				t.Fatalf("status %d: %s", rec.Code, rec.Body)
			}
			if f.chatCalls != 1 || f.responseCalls != 1 {
				t.Fatalf("calls: chat=%d responses=%d", f.chatCalls, f.responseCalls)
			}
		})
	}
}

// The other way round: a model served only on /chat/completions (Copilot's
// Claude models), asked for by a Responses client (Codex).
func TestResponsesClientFallsBackToChat(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	t.Setenv("XDG_CACHE_HOME", t.TempDir())
	var chatCalls, responseCalls int
	up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		io.ReadAll(r.Body)
		switch r.URL.Path {
		case "/v1/responses":
			responseCalls++
			w.WriteHeader(http.StatusBadRequest)
			io.WriteString(w, `{"error":{"message":"model \"claude-x\" is not accessible via the /responses endpoint","code":"unsupported_api_for_model"}}`)
		case "/v1/chat/completions":
			chatCalls++
			w.Header().Set("Content-Type", "text/event-stream")
			io.WriteString(w, sse(
				`data: {"id":"c1","model":"claude-x","choices":[{"delta":{"role":"assistant","content":"chat ok"}}]}`,
				`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}`,
				`data: [DONE]`))
		default:
			http.NotFound(w, r)
		}
	}))
	t.Cleanup(up.Close)
	if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"claude-x"},
		Chat: up.URL + "/v1", Responses: up.URL + "/v1"}); err != nil {
		t.Fatal(err)
	}
	handler := New().Handler()
	for i, want := range []int{1, 2} {
		rec := httptest.NewRecorder()
		handler.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"claude-x","input":"hi","stream":true}`)))
		if rec.Code != 200 || !strings.Contains(rec.Body.String(), "chat ok") {
			t.Fatalf("call %d: status %d: %s", i, rec.Code, rec.Body.String())
		}
		if responseCalls != 1 || chatCalls != want {
			t.Fatalf("call %d: responses=%d chat=%d", i, responseCalls, chatCalls)
		}
	}
}

// OpenCode Go serves its Grok models on /responses only, and says so on
// /messages and /chat/completions alike: Claude Code's request goes on
// past both, and the next is sent straight to /responses.
func TestAnthropicClientFallsBackPastOpenCodeFormats(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	t.Setenv("XDG_CACHE_HOME", t.TempDir())
	calls := map[string]int{}
	up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		io.ReadAll(r.Body)
		calls[r.URL.Path]++
		switch r.URL.Path {
		case "/v1/messages", "/v1/chat/completions":
			format := map[string]string{"/v1/messages": "anthropic", "/v1/chat/completions": "oa-compat"}[r.URL.Path]
			w.Header().Set("Content-Type", "application/json")
			w.WriteHeader(http.StatusBadRequest)
			io.WriteString(w, `{"type":"error","error":{"type":"ModelError","message":"Model grok-x is not supported for format `+format+`"}}`)
		case "/v1/responses":
			w.Header().Set("Content-Type", "text/event-stream")
			io.WriteString(w, sse(
				`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"grok ok"}`,
				`event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r1","model":"grok-x","status":"completed","usage":{"input_tokens":4,"output_tokens":2}}}`,
			))
		default:
			http.NotFound(w, r)
		}
	}))
	t.Cleanup(up.Close)
	if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"grok-x"},
		Chat: up.URL + "/v1", Responses: up.URL + "/v1", Anthropic: up.URL}); err != nil {
		t.Fatal(err)
	}
	handler := New().Handler()
	for i, want := range []int{1, 2} {
		rec := httptest.NewRecorder()
		handler.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"grok-x","max_tokens":100,"messages":[{"role":"user","content":"hi"}]}`)))
		if rec.Code != 200 || !strings.Contains(rec.Body.String(), "grok ok") {
			t.Fatalf("call %d: status %d: %s", i, rec.Code, rec.Body.String())
		}
		if calls["/v1/messages"] != 1 || calls["/v1/chat/completions"] != 1 || calls["/v1/responses"] != want {
			t.Fatalf("call %d: %v", i, calls)
		}
	}
}

// Copilot's /v1/messages turns away a model it serves on chat completions
// alone (GPT-4o, GPT-4.1) in plain text, "no model endpoints available
// given user constraints": Claude Code's request goes on to chat
// completions, where it is answered, not back to the user as a refusal of
// their plan (#754).
func TestAnthropicClientFallsBackPastCopilotNoEndpoints(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	t.Setenv("XDG_CACHE_HOME", t.TempDir())
	calls := map[string]int{}
	up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		io.ReadAll(r.Body)
		calls[r.URL.Path]++
		switch r.URL.Path {
		case "/v1/messages":
			w.Header().Set("Content-Type", "text/plain; charset=utf-8")
			w.WriteHeader(http.StatusBadRequest)
			io.WriteString(w, "no model endpoints available given user constraints\n")
		case "/v1/chat/completions":
			w.Header().Set("Content-Type", "text/event-stream")
			io.WriteString(w, sse(
				`data: {"id":"c1","model":"gpt-4o","choices":[{"delta":{"role":"assistant","content":"chat ok"}}]}`,
				`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}`,
				`data: [DONE]`))
		default:
			http.NotFound(w, r)
		}
	}))
	t.Cleanup(up.Close)
	if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"gpt-4o"},
		Chat: up.URL + "/v1", Anthropic: up.URL}); err != nil {
		t.Fatal(err)
	}
	handler := New().Handler()
	for i, want := range []int{1, 2} {
		rec := httptest.NewRecorder()
		handler.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"gpt-4o","max_tokens":5,"messages":[{"role":"user","content":"."}]}`)))
		if rec.Code != 200 || !strings.Contains(rec.Body.String(), "chat ok") {
			t.Fatalf("call %d: status %d: %s", i, rec.Code, rec.Body.String())
		}
		if calls["/v1/messages"] != 1 || calls["/v1/chat/completions"] != want {
			t.Fatalf("call %d: %v", i, calls)
		}
	}
}

func TestWrongEndpoint(t *testing.T) {
	for _, tc := range []struct {
		status int
		body   string
		want   bool
	}{
		{400, `{"error":{"message":"not a chat model; use /v1/responses"}}`, true},
		{404, `use v1/completions`, true},
		{400, `{"code":null,"message":"model \"gpt-6-sol\" is not accessible via the /chat/completions endpoint","type":"invalid_request_error"}`, true},
		{400, `{"error":{"message":"model not accessible","code":"unsupported_api_for_model"}}`, true},
		{400, `{"type":"error","error":{"type":"ModelError","message":"Model grok-4.7 is not supported for format anthropic"}}`, true},
		{400, `{"type":"error","error":{"type":"invalid_request_error","message":"Model does not support this protocol."}}`, true},
		{400, "no model endpoints available given user constraints\n", true}, // Copilot's /v1/messages (#754)
		{400, toolsOnChatRefused, true}, // a relay (#1308)
		{400, agentRouterRefusal, true}, // in Chinese (#3766)
		{400, agentRouterEscaped, true}, // escaped (#1669)
		{400, `{"error":{"message":"模型 gpt-5.6-sol 不支持 /v1/chat/completions 接口"}}`, true}, // its first clause alone
		{400, `{"error":{"message":"请改用 /v1/responses(OpenAI Responses API)。"}}`, true},   // its second alone
		// new-api's words for a model no channel serves (written from
		// memory, not from the report): the model, not the API
		{400, `{"error":{"message":"当前分组 default 下对于模型 gone 无可用渠道"}}`, false},
		{400, `{"error":{"message":"The requested model is not supported.","code":"model_not_supported"}}`, false},
		{429, `rate limit`, false},
		{500, `use v1/responses`, false},
		{200, `use v1/responses`, false},
	} {
		if got := wrongEndpoint(tc.status, []byte(tc.body)); got != tc.want {
			t.Errorf("wrongEndpoint(%d, %q) = %v, want %v", tc.status, tc.body, got, tc.want)
		}
	}
}

func TestAnthropicClientChatUpstream(t *testing.T) {
	f := &fake{t: t, reply: sse(
		`data: {"id":"c1","model":"m1","choices":[{"delta":{"role":"assistant","reasoning_content":"hmm"}}]}`,
		`data: {"id":"c1","choices":[{"delta":{"content":"Hi "}}]}`,
		`data: {"id":"c1","choices":[{"delta":{"content":"there"}}]}`,
		`data: {"id":"c1","choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"read","arguments":""}}]}}]}`,
		`data: {"id":"c1","choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"path\":"}}]}}]}`,
		`data: {"id":"c1","choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"a.go\"}"}}]}}]}`,
		`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"tool_calls"}]}`,
		`data: {"id":"c1","choices":[],"usage":{"prompt_tokens":10,"completion_tokens":5}}`,
		`data: [DONE]`)}
	setup(t, provider.Chat, f)
	code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":100,"stream":true,"system":"be brief",
	  "messages":[{"role":"user","content":"read a.go"},
	    {"role":"assistant","content":[{"type":"tool_use","id":"t0","name":"read","input":{"path":"x"}}]},
	    {"role":"user","content":[{"type":"tool_result","tool_use_id":"t0","content":"package x"}]}],
	  "tools":[{"name":"read","description":"read a file","input_schema":{"type":"object","properties":{"path":{"type":"string"}}}}],
	  "thinking":{"type":"enabled","budget_tokens":5000}}`)
	if code != 200 {
		t.Fatalf("status %d: %s", code, body)
	}
	// what the upstream saw
	var up map[string]any
	json.Unmarshal(f.got, &up)
	if f.path != "/v1/chat/completions" || f.head.Get("Authorization") != "Bearer k" {
		t.Errorf("upstream path/auth: %s %v", f.path, f.head)
	}
	msgs := up["messages"].([]any)
	if len(msgs) != 4 || msgs[0].(map[string]any)["role"] != "system" || msgs[3].(map[string]any)["role"] != "tool" {
		t.Errorf("messages: %v", msgs)
	}
	if up["reasoning_effort"] != "medium" || up["stream"] != true || up["max_tokens"] != float64(100) {
		t.Errorf("params: %v", up)
	}
	if _, ok := up["tools"].([]any)[0].(map[string]any)["function"]; !ok {
		t.Errorf("tools: %v", up["tools"])
	}
	// what the client got
	evs := events(body)
	var types []string
	for _, e := range evs {
		types = append(types, e["type"].(string))
	}
	want := "message_start content_block_start content_block_delta content_block_stop content_block_start content_block_delta content_block_delta content_block_stop content_block_start content_block_delta content_block_delta content_block_stop message_delta message_stop"
	if got := strings.Join(types, " "); got != want {
		t.Errorf("events:\n got %s\nwant %s", got, want)
	}
	if b := evs[1]["content_block"].(map[string]any); b["type"] != "thinking" {
		t.Errorf("first block: %v", b)
	}
	if b := evs[8]["content_block"].(map[string]any); b["type"] != "tool_use" || b["name"] != "read" || b["id"] != "call_1" {
		t.Errorf("tool block: %v", b)
	}
	md := evs[len(evs)-2]
	if md["delta"].(map[string]any)["stop_reason"] != "tool_use" || md["usage"].(map[string]any)["output_tokens"] != float64(5) {
		t.Errorf("message_delta: %v", md)
	}
}

func TestChatClientAnthropicUpstream(t *testing.T) {
	f := &fake{t: t, reply: sse(
		`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","model":"m1","usage":{"input_tokens":7,"cache_read_input_tokens":3}}}`,
		`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
		`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hello"}}`,
		`data: {"type":"content_block_stop","index":0}`,
		`data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_1","name":"ls","input":{}}}`,
		`data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"dir\":\".\"}"}}`,
		`data: {"type":"content_block_stop","index":1}`,
		`data: {"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":4}}`,
		`data: {"type":"message_stop"}`)}
	setup(t, provider.Anthropic, f)
	// non-streaming client
	code, body := post(t, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"system","content":"sys"},{"role":"user","content":"ls"}],
	  "tools":[{"type":"function","function":{"name":"ls","parameters":{"type":"object"}}}],"temperature":0.2,"max_tokens":50}`)
	if code != 200 {
		t.Fatalf("status %d: %s", code, body)
	}
	var up map[string]any
	json.Unmarshal(f.got, &up)
	// the system prompt and the conversation so far are marked for caching
	if !strings.Contains(string(f.got), `"system":[{"type":"text","text":"sys","cache_control":{"type":"ephemeral"}}]`) ||
		!strings.Contains(string(f.got), `{"type":"text","text":"ls","cache_control":{"type":"ephemeral"}}`) ||
		up["max_tokens"] != float64(50) || up["temperature"] != 0.2 || up["stream"] != true {
		t.Errorf("upstream: %s", f.got)
	}
	if f.head.Get("x-api-key") != "k" || f.head.Get("anthropic-version") == "" {
		t.Errorf("headers: %v", f.head)
	}
	var out struct {
		Choices []struct {
			Message struct {
				Content   string `json:"content"`
				ToolCalls []struct {
					ID       string `json:"id"`
					Function struct{ Name, Arguments string }
				} `json:"tool_calls"`
			}
			FinishReason string `json:"finish_reason"`
		}
		Usage struct {
			PromptTokens     int `json:"prompt_tokens"`
			CompletionTokens int `json:"completion_tokens"`
		}
	}
	json.Unmarshal([]byte(body), &out)
	c := out.Choices[0]
	if c.Message.Content != "hello" || c.FinishReason != "tool_calls" || len(c.Message.ToolCalls) != 1 ||
		c.Message.ToolCalls[0].Function.Arguments != `{"dir":"."}` || c.Message.ToolCalls[0].ID != "toolu_1" {
		t.Errorf("reply: %s", body)
	}
	if out.Usage.PromptTokens != 10 || out.Usage.CompletionTokens != 4 {
		t.Errorf("usage: %s", body)
	}
}

func TestResponsesClientChatUpstream(t *testing.T) {
	f := &fake{t: t, reply: sse(
		`data: {"id":"c1","model":"m1","choices":[{"delta":{"content":"ok"}}]}`,
		`data: {"id":"c1","choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_9","function":{"name":"shell","arguments":"{\"cmd\":\"ls\"}"}}]}}]}`,
		`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"tool_calls"}]}`,
		`data: {"id":"c1","choices":[],"usage":{"prompt_tokens":3,"completion_tokens":2}}`,
		`data: [DONE]`)}
	setup(t, provider.Chat, f)
	code, body := post(t, "/v1/responses", `{"model":"m1","stream":true,"instructions":"you are codex",
	  "input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"list"}]},
	    {"type":"function_call","call_id":"c0","name":"shell","arguments":"{\"cmd\":\"pwd\"}"},
	    {"type":"function_call_output","call_id":"c0","output":"/tmp"}],
	  "tools":[{"type":"function","name":"shell","parameters":{"type":"object"}}],"reasoning":{"effort":"high"}}`)
	if code != 200 {
		t.Fatalf("status %d: %s", code, body)
	}
	var up map[string]any
	json.Unmarshal(f.got, &up)
	msgs := up["messages"].([]any)
	if len(msgs) != 4 || msgs[2].(map[string]any)["tool_calls"] == nil || msgs[3].(map[string]any)["tool_call_id"] != "c0" {
		t.Errorf("upstream messages: %v", msgs)
	}
	if up["reasoning_effort"] != "high" {
		t.Errorf("effort: %v", up["reasoning_effort"])
	}
	var types []string
	var done []map[string]any
	for _, e := range events(body) {
		types = append(types, e["type"].(string))
		if e["type"] == "response.output_item.done" {
			done = append(done, e["item"].(map[string]any))
		}
	}
	want := "response.created response.in_progress response.output_item.added response.content_part.added response.output_text.delta response.output_text.done response.content_part.done response.output_item.done response.output_item.added response.function_call_arguments.delta response.function_call_arguments.done response.output_item.done response.completed"
	if got := strings.Join(types, " "); got != want {
		t.Errorf("events:\n got %s\nwant %s", got, want)
	}
	if len(done) != 2 || done[1]["type"] != "function_call" || done[1]["call_id"] != "call_9" || done[1]["arguments"] != `{"cmd":"ls"}` {
		t.Errorf("items: %v", done)
	}
}

func TestPassthroughRewritesModel(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`}
	setup(t, provider.Anthropic, f)
	p, _ := provider.Find("fake")
	p.Models = []string{"real-model"}
	provider.Save(*p)
	code, body := post(t, "/v1/messages", `{"model":"fake/real-model","max_tokens":1.5e2,"messages":[],"metadata":{"x":12345678901234567890}}`)
	if code != 200 || !strings.Contains(body, `"id":"msg"`) {
		t.Fatalf("%d %s", code, body)
	}
	if !bytes.Contains(f.got, []byte(`"model":"real-model"`)) || !bytes.Contains(f.got, []byte(`12345678901234567890`)) || !bytes.Contains(f.got, []byte(`1.5e2`)) {
		t.Errorf("rewritten body: %s", f.got)
	}
	if f.path != "/v1/messages" {
		t.Errorf("path %s", f.path)
	}
}

func TestChatPassthroughSendsDeveloperAsSystem(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"c1","choices":[]}`}
	setup(t, provider.Chat, f)
	code, body := post(t, "/v1/chat/completions", `{"model":"m1","reasoning_effort":"high","messages":[{"role":"developer","content":"be brief"},{"role":"user","content":"a developer asks"}]}`)
	if code != 200 {
		t.Fatalf("%d %s", code, body)
	}
	if !bytes.Contains(f.got, []byte(`{"content":"be brief","role":"system"}`)) || bytes.Contains(f.got, []byte(`"role":"developer"`)) || !bytes.Contains(f.got, []byte(`"content":"a developer asks"`)) {
		t.Errorf("forwarded body: %s", f.got)
	}
}

// Claude Code asks for a session title without thinking; DeepSeek's
// Anthropic endpoint thinks unless told not to
func TestAnthropicPassthroughTurnsThinkingOffUnlessAsked(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`}
	setup(t, provider.Anthropic, f)
	for _, c := range []struct{ req, want string }{
		{`{"model":"m1","max_tokens":5,"messages":[],"output_config":{"effort":"high"}}`, `"thinking":{"type":"disabled"}`},
		{`{"model":"m1","max_tokens":5,"messages":[],"thinking":{"type":"adaptive"}}`, `"thinking":{"type":"adaptive"}`},
		{`{"model":"m1","max_tokens":5,"messages":[],"thinking":{"type":"enabled","budget_tokens":2048}}`, `"thinking":{"type":"enabled","budget_tokens":2048}`},
	} {
		if code, body := post(t, "/v1/messages", c.req); code != 200 {
			t.Fatalf("%d %s", code, body)
		}
		if !bytes.Contains(f.got, []byte(c.want)) {
			t.Errorf("%s forwarded as %s", c.req, f.got)
		}
	}
}

// GLM-5.3 always thinks and refuses thinking turned off (Z.ai's 1210):
// the request magpie turned it off for is asked again with it left to the
// model, once
func TestAnthropicPassthroughModelThatAlwaysThinks(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`}
	f.refuse = func(b []byte) (int, string) {
		if bytes.Contains(b, []byte(`"disabled"`)) {
			return 400, `{"type":"error","error":{"type":"1210","message":"GLM-5.3 always engages in thinking; use low, high, or max"}}`
		}
		return 0, ""
	}
	setup(t, provider.Anthropic, f)
	code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[{"role":"user","content":"title?"}]}`)
	if code != 200 || f.calls != 2 || bytes.Contains(f.got, []byte("thinking")) || !bytes.Contains(f.got, []byte(`"title?"`)) {
		t.Fatalf("%d %s after %d calls, last sent %s", code, body, f.calls, f.got)
	}
	// another 400 is the agent's to see, not asked again
	f.calls = 0
	f.refuse = func([]byte) (int, string) { return 400, `{"type":"error","error":{"message":"bad request"}}` }
	if code, _ := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[]}`); code != 400 || f.calls != 1 {
		t.Errorf("%d after %d calls", code, f.calls)
	}
}

// DashScope's glm-5.3 cannot think with it off either, in its own words
// ("The value of the enable_thinking parameter is restricted to True."):
// the request magpie turned it off for is asked again with it left to the
// model, once
func TestAnthropicPassthroughDashScopeThinkingRestricted(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`}
	f.refuse = func(b []byte) (int, string) {
		if bytes.Contains(b, []byte(`"disabled"`)) {
			return 400, `{"error":{"code":"InternalError.Algo.InvalidParameter","message":"The value of the enable_thinking parameter is restricted to True.","type":"invalid_request_error"}}`
		}
		return 0, ""
	}
	setup(t, provider.Anthropic, f)
	code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[{"role":"user","content":"title?"}]}`)
	if code != 200 || f.calls != 2 || bytes.Contains(f.got, []byte("thinking")) || !bytes.Contains(f.got, []byte(`"title?"`)) {
		t.Fatalf("%d %s after %d calls, last sent %s", code, body, f.calls, f.got)
	}
	// another 400 is the agent's to see, not asked again
	f.calls = 0
	f.refuse = func([]byte) (int, string) { return 400, `{"type":"error","error":{"message":"bad request"}}` }
	if code, _ := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[]}`); code != 400 || f.calls != 1 {
		t.Errorf("%d after %d calls", code, f.calls)
	}
}

// #699: ZCode's GLM-5.3-Flash refuses thinking turned off in Chinese
// ("该模型始终支持思考,不可关闭"), which Pi's off sent and the agent got as
// a 400: asked again with thinking left to the model, once
func TestAnthropicPassthroughAlwaysThinksInChinese(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`}
	f.refuse = func(b []byte) (int, string) {
		if bytes.Contains(b, []byte(`"disabled"`)) {
			return 400, `{"type":"error","error":{"type":"invalid_request_error","message":"该模型始终支持思考,不可关闭"}}`
		}
		return 0, ""
	}
	setup(t, provider.Anthropic, f)
	code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[{"role":"user","content":"title?"}],"thinking":{"type":"disabled"}}`)
	if code != 200 || f.calls != 2 || bytes.Contains(f.got, []byte("thinking")) || !bytes.Contains(f.got, []byte(`"title?"`)) {
		t.Fatalf("%d %s after %d calls, last sent %s", code, body, f.calls, f.got)
	}
}

// #1141: MiniMax and Volcengine Ark refuse thinking turned off in their
// own words, the reporters' literal messages. The request magpie turned it
// off for (Claude Desktop's model_validation probe, its health probe and
// titles send none) is asked again with thinking left to the model, once;
// thinking the caller asked for goes as it came, and another 400 on the
// same request, or a near wording, is the caller's to see.
func TestAnthropicPassthroughMiniMaxArkThinkingOffRefused(t *testing.T) {
	for _, c := range []struct{ vendor, refusal string }{
		{"MiniMax", `{"type":"error","error":{"type":"invalid_request_error","message":"invalid params, model \"MiniMax-M3.1-Flash-Preview\" requires adaptive thinking; thinking.type=\"disabled\" (including reasoning.effort=none) is not allowed (2013)"}}`},
		{"Ark", `{"type":"error","error":{"type":"invalid_request_error","message":"thinking.type ` + "`disabled`" + ` is not supported by this model Request id: 021760000000000000000000000000000000000000000000000000"}}`},
	} {
		t.Run(c.vendor, func(t *testing.T) {
			f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`}
			f.refuse = func(b []byte) (int, string) {
				if bytes.Contains(b, []byte(`"disabled"`)) {
					return 400, c.refusal
				}
				return 0, ""
			}
			setup(t, provider.Anthropic, f)
			code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":1,"messages":[{"role":"user","content":[{"type":"text","text":"Hi","cache_control":{"type":"ephemeral"}}]}]}`)
			if code != 200 || f.calls != 2 || bytes.Contains(f.got, []byte("thinking")) || !bytes.Contains(f.got, []byte(`"Hi"`)) {
				t.Fatalf("omitted: %d %s after %d calls, last sent %s", code, body, f.calls, f.got)
			}
			f.calls = 0
			if code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":1,"thinking":{"type":"adaptive"},"messages":[{"role":"user","content":"."}]}`); code != 200 || f.calls != 1 || !bytes.Contains(f.got, []byte(`"thinking":{"type":"adaptive"}`)) {
				t.Errorf("adaptive: %d %s after %d calls, sent %s", code, body, f.calls, f.got)
			}
			for _, other := range []string{
				`{"type":"error","error":{"type":"invalid_request_error","message":"invalid params, max_tokens must be less than 8192 (2013)"}}`,
				`{"type":"error","error":{"type":"invalid_request_error","message":"top_k is not supported by this model Request id: 0217"}}`,
			} {
				f.calls = 0
				f.refuse = func([]byte) (int, string) { return 400, other }
				if code, _ := post(t, "/v1/messages", `{"model":"m1","max_tokens":1,"messages":[{"role":"user","content":"."}]}`); code != 400 || f.calls != 1 {
					t.Errorf("%s: %d after %d calls", other, code, f.calls)
				}
			}
		})
	}
}

// The same refusal, escaped as \uXXXX the way a relay whose encoder
// escapes what isn't ASCII sends it (#1669): asked again all the same.
func TestAnthropicPassthroughAlwaysThinksInEscapedChinese(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`}
	f.refuse = func(b []byte) (int, string) {
		if bytes.Contains(b, []byte(`"disabled"`)) {
			return 400, `{"type":"error","error":{"type":"invalid_request_error","message":"\u8be5\u6a21\u578b\u59cb\u7ec8\u652f\u6301\u601d\u8003\uff0c\u4e0d\u53ef\u5173\u95ed"}}`
		}
		return 0, ""
	}
	setup(t, provider.Anthropic, f)
	code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[{"role":"user","content":"title?"}],"thinking":{"type":"disabled"}}`)
	if code != 200 || f.calls != 2 || bytes.Contains(f.got, []byte("thinking")) || !bytes.Contains(f.got, []byte(`"title?"`)) {
		t.Fatalf("%d %s after %d calls, last sent %s", code, body, f.calls, f.got)
	}
}

func TestOpenRouterMandatoryReasoningRetries(t *testing.T) {
	const refusal = `{"error":{"message":"OpenRouter: Reasoning is mandatory for this endpoint and cannot be disabled.","type":"invalid_request_error"},"type":"error"}`
	for _, tc := range []struct {
		name  string
		proto provider.Protocol
		path  string
		body  string
		calls int
	}{
		{"chat effort none", provider.Chat, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hi"}],"reasoning_effort":"none"}`, 2},
		{"chat enabled false", provider.Chat, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hi"}],"reasoning":{"enabled":false,"summary":"auto"}}`, 2},
		{"chat reasoning effort none", provider.Chat, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hi"}],"reasoning":{"effort":"none"}}`, 2},
		{"anthropic explicit disabled", provider.Anthropic, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[{"role":"user","content":"hi"}],"thinking":{"type":"disabled"}}`, 2},
		{"anthropic omitted", provider.Anthropic, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[{"role":"user","content":"hi"}]}`, 2},
	} {
		t.Run(tc.name, func(t *testing.T) {
			f := &fake{t: t, ctype: "application/json", reply: `{"id":"ok","choices":[],"content":[]}`}
			f.refuse = func(b []byte) (int, string) {
				var q map[string]json.RawMessage
				if json.Unmarshal(b, &q) != nil {
					t.Fatalf("invalid upstream body: %s", b)
				}
				if tc.proto == provider.Chat && hasReasoningDisabled(b) || tc.proto == provider.Anthropic && bytes.Contains(q["thinking"], []byte(`"disabled"`)) {
					return 400, refusal
				}
				return 0, ""
			}
			setup(t, tc.proto, f)
			p, err := provider.Find("fake")
			if err != nil {
				t.Fatal(err)
			}
			p.Preset = "openrouter"
			if err := provider.Save(*p); err != nil {
				t.Fatal(err)
			}
			code, body := post(t, tc.path, tc.body)
			if code != 200 || f.calls != tc.calls {
				t.Fatalf("%d %s after %d calls, last sent %s", code, body, f.calls, f.got)
			}
			if tc.proto == provider.Chat && hasReasoningDisabled(f.got) || tc.proto == provider.Anthropic && bytes.Contains(f.got, []byte(`"disabled"`)) {
				t.Fatalf("retry still disables reasoning: %s", f.got)
			}
			if tc.name == "chat enabled false" && !bytes.Contains(f.got, []byte(`"summary":"auto"`)) {
				t.Fatalf("retry dropped unrelated reasoning setting: %s", f.got)
			}
		})
	}
}

// Qoder asks "none" for its permission checks, which Command Code turns
// away with the levels it takes: asked again at the lowest, and at the
// lowest from then on
func TestPassthroughEffortNoneRefused(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"c1","choices":[]}`}
	f.refuse = func(b []byte) (int, string) {
		if bytes.Contains(b, []byte(`"reasoning_effort":"none"`)) {
			return 400, `{"error":{"message":"Command Code: Invalid option: expected one of \"low\"|\"medium\"|\"high\"|\"xhigh\"|\"max\""}}`
		}
		return 0, ""
	}
	setup(t, provider.Chat, f)
	srv := New()
	for turn, calls := range []int{2, 1} {
		f.calls = 0
		code, body := postTo(t, srv, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"ok?"}],"reasoning_effort":"none"}`)
		if code != 200 || f.calls != calls || !bytes.Contains(f.got, []byte(`"reasoning_effort":"low"`)) || !bytes.Contains(f.got, []byte(`"ok?"`)) {
			t.Fatalf("turn %d: %d %s after %d calls, last sent %s", turn+1, code, body, f.calls, f.got)
		}
	}
	// the levels named mark reasoning off as a whole: minimal, never
	// refused itself, goes at low too
	f.calls = 0
	if code, body := postTo(t, srv, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"ok?"}],"reasoning_effort":"minimal"}`); code != 200 || f.calls != 1 || !bytes.Contains(f.got, []byte(`"reasoning_effort":"low"`)) {
		t.Fatalf("minimal: %d %s after %d calls, last sent %s", code, body, f.calls, f.got)
	}
	// another 400 is the agent's to see
	f.calls = 0
	f.refuse = func([]byte) (int, string) { return 400, `{"error":{"message":"bad request"}}` }
	if code, _ := post(t, "/v1/chat/completions", `{"model":"m1","messages":[],"reasoning_effort":"none"}`); code != 400 || f.calls != 1 {
		t.Errorf("%d after %d calls", code, f.calls)
	}
}

// minimal refused with none among the levels taken, as OpenAI does for
// gpt-5.1: minimal goes at low from then on, and none still goes as sent.
// The error is OpenAI's wording as published for gpt-5.1, reconstructed,
// not captured.
func TestPassthroughMinimalRefusedNoneTaken(t *testing.T) {
	for _, c := range []struct {
		proto      provider.Protocol
		path, body string
	}{
		{provider.Chat, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"ok?"}],"reasoning_effort":%q}`},
		{provider.Responses, "/v1/responses", `{"model":"m1","input":"ok?","reasoning":{"effort":%q}}`},
	} {
		f := &fake{t: t, ctype: "application/json", reply: `{"id":"c1","choices":[]}`}
		f.refuse = func(b []byte) (int, string) {
			if bytes.Contains(b, []byte(`"minimal"`)) {
				return 400, `{"error":{"message":"Unsupported value: 'minimal' is not supported with the 'gpt-5.1' model. Supported values are: 'none', 'low', 'medium', and 'high'.","type":"invalid_request_error","param":"reasoning.effort","code":"unsupported_value"}}`
			}
			return 0, ""
		}
		setup(t, c.proto, f)
		srv := New()
		for i, turn := range []struct {
			effort, sent string
			calls        int
		}{{"minimal", "low", 2}, {"minimal", "low", 1}, {"none", "none", 1}} {
			f.calls = 0
			code, body := postTo(t, srv, c.path, fmt.Sprintf(c.body, turn.effort))
			if code != 200 || f.calls != turn.calls || bodyEffort(c.proto, f.got) != turn.sent {
				t.Fatalf("%s turn %d at %s: %d %s after %d calls, last sent %s", c.proto, i+1, turn.effort, code, body, f.calls, f.got)
			}
		}
	}
}

// the other level of reasoning off counts as taken only where an error
// lists it quoted, in JSON's escaped quotes too, not as a word in a sentence
func TestOtherOffNamed(t *testing.T) {
	for _, c := range []struct {
		effort, body string
		want         bool
	}{
		{"minimal", `{"error":{"message":"Unsupported value: 'minimal' is not supported with the 'gpt-5.1' model. Supported values are: 'none', 'low', 'medium', and 'high'."}}`, true},
		{"none", `{"error":{"message":"Invalid option: expected one of \"minimal\"|\"low\"|\"high\""}}`, true},
		{"minimal", `{"error":{"message":"Invalid option: expected one of \"none\"|\"low\"|\"high\""}}`, true},
		{"minimal", `{"error":{"message":"Invalid option: expected one of \"low\"|\"medium\"|\"high\". None of these is minimal."}}`, false},
	} {
		if got := otherOffNamed[c.effort].MatchString(c.body); got != c.want {
			t.Errorf("otherOffNamed[%s] on %s = %v", c.effort, c.body, got)
		}
	}
}

// effort without thinking asks for no reasoning on a Chat upstream
func TestAnthropicEffortWithoutThinking(t *testing.T) {
	f := &fake{t: t, reply: sse(`data: {"id":"c1","choices":[{"delta":{"content":"T"},"finish_reason":"stop"}]}`, `data: [DONE]`)}
	setup(t, provider.Chat, f)
	if code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"stream":true,"messages":[{"role":"user","content":"title?"}],"output_config":{"effort":"high"}}`); code != 200 {
		t.Fatalf("%d %s", code, body)
	}
	if bytes.Contains(f.got, []byte("reasoning_effort")) {
		t.Errorf("upstream asked to reason: %s", f.got)
	}
	if code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"stream":true,"messages":[{"role":"user","content":"hi"}],"thinking":{"type":"adaptive"},"output_config":{"effort":"high"}}`); code != 200 {
		t.Fatalf("%d %s", code, body)
	}
	if !bytes.Contains(f.got, []byte(`"reasoning_effort":"high"`)) {
		t.Errorf("adaptive thinking lost its effort: %s", f.got)
	}
}

func TestErrorsAndUnknownModel(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", code: 402, reply: `{"error":{"message":"Insufficient Balance","type":"x"}}`}
	setup(t, provider.Chat, f)
	code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"messages":[{"role":"user","content":"hi"}]}`)
	if code != 402 || !strings.Contains(body, `"type":"error"`) || !strings.Contains(body, "Fake: Insufficient Balance") {
		t.Errorf("%d %s", code, body)
	}
	code, body = post(t, "/v1/chat/completions", `{"model":"nope","messages":[]}`)
	if code != 404 || !strings.Contains(body, `"error":{`) || !strings.Contains(body, "m1") {
		t.Errorf("%d %s", code, body)
	}
}

func TestModelsList(t *testing.T) {
	setup(t, provider.Chat, &fake{t: t})
	rec := httptest.NewRecorder()
	New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil))
	if !strings.Contains(rec.Body.String(), `"id":"fake/m1"`) || !strings.Contains(rec.Body.String(), `"display_name"`) {
		t.Errorf("%s", rec.Body.String())
	}
}

// a group's context reaches /v1/models, for clients that read the window
// there
func TestModelsListContext(t *testing.T) {
	setup(t, provider.Chat, &fake{t: t})
	if err := provider.SaveGroup(provider.Group{ID: "big", Name: "Big", Members: []string{"fake/m1"}, Context: 1000000}); err != nil {
		t.Fatal(err)
	}
	rec := httptest.NewRecorder()
	New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil))
	var out struct {
		Data []map[string]any `json:"data"`
	}
	json.Unmarshal(rec.Body.Bytes(), &out)
	for _, m := range out.Data {
		if m["id"] == "group/big" {
			if m["context_window"] != float64(1000000) || m["context_length"] != float64(1000000) {
				t.Fatalf("%v", m)
			}
			return
		}
	}
	t.Fatalf("no group/big: %s", rec.Body.String())
}

func TestModelsListReasoning(t *testing.T) {
	fresh(t)
	if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil {
		t.Fatal(err)
	}
	data := `{"a":{"models":{
		"sol":{"id":"sol","reasoning_options":[{"type":"effort","values":["low","medium","high","max"]}]},
		"mixed":{"id":"mixed","reasoning_options":[{"type":"effort","values":["low","high"]}]},
		"plain":{"id":"plain"}}},
		"b":{"models":{
		"sol":{"id":"sol","reasoning_options":[{"type":"effort","values":["medium","high"]}]},
		"mixed":{"id":"mixed"}}}}`
	if err := os.WriteFile(catalog.CachePath(), []byte(data), 0o644); err != nil {
		t.Fatal(err)
	}
	catalog.Reset()
	t.Cleanup(catalog.Reset)
	for _, id := range []string{"a", "b"} {
		if err := provider.Save(provider.Provider{ID: id, Name: id, Catalog: id, Key: "k", Chat: "http://127.0.0.1:1/v1"}); err != nil {
			t.Fatal(err)
		}
	}
	rec := httptest.NewRecorder()
	New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil))
	if rec.Code != http.StatusOK {
		t.Fatalf("status %d: %s", rec.Code, rec.Body.String())
	}
	var response struct {
		Data []struct {
			ID        string `json:"id"`
			Reasoning *bool  `json:"reasoning"`
			Levels    []struct {
				Effort string `json:"effort"`
			} `json:"supported_reasoning_levels"`
			Flat *[]string `json:"reasoning_levels"`
		} `json:"data"`
	}
	if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
		t.Fatal(err)
	}
	want := map[string]string{
		"a/sol": "low,medium,high,max", "b/sol": "medium,high", "auto/sol": "medium,high",
		"a/mixed": "low,high", "b/mixed": "", "auto/mixed": "low,high", "a/plain": "",
	}
	// a group reasons when any member does: b/mixed has no levels and a
	// list that says nothing of reasoning, but a/mixed does, so the group
	// is told to reason (#756's rule for images, applied to reasoning),
	// at a/mixed's levels: b/mixed is sent no reasoning ask (quiet.go), so
	// it doesn't take them away (#2127: Codex showed no effort control for
	// a group with none, and ran it at medium)
	reasoning := map[string]bool{"auto/mixed": true}
	for _, m := range response.Data {
		expected, ok := want[m.ID]
		if !ok {
			continue
		}
		delete(want, m.ID)
		var levels []string
		for _, level := range m.Levels {
			levels = append(levels, level.Effort)
		}
		if m.Reasoning == nil || *m.Reasoning != (expected != "" || reasoning[m.ID]) || strings.Join(levels, ",") != expected {
			t.Errorf("%s: reasoning %v, levels %v; want %q", m.ID, m.Reasoning, levels, expected)
		}
		// the same levels as plain names, in the same order, for clients
		// of the Catwalk schema (#2576): [] when there are none, never
		// null or missing, which such a client reads as "takes no effort"
		if m.Flat == nil || *m.Flat == nil || strings.Join(*m.Flat, ",") != expected {
			t.Errorf("%s: reasoning_levels %v; want [%s]", m.ID, m.Flat, expected)
		}
	}
	if len(want) > 0 {
		t.Errorf("missing models: %v", want)
	}
	// one model asked for by its id says them too
	rec = httptest.NewRecorder()
	New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models/b/sol", nil))
	if one := rec.Body.String(); !strings.Contains(one, `"reasoning_levels":["medium","high"]`) {
		t.Errorf("/v1/models/b/sol: %s; want reasoning_levels [medium high]", one)
	}
}

func TestGeminiClientChatUpstream(t *testing.T) {
	f := &fake{t: t, reply: sse(
		`data: {"id":"c1","model":"m1","choices":[{"delta":{"role":"assistant","reasoning_content":"think"}}]}`,
		`data: {"id":"c1","choices":[{"delta":{"content":"Sure"}}]}`,
		`data: {"id":"c1","choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"name":"read_file","arguments":"{\"path\":"}}]}}]}`,
		`data: {"id":"c1","choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"a.go\"}"}}]}}]}`,
		`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"tool_calls"}]}`,
		`data: {"id":"c1","choices":[],"usage":{"prompt_tokens":10,"completion_tokens":5,"completion_tokens_details":{"reasoning_tokens":2}}}`,
		`data: [DONE]`)}
	setup(t, provider.Chat, f)
	req := `{"systemInstruction":{"parts":[{"text":"be brief"}]},
	  "contents":[{"role":"user","parts":[{"text":"read a.go"}]},
	    {"role":"model","parts":[{"functionCall":{"id":"fc0","name":"read_file","args":{"path":"x"}}}]},
	    {"role":"user","parts":[{"functionResponse":{"id":"fc0","name":"read_file","response":{"output":"package x"}}}]}],
	  "tools":[{"functionDeclarations":[{"name":"read_file","description":"read","parameters":{"type":"OBJECT","properties":{"path":{"type":"STRING"}}}}]}],
	  "generationConfig":{"maxOutputTokens":100,"thinkingConfig":{"thinkingBudget":-1,"includeThoughts":true}}}`
	code, body := post(t, "/v1beta/models/fake/m1:streamGenerateContent?alt=sse", req)
	if code != 200 {
		t.Fatalf("status %d: %s", code, body)
	}
	var up map[string]any
	json.Unmarshal(f.got, &up)
	if up["model"] != "m1" || up["stream"] != true || up["reasoning_effort"] != "medium" || up["max_tokens"] != float64(100) {
		t.Errorf("upstream: %s", f.got)
	}
	msgs := up["messages"].([]any)
	if len(msgs) != 4 || msgs[0].(map[string]any)["content"] != "be brief" || msgs[3].(map[string]any)["tool_call_id"] != "fc0" {
		t.Errorf("messages: %v", msgs)
	}
	if fn := up["tools"].([]any)[0].(map[string]any)["function"].(map[string]any); !strings.Contains(string(mustJSON(fn["parameters"])), `"type":"object"`) {
		t.Errorf("schema not lowercased: %v", fn)
	}
	evs := events(body)
	var parts []map[string]any
	var finish string
	for _, e := range evs {
		c := e["candidates"].([]any)[0].(map[string]any)
		for _, p := range c["content"].(map[string]any)["parts"].([]any) {
			parts = append(parts, p.(map[string]any))
		}
		if fr, ok := c["finishReason"]; ok {
			finish = fr.(string)
		}
	}
	if len(parts) != 3 || parts[0]["thought"] != true || parts[1]["text"] != "Sure" {
		t.Errorf("parts: %v", parts)
	}
	fc, _ := parts[2]["functionCall"].(map[string]any)
	if fc == nil || fc["name"] != "read_file" || fc["id"] != "call_1" || fc["args"].(map[string]any)["path"] != "a.go" {
		t.Errorf("function call: %v", parts[2])
	}
	last := evs[len(evs)-1]
	um := last["usageMetadata"].(map[string]any)
	if finish != "STOP" || um["promptTokenCount"] != float64(10) || um["candidatesTokenCount"] != float64(3) || um["thoughtsTokenCount"] != float64(2) {
		t.Errorf("finish/usage: %s %v", finish, um)
	}
	// non-streaming, error shape, countTokens, model list
	code, body = post(t, "/v1beta/models/fake/m1:generateContent", req)
	if code != 200 || !strings.Contains(body, `"finishReason":"STOP"`) || !strings.Contains(body, `"name":"read_file"`) {
		t.Errorf("generateContent: %d %s", code, body)
	}
	code, body = post(t, "/v1beta/models/nope:generateContent", `{"contents":[]}`)
	if code != 404 || !strings.Contains(body, `"status":"NOT_FOUND"`) {
		t.Errorf("unknown model: %d %s", code, body)
	}
	code, body = post(t, "/v1beta/models/fake/m1:countTokens", req)
	if code != 200 || !strings.Contains(body, `"totalTokens":`) {
		t.Errorf("countTokens: %d %s", code, body)
	}
	rec := httptest.NewRecorder()
	New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1beta/models", nil))
	if !strings.Contains(rec.Body.String(), `"name":"models/fake/m1"`) {
		t.Errorf("models: %s", rec.Body.String())
	}
}

func mustJSON(v any) []byte { b, _ := json.Marshal(v); return b }

// A signed-in Codex account: the ChatGPT backend only streams and rejects
// the sampling knobs, so a plain non-streaming Responses call is
// translated, signed with the account's tokens, and answered as JSON.
func TestCodexAccountUpstream(t *testing.T) {
	home := t.TempDir()
	t.Setenv("HOME", home)
	t.Setenv("USERPROFILE", home)
	t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
	t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
	claims := func(m map[string]any) string {
		b, _ := json.Marshal(m)
		return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
	}
	os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
	auth := fmt.Sprintf(`{"auth_mode":"chatgpt","tokens":{"id_token":%q,"access_token":%q,"refresh_token":"r","account_id":"acct-1"}}`,
		claims(map[string]any{"email": "[email protected]"}), claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}))
	os.WriteFile(filepath.Join(home, ".codex", "auth.json"), []byte(auth), 0o600)

	f := &fake{t: t, ctype: "none", reply: sse(
		`data: {"type":"response.created","response":{"id":"r1","model":"gpt-5.5"}}`,
		`data: {"type":"response.output_text.delta","delta":"pong"}`,
		`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`)}
	up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		body, err := io.ReadAll(r.Body)
		if err != nil {
			t.Error(err)
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		var request struct {
			Input []struct {
				Role string `json:"role"`
			} `json:"input"`
		}
		if err := json.Unmarshal(body, &request); err != nil {
			t.Error(err)
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		for _, item := range request.Input {
			if item.Role == "system" {
				w.WriteHeader(http.StatusBadRequest)
				fmt.Fprint(w, `{"detail":"System messages are not allowed"}`)
				return
			}
		}
		r.Body = io.NopCloser(bytes.NewReader(body))
		f.ServeHTTP(w, r)
	}))
	defer up.Close()
	old := provider.CodexBase
	provider.CodexBase = up.URL + "/backend-api/codex"
	defer func() { provider.CodexBase = old }()

	code, body := post(t, "/v1/responses", `{"model":"codex/gpt-5.5","input":"ping","max_output_tokens":20,"temperature":0.3}`)
	if code != 200 {
		t.Fatalf("status %d: %s", code, body)
	}
	if f.path != "/backend-api/codex/responses" || f.head.Get("chatgpt-account-id") != "acct-1" || !strings.HasPrefix(f.head.Get("Authorization"), "Bearer h.") {
		t.Fatalf("upstream call: %s %v", f.path, f.head)
	}
	var upstream map[string]any
	json.Unmarshal(f.got, &upstream)
	if _, ok := upstream["max_output_tokens"]; ok || upstream["temperature"] != nil || upstream["stream"] != true || upstream["store"] != false || upstream["model"] != "gpt-5.5" {
		t.Fatalf("upstream body: %s", f.got)
	}
	var res map[string]any
	json.Unmarshal([]byte(body), &res)
	if res["object"] != "response" || !strings.Contains(body, "pong") {
		t.Fatalf("reply: %s", body)
	}

	// a streaming call is relayed as-is, minus what the backend rejects
	f.got, f.head = nil, nil
	code, body = post(t, "/v1/responses", `{"model":"codex/gpt-5.5","input":"ping","stream":true,"max_output_tokens":20}`)
	if code != 200 || !strings.Contains(body, "response.output_text.delta") {
		t.Fatalf("stream: %d %s", code, body)
	}
	json.Unmarshal(f.got, &upstream)
	if _, ok := upstream["max_output_tokens"]; ok {
		t.Fatalf("relayed body: %s", f.got)
	}
	if _, isList := upstream["input"].([]any); !isList {
		t.Fatalf("relayed input not a list: %s", f.got)
	}

	for _, tc := range []struct {
		name string
		path string
		body string
	}{
		{"anthropic", "/v1/messages", `{"model":"codex/gpt-5.5","max_tokens":20,"system":"Top-level instructions","messages":[{"role":"system","content":"First instruction"},{"role":"user","content":"ping"},{"role":"system","content":"Later instruction"}]}`},
		{"responses passthrough", "/v1/responses", `{"model":"codex/gpt-5.5","stream":true,"instructions":"Top-level instructions","input":[{"type":"message","role":"system","content":[{"type":"input_text","text":"First instruction"}]},{"role":"user","content":"ping"},{"role":"system","content":"Later instruction"}]}`},
	} {
		t.Run(tc.name, func(t *testing.T) {
			code, body := post(t, tc.path, tc.body)
			if code != http.StatusOK || !strings.Contains(body, "pong") {
				t.Fatalf("system instructions rejected: %d %s", code, body)
			}
			var request struct {
				Instructions string `json:"instructions"`
				Input        []struct {
					Role    string          `json:"role"`
					Content json.RawMessage `json:"content"`
				} `json:"input"`
			}
			if err := json.Unmarshal(f.got, &request); err != nil {
				t.Fatal(err)
			}
			// Codex's instructions go as Codex CLI sends them, the client's first in the input
			if !strings.HasPrefix(request.Instructions, "You are Codex") || len(request.Input) != 4 {
				t.Fatalf("instructions lost or reordered: %s", f.got)
			}
			for i, want := range []struct{ role, text string }{{"developer", "Top-level instructions"}, {"developer", "First instruction"}, {"user", "ping"}, {"developer", "Later instruction"}} {
				item := request.Input[i]
				if item.Role != want.role || stringOrText(item.Content) != want.text {
					t.Errorf("input[%d]: role=%q content=%s, want %s %q", i, item.Role, item.Content, want.role, want.text)
				}
			}
		})
	}
}

func TestConversationID(t *testing.T) {
	h := http.Header{}
	h.Set("X-Session-Affinity", "ses_1")
	if got := conversationID(h, []byte(`{}`)); got != "ses_1" {
		t.Errorf("agent session header: %q", got)
	}
	turn1 := `{"messages":[{"role":"system","content":"s"},{"role":"user","content":"fix the bug"}]}`
	turn2 := `{"messages":[{"role":"system","content":"s"},{"role":"user","content":"fix the bug"},{"role":"assistant","content":"done"},{"role":"user","content":"thanks"}]}`
	other := `{"messages":[{"role":"system","content":"s"},{"role":"user","content":"write docs"}]}`
	a, b, c := conversationID(http.Header{}, []byte(turn1)), conversationID(http.Header{}, []byte(turn2)), conversationID(http.Header{}, []byte(other))
	if a != b || a == c || !strings.HasPrefix(a, "magpie-") {
		t.Errorf("derived ids: %q %q %q", a, b, c)
	}
	r1 := conversationID(http.Header{}, []byte(`{"input":[{"role":"user","content":"hi"}]}`))
	r2 := conversationID(http.Header{}, []byte(`{"input":[{"role":"user","content":"hi"},{"type":"function_call_output","output":"x"}]}`))
	if r1 != r2 {
		t.Errorf("responses ids: %q %q", r1, r2)
	}
	// Gemini requests have contents, not messages or input. Later turns repeat
	// the first user content even as the system instruction and history grow.
	g1 := conversationID(http.Header{}, []byte(`{"model":"m","contents":[{"role":"user","parts":[{"text":"fix the bug"}]}]}`))
	g2 := conversationID(http.Header{}, []byte(`{"model":"m","systemInstruction":{"parts":[{"text":"help"}]},"contents":[{"role":"user","parts":[{"text":"fix the bug"}]},{"role":"model","parts":[{"text":"done"}]},{"role":"user","parts":[{"text":"thanks"}]}]}`))
	gOther := conversationID(http.Header{}, []byte(`{"model":"m","contents":[{"role":"user","parts":[{"text":"write docs"}]}]}`))
	if g1 != g2 || g1 == gOther || !strings.HasPrefix(g1, "magpie-") {
		t.Errorf("gemini derived ids: %q %q %q", g1, g2, gOther)
	}
	// Gemini accepts an omitted role on a user content. When history adds an
	// explicit user turn, affinity must still use the first role-less content.
	rolelessFirst := `{"parts":[{"text":"fix the bug"}]}`
	gRoleless1 := conversationID(http.Header{}, []byte(`{"contents":[`+rolelessFirst+`]}`))
	gRoleless2 := conversationID(http.Header{}, []byte(`{"contents":[`+rolelessFirst+`,{"role":"model","parts":[{"text":"done"}]},{"role":"user","parts":[{"text":"thanks"}]}]}`))
	gRolelessOther := conversationID(http.Header{}, []byte(`{"contents":[{"parts":[{"text":"write docs"}]},{"role":"user","parts":[{"text":"thanks"}]}]}`))
	if gRoleless1 != gRoleless2 || gRoleless1 == gRolelessOther {
		t.Errorf("role-less gemini derived ids: %q %q %q", gRoleless1, gRoleless2, gRolelessOther)
	}
	// A role-less item is Gemini-specific. Chat and Responses still look for
	// the first explicitly marked user turn.
	for name, pair := range map[string][2]string{
		"chat":      {`{"messages":[{"content":"preface"},{"role":"user","content":"hi"}]}`, `{"messages":[{"role":"user","content":"hi"}]}`},
		"responses": {`{"input":[{"content":"preface"},{"role":"user","content":"hi"}]}`, `{"input":[{"role":"user","content":"hi"}]}`},
	} {
		if got, want := conversationID(http.Header{}, []byte(pair[0])), conversationID(http.Header{}, []byte(pair[1])); got != want {
			t.Errorf("%s changed explicit-user selection: %q != %q", name, got, want)
		}
	}
}

func TestSessionOf(t *testing.T) {
	h := http.Header{}
	if got := sessionOf(h); got != "" {
		t.Errorf("none: %q", got)
	}
	h.Set("X-Claude-Code-Session-Id", "cc-1")
	if got := sessionOf(h); got != "cc-1" {
		t.Errorf("claude code's: %q", got)
	}
	h.Set(SessionHeader, " mine ")
	if got := sessionOf(h); got != "mine" {
		t.Errorf("named: %q", got)
	}
	h.Set(SessionHeader, strings.Repeat("x", 300))
	if got := sessionOf(h); len(got) != 128 {
		t.Errorf("long: %d", len(got))
	}
}

func TestOpenCodeGetsConversationSession(t *testing.T) {
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"c1","choices":[]}`}
	up := setup(t, provider.Chat, f)
	body := `{"model":"m1","messages":[{"role":"user","content":"hi"}]}`
	if code, out := post(t, "/v1/chat/completions", body); code != 200 || f.head.Get("x-opencode-session") != "" {
		t.Fatalf("other vendors get no OpenCode header: %d %s %v", code, out, f.head)
	}

	// The same upstream, reached under OpenCode's host name.
	if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Chat: "http://opencode.ai/zen/go/v1"}); err != nil {
		t.Fatal(err)
	}
	s := New()
	addr := strings.TrimPrefix(up.URL, "http://")
	s.client = &http.Client{Transport: &http.Transport{DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
		return (&net.Dialer{}).DialContext(ctx, network, addr)
	}}}
	send := func(h http.Header) string {
		rec := httptest.NewRecorder()
		req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body))
		for k, v := range h {
			req.Header[k] = v
		}
		s.Handler().ServeHTTP(rec, req)
		if rec.Code != 200 {
			t.Fatalf("%d %s", rec.Code, rec.Body.String())
		}
		return f.head.Get("x-opencode-session")
	}
	// in OpenCode's form (ses_…), one for each of the agent's sessions
	codex := send(http.Header{"Session_id": {"codex-ses"}})
	if !strings.HasPrefix(codex, "ses_") || send(http.Header{"Session_id": {"codex-ses"}}) != codex {
		t.Errorf("agent's session: %q", codex)
	}
	if a, b := send(nil), send(nil); !strings.HasPrefix(a, "ses_") || a != b || a == codex {
		t.Errorf("derived session: %q %q", a, b)
	}
}

// A translation goes to Chat Completions first, but an OpenAI model on
// OpenAI's or Copilot's API goes to Responses first; either falls back.
func TestUsableOrder(t *testing.T) {
	fresh(t) // not this machine's cached model lists, which say where each model is served
	s := New()
	openai := provider.Provider{ID: "openai", Chat: "https://api.openai.com/v1", Responses: "https://api.openai.com/v1"}
	copilot := provider.Provider{ID: "copilot", Chat: "https://api.githubcopilot.com", Responses: "https://api.githubcopilot.com"}
	other := provider.Provider{ID: "groq", Chat: "https://api.groq.com/openai/v1", Responses: "https://api.groq.com/openai/v1"}
	for _, tc := range []struct {
		p     provider.Provider
		model string
		want  string
	}{
		{openai, "gpt-6-sol", "responses chat"},
		{openai, "o4-mini", "responses chat"},
		{copilot, "gpt-6-sol", "responses chat"},
		{copilot, "claude-opus-5.5", "chat responses"},
		{other, "openai/gpt-oss-120b", "chat responses"},
	} {
		var got []string
		for _, p := range s.usable(tc.p, tc.model) {
			got = append(got, string(p))
		}
		if strings.Join(got, " ") != tc.want {
			t.Errorf("usable(%s, %s) = %v, want %s", tc.p.ID, tc.model, got, tc.want)
		}
	}
	s.markUnfit("copilot", "gpt-6-sol", provider.Responses)
	if got := s.usable(copilot, "gpt-6-sol"); len(got) != 1 || got[0] != provider.Chat {
		t.Errorf("after /responses turned it away: %v", got)
	}
}

// A provider whose model list says which APIs each model is served on
// (Copilot's does) has each request sent straight to one of them: nothing
// is tried on an API the model isn't served on.
func TestModelListSaysWhichAPI(t *testing.T) {
	t.Setenv("XDG_CONFIG_HOME", t.TempDir())
	t.Setenv("XDG_CACHE_HOME", t.TempDir())
	paths := map[string]int{}
	up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		io.ReadAll(r.Body)
		paths[r.URL.Path]++
		w.Header().Set("Content-Type", "text/event-stream")
		switch r.URL.Path {
		case "/v1/responses":
			io.WriteString(w, sse(
				`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1","model":"gpt-x","usage":{"input_tokens":0,"output_tokens":0}}}`,
				`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"ok"}`,
				`event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r1","model":"gpt-x","status":"completed","usage":{"input_tokens":1,"output_tokens":1}}}`))
		case "/v1/chat/completions":
			io.WriteString(w, sse(
				`data: {"id":"c1","model":"claude-x","choices":[{"delta":{"role":"assistant","content":"ok"}}]}`,
				`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}]}`,
				`data: [DONE]`))
		default:
			w.WriteHeader(http.StatusBadRequest)
			io.WriteString(w, `{"error":{"message":"unexpected"}}`)
		}
	}))
	t.Cleanup(up.Close)
	if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"gpt-x", "claude-x"},
		Chat: up.URL + "/v1", Responses: up.URL + "/v1", Anthropic: up.URL}); err != nil {
		t.Fatal(err)
	}
	if err := catalog.SaveLive("fake", up.URL+"/v1", []catalog.Model{
		{ID: "gpt-x", APIs: []string{"responses"}},
		{ID: "claude-x", APIs: []string{"chat"}},
	}); err != nil {
		t.Fatal(err)
	}
	handler := New().Handler()
	for _, tc := range []struct{ path, body, want string }{
		// an Anthropic client, and the model on Responses alone
		{"/v1/messages", `{"model":"gpt-x","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`, "/v1/responses"},
		// a Chat client, likewise: not relayed
		{"/v1/chat/completions", `{"model":"gpt-x","messages":[{"role":"user","content":"hi"}]}`, "/v1/responses"},
		// a Responses client, and the model on Chat alone
		{"/v1/responses", `{"model":"claude-x","input":"hi","stream":true}`, "/v1/chat/completions"},
		// an Anthropic client the provider speaks to, but not for this model
		{"/v1/messages", `{"model":"claude-x","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`, "/v1/chat/completions"},
	} {
		clear(paths)
		rec := httptest.NewRecorder()
		handler.ServeHTTP(rec, httptest.NewRequest("POST", tc.path, strings.NewReader(tc.body)))
		if rec.Code != 200 || len(paths) != 1 || paths[tc.want] != 1 {
			t.Errorf("%s %s: status %d, upstream %v, want only %s: %s", tc.path, tc.body, rec.Code, paths, tc.want, rec.Body.String())
		}
	}
}

// An agent that hangs up once the last event is in — Codex does, while the
// ChatGPT backend is still to close its end — had its answer: the call is
// served, not canceled. One that hangs up before is canceled.
func TestHangUpAfterTheLastEvent(t *testing.T) {
	for _, whole := range []bool{true, false} {
		last := `data: {"type":"response.output_text.delta","delta":"hi"}`
		if whole {
			last = `data: {"type":"response.completed","response":{"usage":{"input_tokens":5,"output_tokens":1}}}`
		}
		up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Content-Type", "text/event-stream")
			io.WriteString(w, sse(`data: {"type":"response.created","response":{}}`, last))
			w.(http.Flusher).Flush()
			<-r.Context().Done()
		}))
		t.Setenv("XDG_CONFIG_HOME", t.TempDir())
		t.Setenv("XDG_CACHE_HOME", t.TempDir())
		provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Responses: up.URL + "/v1"})
		s := New()
		gw := httptest.NewServer(s.Handler())
		ctx, cancel := context.WithCancel(context.Background())
		req, _ := http.NewRequestWithContext(ctx, "POST", gw.URL+"/v1/responses", strings.NewReader(`{"model":"fake/m1","stream":true,"input":"hi"}`))
		res, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		buf := make([]byte, 4096)
		for got := ""; !strings.Contains(got, strings.TrimPrefix(last, "data: ")); {
			n, err := res.Body.Read(buf)
			if err != nil {
				t.Fatal(err)
			}
			got += string(buf[:n])
		}
		cancel()
		var calls []Call
		for deadline := time.Now().Add(5 * time.Second); len(calls) == 0 && time.Now().Before(deadline); {
			time.Sleep(10 * time.Millisecond)
			calls = s.Recent()
		}
		if want := map[bool]int{true: 200, false: 499}[whole]; len(calls) != 1 || calls[0].Status != want {
			t.Fatalf("whole %v: %+v", whole, calls)
		}
		gw.Close()
		up.Close()
	}
}

// Anthropic's input count leaves out what the prompt read from its cache
// and wrote to it; OpenAI's and Gemini's count both.
func TestUsagePromptCountsCacheWrites(t *testing.T) {
	u := Usage{Input: 2, Output: 30, CacheRead: 6176, CacheWrite: 20563}
	if r := u.responses(); r["input_tokens"] != 26741 || r["total_tokens"] != 26771 {
		t.Errorf("responses: %v", r)
	}
	if c := u.chat(); c["prompt_tokens"] != 26741 {
		t.Errorf("chat: %v", c)
	}
	if g := u.gemini(); g["promptTokenCount"] != 26741 || g["totalTokenCount"] != 26771 {
		t.Errorf("gemini: %v", g)
	}
}

// Reject invalid envelopes before routing, including on passthrough APIs.
func TestRequestValidationBeforeRouting(t *testing.T) {
	f := &fake{t: t}
	setup(t, provider.Responses, f)
	chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
		t.Error("invalid request reached OpenAI")
		w.WriteHeader(400)
	})
	cases := []struct{ name, body string }{
		{"empty", ""},
		{"truncated", `{"model":"fake/m1","input":`},
		{"null", `null`},
		{"array", `[]`},
		{"missing_model", `{}`},
		{"null_model", `{"model":null}`},
		{"wrong_model_type", `{"model":7}`},
		{"blank_model", `{"model":"  "}`},
		{"empty_provider", `{"model":"/m1"}`},
		{"empty_vendor_model", `{"model":"fake/"}`},
		{"trailing_junk", `{"model":"fake/m1"}garbage`},
		{"second_object", `{"model":"fake/m1"}{}`},
	}
	for _, path := range []string{"/v1/responses", "/v1/chat/completions", "/v1/messages", "/v1/messages/count_tokens", CodexPath + "/responses", CodexPath + "/responses/compact"} {
		t.Run(path, func(t *testing.T) {
			for _, tc := range cases {
				t.Run(tc.name, func(t *testing.T) {
					code, body := post(t, path, tc.body)
					if code != 400 || !json.Valid([]byte(body)) {
						t.Fatalf("status %d: %s", code, body)
					}
					if f.calls != 0 {
						t.Fatalf("invalid request reached provider: %d calls", f.calls)
					}
				})
			}
		})
	}
}

func TestGeminiRequestValidation(t *testing.T) {
	setHome(t, t.TempDir())
	f := &fake{t: t, reply: sse(
		`data: {"id":"c1","choices":[{"delta":{"content":"OK"},"finish_reason":"stop"}]}`,
		`data: [DONE]`)}
	setup(t, provider.Chat, f)
	for _, method := range []string{"generateContent", "streamGenerateContent", "countTokens"} {
		t.Run(method, func(t *testing.T) {
			for _, body := range []string{"", `null`, `[]`, `{"contents":`, `{"contents":[]}junk`, `{"contents":[]}{}`} {
				code, reply := post(t, "/v1beta/models/fake/m1:"+method, body)
				if code != 400 || !strings.Contains(reply, `"INVALID_ARGUMENT"`) {
					t.Fatalf("body %q: %d %s", body, code, reply)
				}
			}
		})
	}
	if f.calls != 0 {
		t.Fatalf("invalid request reached provider: %d calls", f.calls)
	}
	// Gemini gets its model from the URL, not a required body field.
	code, body := post(t, "/v1beta/models/fake/m1:generateContent", `{"contents":[{"parts":[{"text":"hi"}]}]}`)
	if code != 200 || f.calls != 1 || !strings.Contains(body, "OK") {
		t.Fatalf("URL model: %d %s, calls %d", code, body, f.calls)
	}
}

func TestRequestValidationPreservesPayload(t *testing.T) {
	setHome(t, t.TempDir())
	f := &fake{t: t, ctype: "application/json", reply: `{"id":"r1","output":[]}`}
	setup(t, provider.Responses, f)
	code, body := post(t, "/v1/responses", `{"model":"  fake/vendor/new-model  ","input":"hi","extension":{"number":9007199254740993}}`)
	if code != 200 || f.calls != 1 || modelOf(f.got) != "vendor/new-model" || !strings.Contains(string(f.got), "9007199254740993") {
		t.Fatalf("%d %s; upstream %s, calls %d", code, body, f.got, f.calls)
	}
	// Valid unknown models can still use the existing local token estimate.
	code, body = post(t, "/v1/messages/count_tokens", `{"model":"unknown","messages":[{"role":"user","content":"hello"}]}`)
	if code != 200 || !strings.Contains(body, `"input_tokens"`) || f.calls != 1 {
		t.Fatalf("token estimate: %d %s, calls %d", code, body, f.calls)
	}
}