- 「拆解检索问题」→「梳理写作思路」(已梳理出 N 个论述角度) - 「检索参考资料」→「整理对话资料」(已整理 1/2 个论述角度的 资料 · 累计 N 条) - 管线实际只汇总对话已收集的资料再成稿,原文案会让用户误以为 管线在重新检索;测试补标签断言防止回退
294 lines
12 KiB
TypeScript
294 lines
12 KiB
TypeScript
import assert from "node:assert/strict";
|
||
import test from "node:test";
|
||
|
||
import type { ResearchReportStreamState } from "./use-research-report-stream";
|
||
import {
|
||
buildResearchFeedMessages,
|
||
buildResearchProgressToolCalls,
|
||
buildResearchReportVirtualUrl,
|
||
composeResearchDisplayMessages,
|
||
clearResearchReportStream,
|
||
finalizeResearchReportStream,
|
||
ingestResearchReportEvent,
|
||
peekResearchReportStream,
|
||
subscribeResearchReportStream,
|
||
} from "./use-research-report-stream";
|
||
import { buildWriteFileVirtualUrl } from "../artifacts/streaming-write-preview";
|
||
|
||
// 模块级 store 按 threadId 隔离;每个测试用独立 threadId 避免串扰。
|
||
let seq = 0;
|
||
const nextThread = () => `t-${++seq}`;
|
||
|
||
function ingestAll(threadId: string, frames: Array<Record<string, unknown>>) {
|
||
for (const frame of frames) {
|
||
ingestResearchReportEvent(
|
||
threadId,
|
||
frame as Parameters<typeof ingestResearchReportEvent>[1],
|
||
);
|
||
}
|
||
}
|
||
|
||
function streamOf(overrides: Partial<ResearchReportStreamState> = {}): ResearchReportStreamState {
|
||
return {
|
||
status: "active",
|
||
jobId: "j",
|
||
phase: "initializing",
|
||
queries: [],
|
||
completedQueries: 0,
|
||
sourceCount: 0,
|
||
reportText: "",
|
||
thinking: "",
|
||
error: null,
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
test("ingest accumulates plan/collect progress frames", () => {
|
||
const tid = nextThread();
|
||
ingestAll(tid, [
|
||
{ type: "phase_changed", jobId: "j1", phase: "planning", payload: { to: "planning" } },
|
||
{ type: "queries_planned", jobId: "j1", payload: { queries: ["q1", "q2", "q3"] } },
|
||
{ type: "phase_changed", jobId: "j1", phase: "collecting", payload: { to: "collecting" } },
|
||
{ type: "query_completed", jobId: "j1", payload: { query: "q1", resultCount: 5 } },
|
||
{ type: "query_completed", jobId: "j1", payload: { query: "q2", resultCount: 3 } },
|
||
{ type: "sources_curated", jobId: "j1", payload: { selectedIds: ["a", "b", "c", "d"] } },
|
||
]);
|
||
|
||
const state = peekResearchReportStream(tid);
|
||
assert.equal(state?.jobId, "j1");
|
||
assert.equal(state?.phase, "collecting");
|
||
assert.deepEqual(state?.queries, ["q1", "q2", "q3"]);
|
||
assert.equal(state?.completedQueries, 2);
|
||
// query_completed 累计 8,sources_curated 的 4 条选中集覆盖为最终值。
|
||
assert.equal(state?.sourceCount, 4);
|
||
assert.equal(state?.status, "active");
|
||
});
|
||
|
||
test("report_delta and report_chunk dedupe by offset when both arrive", () => {
|
||
const tid = nextThread();
|
||
ingestAll(tid, [
|
||
{ type: "report_delta", jobId: "j2", phase: "writing", payload: { delta: "第一段", offset: 0 } },
|
||
{ type: "report_chunk", jobId: "j2", phase: "writing", payload: { delta: "第一段", offset: 0 } },
|
||
{ type: "report_chunk", jobId: "j2", phase: "writing", payload: { delta: "第二段", offset: 3 } },
|
||
{ type: "report_delta", jobId: "j2", phase: "writing", payload: { delta: "第二段", offset: 3 } },
|
||
]);
|
||
|
||
assert.equal(peekResearchReportStream(tid)?.reportText, "第一段第二段");
|
||
});
|
||
|
||
test("an offset-0 frame with different content rewrites from that offset (workbench parity)", () => {
|
||
const tid = nextThread();
|
||
ingestAll(tid, [
|
||
{ type: "report_delta", jobId: "j2b", payload: { delta: "第一段第二段", offset: 0 } },
|
||
// 生产端从头重写(如 reset 后重发):内容不匹配 → 从 offset 0 覆盖。
|
||
{ type: "report_delta", jobId: "j2b", payload: { delta: "全新开头", offset: 0 } },
|
||
]);
|
||
assert.equal(peekResearchReportStream(tid)?.reportText, "全新开头");
|
||
});
|
||
|
||
test("non-report targets (sources/plan json) never pollute reportText", () => {
|
||
const tid = nextThread();
|
||
ingestAll(tid, [
|
||
{ type: "report_delta", jobId: "j3", payload: { target: "sources", delta: "[1] 来源", offset: 0 } },
|
||
{ type: "report_chunk", jobId: "j3", payload: { target: "report", delta: "# 报告", offset: 0 } },
|
||
]);
|
||
assert.equal(peekResearchReportStream(tid)?.reportText, "# 报告");
|
||
});
|
||
|
||
test("a new jobId resets prior state", () => {
|
||
const tid = nextThread();
|
||
ingestAll(tid, [
|
||
{ type: "report_delta", jobId: "old", payload: { delta: "旧报告", offset: 0 } },
|
||
{ type: "queries_planned", jobId: "new", payload: { queries: ["n1"] } },
|
||
]);
|
||
const state = peekResearchReportStream(tid);
|
||
assert.equal(state?.jobId, "new");
|
||
assert.equal(state?.reportText, "");
|
||
assert.deepEqual(state?.queries, ["n1"]);
|
||
});
|
||
|
||
test("tool_finished marks success and finalize guards only active runs", () => {
|
||
const tid = nextThread();
|
||
ingestAll(tid, [
|
||
{ type: "report_delta", jobId: "j4", payload: { delta: "正文", offset: 0 } },
|
||
{ type: "tool_finished", jobId: "j4", payload: { status: "success" } },
|
||
]);
|
||
assert.equal(peekResearchReportStream(tid)?.status, "success");
|
||
// 已 success 的状态不被 finalize 翻转。
|
||
finalizeResearchReportStream(tid);
|
||
assert.equal(peekResearchReportStream(tid)?.status, "success");
|
||
|
||
const tid2 = nextThread();
|
||
ingestAll(tid2, [
|
||
{ type: "report_delta", jobId: "j5", payload: { delta: "写到一半", offset: 0 } },
|
||
]);
|
||
finalizeResearchReportStream(tid2);
|
||
const interrupted = peekResearchReportStream(tid2);
|
||
assert.equal(interrupted?.status, "error");
|
||
assert.equal(interrupted?.error, "已中断");
|
||
|
||
clearResearchReportStream(tid2);
|
||
assert.equal(peekResearchReportStream(tid2), null);
|
||
});
|
||
|
||
test("progress tool calls reveal stages stepwise and mark interruption", () => {
|
||
const active = streamOf({
|
||
jobId: "j6",
|
||
phase: "collecting",
|
||
queries: ["q1", "q2"],
|
||
completedQueries: 1,
|
||
sourceCount: 3,
|
||
});
|
||
const calls = buildResearchProgressToolCalls(active);
|
||
assert.equal(calls.length, 2); // 梳理思路已完成 + 整理资料进行中
|
||
assert.equal(calls[0].args.status, "complete");
|
||
assert.equal(calls[0].args.label, "梳理写作思路");
|
||
assert.equal(calls[1].args.status, "active");
|
||
assert.equal(calls[1].args.label, "整理对话资料");
|
||
assert.match(calls[1].args.detail, /1\/2/);
|
||
|
||
const writing = streamOf({ jobId: "j7", phase: "writing", reportText: "# x" });
|
||
assert.equal(buildResearchProgressToolCalls(writing).length, 3);
|
||
assert.equal(buildResearchProgressToolCalls(writing)[2].args.status, "active");
|
||
|
||
const completedByPhase = streamOf({ jobId: "j7b", phase: "done", reportText: "# x" });
|
||
const completedCalls = buildResearchProgressToolCalls(completedByPhase);
|
||
assert.equal(completedCalls.length, 3);
|
||
assert.ok(completedCalls.every((call) => call.args.status === "complete"));
|
||
assert.match(completedCalls[2].args.detail, /report\.md/);
|
||
|
||
const completedByStatus = streamOf({
|
||
jobId: "j7c",
|
||
phase: "writing",
|
||
status: "success",
|
||
reportText: "# x",
|
||
});
|
||
assert.ok(
|
||
buildResearchProgressToolCalls(completedByStatus).every((call) => call.args.status === "complete"),
|
||
);
|
||
|
||
const failed = streamOf({ jobId: "j8", phase: "writing", status: "error", error: "模型超时" });
|
||
const failedCalls = buildResearchProgressToolCalls(failed);
|
||
assert.match(failedCalls.at(-1)?.args.label ?? "", /已中断/);
|
||
assert.match(failedCalls.at(-1)?.args.detail ?? "", /模型超时/);
|
||
});
|
||
|
||
test("feed messages attach the write_file stub only once report text exists", () => {
|
||
const noText = streamOf({ jobId: "j9", phase: "collecting" });
|
||
assert.equal(buildResearchFeedMessages(noText).length, 1);
|
||
assert.equal(buildResearchFeedMessages(noText)[0].tool_calls?.[0]?.name, "deep_research_progress");
|
||
|
||
const writingEmpty = streamOf({ jobId: "j9b", phase: "writing" });
|
||
assert.equal(buildResearchFeedMessages(writingEmpty).length, 1);
|
||
|
||
const withText = streamOf({ jobId: "j10", phase: "writing", reportText: "# 报告正文" });
|
||
const feed = buildResearchFeedMessages(withText);
|
||
assert.equal(feed.length, 2);
|
||
const stub = feed[1].tool_calls?.[0];
|
||
assert.equal(stub?.name, "write_file");
|
||
assert.equal(stub?.args.path, "/mnt/user-data/outputs/report.md");
|
||
assert.equal(stub?.args.content, "# 报告正文");
|
||
|
||
const done = streamOf({ jobId: "j10b", phase: "done", reportText: "# 报告正文" });
|
||
const doneFeed = buildResearchFeedMessages(done);
|
||
assert.equal(doneFeed.length, 3);
|
||
assert.equal(doneFeed[2].type, "tool");
|
||
assert.equal(doneFeed[2].tool_call_id, "research-report-j10b-write");
|
||
});
|
||
|
||
test("report_completed flips phase to done and write step completes before tool_finished", () => {
|
||
const tid = nextThread();
|
||
ingestAll(tid, [
|
||
{ type: "report_delta", jobId: "jdone", payload: { delta: "正文", offset: 0 } },
|
||
{ type: "report_completed", jobId: "jdone", payload: { sourceCount: 2 } },
|
||
]);
|
||
const state = peekResearchReportStream(tid);
|
||
assert.equal(state?.phase, "done");
|
||
assert.equal(state?.status, "active");
|
||
const calls = buildResearchProgressToolCalls(state!);
|
||
assert.ok(calls.every((call) => call.args.status === "complete"));
|
||
});
|
||
|
||
test("composeResearchDisplayMessages inserts the bar before the summary and drops leaked prose", () => {
|
||
const feed = buildResearchFeedMessages(streamOf({
|
||
jobId: "jsplice",
|
||
phase: "done",
|
||
reportText: "# 报告",
|
||
}));
|
||
const composed = composeResearchDisplayMessages(
|
||
[
|
||
{ id: "h", type: "human", content: "写论文" },
|
||
{
|
||
id: "ai-tool",
|
||
type: "ai",
|
||
content: "",
|
||
tool_calls: [{ id: "c1", name: "deep_research_report", args: {} }],
|
||
},
|
||
{ id: "leak", type: "ai", content: "# 报告\n\n泄漏到消息列表的正文" },
|
||
{ id: "tool", type: "tool", tool_call_id: "c1", content: "ok" },
|
||
{ id: "summary", type: "ai", content: "已生成论文,详见 report.md。" },
|
||
],
|
||
feed,
|
||
);
|
||
|
||
assert.deepEqual(
|
||
composed.map((message) => message.id),
|
||
[
|
||
"h",
|
||
"ai-tool",
|
||
"tool",
|
||
"research-progress-jsplice",
|
||
"research-report-jsplice",
|
||
"research-report-jsplice-write-result",
|
||
"summary",
|
||
],
|
||
);
|
||
});
|
||
|
||
test("virtual URL matches the message-list ArtifactWriteToolCall URL (one suppression key)", () => {
|
||
const state = streamOf({ jobId: "j11", phase: "writing", reportText: "# x" });
|
||
// 显式自动开沙箱(useResearchWritingFeed)与消息列表里 ArtifactWriteToolCall
|
||
// 各自构造的 write-file 虚拟 URL 必须完全一致:二者共用同一 previewKey
|
||
// (`message_id:tool_call_id`),用户手动关闭一次即对两条路径同时生效。
|
||
assert.equal(
|
||
buildResearchReportVirtualUrl(state),
|
||
buildWriteFileVirtualUrl({
|
||
path: "/mnt/user-data/outputs/report.md",
|
||
messageId: "research-report-j11",
|
||
toolCallId: "research-report-j11-write",
|
||
}),
|
||
);
|
||
});
|
||
|
||
test("token-frame notifications are coalesced; terminal frames flush immediately", async () => {
|
||
const tid = nextThread();
|
||
let notifications = 0;
|
||
const unsubscribe = subscribeResearchReportStream(tid, () => {
|
||
notifications += 1;
|
||
});
|
||
|
||
try {
|
||
ingestAll(tid, [
|
||
{ type: "report_thinking", jobId: "jc", phase: "writing", payload: { delta: "思考" } },
|
||
{ type: "report_delta", jobId: "jc", phase: "writing", payload: { delta: "第一", offset: 0 } },
|
||
{ type: "report_delta", jobId: "jc", phase: "writing", payload: { delta: "第二", offset: 2 } },
|
||
{ type: "report_delta", jobId: "jc", phase: "writing", payload: { delta: "第三", offset: 4 } },
|
||
]);
|
||
|
||
// 状态同步更新(快照立即可见),但通知被合并——节流窗口内一次不发。
|
||
assert.equal(peekResearchReportStream(tid)?.reportText, "第一第二第三");
|
||
assert.equal(notifications, 0);
|
||
|
||
// 窗口(100ms)过后一次性通知。
|
||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||
assert.equal(notifications, 1);
|
||
|
||
// 终态帧不等待节流窗口,立即通知。
|
||
ingestAll(tid, [{ type: "tool_finished", jobId: "jc", payload: { status: "success" } }]);
|
||
assert.equal(peekResearchReportStream(tid)?.status, "success");
|
||
assert.equal(notifications, 2);
|
||
} finally {
|
||
unsubscribe();
|
||
}
|
||
});
|