test(server): make the notebook watcher test robust under load
The full parallel test run on Windows can delay file events by more than the 100 ms debounce, which split one burst into two events and failed the test intermittently. Use a 300 ms debounce, longer quiet windows and explicit wait timeouts.
This commit is contained in:
@@ -4,34 +4,39 @@ import { join } from "node:path";
|
||||
import { expect, it, vi } from "vitest";
|
||||
import { createNotebookWatcher, type NotebookFileEvent } from "../src/watch/index";
|
||||
|
||||
// Generous timings: the full parallel test run on Windows can delay fs events by >100 ms.
|
||||
const debounceMs = 300;
|
||||
const settle = { timeout: 5000 };
|
||||
const quiet = () => new Promise((r) => setTimeout(r, debounceMs * 3));
|
||||
|
||||
it("watches nested Markdown, debounces writes, handles atomic saves/deletes and unsubscribes", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "shellbook-watch-"));
|
||||
await mkdir(join(dir, "notebooks", "setup"), { recursive: true });
|
||||
const file = join(dir, "notebooks", "setup", "test.md");
|
||||
await writeFile(file, "initial");
|
||||
const watcher = createNotebookWatcher(dir, { debounceMs: 100 });
|
||||
const watcher = createNotebookWatcher(dir, { debounceMs });
|
||||
const events: NotebookFileEvent[] = [];
|
||||
const off = watcher.on((event) => events.push(event));
|
||||
try {
|
||||
await watcher.ready;
|
||||
expect(events).toEqual([]);
|
||||
await writeFile(file, "first"); await writeFile(file, "second");
|
||||
await vi.waitFor(() => expect(events).toEqual([{ type: "file.changed", path: "setup/test.md" }]));
|
||||
await vi.waitFor(() => expect(events).toEqual([{ type: "file.changed", path: "setup/test.md" }]), settle);
|
||||
await writeFile(`${file}.tmp`, "replacement"); await rename(`${file}.tmp`, file);
|
||||
await vi.waitFor(() => expect(events).toHaveLength(2));
|
||||
await vi.waitFor(() => expect(events).toHaveLength(2), settle);
|
||||
expect(events[1]).toEqual({ type: "file.changed", path: "setup/test.md" });
|
||||
await rm(file);
|
||||
await vi.waitFor(() => expect(events[2]).toEqual({ type: "file.deleted", path: "setup/test.md" }));
|
||||
await vi.waitFor(() => expect(events[2]).toEqual({ type: "file.deleted", path: "setup/test.md" }), settle);
|
||||
await mkdir(join(dir, "notebooks", "new"));
|
||||
await writeFile(join(dir, "notebooks", "new", "added.md"), "new");
|
||||
await vi.waitFor(() => expect(events[3]).toEqual({ type: "file.changed", path: "new/added.md" }));
|
||||
await vi.waitFor(() => expect(events[3]).toEqual({ type: "file.changed", path: "new/added.md" }), settle);
|
||||
await writeFile(join(dir, "notebooks", "ignored.txt"), "ignore");
|
||||
await writeFile(join(dir, "outside.md"), "ignore");
|
||||
await new Promise((r) => setTimeout(r, 300)); expect(events).toHaveLength(4);
|
||||
await quiet(); expect(events).toHaveLength(4);
|
||||
off(); await writeFile(file, "unsubscribed");
|
||||
await new Promise((r) => setTimeout(r, 300)); expect(events).toHaveLength(4);
|
||||
await quiet(); expect(events).toHaveLength(4);
|
||||
} finally { await watcher.close(); await rm(dir, { recursive: true, force: true }); }
|
||||
});
|
||||
}, 30_000);
|
||||
|
||||
it("starts before notebooks exists and cancels pending notifications on close", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "shellbook-watch-"));
|
||||
|
||||
Reference in New Issue
Block a user