import { describe, expect, it } from "bun:test"; import { createBusinessStageTracker } from "../../src/routes/chatBusinessStages.js"; import { streamPromptResponse } from "../../src/routes/chatStream.js"; import type { OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js"; describe("business stages and final answer", () => { it("keeps a stage running while parallel actions remain, then preserves errors", () => { const events: Record[] = []; const track = createBusinessStageTracker((_event, data) => events.push(data)); const part = (id: string, status: string) => ({ id, type: "tool", tool: "query_project_data", state: { status } }) as Parameters[0]; track(part("a", "running")); track(part("b", "running")); track(part("a", "completed")); expect(events.at(-1)?.status).toBe("running"); track(part("b", "error")); expect(events.at(-1)).toMatchObject({ id: "business-data", status: "error" }); }); it("buffers final_answer until the runtime succeeds and does not end the run at the tool", async () => { const output: string[] = []; const events = [ { type: "message.part.updated", properties: { sessionID: "s", part: { id: "f", type: "tool", tool: "final_answer", state: { status: "completed", input: { answer: "最终结论" }, output: JSON.stringify({ answer: "最终结论" }) } } } }, { type: "message.part.updated", properties: { sessionID: "s", part: { id: "q", type: "tool", tool: "query_project_data", state: { status: "completed", input: { source: "scada_devices" }, output: "{}" } } } }, { type: "session.idle", properties: { sessionID: "s" } }, ]; const runtime = { prompt: async () => undefined, subscribeEvents: async () => ({ async *[Symbol.asyncIterator]() { yield* events; } }), messages: async () => { throw new Error("final answer needs no text fallback"); } } as unknown as OpencodeRuntimeAdapter; const result = await streamPromptResponse({ runtime, sessionId: "s", clientSessionId: "c", message: "test", write: (event, data) => { output.push(event); if (event === "final_answer") expect(data.content).toBe("最终结论"); } }); expect(result.failed).toBeFalse(); expect(output.indexOf("tool_call")).toBeLessThan(output.indexOf("final_answer")); expect(output.at(-1)).toBe("done"); }); });