pi-ext
bugabingas pi extensions
owned by bugabinga
test(os-notifier): exercise native runtime delivery
84ad1d6fdacd9a3f6402441c13c159ce6d8407cb
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (1)
extensions/os-notifier/__tests__/harness.test.ts
Modified 1 +import { chmodSync, mkdirSync, readFileSync, writeFileSync }
from "node:fs";
2 +import { delimiter, join }
from "node:path";
1 3 import path
from "node:path";
2 4 import { afterEach, describe, expect, it }
from "vitest";
3 5 import { createTestSession, type TestSession }
from "../../../test/harness";
183 185 describe(
"os-notifier pi-test-harness", ()
=> {
184 186 let t
: TestSession | undefined;
187 + const focusState = (
188 + globalThis
as unknown as {
189 + __bugabinga_pi_focus
: { focused
: boolean };
191 + ).__bugabinga_pi_focus;
192 + const originalFocus
= focusState.focused;
186 194 afterEach(()
=> {
195 + focusState.focused
= originalFocus;
195 204 expect(t.session.extensionRunner.extensions).
toHaveLength(
1);
196 205 expect(t.session.extensionRunner.extensions[
0].path).
toBe(extensionPath);
208 + it.
skipIf(
process.
platform !== "linux")(
209 + "uses native delivery only for unfocused interactive TUI runs",
211 + const extensionPath
= path.
resolve(
import.
meta.dirname,
"../index.ts");
212 + t
= await createTestSession({ extensions: [extensionPath] });
213 + const bin
= join(t.cwd,
"bin");
214 + const log
= join(t.cwd,
"notify.log");
215 + const executable
= join(bin,
"notify-send");
219 + `#!${process.execPath}\nrequire("node:fs").appendFileSync(process.env.TEST_NOTIFY_LOG, JSON.stringify(process.argv.slice(2)) + "\\n");\n`,
221 + chmodSync(executable,
0o755);
222 + t.env.PATH
= `${bin}${delimiter}${t.env.PATH ?? ""}`;
223 + t.env.TEST_NOTIFY_LOG
= log;
224 + t.session.extensionRunner.
setUIContext(
225 + t.session.extensionRunner.
getUIContext(),
229 + focusState.focused
= false;
230 + await emitCompletion(t,
"interactive");
231 + await waitFor(()
=> readFileSync(log,
"utf8").
trim().
length > 0);
232 + const first
= readFileSync(log,
"utf8").
trim().
split(
"\n");
233 + expect(
JSON.
parse(first[
0])).
toContain(
"Pi finished");
235 + focusState.focused
= true;
236 + await emitCompletion(t,
"interactive");
238 + focusState.focused
= false;
239 + await emitCompletion(t,
"extension");
240 + t.session.extensionRunner.
setUIContext(
241 + t.session.extensionRunner.
getUIContext(),
244 + await emitCompletion(t,
"interactive");
246 + await new Promise((resolve)
=> setTimeout(resolve,
50));
247 + expect(
readFileSync(log,
"utf8").
trim().
split(
"\n")).
toHaveLength(
1);
252 +async function emitCompletion(
254 + source
: "interactive" | "rpc" | "extension",
256 + await t.session.extensionRunner.
emit({
258 + text:
"test notification",
261 + await t.session.extensionRunner.
emit({ type:
"agent_start" }
as never);
262 + await t.session.extensionRunner.
emit({
265 + { role:
"user", content:
"test notification" },
266 + { role:
"assistant", stopReason:
"stop" },
271 +async function waitFor(
272 + assertion: ()
=> boolean,
275 + const deadline
= Date.
now()
+ timeoutMs;
276 + while (
Date.
now()
< deadline) {
278 + if (
assertion())
return;
282 + await new Promise((resolve)
=> setTimeout(resolve,
10));
284 + throw new Error(
"timed out waiting for native notification executable");