test(os-notifier): exercise native runtime delivery
84ad1d6fdacd9a3f6402441c13c159ce6d8407cb
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 708c07d451db
bugabingas pi extensions
84ad1d6fdacd9a3f6402441c13c159ce6d8407cb
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+87 / −0 text lines
+87 / −0 text lines
focusState.focused = originalFocus; it.skipIf(process.platform !== "linux")( "uses native delivery only for unfocused interactive TUI runs", async () => { const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); t = await createTestSession({ extensions: [extensionPath] }); const bin = join(t.cwd, "bin"); const log = join(t.cwd, "notify.log"); const executable = join(bin, "notify-send"); mkdirSync(bin); writeFileSync( executable, `#!${process.execPath}\nrequire("node:fs").appendFileSync(process.env.TEST_NOTIFY_LOG, JSON.stringify(process.argv.slice(2)) + "\\n");\n`, ); chmodSync(executable, 0o755); t.env.PATH = `${bin}${delimiter}${t.env.PATH ?? ""}`; t.env.TEST_NOTIFY_LOG = log; t.session.extensionRunner.setUIContext( t.session.extensionRunner.getUIContext(), "tui", ); focusState.focused = false; await emitCompletion(t, "interactive"); await waitFor(() => readFileSync(log, "utf8").trim().length > 0); const first = readFileSync(log, "utf8").trim().split("\n"); expect(JSON.parse(first[0])).toContain("Pi finished"); focusState.focused = true; await emitCompletion(t, "interactive"); focusState.focused = false; await emitCompletion(t, "extension"); t.session.extensionRunner.setUIContext( t.session.extensionRunner.getUIContext(), "rpc", ); await emitCompletion(t, "interactive"); await new Promise((resolve) => setTimeout(resolve, 50)); expect(readFileSync(log, "utf8").trim().split("\n")).toHaveLength(1); }, );async function emitCompletion( t: TestSession, source: "interactive" | "rpc" | "extension",): Promise<void> { await t.session.extensionRunner.emit({ type: "input", text: "test notification", source, } as never); await t.session.extensionRunner.emit({ type: "agent_start" } as never); await t.session.extensionRunner.emit({ type: "agent_end", messages: [ { role: "user", content: "test notification" }, { role: "assistant", stopReason: "stop" }, ], } as never);}async function waitFor( assertion: () => boolean, timeoutMs = 2000,): Promise<void> { const deadline = Date.now() + timeoutMs; while (Date.now() < deadline) { try { if (assertion()) return; } catch { // File may not exist until the child process starts. } await new Promise((resolve) => setTimeout(resolve, 10)); } throw new Error("timed out waiting for native notification executable");}