Files
next-tjwater-agent/tests/routes/chatBusinessStages.test.ts
T
jiang 6336157ff4
Generic Container CI/CD / test-build-publish (push) Successful in 1m6s
Agent CI/CD / build-test-publish-and-deploy (push) Successful in 1m7s
feat: upgrade investigation and presentation runtime
2026-10-30 17:30:19 +08:00

30 lines
2.3 KiB
TypeScript

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<string, unknown>[] = [];
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<typeof track>[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("keeps background task completion under runtime and Server control", 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", suppressAnswerUi: true, 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");
});
});