/** * interrupt-marker 纯逻辑(core.ts)的单测:标记文案决策、分支条目 → 消息的转换、 * 半截输出复活、单行截断。不依赖 pi,也不 mock 任何东西 —— 输入就是 pi 会话里真实的消息形状。 * * 用命名空间导入 `* as core`:后续任务追加的用例才会各自失败(`core.xxx is not a function`), * 而不是整份文件在链接期就崩。 * * node --test clients/pi/extensions/interrupt-marker/core.test.ts */ import assert from "node:assert/strict"; import test from "node:test"; import * as core from "./core.ts"; const { MARKER_TYPE, MARKER_TEXT, MARKER_TEXT_TOOL_USE } = core; const text = (t: string) => ({ type: "text", text: t }); const user = (t: string) => ({ role: "user", content: t }); const assistant = (stopReason: string, content: unknown[], extra: Record = {}) => ({ role: "assistant", stopReason, content, ...extra }); const toolCall = (id: string) => ({ type: "toolCall", id, name: "bash", arguments: {} }); const toolResult = (id: string) => ({ role: "toolResult", toolCallId: id, toolName: "bash", isError: true, content: [text("Command aborted")] }); const toolResultOk = (id: string) => ({ role: "toolResult", toolCallId: id, toolName: "bash", isError: false, content: [text("ok")] }); const emptyError = () => assistant("error", [], { errorMessage: "This operation was aborted" }); const emptyAborted = () => assistant("aborted", [], { errorMessage: "This operation was aborted" }); const marker = (t = MARKER_TEXT) => ({ role: "custom", customType: MARKER_TYPE, content: t, display: true }); const msgEntry = (message: unknown) => ({ type: "message", message }); test("isMarker: 只认 role=custom 且 customType=user-interrupt", () => { assert.equal(core.isMarker(marker()), true); assert.equal(core.isMarker({ role: "custom", customType: "verify-loop" }), false); assert.equal(core.isMarker({ role: "user", customType: MARKER_TYPE }), false); }); test("decideMarkerText: 流式中止(尾部是带半截文本的 aborted assistant)→ 普通文案", () => { assert.equal(core.decideMarkerText([user("OLD"), assistant("aborted", [text("step0 ")])]), MARKER_TEXT); }); test("decideMarkerText: 工具中止(toolResult 后跟一条空的 error assistant)→ for tool use 文案", () => { const tail = [user("OLD"), assistant("toolUse", [text("run"), toolCall("c1")]), toolResult("c1"), emptyError()]; assert.equal(core.decideMarkerText(tail), MARKER_TEXT_TOOL_USE); }); test("decideMarkerText: 并行多个 toolResult 同样用 for tool use 文案", () => { const tail = [user("OLD"), assistant("toolUse", [toolCall("c1"), toolCall("c2")]), toolResult("c1"), toolResult("c2"), emptyError()]; assert.equal(core.decideMarkerText(tail), MARKER_TEXT_TOOL_USE); }); test("decideMarkerText: 工具已成功、模型回复阶段被 ESC(toolResult 成功 + 空的 aborted assistant)→ 普通文案", () => { const tail = [user("OLD"), assistant("toolUse", [toolCall("c1")]), toolResultOk("c1"), emptyAborted()]; assert.equal(core.decideMarkerText(tail), MARKER_TEXT); }); test("decideMarkerText: 工具已成功、模型只想了想(toolResult 成功 + 只有 thinking 的 aborted assistant)→ 普通文案", () => { const tail = [user("OLD"), assistant("toolUse", [toolCall("c1")]), toolResultOk("c1"), assistant("aborted", [{ type: "thinking", thinking: "hm" }])]; assert.equal(core.decideMarkerText(tail), MARKER_TEXT); }); test("decideMarkerText: 工具自己失败(isError)、随后模型回复阶段被 ESC → 普通文案;失败不等于被打断", () => { const tail = [user("OLD"), assistant("toolUse", [toolCall("c1")]), toolResult("c1"), emptyAborted()]; assert.equal(core.decideMarkerText(tail), MARKER_TEXT); }); test("decideMarkerText: 工具成功后尾部没有任何 assistant(或只有空的 error)→ 普通文案", () => { const head = [user("OLD"), assistant("toolUse", [toolCall("c1")]), toolResultOk("c1")]; assert.equal(core.decideMarkerText(head), MARKER_TEXT); assert.equal(core.decideMarkerText([...head, emptyError()]), MARKER_TEXT); }); test("decideMarkerText: 并行工具里只要有一个没成功(不论顺序)→ for tool use 文案", () => { const calls = assistant("toolUse", [toolCall("c1"), toolCall("c2")]); assert.equal(core.decideMarkerText([user("OLD"), calls, toolResultOk("c1"), toolResult("c2"), emptyError()]), MARKER_TEXT_TOOL_USE); assert.equal(core.decideMarkerText([user("OLD"), calls, toolResult("c1"), toolResultOk("c2"), emptyError()]), MARKER_TEXT_TOOL_USE); }); test("decideMarkerText: 尾部已是标记 → undefined(标记后再跟空的失败 assistant 也一样)", () => { assert.equal(core.decideMarkerText([user("OLD"), assistant("aborted", [text("x")]), marker()]), undefined); assert.equal(core.decideMarkerText([user("OLD"), marker(MARKER_TEXT_TOOL_USE), emptyError()]), undefined); }); test("decideMarkerText: 空列表、尾部是别的扩展的 custom 消息 → 普通文案", () => { assert.equal(core.decideMarkerText([]), MARKER_TEXT); assert.equal(core.decideMarkerText([user("OLD"), { role: "custom", customType: "verify-loop", content: "x" }]), MARKER_TEXT); }); test("entriesToMessages: message 取 message;custom_message 转成 role=custom;其余类型一律跳过", () => { const out = core.entriesToMessages([ { type: "model_change", provider: "p", modelId: "m" }, msgEntry(user("OLD")), { type: "custom_message", customType: MARKER_TYPE, content: MARKER_TEXT, display: true }, { type: "custom", customType: "goal", data: {} }, { type: "thinking_level_change", thinkingLevel: "high" }, { type: "compaction", summary: "s" }, ]); assert.deepEqual(out.map((m) => m.role), ["user", "custom"]); assert.equal(out[1]!.customType, MARKER_TYPE); }); test("非消息条目夹在尾部(thinking_level_change / custom / model_change)不影响判定与去重", () => { const aborted = [msgEntry(user("OLD")), msgEntry(assistant("aborted", [text("x")]))]; const noise = [{ type: "thinking_level_change", thinkingLevel: "high" }, { type: "custom", customType: "goal", data: {} }, { type: "model_change" }]; assert.equal(core.decideMarkerText(core.entriesToMessages([...aborted, ...noise])), MARKER_TEXT); const marked = [...aborted, { type: "custom_message", customType: MARKER_TYPE, content: MARKER_TEXT, display: true }, ...noise]; assert.equal(core.decideMarkerText(core.entriesToMessages(marked)), undefined); }); test("revive: 标记在场 → 半截文本复活为 stop,只留非空 text,去掉 errorMessage,入参不被修改", () => { const aborted = assistant("aborted", [{ type: "thinking", thinking: "hm" }, text("step0 "), toolCall("c1")], { errorMessage: "This operation was aborted", model: "m" }); const input = [user("OLD"), aborted, marker(), user("NEW")]; const out = core.reviveAbortedPartials(input)!; assert.deepEqual(out[1], { role: "assistant", stopReason: "stop", content: [text("step0 ")], model: "m" }); assert.equal(out.length, 4); assert.equal(out[0], input[0]); assert.equal(out[2], input[2]); assert.equal(aborted.stopReason, "aborted"); assert.equal(aborted.errorMessage, "This operation was aborted"); assert.equal(aborted.content.length, 3); }); test("revive: 没有标记 → undefined(旧会话 / 从被中止条目分叉)", () => { assert.equal(core.reviveAbortedPartials([user("OLD"), assistant("aborted", [text("x")]), user("NEW")]), undefined); }); test("revive: 标记出现在下一条 assistant 之后 → undefined", () => { const input = [user("OLD"), assistant("aborted", [text("x")]), assistant("stop", [text("later")]), marker()]; assert.equal(core.reviveAbortedPartials(input), undefined); }); test("revive: 标记与被中止消息之间夹着别的扩展的 custom 消息 → 仍复活", () => { const input = [user("OLD"), assistant("aborted", [text("x")]), { role: "custom", customType: "verify-loop", content: "y" }, marker(), user("NEW")]; assert.equal(core.reviveAbortedPartials(input)![1]!.stopReason, "stop"); }); test("revive: 没有非空文本(只有 thinking / 半截 toolCall)→ undefined", () => { const input = [user("OLD"), assistant("aborted", [{ type: "thinking", thinking: "hm" }, toolCall("c1")]), marker()]; assert.equal(core.reviveAbortedPartials(input), undefined); }); test("revive: 文本全是空白 → undefined", () => { assert.equal(core.reviveAbortedPartials([user("OLD"), assistant("aborted", [text("\n "), text("")]), marker()]), undefined); }); test("revive: content 缺失 / 是字符串 → 不抛,原样(undefined)", () => { for (const content of [undefined, "partial text"]) { const aborted = { role: "assistant", stopReason: "aborted", content }; assert.equal(core.reviveAbortedPartials([user("OLD"), aborted, marker()]), undefined); } }); test("revive: 非 aborted 的 assistant(stop / error / toolUse)不动", () => { for (const stopReason of ["stop", "error", "toolUse"]) { assert.equal(core.reviveAbortedPartials([user("OLD"), assistant(stopReason, [text("x")]), marker()]), undefined, stopReason); } }); test("revive: 多段历史各自复活;对输出再调一次是幂等的", () => { const input = [user("A"), assistant("aborted", [text("a1")]), marker(), user("B"), assistant("aborted", [text("b1")]), marker(MARKER_TEXT_TOOL_USE), user("C")]; const out = core.reviveAbortedPartials(input)!; assert.deepEqual(out.map((m) => m.stopReason), [undefined, "stop", undefined, undefined, "stop", undefined, undefined]); assert.equal(core.reviveAbortedPartials(out), undefined); }); test("fitLine: 放得下原样;放不下截断加 …;width<=0 为空串", () => { assert.equal(core.fitLine("abc", 10, 1), " abc"); assert.equal(core.fitLine("abcdef", 4, 1), " ab…"); assert.equal(core.fitLine("abc", 1, 1), "…"); assert.equal(core.fitLine("abc", 0, 1), ""); });