repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pi/agent/extensions/llmiterate/__tests__/watcher.test.ts

Raw
File outline
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, test } from "bun:test";
import { compilePathFilter, normalizeConfig } from "../core";
import { ProjectWatcher } from "../watcher";

const tempDirs: string[] = [];
const watchers: ProjectWatcher[] = [];

afterEach(() => {
  while (watchers.length) watchers.pop()!.stop();
  while (tempDirs.length) rmSync(tempDirs.pop()!, { 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 nothing", () => {
    const root = tempDir();
    mkdirSync(join(root, "src"));
    writeFileSync(join(root, "src", "a.ts"), "LLM 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(0);
    expect(seen).toEqual([]);
  });

  test("default config starts no 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"), "LLM ignored");
    await sleep(40);
    expect(watcher.errorCount).toBe(0);
    expect(seen).toEqual([]);
  });

  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("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"), "LLM hello");
    await eventually(() => seen.some((p) => p.endsWith("moved.ts")));
  });
});

function sleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function eventually(predicate: () => boolean): Promise<void> {
  const deadline = Date.now() + 1500;
  while (Date.now() < deadline) {
    if (predicate()) return;
    await sleep(25);
  }
  expect(predicate()).toBe(true);
}