// harness/tests/fake_pi/test_fake_pi_contract.mjs — run: node --test import test from "node:test"; import assert from "node:assert"; import { spawn } from "node:child_process"; import path from "node:path"; function drive(scriptPath, commands) { return new Promise((resolve) => { const fp = spawn("node", [path.join(import.meta.dirname, "fake_pi_rpc.mjs"), scriptPath]); const events = []; let buf = ""; fp.stdout.on("data", (c) => { buf += c.toString("utf8"); for (let nl; (nl = buf.indexOf("\n")) !== -1; ) { const line = buf.slice(0, nl).replace(/\r$/, ""); buf = buf.slice(nl + 1); if (line) events.push(JSON.parse(line)); } }); fp.on("exit", () => resolve(events)); for (const cmd of commands) fp.stdin.write(JSON.stringify(cmd) + "\n"); setTimeout(() => fp.stdin.end(), 300); }); } test("on prompt emette il widget F1; on response avanza", async () => { const sp = path.join(import.meta.dirname, "scripts/f1_disambiguation.json"); const events = await drive(sp, [ { type: "prompt", message: "/nuova-domanda \"x\"" }, { type: "extension_ui_response", id: "u1", value: JSON.stringify({ id: "u1", choices: ["a"], decision: { type: "concept_clarified" } }) }, ]); const widget = events.find((e) => e.type === "extension_ui_request"); assert.equal(widget.method, "input"); // shape nativa di Pi assert.equal(widget.id, "u1"); const descriptor = JSON.parse(widget.title); // il descriptor viaggia nel title assert.equal(descriptor.widget, "select"); assert.equal(descriptor.id, "u1"); assert.ok(events.some((e) => e.type === "agent_end")); });