Files
magpie/internal/gateway/qoder_test.go
T
Ethan Xu 81736b2fb2 subscription: Qoder is a subscription you sign in to and route throug… (#162)
* subscription: Qoder is a subscription you sign in to and route through — Qoder lists no public API, so magpie speaks the endpoint its own desktop client uses: api3.qoder.sh's agent_chat_generation SSE, wrapped in the COSY envelope the client signs with (an RSA-encrypted key and a signed request line), the body through the client's own base64 codec, and the account reached by Qoder's OAuth device flow (qoder.com -> a dt- device token -> a jt- job token that serves models and rotates from its refresh token). The protocol lives in internal/qoder (endpoints, codec, cosy, oauth, models, a credential store, and /api/v1/userinfo, asked with the device token at sign-in so the account is labelled by its email and name rather than a bare uid); provider/qoder.go is who is signed in, its models, and the sign-in; gateway/qoder.go turns a request into a Qoder body and reads the SSE back, folding tool calls and results the way the client does. It is offered as a single, risky subscription in the Providers page: Qoder has no public API for this, and may treat a request that isn't its own client's as third-party use — the tile says so and points at an account you can afford to lose. Tested: the codec, the COSY signing, the model list, the request body and a served round-trip against stubs, and live — a signed-in account answered a Pi request for qfmodel end to end. Not tested: the userinfo email/name path against a fresh real sign-in (the device token is short-lived), and multi-turn tool use against the real client's exact framing.

* subscription: Qoder uses the chosen model and keeps rotating tokens consistent

Qoder used qfmodel regardless of the caller's model, refreshed the same rotating token concurrently, and could return upstream failures as empty successful replies. Requests now use the selected account's enabled model key and complete model_config; unknown or disabled models are refused. Token checking, rotation and atomic persistence are serialized, each caller receives a separate credential, and a rotated pair is retained for a persistence retry if its disk write fails.

Accounts are kept in logins.json with multiple accounts, selection, secondary-account toggles, usage and removal, including the last account. The old qoder.json is migrated after a successful write. A sign-in generates one machine ID that the account keeps for subsequent requests; the OS and architecture follow the running platform. Login polling expires after 15 minutes.

Qoder's response path maps non-200 SSE envelope statuses to HTTP errors before output and terminal error events after streaming begins, including requests using magpie's search rounds. Tool-result headings stay beside their own results; normal EOF flushes pending text and emits a stop event even after tool calls. Thinking, effort and context settings follow the selected model (low/medium/high is the fallback when no effort levels are declared). Corrected the Chinese risk wording and account plan label, removed unused protocol fields and the unsupported China-client claim, and included ufec/CLIProxyAPI's MIT copyright and license. Codec and signature tests now have fixed expected answers instead of relying only on round trips or an absolute Windows capture path.

The shared relay in claude_subscription.go is unchanged. Its error-status propagation and handling of failures after partial non-streaming output are existing upstream issues outside this change; Qoder handles these failures in its own relayQoder response path.

Tested: Qoder, codec, COSY and shared stream-error regressions passed 20 consecutive runs, including eight concurrent requests refreshing once, persistence failure recovery, account migration and removal, sign-in and usage against local HTTP stubs, and error handling across Chat, Responses, Anthropic and Gemini. go vet ./..., the production build, JavaScript syntax checks and git diff --check pass. The complete Windows test run has the same 72 failing tests/subtests as HEAD, with no new failures; the existing Claude shell-client test still times out. Not tested against a live Qoder account. The race detector could not build because gcc is unavailable.

* subscription: Qoder keeps its rotating tokens, speaks tools in its own
API's way, and thinks at the effort each model declares

An aborted request could sign the account out. A refresh ran on the
request's own context, so one cancelled after Qoder had rotated the pair
but before its reply arrived dropped the new pair and left the spent one
saved: every later call failed with "qoder job token refresh: status 401"
until the account was signed in again. The failure wasn't recorded
either, so the account looked healthy in the app while every request
failed. Refreshing now runs on context.WithoutCancel with its own 20s
timeout, in provider/qoder.go and the device-token refresh in
qoder_usage.go, and a refresh Qoder refuses (401 or 403, either token)
marks the account lapsed: Qoder's job-token refresh returns a
JobTokenRefreshHTTPError with its status, as the device one did. Side
logins don't carry Lapsed out, so qoderLogins passes it on.

A refresh blocked every other subscription. Both held qoderMu and the
global loginsMu across the HTTP call, up to 20s — with a 2s refresh in
flight, Logins("workbuddy") blocked 1.9s. qoderMu still serializes read,
rotate and write-back so one pair is rotated once; loginsMu is taken only
to read (qoderLookup) and to write back (qoderPersist), and the write-back
re-reads logins.json, so a change made meanwhile is kept. migrateQoder
takes no lock when there's no legacy file to move, or listing Qoder's own
accounts would wait behind a refresh too.

Tool calls were carried as XML in the text with values magpie HTML-
escaped, so an argument reached the model as "x", the model took
to imitating the escaping, and a value it meant literally — "< b &&"
— was unescaped on the way back and corrupted. A call had no id while
its result said "Function result for call A". Qoder's endpoint serves
OpenAI's own tool calls: asked with a tools array it answers with
tool_calls carrying ids, and accepts assistant tool_calls plus a role:tool
turn named by tool_call_id. So they are sent and read that way now: no
XML contract in the prompt, no escaping on either side, and a result
paired with its call by the id Qoder sent. The XML splitter stays as a
fallback for a model that still writes a call in its text.

Effort was chosen from reasoning_efforts, a field no model lists, so
every model got the low/medium/high fallback: gfmodel, which offers only
high and max, was asked for medium and refused with "该模型始终思考,
不支持关闭思考", and xhigh was clamped to high. The levels and the
default come from thinking_config now — asked for nothing, or for a
level Qoder doesn't name, a model thinks at its own default; a model
that can't stop thinking thinks at its lowest when asked for none; one
that declares no levels is sent none. A model whose only thinking_config
is enabled now thinks, as it does in Qoder's own client. Upstream
failures carry the vendor's reason from details, which had been dropped.

Tested: go test ./... and go vet on the changed packages, and the darwin
and windows builds. New regressions cover a refresh outliving an aborted
request, a refused refresh lapsing the account, logins and a concurrent
write-back during a refresh in flight, tool turns paired by id, native
tools and streamed tool_calls, and effort defaults. Live, against a
signed-in account: two parallel calls and a follow-up call each got
their own result; a file written with "< b &&" kept them; gfmodel,
asked for nothing, answered at max; qfmodel's xhigh reached Qoder as
xhigh. Not tested: a refresh the upstream refuses for real, and models
the account isn't offered.
2026-09-29 16:40:20 +08:00

435 lines
17 KiB
Go

package gateway
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/qoder"
)
func testQoderModel(t *testing.T) qoder.ModelInfo {
t.Helper()
ms, err := qoder.ModelConfigs([]byte(`{"chat":[{"key":"qfmodel","enable":true,"display_name":"Qwen3.8-Flash","format":"openai","source":"system","is_vl":true,"is_reasoning":true,"max_input_tokens":200000,
"thinking_config":{"disabled":{"description":"Disable thinking"},"enabled":{"is_default":true,"efforts":{"xhigh":{},"medium":{"is_default":true},"low":{}}}}}]}`))
if err != nil || len(ms) != 1 {
t.Fatalf("model fixture: %v", err)
}
return ms[0]
}
// qoderUpstream stands in for api3.qoder.sh: it checks the wire body decodes to
// a well-formed Qoder chat body carrying the COSY envelope, then streams a
// Qoder SSE reply (each line an OpenAI chunk inside the envelope). It returns a
// cleanup that restores the real auth/URL hooks.
func qoderUpstream(t *testing.T, lines []string, modelKeys ...string) func() {
t.Helper()
modelKey := "qfmodel"
if len(modelKeys) > 0 {
modelKey = modelKeys[0]
}
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
if !strings.HasPrefix(r.Header.Get("Authorization"), "Bearer COSY.") {
t.Errorf("missing COSY Authorization")
}
if r.Header.Get("X-Model-Key") != modelKey {
t.Errorf("X-Model-Key = %q", r.Header.Get("X-Model-Key"))
}
plain := qoder.DecodeRequestBody(string(raw))
if !gjson.ValidBytes(plain) {
t.Errorf("decoded body is not JSON: %q", plain)
}
if gjson.GetBytes(plain, "model_config.key").String() != modelKey {
t.Errorf("wrong body model configuration")
}
if gjson.GetBytes(plain, "agent_id").String() != "agent_common" {
t.Errorf("agent_id = %q", gjson.GetBytes(plain, "agent_id").String())
}
if !gjson.GetBytes(plain, "messages").IsArray() {
t.Errorf("no messages array in decoded body")
}
w.Header().Set("Content-Type", "text/event-stream")
for _, l := range lines {
io.WriteString(w, "data: "+l+"\n")
}
}))
auth, url, model := qoderAuth, qoderURL, qoderModel
qoderAuth = func(ctx context.Context, user string) (*qoder.Credential, error) {
return &qoder.Credential{UID: "uid-test", Token: "jt-test", MachineID: "machine-test"}, nil
}
qoderModel = func(context.Context, string, string) (qoder.ModelInfo, error) { return testQoderModel(t), nil }
qoderURL = func() string { return up.URL }
return func() { up.Close(); qoderAuth, qoderURL, qoderModel = auth, url, model }
}
func TestQoderSelectedModel(t *testing.T) {
done := qoderUpstream(t, []string{qoderChunk(qoderContent(t, "ok"))}, "other-model")
defer done()
ms, err := qoder.ModelConfigs([]byte(`{"chat":[{"key":"other-model","enable":true,"display_name":"Other","format":"custom","source":"system","is_vl":false,"is_reasoning":false,"max_input_tokens":64000,"custom_field":17}]}`))
if err != nil {
t.Fatal(err)
}
qoderModel = func(_ context.Context, user, key string) (qoder.ModelInfo, error) {
if user != "selected@x" || key != "other-model" {
t.Errorf("selected user/model %s %s", user, key)
}
return ms[0], nil
}
s := New()
ch, status, msg := s.askQoder("other-model", "selected@x")(context.Background(), &Request{Model: "other-model", Effort: "max"})
if ch == nil {
t.Fatalf("ask: %d %s", status, msg)
}
for range ch {
}
b, err := qoderChatBody(&Request{Effort: "high"}, ms[0])
if err != nil {
t.Fatal(err)
}
if gjson.GetBytes(b, "model_config.custom_field").Int() != 17 || gjson.GetBytes(b, "parameters.context_length").Int() != 64000 || gjson.GetBytes(b, "parameters.enable_thinking").Bool() || gjson.GetBytes(b, "parameters.reasoning_effort").Exists() {
t.Fatalf("selected config: %s", b)
}
qoderModel = func(context.Context, string, string) (qoder.ModelInfo, error) {
return qoder.ModelInfo{}, errors.New("unknown model")
}
ch, status, _ = s.askQoder("unknown", "selected@x")(context.Background(), &Request{})
if ch != nil || status != 400 {
t.Fatalf("unknown model: %d", status)
}
}
// Qoder's efforts come from thinking_config; asked for nothing, or for one
// the model doesn't offer, it thinks at its own default.
func TestQoderReasoning(t *testing.T) {
model := testQoderModel(t)
if model.DefaultEffort != "medium" || !model.Thinks || model.AlwaysThinks {
t.Fatalf("thinking_config read: %+v", model)
}
for _, tt := range []struct {
want, got string
thinking bool
}{
{"", "medium", true}, {"xhigh", "xhigh", true}, {"max", "xhigh", true}, {"none", "", false}, {"invalid", "medium", true},
} {
b, err := qoderChatBody(&Request{Effort: tt.want}, model)
if err != nil {
t.Fatal(err)
}
if gjson.GetBytes(b, "parameters.reasoning_effort").String() != tt.got || gjson.GetBytes(b, "parameters.enable_thinking").Bool() != tt.thinking {
t.Fatalf("effort %q: %s", tt.want, b)
}
}
// a model that can't stop thinking thinks at its lowest when asked for none
always, err := qoder.ModelConfigs([]byte(`{"chat":[{"key":"gf","enable":true,"is_reasoning":true,"thinking_config":{"enabled":{"is_default":true,"efforts":{"max":{"is_default":true},"high":{},"low":{}}}}}]}`))
if err != nil || !always[0].AlwaysThinks {
t.Fatalf("always-thinking model: %+v %v", always, err)
}
b, _ := qoderChatBody(&Request{Effort: "none"}, always[0])
if !gjson.GetBytes(b, "parameters.enable_thinking").Bool() || gjson.GetBytes(b, "parameters.reasoning_effort").String() != "low" {
t.Fatalf("always-thinking asked for none: %s", b)
}
}
// A tool call and its result are sent as OpenAI tool turns, so the result is
// paired with the call by id, not by order.
func TestQoderToolTurns(t *testing.T) {
msgs := qoderMessages([]Message{
{Role: "user", Parts: []Part{{Kind: Text, Text: "read it"}}},
{Role: "assistant", Parts: []Part{
{Kind: ToolCall, ID: "call_A", Name: "read", Args: json.RawMessage(`{"path":"/tmp/x & y"}`)},
{Kind: ToolCall, ID: "call_B", Name: "bash", Args: json.RawMessage(`{"command":"ls"}`)}}},
{Role: "user", Parts: []Part{
{Kind: ToolResult, CallID: "call_B", Text: "second result"},
{Kind: ToolResult, CallID: "call_A", Text: "first result", IsError: true}}},
})
b, _ := json.Marshal(msgs)
if len(msgs) != 4 {
t.Fatalf("turns %s", b)
}
got := gjson.ParseBytes(b)
if got.Get("1.tool_calls.0.id").String() != "call_A" || got.Get("1.tool_calls.1.id").String() != "call_B" {
t.Fatalf("tool_calls %s", got.Get("1").Raw)
}
// arguments reach Qoder as they stand, not HTML-escaped
if got.Get("1.tool_calls.0.function.arguments").String() != `{"path":"/tmp/x & y"}` {
t.Fatalf("arguments %s", got.Get("1.tool_calls.0").Raw)
}
if got.Get("2.role").String() != "tool" || got.Get("2.tool_call_id").String() != "call_B" ||
got.Get("2.content").String() != "second result" {
t.Fatalf("tool turn %s", got.Get("2").Raw)
}
}
// The caller's tools go to Qoder as native function tools.
func TestQoderNativeTools(t *testing.T) {
b, err := qoderChatBody(&Request{Tools: []Tool{{Name: "read", Description: "Read a file",
Schema: json.RawMessage(`{"type":"object"}`)}}}, testQoderModel(t))
if err != nil {
t.Fatal(err)
}
if gjson.GetBytes(b, "tools.0.type").String() != "function" || gjson.GetBytes(b, "tools.0.function.name").String() != "read" {
t.Fatalf("tools %s", b)
}
none, _ := qoderChatBody(&Request{ToolChoice: "none", Tools: []Tool{{Name: "read"}}}, testQoderModel(t))
if gjson.GetBytes(none, "tools").Exists() {
t.Fatalf("tools sent for tool_choice none: %s", none)
}
}
// Qoder's own tool_calls come back with the id they are answered with.
func TestQoderNativeToolCall(t *testing.T) {
calls := []string{
`{"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_up1","type":"function","function":{"name":"read","arguments":""}}]}}]}`,
`{"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"path\": \"/tmp/x"}}]}}]}`,
`{"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":" \\u0026 y\"}"}}]}}]}`,
`{"choices":[{"delta":{"tool_calls":[{"index":1,"id":"call_up2","type":"function","function":{"name":"bash","arguments":"{\"command\":\"ls\"}"}}]}}]}`,
`{"choices":[{"delta":{},"finish_reason":"tool_calls"}]}`,
}
var env []string
for _, c := range calls {
env = append(env, qoderChunk(c))
}
events := qoderDecoded(env...)
var ids []string
var args, stop string
for _, ev := range events {
switch ev.Kind {
case KToolStart:
ids = append(ids, ev.ID)
case KToolArgs:
args += ev.Text
case KStop:
stop = ev.Stop
}
}
if len(ids) != 2 || ids[0] != "call_up1" || ids[1] != "call_up2" {
t.Fatalf("call ids %v", ids)
}
// arguments are handed on as they arrive: nothing is unescaped twice
if args != `{"path": "/tmp/x \u0026 y"}{"command":"ls"}` {
t.Fatalf("arguments %q", args)
}
if stop != "tool" {
t.Fatalf("stop %q", stop)
}
}
func qoderDecoded(lines ...string) []Event {
var wire strings.Builder
for _, line := range lines {
fmt.Fprintf(&wire, "data: %s\n\n", line)
}
ch := make(chan Event, 32)
go decodeQoder(context.Background(), &http.Response{Body: io.NopCloser(strings.NewReader(wire.String()))}, ch, "qfmodel")
var events []Event
for ev := range ch {
events = append(events, ev)
}
return events
}
func TestQoderEOF(t *testing.T) {
call := qoderCallOpen + `{"name":"read","arguments":{"path":"x"}}` + qoderCallClose
for _, tt := range []struct{ input, text, stop string }{
{"a < b and x <", "a < b and x <", "stop"},
{call + "tail <", "tail <", "tool"},
{"before " + qoderCallOpen + "incomplete", "before " + qoderCallOpen + "incomplete", "stop"},
{"", "", "stop"},
} {
events := qoderDecoded(qoderChunk(qoderContent(t, tt.input)), "[DONE]")
var text, stop string
stops := 0
for _, ev := range events {
if ev.Kind == KText {
text += ev.Text
}
if ev.Kind == KStop {
stop = ev.Stop
stops++
}
}
if text != tt.text || stop != tt.stop || stops != 1 {
t.Fatalf("EOF %q => %q %q stops %d", tt.input, text, stop, stops)
}
}
}
func TestQoderEnvelopeErrors(t *testing.T) {
for _, status := range []int{401, 403, 429, 500} {
for _, proto := range []provider.Protocol{provider.Chat, provider.Responses, provider.Anthropic, provider.Gemini} {
for _, stream := range []bool{false, true} {
for _, partial := range []bool{false, true} {
t.Run(fmt.Sprintf("%s/%d/stream=%t/partial=%t", proto, status, stream, partial), func(t *testing.T) {
var lines []string
if partial {
lines = append(lines, qoderChunk(qoderContent(t, "some text")))
}
b, _ := json.Marshal(map[string]any{"statusCodeValue": status, "body": `{"message":"upstream failure"}`})
if status == 429 {
b, _ = json.Marshal(map[string]any{"statusCodeValue": status, "body": `{"message":"额度已耗尽"}`})
}
lines = append(lines, string(b))
done := qoderUpstream(t, lines)
defer done()
s := New()
req := &Request{Model: "qfmodel", Stream: stream}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
events, _, _ := s.askQoder("qfmodel", "test")(ctx, req)
w := httptest.NewRecorder()
var usage Usage
code, msg := relayQoder(w, proto, req, events, &usage, cancel)
want := status
if want == 403 {
want = 401
}
if stream && partial {
want = 200
}
if code != want || w.Code != want || msg == "" || !strings.Contains(w.Body.String(), "error") {
t.Fatalf("code %d/%d want %d, msg %s body %s", code, w.Code, want, msg, w.Body.String())
}
})
}
}
}
}
}
func TestQoderSearchEnvelopeError(t *testing.T) {
for _, stream := range []bool{false, true} {
for _, partial := range []bool{false, true} {
ctx, cancel := context.WithCancel(context.Background())
in := make(chan Event, 3)
in <- Event{Kind: KStart}
if partial {
in <- Event{Kind: KText, Text: "partial answer"}
}
in <- Event{Kind: KError, Status: 503, Text: "Qoder upstream unavailable"}
close(in)
out := make(chan Event, 4)
req := &Request{Model: "qfmodel", Stream: stream}
go func() {
defer close(out)
New().searchRounds(ctx, req, "search", in, nil, out)
}()
w := httptest.NewRecorder()
var usage Usage
code, msg := relayQoder(w, provider.Chat, req, out, &usage, cancel)
cancel()
want := 503
if stream && partial {
want = 200
}
if code != want || w.Code != want || msg == "" || !strings.Contains(w.Body.String(), "error") {
t.Fatalf("stream=%t partial=%t: code %d/%d want %d, msg %s body %s", stream, partial, code, w.Code, want, msg, w.Body.String())
}
}
}
}
// qoderChunk wraps an OpenAI chat.completion.chunk as Qoder's SSE envelope.
func qoderChunk(inner string) string {
b, _ := json.Marshal(map[string]any{
"headers": map[string][]string{"Content-Type": {"application/json"}},
"body": inner, "statusCodeValue": 200, "statusCode": "OK",
})
return string(b)
}
// TestQoderChatBody checks the IR request becomes a Qoder body: the caller's
// system prompt and tool contract ride in the system field, tool calls are
// replayed as XML, and tool results fold into a user turn.
func TestQoderChatBody(t *testing.T) {
req := &Request{
System: "Be terse.",
ToolChoice: "",
Tools: []Tool{{Name: "read", Description: "Read a file", Schema: json.RawMessage(`{"type":"object"}`)}},
Messages: []Message{
{Role: "user", Parts: []Part{{Kind: Text, Text: "hi"}}},
{Role: "assistant", Parts: []Part{{Kind: ToolCall, ID: "c1", Name: "read", Args: json.RawMessage(`{"path":"x"}`)}}},
{Role: "user", Parts: []Part{{Kind: ToolResult, CallID: "c1", Text: "the file"}}},
},
}
b, err := qoderChatBody(req, testQoderModel(t))
if err != nil {
t.Fatal(err)
}
if gjson.GetBytes(b, "agent_id").String() != "agent_common" {
t.Fatalf("agent_id")
}
if gjson.GetBytes(b, "model_config.key").String() != "qfmodel" {
t.Fatalf("model key")
}
// the caller's tools go as Qoder's own function tools, not in the prompt
if gjson.GetBytes(b, "tools.0.function.name").String() != "read" ||
!strings.Contains(gjson.GetBytes(b, "system.0.text").String(), "Be terse.") {
t.Fatalf("system/tools: %s", b)
}
msgs := gjson.GetBytes(b, "messages")
if msgs.Get("0.role").String() != "system" {
t.Fatalf("first message is %s, want system", msgs.Get("0.role").String())
}
// assistant tool call and tool result are tool turns named by id
if msgs.Get("2.tool_calls.0.id").String() != "c1" ||
msgs.Get("3.role").String() != "tool" || msgs.Get("3.tool_call_id").String() != "c1" ||
msgs.Get("3.content").String() != "the file" {
t.Fatalf("tool turns: %s", msgs.Raw)
}
}
// qoderContent builds an OpenAI chunk inner JSON whose delta carries content,
// assembled through encoding/json so a Qoder tool-call block, written here with
// hex-escaped brackets, reaches the wire as real angle brackets without any
// literal tag in this source.
func qoderContent(t *testing.T, content string) string {
t.Helper()
b, err := json.Marshal(map[string]any{
"choices": []any{map[string]any{"delta": map[string]any{"content": content}}},
"object": "chat.completion.chunk",
})
if err != nil {
t.Fatal(err)
}
return string(b)
}
// TestServeQoder runs a request through serveQoder against a stubbed Qoder:
// text, a reasoning delta, an embedded tool call lifted into events, and usage
// all reach the client's protocol.
func TestServeQoder(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
call := "\x3ctool_call\x3e\n\x3cfunction=read\x3e\n\x3cparameter=path\x3ex\x3c/parameter\x3e\n\x3c/function\x3e\n\x3c/tool_call\x3e"
done := qoderUpstream(t, []string{
qoderChunk(`{"choices":[{"delta":{"role":"assistant","reasoning_content":"thinking"}}],"object":"chat.completion.chunk"}`),
qoderChunk(`{"choices":[{"delta":{"content":"Hi there"}}],"object":"chat.completion.chunk"}`),
qoderChunk(qoderContent(t, call)),
qoderChunk(`{"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":5}}`),
`{"firstTokenDuration":50,"totalDuration":200}`,
})
defer done()
s := New()
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"model":"qoder/qfmodel","stream":true,"messages":[{"role":"user","content":"hi"}]}`))
var u Usage
if status, msg := s.serveQoder(w, r, provider.Chat, provider.Provider{Account: &provider.Account{User: "test"}}, "qfmodel", []byte(`{"model":"qfmodel","stream":true,"messages":[{"role":"user","content":"hi"}]}`), &u); status != 200 {
t.Fatalf("status %d msg %q", status, msg)
}
out := w.Body.String()
for _, kw := range []string{"thinking", "Hi there", "read", "path", "x"} {
if !strings.Contains(out, kw) {
t.Fatalf("streamed output missing %q:\n%s", kw, out)
}
}
}