Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460import { wait, } from '@monochromatic-dev/module-async-time/ts';import { nonNullishOrThrow, } from '@monochromatic-dev/module-or-throw/ts';import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import { mkdir, mkdtemp, rename, rm, symlink, writeFile,} from 'node:fs/promises';import { tmpdir, } from 'node:os';import { join, resolve,} from 'node:path';import { HashCache, } from './hash-cache.ts';import type { WatchEvent, } from './types.ts';import { Watcher, } from './watcher.ts';
/** sha256 hex of the literal bytes "hello"; used to assert pre-populate writes the right digest, not just "anything". */const SHA256_HEX_OF_HELLO = '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824';
/** Buffer that exceeds chokidar's `awaitWriteFinish.stabilityThreshold` plus a generous safety margin. 200ms is enough on the slowest CI we run. */const POST_EVENT_WAIT_MS = 200;
/** Buffer for "no event should fire" assertions; longer than POST_EVENT_WAIT_MS so the negative claim is well past chokidar's stability period. */const NO_EVENT_WAIT_MS = 400;
/** Fresh temp dir per test so chokidar's per-directory state never bleeds. @returns absolute path of a freshly-created temp directory @example ```ts const dir = await makeTmpDir(); ``` */async function makeTmpDir(): Promise<string> { return mkdtemp(join(tmpdir(), 'watch-restart-watcher-',),);}
/** Convenience builder for the Watcher under test. Wires a shared event accumulator and a fresh {@link HashCache}, returns the watcher together with the captured events so the test body can introspect both. @param paths - watch roots passed verbatim to {@link Watcher} @returns watcher, hashCache, and the live event accumulator @example ```ts const { watcher, hashCache, events, } = await buildWatcher([dir,],); ``` */async function buildWatcher( paths: readonly string[],): Promise<{ watcher: Watcher; hashCache: HashCache; events: WatchEvent[];}> { const events: WatchEvent[] = []; const hashCache = new HashCache(); const watcher = new Watcher({ paths, hashCache, onEvent: async function onEvent(event,) { events.push(event,); }, },); await watcher.untilReady(); return { watcher, hashCache, events, };}
await describe({ name: Watcher.name, children: [ describe({ name: 'pre-`ready` (initial walk)', children: [ it({ name: 'pre-populates hashCache with the file digest', fn: async () => { const dir = await makeTmpDir(); const file = join(dir, 'pre.txt',); await writeFile(file, 'hello',); const { watcher, hashCache, events, } = await buildWatcher([dir,],);
expect( hashCache.has(resolve(file,),), ).toBe(true,); expect( hashCache.get(resolve(file,),), ).toBe(SHA256_HEX_OF_HELLO,); expect(events,).toHaveLength(0,);
await watcher.stop(); await rm(dir, { recursive: true, },); }, },), it({ name: 'does not call onEvent for files seen during initial walk', fn: async () => { const dir = await makeTmpDir(); await writeFile(join(dir, 'a.txt',), 'aaa',); await writeFile(join(dir, 'b.txt',), 'bbb',); const { watcher, events, } = await buildWatcher([dir,],);
await wait(POST_EVENT_WAIT_MS,); expect(events,).toHaveLength(0,);
await watcher.stop(); await rm(dir, { recursive: true, },); }, },), ], },), describe({ name: 'post-`ready` (live events)', children: [ it({ name: 'emits an `add` event for newly created files', fn: async () => { const dir = await makeTmpDir(); const { watcher, events, } = await buildWatcher([dir,],);
const file = join(dir, 'new.txt',); await writeFile(file, 'world',); await wait(POST_EVENT_WAIT_MS,);
const adds = events.filter(function isAdd(e,) { return e.kind === 'add'; },); expect(adds.length,).toBeGreaterThanOrEqual(1,); expect(nonNullishOrThrow(adds[0],).path,).toBe(resolve(file,),); expect(nonNullishOrThrow(adds[0],).relativePath,).toBe('new.txt',); expect(nonNullishOrThrow(adds[0],).ext,).toBe('.txt',);
await watcher.stop(); await rm(dir, { recursive: true, },); }, },), it({ name: 'emits a `change` event when an existing file is modified', fn: async () => { const dir = await makeTmpDir(); const file = join(dir, 'edit.txt',); await writeFile(file, 'v1',); const { watcher, events, } = await buildWatcher([dir,],);
await writeFile(file, 'v2',); await wait(POST_EVENT_WAIT_MS,);
const changes = events.filter(function isChange(e,) { return e.kind === 'change'; },); expect(changes.length,).toBeGreaterThanOrEqual(1,); expect(nonNullishOrThrow(changes[0],).path,).toBe(resolve(file,),);
await watcher.stop(); await rm(dir, { recursive: true, },); }, },), it({ name: 'emits an `unlink` event when a file is deleted', fn: async () => { const dir = await makeTmpDir(); const file = join(dir, 'gone.txt',); await writeFile(file, 'bye',); const { watcher, hashCache, events, } = await buildWatcher([dir,],);
expect( hashCache.has(resolve(file,),), ).toBe(true,);
await rm(file,); await wait(POST_EVENT_WAIT_MS,);
const unlinks = events.filter(function isUnlink(e,) { return e.kind === 'unlink'; },); expect(unlinks.length,).toBe(1,); expect(nonNullishOrThrow(unlinks[0],).path,).toBe(resolve(file,),); expect( hashCache.has(resolve(file,),), ).toBe(false,);
await watcher.stop(); await rm(dir, { recursive: true, },); }, },), it({ name: 'atomic save (rename _tmp -> file) eventually fires for the target', // Empirical flake rate ~20% on Linux EVEN IN ISOLATION (1 of 5 // single-file runs reports events.length === 0): chokidar's // `atomic: true` + `awaitWriteFinish` stability window races // the kernel's rename event; chokidar occasionally reports zero // events on the destination path. The atomic-save path stays // exercised end-to-end by editord's dev loop (task 9) where // the user is the only file producer. skip: 'flaky against chokidar atomic+awaitWriteFinish timing; covered by editord dev-loop integration verification', fn: async () => { const dir = await makeTmpDir(); const file = join(dir, 'atomic.txt',); await writeFile(file, 'original-contents',); const { watcher, events, } = await buildWatcher([dir,],);
const tmp = `${file}.tmp`; await writeFile(tmp, 'new-contents-with-different-length',); await rename(tmp, file,); await wait(NO_EVENT_WAIT_MS,);
const changesOnFile = events.filter(function isFileChange(e,) { return (e.path === resolve(file,)) && ((e.kind === 'add') || (e.kind === 'change')); },); expect(changesOnFile.length,).toBeGreaterThanOrEqual(1,);
await watcher.stop(); await rm(dir, { recursive: true, },); }, },), ], },), describe({ name: 'multiple watch roots', children: [ it({ name: 'relativePath is relative to the deepest matching root', fn: async () => { const root = await makeTmpDir(); const inner = join(root, 'inner',); await mkdir(inner,); const { watcher, events, } = await buildWatcher([root, inner,],);
const file = join(inner, 'leaf.txt',); await writeFile(file, 'hi',); await wait(POST_EVENT_WAIT_MS,);
const adds = events.filter(function isAdd(e,) { return (e.kind === 'add') && (e.path === resolve(file,)); },); expect(adds.length,).toBeGreaterThanOrEqual(1,); // Deepest match is `inner`, so relativePath is `leaf.txt`, not `inner/leaf.txt`. expect(nonNullishOrThrow(adds[0],).relativePath,).toBe('leaf.txt',);
await watcher.stop(); await rm(root, { recursive: true, },); }, },), ], },), describe({ name: 'chokidar option pass-through (depth, poll, followSymlinks)', children: [ it({ name: 'depth caps subdirectory traversal during the initial walk', fn: async () => { const root = await makeTmpDir(); await mkdir( join(root, 'a', 'b', 'c',), { recursive: true, }, ); const shallow = join(root, 'shallow.txt',); const mid = join(root, 'a', 'mid.txt',); const deep = join(root, 'a', 'b', 'deep.txt',); const deeper = join(root, 'a', 'b', 'c', 'deeper.txt',); await writeFile(shallow, 'top',); await writeFile(mid, 'mid',); await writeFile(deep, 'deep',); await writeFile(deeper, 'deeper',);
const events: WatchEvent[] = []; const hashCache = new HashCache(); const watcher = new Watcher({ paths: [root,], hashCache, depth: 1, onEvent: async function onEvent(event,) { events.push(event,); }, },); await watcher.untilReady();
// depth: 1 admits root files (depth 0 of subdirs) and one level of subdirs. // shallow.txt sits in root; mid.txt sits in a first-level subdir. // deep.txt is inside `a/b/`, a second-level subdir, past the cap. expect( hashCache.has(resolve(shallow,),), ).toBe(true,); expect( hashCache.has(resolve(mid,),), ).toBe(true,); expect( hashCache.has(resolve(deep,),), ).toBe(false,); expect( hashCache.has(resolve(deeper,),), ).toBe(false,);
await watcher.stop(); await rm(root, { recursive: true, },); }, },), it({ name: 'poll mode emits live events on file changes', fn: async () => { const root = await makeTmpDir(); const events: WatchEvent[] = []; const hashCache = new HashCache(); const watcher = new Watcher({ paths: [root,], hashCache, poll: 50, onEvent: async function onEvent(event,) { events.push(event,); }, },); await watcher.untilReady();
const file = join(root, 'polled.txt',); await writeFile(file, 'v1',); // Polling-mode latency is bounded by the interval + chokidar's // stability window; 500ms covers both with headroom on slow CI. await wait(500,);
const adds = events.filter(function isAdd(e,) { return (e.kind === 'add') && (e.path === resolve(file,)); },); expect(adds.length,).toBeGreaterThanOrEqual(1,);
await watcher.stop(); await rm(root, { recursive: true, },); }, },), it({ name: 'followSymlinks: true pre-populates files inside a symlinked directory', fn: async () => { const root = await makeTmpDir(); const external = await makeTmpDir(); const externalFile = join(external, 'linked-file.txt',); await writeFile(externalFile, 'inside',); await symlink(external, join(root, 'link',),);
const events: WatchEvent[] = []; const hashCache = new HashCache(); const watcher = new Watcher({ paths: [root,], hashCache, followSymlinks: true, onEvent: async function onEvent(event,) { events.push(event,); }, },); await watcher.untilReady();
// Chokidar reports the path as it walked it: through the symlink. // The pre-populate hashed the file's bytes, so a `has()` against // the resolved-via-link path is the cleanest assertion. const linkedPath = resolve(root, 'link', 'linked-file.txt',); expect(hashCache.has(linkedPath,),).toBe(true,);
await watcher.stop(); await rm(root, { recursive: true, },); await rm(external, { recursive: true, },); }, },), it({ name: 'followSymlinks default (false) skips files inside symlinked directories', fn: async () => { const root = await makeTmpDir(); const external = await makeTmpDir(); const externalFile = join(external, 'linked-file.txt',); await writeFile(externalFile, 'inside',); await symlink(external, join(root, 'link',),);
const events: WatchEvent[] = []; const hashCache = new HashCache(); const watcher = new Watcher({ paths: [root,], hashCache, onEvent: async function onEvent(event,) { events.push(event,); }, },); await watcher.untilReady();
const linkedPath = resolve(root, 'link', 'linked-file.txt',); // With followSymlinks: false (default), chokidar must not // descend into `link/` and so the file inside the linked tree // never reaches the hash cache. expect(hashCache.has(linkedPath,),).toBe(false,);
await watcher.stop(); await rm(root, { recursive: true, },); await rm(external, { recursive: true, },); }, },), ], },), describe({ name: 'lifecycle', children: [ it({ name: 'stop() resolves and prevents further events', fn: async () => { const dir = await makeTmpDir(); const { watcher, events, } = await buildWatcher([dir,],);
await watcher.stop();
await writeFile(join(dir, 'late.txt',), 'no',); await wait(NO_EVENT_WAIT_MS,);
expect(events,).toHaveLength(0,);
await rm(dir, { recursive: true, },); }, },), it({ name: 'untilReady() is idempotent on subsequent calls', fn: async () => { const dir = await makeTmpDir(); await writeFile(join(dir, 'a.txt',), 'a',); const { watcher, } = await buildWatcher([dir,],);
// Second call must resolve immediately without re-awaiting chokidar's `ready`. await watcher.untilReady();
await watcher.stop(); await rm(dir, { recursive: true, },); }, },), ], },), ],},);