mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-25 01:35:31 +00:00
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>
This commit is contained in:
@@ -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<FileChange>();
|
||||
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 });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user