|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { BackgroundShellStore } from "./background-shell-store.js"; |
| 3 | +import { type MonitorMatchEvent, MonitorStore } from "./monitor-store.js"; |
| 4 | + |
| 5 | +async function wait(ms: number): Promise<void> { |
| 6 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 7 | +} |
| 8 | + |
| 9 | +async function waitUntil<T>(fn: () => T | undefined, timeoutMs = 4000): Promise<T> { |
| 10 | + const deadline = Date.now() + timeoutMs; |
| 11 | + while (Date.now() < deadline) { |
| 12 | + const v = fn(); |
| 13 | + if (v !== undefined) return v; |
| 14 | + await wait(20); |
| 15 | + } |
| 16 | + throw new Error("waitUntil timed out"); |
| 17 | +} |
| 18 | + |
| 19 | +describe("MonitorStore", () => { |
| 20 | + let bg: BackgroundShellStore; |
| 21 | + let monitors: MonitorStore; |
| 22 | + let captured: MonitorMatchEvent[]; |
| 23 | + let unsub: () => void; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + bg = new BackgroundShellStore(); |
| 27 | + monitors = new MonitorStore(bg); |
| 28 | + captured = []; |
| 29 | + unsub = monitors.onMatch((e) => captured.push(e)); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + unsub(); |
| 34 | + bg.killAllSync(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("emits a match event for every line when no regex is set", async () => { |
| 38 | + const record = bg.spawn("printf 'one\\ntwo\\nthree\\n'", process.cwd()); |
| 39 | + monitors.register({ taskId: record.id }); |
| 40 | + await waitUntil(() => (captured.length >= 3 ? captured : undefined)); |
| 41 | + expect(captured.map((e) => e.line)).toEqual(["one", "two", "three"]); |
| 42 | + }); |
| 43 | + |
| 44 | + it("filters lines by regex", async () => { |
| 45 | + const record = bg.spawn("printf 'info: ok\\nERROR: bad\\ninfo: ok\\nFATAL: dead\\n'", process.cwd()); |
| 46 | + monitors.register({ taskId: record.id, regex: /ERROR|FATAL/ }); |
| 47 | + await waitUntil(() => (captured.length >= 2 ? captured : undefined)); |
| 48 | + expect(captured.map((e) => e.line)).toEqual(["ERROR: bad", "FATAL: dead"]); |
| 49 | + }); |
| 50 | + |
| 51 | + it("auto-unregisters after maxMatches is reached", async () => { |
| 52 | + const record = bg.spawn("printf 'one\\ntwo\\nthree\\nfour\\n'", process.cwd()); |
| 53 | + const mon = monitors.register({ taskId: record.id, maxMatches: 2 }); |
| 54 | + await waitUntil(() => (captured.length >= 2 ? captured : undefined)); |
| 55 | + await wait(100); |
| 56 | + expect(captured.length).toBe(2); |
| 57 | + expect(monitors.get(mon.id)).toBeUndefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("auto-cleans the monitor when the watched shell exits", async () => { |
| 61 | + const record = bg.spawn("printf 'hi\\n' && true", process.cwd()); |
| 62 | + const mon = monitors.register({ taskId: record.id }); |
| 63 | + // Wait until the shell exits. |
| 64 | + await waitUntil(() => (bg.get(record.id)?.status === "exited" ? true : undefined)); |
| 65 | + // The subscription is dropped synchronously in the BgShell exit |
| 66 | + // handler; the MonitorStore subscriber sees the status flip and |
| 67 | + // removes the monitor. Give one event-loop tick to settle. |
| 68 | + await wait(20); |
| 69 | + expect(monitors.get(mon.id)).toBeUndefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("remove() returns true the first time, false on a repeat", async () => { |
| 73 | + const record = bg.spawn("sleep 1", process.cwd()); |
| 74 | + const mon = monitors.register({ taskId: record.id }); |
| 75 | + expect(monitors.remove(mon.id)).toBe(true); |
| 76 | + expect(monitors.remove(mon.id)).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it("buffers a partial-line tail across chunks", async () => { |
| 80 | + // We can't easily split chunks at the test level — printf delivers |
| 81 | + // in one chunk on this platform. Verify behavior by registering |
| 82 | + // after some output has already been buffered by hand-feeding the |
| 83 | + // subscriber: directly exercise consumeChunk via a fake by spawning |
| 84 | + // a process that emits two writes (sleep between them). |
| 85 | + const record = bg.spawn("printf 'partial-' && sleep 0.05 && printf 'line\\ndone\\n'", process.cwd()); |
| 86 | + monitors.register({ taskId: record.id }); |
| 87 | + await waitUntil(() => (captured.length >= 2 ? captured : undefined)); |
| 88 | + expect(captured.map((e) => e.line)).toEqual(["partial-line", "done"]); |
| 89 | + }); |
| 90 | + |
| 91 | + it("a misbehaving listener doesn't break the others", async () => { |
| 92 | + const record = bg.spawn("printf 'x\\ny\\n'", process.cwd()); |
| 93 | + const noisy = vi.fn(() => { |
| 94 | + throw new Error("boom"); |
| 95 | + }); |
| 96 | + monitors.onMatch(noisy); |
| 97 | + monitors.register({ taskId: record.id }); |
| 98 | + await waitUntil(() => (captured.length >= 2 ? captured : undefined)); |
| 99 | + expect(noisy).toHaveBeenCalled(); |
| 100 | + expect(captured.length).toBe(2); |
| 101 | + }); |
| 102 | +}); |
0 commit comments