/** * Dev-only smoke check for the extension wiring (no pi runtime needed): * run: `npm run selfcheck` (node --experimental-strip-types). * * Verifies /peak registration, widget install on session_start, widget * teardown on session_shutdown, and /peak output through both UI paths. */ const { default: peakHours } = await import("../src/index.ts"); type Handler = (event: unknown, ctx: unknown) => void; const handlers: Record = {}; const commands: Record void }> = {}; const widgetCalls: Array<{ key: string; hasFactory: boolean }> = []; const notifications: string[] = []; const pi = { on: (name: string, fn: Handler) => { handlers[name] = fn; }, registerCommand: (name: string, opts: { description: string; handler: (args: string, ctx: unknown) => void }) => { commands[name] = opts; }, } as unknown as import("@earendil-works/pi-coding-agent").ExtensionAPI; peakHours(pi); let failures = 0; const check = (name: string, cond: boolean, detail = ""): void => { if (cond) { console.log(` ok ${name}`); } else { failures += 1; console.error(` FAIL ${name}${detail ? ` — ${detail}` : ""}`); } }; check("/peak command registered", typeof commands.peak?.handler === "function"); check("session_start handler registered", typeof handlers.session_start === "function"); check("session_shutdown handler registered", typeof handlers.session_shutdown === "function"); const makeCtx = (mode: string) => ({ mode, hasUI: mode !== "print", model: { id: "glm-5.3-flash" }, ui: { setWidget: (key: string, content: unknown) => { widgetCalls.push({ key, hasFactory: typeof content === "function" }); }, notify: (message: string) => { notifications.push(message); }, }, }); await handlers.session_start({}, makeCtx("tui")); check("widget installed on session_start", widgetCalls.some((w) => w.key === "peak-hours" && w.hasFactory)); await handlers.session_shutdown({}, makeCtx("tui")); check("widget cleared on session_shutdown", widgetCalls.some((w) => w.key === "peak-hours" && !w.hasFactory)); commands.peak.handler("", makeCtx("tui")); check("/peak notifies with a report", notifications.some((n) => n.includes("peak"))); let printOk = true; try { commands.peak.handler("", makeCtx("print")); } catch (error) { printOk = false; console.error(" print-mode error:", error); } check("/peak works without UI (console fallback)", printOk); console.log(failures === 0 ? "\nselfcheck: all passed" : `\nselfcheck: ${failures} failed`); process.exit(failures === 0 ? 0 : 1);