import { mkdirSync, mkdtempSync, renameSync, rmSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, test } from "vitest"; import { compilePathFilter, normalizeConfig } from "../core"; import { ProjectWatcher } from "../watcher"; const tempDirs: string[] = []; const watchers: ProjectWatcher[] = []; afterEach(() => { while (watchers.length) watchers.pop()?.stop(); for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); function tempDir(): string { const dir = mkdtempSync(join(tmpdir(), "llmiterate-watcher-")); tempDirs.push(dir); return dir; } describe("ProjectWatcher integration", () => { test("default config scans normal files and excludes metadata dirs", () => { const root = tempDir(); mkdirSync(join(root, "src")); mkdirSync(join(root, ".jj")); mkdirSync(join(root, ".local", "share", "Trash", "files"), { recursive: true, }); writeFileSync(join(root, "src", "a.ts"), "// llmiterate: seen"); writeFileSync(join(root, ".jj", "ignored.ts"), "// llmiterate: ignored"); writeFileSync( join(root, ".local", "share", "Trash", "files", "ignored.ts"), "// llmiterate: ignored", ); const config = normalizeConfig({ debounceMs: 10 }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); expect( watcher.scanAll((file) => { seen.push(file); return 1; }), ).toBe(1); expect(seen.map((p) => p.replace(root, ""))).toEqual(["/src/a.ts"]); }); test("default config starts filesystem watchers", async () => { const root = tempDir(); mkdirSync(join(root, "src")); const config = normalizeConfig({ debounceMs: 10 }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); watcher.start(); writeFileSync(join(root, "src", "a.ts"), "// llmiterate: seen"); await eventually(() => seen.some((p) => p.endsWith("a.ts"))); expect(watcher.errorCount).toBe(0); }); test("scanAll respects include whitelist and aggregates callback results", () => { const root = tempDir(); mkdirSync(join(root, "src")); mkdirSync(join(root, "research")); writeFileSync(join(root, "src", "a.ts"), "LLM do it"); writeFileSync(join(root, "src", "a.png"), "no"); writeFileSync(join(root, "research", "b.ts"), "LLM ignore"); const config = normalizeConfig({ include: ["src/**/*.ts"], exclude: [], debounceMs: 10, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); const total = watcher.scanAll((file) => { seen.push(file); return 2; }); expect(total).toBe(2); expect(seen.map((p) => p.replace(root, ""))).toEqual(["/src/a.ts"]); }); test("start watches dirs without queuing old files", async () => { const root = tempDir(); mkdirSync(join(root, "src")); writeFileSync(join(root, "src", "old.ts"), "// llmiterate: old"); const config = normalizeConfig({ include: ["src/**/*.ts"], exclude: [], debounceMs: 20, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); watcher.start(); await sleep(350); expect(watcher.watcherCount).toBeGreaterThanOrEqual(2); expect(seen).toEqual([]); }); test("watcher emits debounced file save events", async () => { const root = tempDir(); mkdirSync(join(root, "src")); const config = normalizeConfig({ include: ["**/*.ts"], exclude: [], debounceMs: 20, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); watcher.start(); await sleep(20); writeFileSync(join(root, "src", "new.ts"), "LLM hello"); await eventually(() => seen.some((p) => p.endsWith("new.ts"))); }); test("watcher ignores new files outside include whitelist", async () => { const root = tempDir(); mkdirSync(join(root, "src")); mkdirSync(join(root, "research")); const config = normalizeConfig({ include: ["src/**/*.ts"], exclude: [], debounceMs: 20, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); watcher.start(); await sleep(20); writeFileSync(join(root, "research", "ignored.ts"), "LLM nope"); await sleep(80); expect(seen).toEqual([]); writeFileSync(join(root, "src", "seen.ts"), "LLM hello"); await eventually(() => seen.some((p) => p.endsWith("seen.ts"))); }); test("watcher notices files moved into new whitelisted directories", async () => { const root = tempDir(); const config = normalizeConfig({ include: ["nested/**/*.ts"], exclude: [], debounceMs: 20, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); watcher.start(); mkdirSync(join(root, "nested")); await sleep(60); writeFileSync(join(root, "nested", "moved.ts"), "// llmiterate: hello"); await eventually(() => seen.some((p) => p.endsWith("moved.ts"))); }); test("fs.watch atomic-save rename queues final path", async () => { const root = tempDir(); mkdirSync(join(root, "src")); const config = normalizeConfig({ include: ["src/**/*.ts"], exclude: [], debounceMs: 20, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); watcher.start(); await sleep(40); writeFileSync(join(root, "src", "a.ts.tmp"), "// temp write"); renameSync(join(root, "src", "a.ts.tmp"), join(root, "src", "a.ts")); await eventually(() => seen.some((p) => p.endsWith("src/a.ts"))); expect(seen.every((p) => !p.endsWith(".tmp"))).toBe(true); }); test("fs.watch debounce collapses save bursts", async () => { const root = tempDir(); mkdirSync(join(root, "src")); const config = normalizeConfig({ include: ["src/**/*.ts"], exclude: [], debounceMs: 50, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); watcher.start(); await sleep(40); const file = join(root, "src", "burst.ts"); writeFileSync(file, "// llmiterate: one"); await sleep(10); writeFileSync(file, "// llmiterate: two"); await sleep(10); writeFileSync(file, "// llmiterate: three"); await eventually(() => seen.some((p) => p.endsWith("burst.ts"))); await sleep(120); expect(seen.filter((p) => p.endsWith("burst.ts"))).toHaveLength(1); }); test("fs.watch removes deleted directory watchers", async () => { const root = tempDir(); mkdirSync(join(root, "src", "gone"), { recursive: true }); const config = normalizeConfig({ include: ["src/**/*.ts"], exclude: [], debounceMs: 20, }); const watcher = new ProjectWatcher( root, config, compilePathFilter(config), () => {}, () => {}, ); watchers.push(watcher); watcher.start(); await eventually(() => watcher.watcherCount >= 3); const before = watcher.watcherCount; rmSync(join(root, "src", "gone"), { recursive: true, force: true }); await eventually(() => watcher.watcherCount < before); }); test("respects .gitignore and .ignore during scan and watch", async () => { const root = tempDir(); mkdirSync(join(root, "src", "ignored"), { recursive: true }); mkdirSync(join(root, "logs")); writeFileSync(join(root, ".gitignore"), "logs/\n"); writeFileSync(join(root, "src", ".ignore"), "ignored/\n*.tmp\n"); writeFileSync(join(root, "logs", "a.ts"), "// llmiterate: ignored"); writeFileSync( join(root, "src", "ignored", "b.ts"), "// llmiterate: ignored", ); writeFileSync(join(root, "src", "c.tmp"), "// llmiterate: ignored"); writeFileSync(join(root, "src", "ok.ts"), "// llmiterate: seen"); const config = normalizeConfig({ include: ["**/*"], exclude: [], debounceMs: 20, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); expect( watcher.scanAll((file) => { seen.push(file); return 1; }), ).toBe(1); expect(seen.map((p) => p.replace(root, ""))).toEqual(["/src/ok.ts"]); seen.length = 0; watcher.start(); await sleep(40); writeFileSync(join(root, "logs", "new.ts"), "// llmiterate: ignored"); writeFileSync( join(root, "src", "ignored", "new.ts"), "// llmiterate: ignored", ); writeFileSync(join(root, "src", "ok2.ts"), "// llmiterate: seen"); await eventually(() => seen.some((p) => p.endsWith("ok2.ts"))); expect( seen.every((p) => !p.includes("ignored") && !p.includes("logs")), ).toBe(true); }); test("fs.watch new ignore rule unwatches newly ignored subtree", async () => { const root = tempDir(); mkdirSync(join(root, "src", "noisy"), { recursive: true }); const config = normalizeConfig({ include: ["**/*"], exclude: [], debounceMs: 20, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); watcher.start(); await eventually(() => watcher.watcherCount >= 3); const before = watcher.watcherCount; writeFileSync(join(root, ".gitignore"), "src/noisy/\n"); await eventually(() => watcher.watcherCount < before); writeFileSync( join(root, "src", "noisy", "ignored.ts"), "// llmiterate: ignored", ); await sleep(100); expect(seen).toEqual([]); }); test("fs.watch caps watcher count", async () => { const root = tempDir(); for (let i = 0; i < 8; i++) mkdirSync(join(root, "dir", String(i)), { recursive: true }); const config = normalizeConfig({ include: ["**/*"], exclude: [], debounceMs: 20, maxWatchDirs: 4, }); const watcher = new ProjectWatcher( root, config, compilePathFilter(config), () => {}, () => {}, ); watchers.push(watcher); watcher.start(); await sleep(100); expect(watcher.watcherCount).toBeLessThanOrEqual(4); expect(watcher.errorCount).toBe(1); }); test("fs.watch does not watch excluded subtrees", async () => { const root = tempDir(); mkdirSync(join(root, "src")); mkdirSync(join(root, "node_modules", "pkg"), { recursive: true }); const config = normalizeConfig({ include: ["**/*.ts"], exclude: ["node_modules/**"], debounceMs: 20, }); const seen: string[] = []; const watcher = new ProjectWatcher( root, config, compilePathFilter(config), (file) => seen.push(file), () => {}, ); watchers.push(watcher); watcher.start(); await sleep(40); writeFileSync( join(root, "node_modules", "pkg", "ignored.ts"), "// llmiterate: ignored", ); await sleep(100); expect(seen).toEqual([]); expect(watcher.watcherCount).toBeLessThan(4); }); }); function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); } async function eventually(predicate: () => boolean): Promise { const deadline = Date.now() + 1500; while (Date.now() < deadline) { if (predicate()) return; await sleep(25); } expect(predicate()).toBe(true); }