|
| 1 | +// webui/test/lib/acp-turn-message-id.test.js |
| 2 | +// webui-parity 83 — which messageId the transport keeps for a turn. |
| 3 | +// |
| 4 | +// The engine persists a turn's file-change record under the msg_id of that |
| 5 | +// turn's LAST assistant MESSAGE |
| 6 | +// (`local-runtime-v2/.../turn-outcome.ts#readAssistantMessageId` reads |
| 7 | +// AgentMessage / AgentMessageChunk and never a thought). A turn carries |
| 8 | +// more than one id on the wire — one per message segment, plus one per |
| 9 | +// thought segment — so "any id" selects the wrong record. |
| 10 | +// |
| 11 | +// The rule is pinned here against the real `McodeAcpClient#prompt`, driven |
| 12 | +// by a fake `request` that replays `session/update` frames. No child process |
| 13 | +// is spawned: the client is constructed but never `start()`ed, and only |
| 14 | +// `on` / `off` / `request` are exercised. |
| 15 | + |
| 16 | +import { test, describe } from "node:test"; |
| 17 | +import assert from "node:assert/strict"; |
| 18 | +import { pathToFileURL } from "node:url"; |
| 19 | +import { join } from "node:path"; |
| 20 | + |
| 21 | +const absPath = (rel) => |
| 22 | + pathToFileURL(join(import.meta.dirname, "..", "..", rel)).href; |
| 23 | +const { McodeAcpClient } = await import(absPath("acp.mjs")); |
| 24 | + |
| 25 | +/** |
| 26 | + * Replay `frames` as `session/update` events, then resolve `session/prompt`. |
| 27 | + * `prompt` subscribes through `on` and unsubscribes in its own settle path, |
| 28 | + * so the fake has to dispatch through the real EventEmitter. |
| 29 | + */ |
| 30 | +function fakeClient(frames) { |
| 31 | + const client = new McodeAcpClient(); |
| 32 | + client.started = false; |
| 33 | + client.request = async (method) => { |
| 34 | + if (method !== "session/prompt") throw new Error(`unexpected request ${method}`); |
| 35 | + for (const frame of frames) client.emit("sessionUpdate", frame); |
| 36 | + return { stopReason: "end_turn" }; |
| 37 | + }; |
| 38 | + return client; |
| 39 | +} |
| 40 | + |
| 41 | +const text = (t) => ({ type: "text", text: t }); |
| 42 | +const thought = (id, t) => ({ sessionUpdate: "agent_thought_chunk", content: text(t), messageId: id }); |
| 43 | +const message = (id, t) => ({ sessionUpdate: "agent_message_chunk", content: text(t), messageId: id }); |
| 44 | + |
| 45 | +describe("prompt keeps the turn's last assistant messageId", () => { |
| 46 | + test("a simple turn yields that turn's only message id", async () => { |
| 47 | + const client = fakeClient([message("m1", "答案")]); |
| 48 | + const result = await client.prompt("mvs_x", "hi", () => {}); |
| 49 | + assert.equal(result.lastAssistantMessageId, "m1"); |
| 50 | + }); |
| 51 | + |
| 52 | + test("a turn with several message segments keeps the LAST one", async () => { |
| 53 | + // The turn the real engine runs: a first message, a tool call, then a |
| 54 | + // second message. The record is filed under the second one. |
| 55 | + const client = fakeClient([ |
| 56 | + message("m1", "我先看看"), |
| 57 | + { sessionUpdate: "tool_call", toolCallId: "t1", title: "bash" }, |
| 58 | + message("m2", "改好了"), |
| 59 | + ]); |
| 60 | + const result = await client.prompt("mvs_x", "hi", () => {}); |
| 61 | + assert.equal(result.lastAssistantMessageId, "m2"); |
| 62 | + }); |
| 63 | + |
| 64 | + test("a thought id never wins over the message id", async () => { |
| 65 | + // The engine's reader ignores thoughts entirely. Taking the thought's id |
| 66 | + // would send the endpoint a selector that matches no diff record — an |
| 67 | + // empty card on a turn that really did change files. |
| 68 | + const client = fakeClient([ |
| 69 | + thought("t1", "先想一下"), |
| 70 | + message("m1", "动手"), |
| 71 | + thought("t2", "再想想"), |
| 72 | + ]); |
| 73 | + const result = await client.prompt("mvs_x", "hi", () => {}); |
| 74 | + assert.equal(result.lastAssistantMessageId, "m1"); |
| 75 | + }); |
| 76 | + |
| 77 | + test("a turn with no message chunk yields no id at all", async () => { |
| 78 | + // A thinking-only turn has no assistant message, so there is no selector. |
| 79 | + // Emitting the thought's id here would be the "any chunk" bug wearing a |
| 80 | + // different hat. |
| 81 | + const client = fakeClient([thought("t1", "只有思考")]); |
| 82 | + const result = await client.prompt("mvs_x", "hi", () => {}); |
| 83 | + assert.equal(result.lastAssistantMessageId, null); |
| 84 | + }); |
| 85 | + |
| 86 | + test("a chunk without a messageId does not clear the one already seen", async () => { |
| 87 | + const client = fakeClient([message("m1", "有 id"), message(undefined, "没有 id")]); |
| 88 | + const result = await client.prompt("mvs_x", "hi", () => {}); |
| 89 | + assert.equal(result.lastAssistantMessageId, "m1"); |
| 90 | + }); |
| 91 | + |
| 92 | + test("the id is per-prompt, not accumulated on the client", async () => { |
| 93 | + // Two turns on one client: the second must not inherit the first turn's |
| 94 | + // id, or every card after the first would query the wrong turn. |
| 95 | + const queue = [[message("first", "第一轮")], [message("second", "第二轮")]]; |
| 96 | + const client = fakeClient([]); |
| 97 | + client.request = async (method) => { |
| 98 | + if (method !== "session/prompt") throw new Error("unexpected"); |
| 99 | + for (const f of queue.shift() ?? []) client.emit("sessionUpdate", f); |
| 100 | + return { stopReason: "end_turn" }; |
| 101 | + }; |
| 102 | + const one = await client.prompt("mvs_x", "one", () => {}); |
| 103 | + assert.equal(one.lastAssistantMessageId, "first"); |
| 104 | + const two = await client.prompt("mvs_x", "two", () => {}); |
| 105 | + assert.equal(two.lastAssistantMessageId, "second"); |
| 106 | + }); |
| 107 | +}); |
0 commit comments