|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import * as os from "node:os"; |
| 5 | +import { FileWatcher, FileChangeEvent } from "../src/watcher/file-watcher.js"; |
| 6 | + |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | +// Helpers |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | + |
| 11 | +/** |
| 12 | + * Wait up to `timeout` ms for `predicate` to return true, polling every `interval` ms. |
| 13 | + * Provides a clear assertion failure message on timeout. |
| 14 | + */ |
| 15 | +function waitFor( |
| 16 | + predicate: () => boolean, |
| 17 | + timeout = 6000, |
| 18 | + interval = 50 |
| 19 | +): Promise<void> { |
| 20 | + return new Promise((resolve, reject) => { |
| 21 | + const deadline = Date.now() + timeout; |
| 22 | + const timer = setInterval(() => { |
| 23 | + if (predicate()) { |
| 24 | + clearInterval(timer); |
| 25 | + resolve(); |
| 26 | + } else if (Date.now() > deadline) { |
| 27 | + clearInterval(timer); |
| 28 | + reject(new Error("waitFor: timeout exceeded")); |
| 29 | + } |
| 30 | + }, interval); |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Wait for chokidar to finish setting up FS listeners. |
| 36 | + * Windows FSEvents/polling can need 500-1500ms before reliably firing. |
| 37 | + */ |
| 38 | +const WATCHER_SETTLE_MS = 1500; |
| 39 | + |
| 40 | +// --------------------------------------------------------------------------- |
| 41 | +// Suite |
| 42 | +// --------------------------------------------------------------------------- |
| 43 | + |
| 44 | +describe("FileWatcher", () => { |
| 45 | + let tmpDir: string; |
| 46 | + let watcher: FileWatcher; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "fw-test-")); |
| 50 | + }); |
| 51 | + |
| 52 | + afterEach(async () => { |
| 53 | + if (watcher) { |
| 54 | + await watcher.stop(); |
| 55 | + } |
| 56 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 57 | + }); |
| 58 | + |
| 59 | + // ----------------------------------------------------------------------- |
| 60 | + // AUTO-01: detect meaningful file changes |
| 61 | + // ----------------------------------------------------------------------- |
| 62 | + |
| 63 | + it("detects a file write as a 'change' event (AUTO-01)", async () => { |
| 64 | + // Create file before watcher starts so initial scan won't fire 'add' |
| 65 | + const filePath = path.join(tmpDir, "test.ts"); |
| 66 | + fs.writeFileSync(filePath, "// initial"); |
| 67 | + |
| 68 | + const events: FileChangeEvent[] = []; |
| 69 | + watcher = new FileWatcher(tmpDir); |
| 70 | + watcher.on("change", (evt) => events.push(evt)); |
| 71 | + watcher.start(); |
| 72 | + |
| 73 | + // Allow chokidar to finish indexing the directory |
| 74 | + await new Promise((r) => setTimeout(r, WATCHER_SETTLE_MS)); |
| 75 | + |
| 76 | + // Modify the file |
| 77 | + fs.writeFileSync(filePath, "// updated"); |
| 78 | + |
| 79 | + // Wait for event |
| 80 | + await waitFor(() => events.some((e) => e.event === "change" && e.path.endsWith("test.ts"))); |
| 81 | + |
| 82 | + const hit = events.find((e) => e.event === "change"); |
| 83 | + expect(hit).toBeDefined(); |
| 84 | + expect(hit!.path).toContain("test.ts"); |
| 85 | + expect(hit!.timestamp).toBeInstanceOf(Date); |
| 86 | + }, 15_000); |
| 87 | + |
| 88 | + it("detects a new file as an 'add' event (AUTO-01)", async () => { |
| 89 | + const events: FileChangeEvent[] = []; |
| 90 | + watcher = new FileWatcher(tmpDir); |
| 91 | + watcher.on("change", (evt) => events.push(evt)); |
| 92 | + watcher.start(); |
| 93 | + |
| 94 | + await new Promise((r) => setTimeout(r, WATCHER_SETTLE_MS)); |
| 95 | + |
| 96 | + fs.writeFileSync(path.join(tmpDir, "new.ts"), "export {}"); |
| 97 | + |
| 98 | + await waitFor(() => events.some((e) => e.event === "add" && e.path.endsWith("new.ts"))); |
| 99 | + |
| 100 | + const hit = events.find((e) => e.event === "add"); |
| 101 | + expect(hit).toBeDefined(); |
| 102 | + expect(hit!.event).toBe("add"); |
| 103 | + expect(hit!.timestamp).toBeInstanceOf(Date); |
| 104 | + }, 15_000); |
| 105 | + |
| 106 | + // ----------------------------------------------------------------------- |
| 107 | + // AUTO-02: noise filter |
| 108 | + // ----------------------------------------------------------------------- |
| 109 | + |
| 110 | + it("does NOT emit events for paths inside node_modules (AUTO-02)", async () => { |
| 111 | + const nmDir = path.join(tmpDir, "node_modules", "some-pkg"); |
| 112 | + fs.mkdirSync(nmDir, { recursive: true }); |
| 113 | + |
| 114 | + const events: FileChangeEvent[] = []; |
| 115 | + watcher = new FileWatcher(tmpDir); |
| 116 | + watcher.on("change", (evt) => events.push(evt)); |
| 117 | + watcher.start(); |
| 118 | + |
| 119 | + await new Promise((r) => setTimeout(r, WATCHER_SETTLE_MS)); |
| 120 | + |
| 121 | + fs.writeFileSync(path.join(nmDir, "index.ts"), "// ignored"); |
| 122 | + |
| 123 | + // Wait and assert silence |
| 124 | + await new Promise((r) => setTimeout(r, 800)); |
| 125 | + const nmEvents = events.filter((e) => e.path.includes("node_modules")); |
| 126 | + expect(nmEvents).toHaveLength(0); |
| 127 | + }, 15_000); |
| 128 | + |
| 129 | + it("does NOT emit events for *.log files (AUTO-02)", async () => { |
| 130 | + const events: FileChangeEvent[] = []; |
| 131 | + watcher = new FileWatcher(tmpDir); |
| 132 | + watcher.on("change", (evt) => events.push(evt)); |
| 133 | + watcher.start(); |
| 134 | + |
| 135 | + await new Promise((r) => setTimeout(r, WATCHER_SETTLE_MS)); |
| 136 | + |
| 137 | + fs.writeFileSync(path.join(tmpDir, "debug.log"), "some log line"); |
| 138 | + |
| 139 | + await new Promise((r) => setTimeout(r, 800)); |
| 140 | + expect(events.filter((e) => e.path.endsWith(".log"))).toHaveLength(0); |
| 141 | + }, 15_000); |
| 142 | + |
| 143 | + // ----------------------------------------------------------------------- |
| 144 | + // stop() — no further events after stop |
| 145 | + // ----------------------------------------------------------------------- |
| 146 | + |
| 147 | + it("stop() prevents further events from being emitted", async () => { |
| 148 | + const filePath = path.join(tmpDir, "track.ts"); |
| 149 | + fs.writeFileSync(filePath, "// v1"); |
| 150 | + |
| 151 | + const events: FileChangeEvent[] = []; |
| 152 | + watcher = new FileWatcher(tmpDir); |
| 153 | + watcher.on("change", (evt) => events.push(evt)); |
| 154 | + watcher.start(); |
| 155 | + |
| 156 | + await new Promise((r) => setTimeout(r, WATCHER_SETTLE_MS)); |
| 157 | + await watcher.stop(); |
| 158 | + |
| 159 | + const countBefore = events.length; |
| 160 | + fs.writeFileSync(filePath, "// v2"); |
| 161 | + await new Promise((r) => setTimeout(r, 800)); |
| 162 | + |
| 163 | + expect(events.length).toBe(countBefore); |
| 164 | + }, 15_000); |
| 165 | +}); |
0 commit comments