Files
archie/ui/test/chat-thinking.test.ts
samandCopilot 731417ab48 feat(ui/chat): run the Vue chat on the archie binary (#2023)
Sessions, turns, streamed replies, thinking and tool cards, held-action
approvals and resume now use modules/chat; /api/stream forwards the chat
topic. Sub-features the binary can't back yet stay hidden.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-20 01:45:35 +11:00

106 lines
4.4 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { register } from "node:module";
import { pathToFileURL } from "node:url";
import { resolve as resolvePath } from "node:path";
// state.ts reads the dashboard through "@/lib/api" and "@/router", which node
// cannot resolve on its own (and which pull in the whole SPA). A resolve hook
// maps the alias into src/ for the pure modules and stubs the two browser-only
// ones, so the real frame handling in state.ts runs under node.
const srcRoot = resolvePath(import.meta.dirname, "../src");
const apiStub =
"data:text/javascript," +
encodeURIComponent(
"export const api = new Proxy({}, { get: (_, p) => globalThis.__archieChatTestApi[p] });" +
"export function describeError(err, fallback) { return err instanceof Error && err.message ? err.message : fallback; }",
);
const sessionStub =
"data:text/javascript," + encodeURIComponent("export function useSessionStore() { return { sections: null }; }");
const routerStub =
"data:text/javascript," +
encodeURIComponent(
"export const router = { currentRoute: { value: { fullPath: '/', name: 'home', meta: {}, params: {} } } };",
);
register(
"data:text/javascript," +
encodeURIComponent(`
const SRC = ${JSON.stringify(pathToFileURL(srcRoot + "/").href)};
export async function resolve(specifier, context, next) {
if (specifier === "@/lib/api") return { url: ${JSON.stringify(apiStub)}, shortCircuit: true };
if (specifier === "@/stores/session") return { url: ${JSON.stringify(sessionStub)}, shortCircuit: true };
if (specifier === "@/router") return { url: ${JSON.stringify(routerStub)}, shortCircuit: true };
if (specifier === "@/copy/errors") return { url: SRC + "copy/errors/index.ts", shortCircuit: true };
if (specifier.startsWith("@/")) return { url: SRC + specifier.slice(2) + ".ts", shortCircuit: true };
if ((specifier.startsWith("./") || specifier.startsWith("../")) && !/\\.[a-z0-9]+$/i.test(specifier)) {
return { url: new URL(specifier + ".ts", context.parentURL).href, shortCircuit: true };
}
return next(specifier, context);
}
`),
);
interface Frame {
type: string;
text?: string;
}
/** One SSE body: each frame as the server writes it, `data: {...}\n\n`. */
function sseBody(frames: Frame[]): ReadableStream<Uint8Array> {
const encoder = new TextEncoder();
const payload = frames.map((frame) => `data: ${JSON.stringify(frame)}\n\n`).join("");
return new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode(payload));
controller.close();
},
});
}
// The stream stub: the turn's events answer with the given frames, sessions
// reads back an empty list so the finished turn does not trigger a reload.
function stubApi(frames: Frame[]): void {
(globalThis as Record<string, unknown>).__archieChatTestApi = {
chatCreateSession: () => ({ session_id: "s1", title: "" }),
chatSend: () => ({ id: "t1" }),
chatTurnEvents: () => ({ body: sseBody(frames) }),
chatSessions: () => ({ sessions: [] }),
};
}
const state = await import("../src/chat/state.ts");
test("reasoning frames accumulate on the live turn as its thinking", async () => {
stubApi([
{ type: "reasoning", text: "considering " },
{ type: "reasoning", text: "the request" },
{ type: "error", text: "provider exploded" },
]);
await state.sendMessage({ textOverride: "hello" });
const turn = state.streamingTurn.value;
assert.ok(turn, "the failed turn stays on the panel");
assert.equal(turn.isError, true);
assert.equal(turn.reasoning, "considering the request");
});
test("reasoning never leaks into the reply text", async () => {
stubApi([
{ type: "reasoning", text: "secret chain of thought" },
{ type: "delta", text: "Hello" },
{ type: "done", text: "Hello" },
]);
await state.sendMessage({ textOverride: "hi" });
assert.equal(state.streamingTurn.value, null);
const last = state.messages.value.at(-1);
assert.equal(last?.from, "assistant");
assert.equal(last?.text, "Hello");
});
test("a turn without reasoning frames completes normally", async () => {
stubApi([{ type: "delta", text: "Hi" }, { type: "done", text: "Hi" }]);
await state.sendMessage({ textOverride: "hey" });
assert.equal(state.streamingTurn.value, null);
assert.equal(state.messages.value.at(-1)?.text, "Hi");
assert.equal(state.statusText.value, "Ready");
});