From fa37fd20caa5a94fa02cc334fda1615f09f97d1e Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Sun, 22 Nov 2026 06:07:40 +0000 Subject: [PATCH] Make the recursive watcher inventory test deterministic (#10462) * test(server): make recursive watcher inventory test deterministic Deliver the file creation notification explicitly so the inventory check does not depend on the native watcher becoming ready. Hold scheduled scans until the test checks the inventory and require reconciliation to report the deletion. * test(server): wait for the watcher callback without advancing audits --------- Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com> --- .../file-observer/native-recursive.test.ts | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) 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 }); } });