repositories / pi-ext

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;
187 196 t?.dispose();
188 197 t = undefined;
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",
210 + async () => {
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");
216 + mkdirSync(bin);
217 + writeFileSync(
218 + executable,
219 + `#!${process.execPath}\nrequire("node:fs").appendFileSync(process.env.TEST_NOTIFY_LOG, JSON.stringify(process.argv.slice(2)) + "\\n");\n`,
220 + );
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(),
226 + "tui",
227 + );
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(),
242 + "rpc",
243 + );
244 + await emitCompletion(t, "interactive");
246 + await new Promise((resolve) => setTimeout(resolve, 50));
247 + expect(readFileSync(log, "utf8").trim().split("\n")).toHaveLength(1);
248 + },
249 + );
252 +async function emitCompletion(
253 + t: TestSession,
254 + source: "interactive" | "rpc" | "extension",
255 +): Promise<void> {
256 + await t.session.extensionRunner.emit({
257 + type: "input",
258 + text: "test notification",
259 + source,
260 + } as never);
261 + await t.session.extensionRunner.emit({ type: "agent_start" } as never);
262 + await t.session.extensionRunner.emit({
263 + type: "agent_end",
264 + messages: [
265 + { role: "user", content: "test notification" },
266 + { role: "assistant", stopReason: "stop" },
267 + ],
268 + } as never);
271 +async function waitFor(
272 + assertion: () => boolean,
273 + timeoutMs = 2000,
274 +): Promise<void> {
275 + const deadline = Date.now() + timeoutMs;
276 + while (Date.now() < deadline) {
277 + try {
278 + if (assertion()) return;
279 + } catch {
280 + // File may not exist until the child process starts.
282 + await new Promise((resolve) => setTimeout(resolve, 10));
284 + throw new Error("timed out waiting for native notification executable");