Files

18 lines
1.5 KiB
TypeScript

import assert from "node:assert/strict";
import { test } from "node:test";
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { NativeHooksRuntime } from "../src/runtime.ts";
test("runtime registers lifecycle hooks and returns the current call's result patch", async () => {
const handlers = new Map<string, Array<(...args: unknown[]) => unknown>>(); const tools: string[] = []; const commands: string[] = [];
const pi = {
on(name: string, handler: (...args: unknown[]) => unknown) { const list = handlers.get(name) ?? []; list.push(handler); handlers.set(name, list); },
registerTool(tool: { name: string }) { tools.push(tool.name); }, registerCommand(name: string) { commands.push(name); },
} as unknown as ExtensionAPI;
const runtime = new NativeHooksRuntime(pi);
assert.deepEqual(tools, ["pi_hooks_waive_verification"]); assert.deepEqual(commands, ["hooks-status"]); assert.ok(handlers.has("agent_settled")); assert.ok(handlers.has("context"));
(runtime as unknown as { waves: { result(): Promise<{ content: Array<{ type: string; text: string }> }> } }).waves = { async result() { return { content: [{ type: "text", text: "augmented" }] }; } };
const handler = handlers.get("tool_result")![0]!; const patch = await handler({ type: "tool_result", toolCallId: "call", toolName: "write", input: { path: "a.ts" }, content: [{ type: "text", text: "original" }], details: undefined, isError: false }, {});
assert.deepEqual(patch, { content: [{ type: "text", text: "augmented" }] });
});