Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192import assert from "node:assert/strict";import test from "node:test";import type { Context } from "@earendil-works/pi-ai";import { createEventBus, initTheme, SessionManager, type ExtensionAPI, type ExtensionCommandContext,} from "@earendil-works/pi-coding-agent";import handoffExtension from "../index.ts";
initTheme("dark");
type Command = Parameters<ExtensionAPI["registerCommand"]>[1];type NewSessionOptions = NonNullable<Parameters<ExtensionCommandContext["newSession"]>[0]>;type LoaderFactory = ( tui: { requestRender(): void }, theme: { fg(color: string, text: string): string }, keybindings: undefined, done: (result: string | null) => void,) => { dispose(): void };
function fixture(cancel?: "generation" | "editor" | "session") { const source = SessionManager.inMemory(); source.appendMessage({ role: "user", content: "Continue the task", timestamp: 1 }); const destination = SessionManager.inMemory(); const events = createEventBus(); const commands = new Map<string, Command>(); const contexts: Context[] = []; const drafts: string[] = []; const sourceEdits: string[] = []; const destinationEdits: string[] = []; const notifications: string[] = []; let replacements = 0; const pi = { events, on() {}, registerCommand(name: string, command: Command) { commands.set(name, command); }, }; // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- The fixture supplies the extension's API members. handoffExtension(pi as never); const ctx = { mode: "tui", sessionManager: source, getSystemPrompt: () => "Test instructions", waitForIdle: async () => {}, model: { maxTokens: 100 }, modelRegistry: { async complete(_model: { maxTokens: number }, context: Context) { contexts.push(context); return { content: [{ type: "text", text: "Generated handoff" }], stopReason: cancel === "generation" ? "aborted" : "stop", }; }, }, ui: { notify(message: string) { notifications.push(message); }, setEditorText(text: string) { sourceEdits.push(text); }, async editor(_title: string, draft: string) { drafts.push(draft); return cancel === "editor" ? undefined : draft; }, async custom(factory: LoaderFactory) { const result = Promise.withResolvers<string | null>(); const loader = factory( { requestRender() {} }, { fg: (_color, text) => text }, undefined, result.resolve, ); try { return await result.promise; } finally { loader.dispose(); } }, }, async newSession(options: NewSessionOptions) { replacements++; if (cancel === "session") { return { cancelled: true }; } await options.setup?.(destination); const replacement = { ui: { setEditorText(text: string) { destinationEdits.push(text); }, notify() {}, }, }; // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Only replacement UI methods are used here. await options.withSession?.(replacement as never); return { cancelled: false }; }, }; return { source, destination, events, contexts, drafts, sourceEdits, destinationEdits, notifications, get replacements() { return replacements; }, async run() { const command = commands.get("handoff"); assert.ok(command); // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- The fixture supplies the command's context members. await command.handler("Continue the task", ctx as never); }, };}
function provideWork(extension: ReturnType<typeof fixture>) { const content = "## Work record\n\nExact record reference at revision 7"; extension.events.on("work:handoff", (request) => { assert.ok(request); Object.assign(request, { content, setup: async (session: SessionManager) => { session.appendCustomEntry("test-selection", { id: "record-example" }); }, }); }); return content;}
test("handoff keeps the exact work reference and initializes the destination", async () => { const extension = fixture(); const reference = provideWork(extension); const sourceLength = extension.source.getEntries().length; await extension.run(); assert.match(JSON.stringify(extension.contexts[0]), /Exact record reference at revision 7/); assert.equal(extension.drafts[0], `Generated handoff\n\n${reference}`); assert.deepEqual(extension.destinationEdits, extension.drafts); assert.deepEqual(extension.sourceEdits, []); assert.deepEqual( extension.destination.getBranch().map((entry) => entry.type), ["custom"], ); const selection = extension.destination.getBranch()[0]; assert.ok(selection.type === "custom"); assert.deepEqual(selection.data, { id: "record-example" }); assert.equal(extension.source.getEntries().length, sourceLength);});
test("handoff works without a work provider", async () => { const extension = fixture(); await extension.run(); assert.deepEqual(extension.drafts, ["Generated handoff"]); assert.deepEqual(extension.destinationEdits, extension.drafts); assert.deepEqual(extension.destination.getBranch(), []);});
test("a failed work read stops handoff before generation or session replacement", async () => { const extension = fixture(); extension.events.on("work:handoff", (request) => { assert.ok(request); Object.assign(request, { error: "Storage unavailable" }); }); await extension.run(); assert.deepEqual(extension.notifications, ["Handoff failed: Storage unavailable"]); assert.deepEqual(extension.contexts, []); assert.deepEqual(extension.drafts, []); assert.equal(extension.replacements, 0); assert.equal(extension.source.getBranch().length, 1);});
test("cancelling handoff does not transfer state or submit the draft", async () => { for (const cancel of ["generation", "editor", "session"] as const) { const extension = fixture(cancel); provideWork(extension); await extension.run(); assert.equal(extension.replacements, cancel === "session" ? 1 : 0); assert.deepEqual(extension.destination.getBranch(), []); assert.deepEqual(extension.destinationEdits, []); assert.deepEqual(extension.sourceEdits, cancel === "session" ? extension.drafts : []); }});