diff --git a/packages/server/src/server/file-observer/native-recursive.test.ts b/packages/server/src/server/file-observer/native-recursive.test.ts index ce97e64fd7..01f80b196f 100644 --- a/packages/server/src/server/file-observer/native-recursive.test.ts +++ b/packages/server/src/server/file-observer/native-recursive.test.ts @@ -1,7 +1,8 @@ +import { EventEmitter } from "node:events"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { tmpdir } from "node:os"; -import { expect, test } from "vitest"; +import { expect, test, vi } from "vitest"; import type { FileChange } from "./index.js"; import { createNativeRecursiveBackend } from "./internal/native-recursive.js"; import { createObserverPaths } from "./internal/paths.js"; @@ -10,6 +11,8 @@ test("the recursive watcher remembers newly announced files before reconciliatio const root = await mkdtemp(join(tmpdir(), "paseo-observer-native-")); const paths = createObserverPaths(process.platform); const changes: FileChange[] = []; + const firstChange = Promise.withResolvers(); + const notifications = new EventEmitter(); let active = true; const backend = createNativeRecursiveBackend( { @@ -34,36 +37,43 @@ test("the recursive watcher remembers newly announced files before reconciliatio isIgnored: () => false, isPathInside: paths.isInside, queueEvent: (type, path) => { - changes.push({ type, path }); + const change = { type, path }; + changes.push(change); + firstChange.resolve(change); }, fail: (error) => { throw error; }, }, paths, + (_root, listener) => { + notifications.on("change", listener); + return { + close: () => notifications.removeAllListeners(), + on: (event, onError) => notifications.on(event, onError), + }; + }, ); try { await backend.start(); - const path = join(root, "new.txt"); + const filename = "new.txt"; + const path = join(root, filename); await writeFile(path, "created"); - await expect - .poll(() => changes.some((event) => event.path === path && event.type === "create"), { - timeout: 10_000, - }) - .toBe(true); + + // Hold scheduled audits until classification has announced and tracked the file. + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); + notifications.emit("change", "rename", filename); + expect(await firstChange.promise).toEqual({ type: "create", path }); // Consumers have seen this file. A later audit needs it in its comparison // inventory even if the native watcher coalesces the subsequent delete. expect(backend.getDiagnostics().nativeTrackedFileCount).toBe(1); await rm(path); await backend.updateIgnore(); - await expect - .poll(() => changes.some((event) => event.path === path && event.type === "delete"), { - timeout: 10_000, - }) - .toBe(true); + expect(changes).toContainEqual({ type: "delete", path }); } finally { active = false; await backend.close(); + vi.useRealTimers(); await rm(root, { recursive: true, force: true }); } });