Files
ShellBook/apps/server/test/watch.test.ts
T

60 lines
3.4 KiB
TypeScript

import { mkdtemp, mkdir, writeFile, rm, rename } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { createNotebookWatcher, type NotebookFileEvent } from "../src/watch/index";
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 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 writeFile(`${file}.tmp`, "replacement"); await rename(`${file}.tmp`, file);
await vi.waitFor(() => expect(events).toHaveLength(2));
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 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 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);
off(); await writeFile(file, "unsubscribed");
await new Promise((r) => setTimeout(r, 300)); expect(events).toHaveLength(4);
} finally { await watcher.close(); await rm(dir, { recursive: true, force: true }); }
});
it("starts before notebooks exists and cancels pending notifications on close", async () => {
const dir = await mkdtemp(join(tmpdir(), "shellbook-watch-"));
const watcher = createNotebookWatcher(dir, { debounceMs: 500 });
const listener = vi.fn(); watcher.on(listener);
try {
await watcher.ready; await mkdir(join(dir, "notebooks"));
await writeFile(join(dir, "notebooks", "new.md"), "new");
await new Promise((r) => setTimeout(r, 100)); await watcher.close();
await new Promise((r) => setTimeout(r, 600)); expect(listener).not.toHaveBeenCalled();
} finally { await watcher.close(); await rm(dir, { recursive: true, force: true }); }
});
it("publishes hosts.yaml changes, atomic replacements and deletion", async () => {
const dir = await mkdtemp(join(tmpdir(), "shellbook-hostwatch-"));
const file = join(dir, "hosts.yaml"); await writeFile(file, "[]");
const watcher = createNotebookWatcher(dir, { debounceMs: 10 });
const events: NotebookFileEvent[] = []; watcher.on(e => events.push(e));
try {
await watcher.ready; await writeFile(file + ".tmp", "# changed\n[]"); await rename(file + ".tmp", file);
await vi.waitFor(() => expect(events).toContainEqual({ type: "file.changed", path: "hosts.yaml" }));
await rm(file);
await vi.waitFor(() => expect(events).toContainEqual({ type: "file.deleted", path: "hosts.yaml" }));
} finally { await watcher.close(); await rm(dir, { recursive: true, force: true }); }
});