diff --git a/src/__tests__/models-cache.test.ts b/src/__tests__/models-cache.test.ts index 73f7c8f..2791866 100644 --- a/src/__tests__/models-cache.test.ts +++ b/src/__tests__/models-cache.test.ts @@ -5,30 +5,47 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { getCachedModels, updateQoderModelsCache } from "../models.js"; import { loadLiveFixture, responseFromFixture } from "./live-fixture.js"; -const CACHE_PATH = join(homedir(), ".pi", "agent", "qoder-models-cache.json"); -let originalCache: string | undefined; +const CACHE_PATHS = { + global: join(homedir(), ".pi", "agent", "qoder-models-cache.json"), + cn: join(homedir(), ".pi", "agent", "qoder-cn-models-cache.json"), +}; +let originalCaches: Record; beforeEach(() => { - originalCache = existsSync(CACHE_PATH) ? readFileSync(CACHE_PATH, "utf8") : undefined; - rmSync(CACHE_PATH, { force: true }); + originalCaches = { + global: existsSync(CACHE_PATHS.global) ? readFileSync(CACHE_PATHS.global, "utf8") : undefined, + cn: existsSync(CACHE_PATHS.cn) ? readFileSync(CACHE_PATHS.cn, "utf8") : undefined, + }; + for (const path of Object.values(CACHE_PATHS)) rmSync(path, { force: true }); }); afterEach(() => { vi.unstubAllGlobals(); - if (originalCache === undefined) rmSync(CACHE_PATH, { force: true }); - else writeFileSync(CACHE_PATH, originalCache, "utf8"); + for (const region of Object.keys(CACHE_PATHS) as Array) { + const path = CACHE_PATHS[region]; + const original = originalCaches[region]; + if (original === undefined) rmSync(path, { force: true }); + else writeFileSync(path, original, "utf8"); + } }); describe("Qoder model cache", () => { - it("replays the recorded-format model-list schema", async () => { - const interaction = loadLiveFixture("global").interactions.modelList; + it.each([ + ["global", ["Lite", "GLM5.2"]], + ["cn", ["Auto", "Qwen3.7-Max"]], + ] as const)("maps the %s recorded-format catalog to friendly picker ids", async (region, expectedIds) => { + const interaction = loadLiveFixture(region).interactions.modelList; vi.stubGlobal("fetch", vi.fn().mockResolvedValue(responseFromFixture(interaction))); - await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", "global"); + await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", region); - const cache = JSON.parse(readFileSync(CACHE_PATH, "utf8")); - expect(cache.models.map((model: { id: string }) => model.id)).toEqual(["lite", "gm51model"]); - expect(cache.models.map((model: { contextWindow: number }) => model.contextWindow)).toEqual([1_000_000, 200_000]); + const cache = JSON.parse(readFileSync(CACHE_PATHS[region], "utf8")); + expect(cache.models.map((model: { id: string }) => model.id)).toEqual(expectedIds); + for (const entry of interaction.response.body.chat as Array<{ key: string; display_name: string }>) { + const friendlyId = entry.display_name.replace(/\s+/g, ""); + expect(cache.configs[entry.key]?.key).toBe(entry.key); + expect(cache.configs[friendlyId]?.key).toBe(entry.key); + } }); it("keeps only enabled service models without adding auto as a fallback", async () => { @@ -50,8 +67,8 @@ describe("Qoder model cache", () => { await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", "global"); - const cache = JSON.parse(readFileSync(CACHE_PATH, "utf8")); - expect(cache.models.map((model: { id: string }) => model.id)).toEqual(["ultimate", "lite"]); + const cache = JSON.parse(readFileSync(CACHE_PATHS.global, "utf8")); + expect(cache.models.map((model: { id: string }) => model.id)).toEqual(["Ultimate", "Lite"]); expect(cache.models.some((model: { id: string }) => model.id === "auto")).toBe(false); }); @@ -66,13 +83,13 @@ describe("Qoder model cache", () => { await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", "global"); - const cache = JSON.parse(readFileSync(CACHE_PATH, "utf8")); - expect(cache.models.map((model: { id: string }) => model.id)).toEqual(["cmodel"]); + const cache = JSON.parse(readFileSync(CACHE_PATHS.global, "utf8")); + expect(cache.models.map((model: { id: string }) => model.id)).toEqual(["Cantus"]); }); it("filters auto from a legacy fallback cache when the service did not enable it", () => { writeFileSync( - CACHE_PATH, + CACHE_PATHS.global, JSON.stringify({ updatedAt: Date.now(), models: [{ id: "auto" }, { id: "ultimate" }], @@ -98,7 +115,7 @@ describe("Qoder model cache", () => { await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", "global"); - const cache = JSON.parse(readFileSync(CACHE_PATH, "utf8")); + const cache = JSON.parse(readFileSync(CACHE_PATHS.global, "utf8")); expect(cache.models[0].contextWindow).toBe(1_000_000); }); @@ -123,7 +140,7 @@ describe("Qoder model cache", () => { await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", "global"); - const cache = JSON.parse(readFileSync(CACHE_PATH, "utf8")); + const cache = JSON.parse(readFileSync(CACHE_PATHS.global, "utf8")); expect(cache.models[0].contextWindow).toBe(200000); }); }); diff --git a/src/__tests__/stream.test.ts b/src/__tests__/stream.test.ts index c8ac077..7e880c8 100644 --- a/src/__tests__/stream.test.ts +++ b/src/__tests__/stream.test.ts @@ -236,6 +236,77 @@ describe("streamQoder", () => { expect(toolCall).toBeDefined(); }); + it("assembles reasoning chunks before the final answer", async () => { + const sse = + sseEnvelope(chunk({ reasoning_content: "check " })) + + sseEnvelope(chunk({ reasoning_content: "twice" })) + + sseEnvelope(chunk({ content: "done" })) + + sseEnvelope(finishChunk("stop")) + + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake", reasoning: "high" })); + const done = events.find((event) => event.type === "done") as { message: AssistantMessage }; + + expect(done.message.content).toEqual([ + { type: "thinking", thinking: "check twice" }, + { type: "text", text: "done" }, + ]); + expect(events.map((event) => event.type)).toContain("thinking_delta"); + }); + + it("assembles parallel tool calls by their stream indexes", async () => { + const sse = + sseEnvelope( + chunk({ + tool_calls: [ + { index: 0, id: "call_a", function: { name: "read", arguments: '{"path":' } }, + { index: 1, id: "call_b", function: { name: "search", arguments: '{"query":' } }, + ], + }), + ) + + sseEnvelope( + chunk({ + tool_calls: [ + { index: 0, function: { arguments: '"/a"}' } }, + { index: 1, function: { arguments: '"needle"}' } }, + ], + }), + ) + + sseEnvelope(finishChunk("tool_calls")) + + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake" })); + const done = events.find((event) => event.type === "done") as { message: AssistantMessage }; + const calls = done.message.content.filter((block): block is ToolCall => block.type === "toolCall"); + + expect(calls).toEqual([ + { type: "toolCall", id: "call_a", name: "read", arguments: { path: "/a" } }, + { type: "toolCall", id: "call_b", name: "search", arguments: { query: "needle" } }, + ]); + }); + + it("preserves text emitted before and after a tool call", async () => { + const sse = + sseEnvelope(chunk({ content: "before" })) + + sseEnvelope( + chunk({ tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: "{}" } }] }), + ) + + sseEnvelope(chunk({ content: " after" })) + + sseEnvelope(finishChunk("tool_calls")) + + DONE_SSE; + globalThis.fetch = mockFetch(sse); + + const events = await consume(streamQoder(makeModel(), makeContext(), { apiKey: "fake" })); + const done = events.find((event) => event.type === "done") as { message: AssistantMessage }; + + expect(done.message.content).toEqual([ + { type: "text", text: "before after" }, + { type: "toolCall", id: "call_1", name: "lookup", arguments: {} }, + ]); + }); + it("emits a tool call that arrives with no arguments", async () => { // A no-argument tool, or a model that sends id+name and stops. The block // used to be created only inside `if (tc.function?.arguments)`, so this @@ -355,4 +426,26 @@ describe("streamQoder", () => { const text = msg.content.find((c) => c.type === "text"); expect(text && "text" in text ? text.text : "").toBe("hi"); }); + + it("reports aborted when the request is cancelled before streaming starts", async () => { + const controller = new AbortController(); + globalThis.fetch = vi.fn( + (_url: URL | RequestInfo, init?: RequestInit) => + new Promise((_resolve, reject) => { + init?.signal?.addEventListener("abort", () => reject(new DOMException("Aborted", "AbortError")), { + once: true, + }); + }), + ) as unknown as typeof fetch; + + const eventsPromise = consume( + streamQoder(makeModel(), makeContext(), { apiKey: "fake", signal: controller.signal }), + ); + controller.abort(); + const events = await eventsPromise; + + const error = events.find((event) => event.type === "error") as { error: AssistantMessage }; + expect(error.error.stopReason).toBe("aborted"); + expect(events.find((event) => event.type === "done")).toBeUndefined(); + }); }); diff --git a/src/__tests__/transform.test.ts b/src/__tests__/transform.test.ts index 6b1365b..48fc0bd 100644 --- a/src/__tests__/transform.test.ts +++ b/src/__tests__/transform.test.ts @@ -348,6 +348,32 @@ describe("transformMessagesForQoder", () => { expect(tool.role).toBe("tool"); expect(tool.tool_call_id).toBe("call_abc123"); }); + + it("preserves parallel tool result pairing and the following user turn", () => { + const msgs = [ + { role: "user", content: "compare both" }, + { + role: "assistant", + content: [ + { type: "text", text: "Checking." }, + { type: "toolCall", id: "call_a", name: "read", arguments: { path: "a" } }, + { type: "toolCall", id: "call_b", name: "read", arguments: { path: "b" } }, + ], + }, + { role: "toolResult", toolCallId: "call_a", content: "A" }, + { role: "toolResult", toolCallId: "call_b", content: "B" }, + { role: "assistant", content: [{ type: "text", text: "A and B differ." }] }, + { role: "user", content: "fix A" }, + ] as unknown as Message[]; + + const result = transformMessagesForQoder(msgs); + const assistant = result[1] as { tool_calls?: Array<{ id: string }> }; + + expect(result.map((message) => message.role)).toEqual(["user", "assistant", "tool", "tool", "assistant", "user"]); + expect(assistant.tool_calls?.map((call) => call.id)).toEqual(["call_a", "call_b"]); + expect(result.slice(2, 4).map((message) => message.tool_call_id)).toEqual(["call_a", "call_b"]); + }); + it("drops the tool result whose assistant turn was aborted", () => { // Cancelling a turn mid-tool used to drop only the assistant message and // keep its tool result, leaving a `tool` message with no matching