| 1 | import assert from "node:assert/strict"; |
| 2 | import { AutoPresentedHTMLGate } from "../lib/autoHTML"; |
| 3 | import type { WireEvent } from "../lib/types"; |
| 4 | |
| 5 | const result = (id: string, files: string[], extra: Partial<WireEvent> = {}): WireEvent => ({ |
| 6 | kind: "tool_result", |
| 7 | tool: { id, name: "present", readOnly: true, presentedFiles: files.map((path) => ({ path })) }, |
| 8 | ...extra, |
| 9 | }); |
| 10 | |
| 11 | const gate = new AutoPresentedHTMLGate(); |
| 12 | gate.observeTurnStart("tab-a"); |
| 13 | assert.equal(gate.claim(result("p1", ["notes.md", "INDEX.HTML"]), "tab-a", "tab-a", 1, "turn-1")?.file.path, "INDEX.HTML"); |
| 14 | assert.equal(gate.claim(result("p2", ["second.html"]), "tab-a", "tab-a", 1, "turn-1"), null, "only the first HTML in a turn is automatic"); |
| 15 | assert.equal(gate.claim(result("background", ["background.html"]), "tab-b", "tab-a", 1, "turn-1"), null, "a background task never steals focus"); |
| 16 | assert.equal(gate.claim(result("failed", ["bad.html"], { tool: { id: "failed", name: "present", readOnly: true, err: "failed", presentedFiles: [{ path: "bad.html" }] } }), "tab-a", "tab-a", 1, "turn-1"), null); |
| 17 | gate.observeTurnStart("tab-a"); |
| 18 | assert.equal(gate.claim(result("p3", ["next.htm"]), "tab-a", "tab-a", 1, "turn-2")?.file.path, "next.htm", "a new turn may auto preview again"); |
| 19 | assert.equal(gate.claim(result("history", ["old.html"]), "tab-a", "tab-a", 2, "turn-2")?.file.path, "old.html", "a changed session generation has an independent gate"); |
| 20 | console.log("PASS automatic presented HTML gate"); |
| 21 |