summaryrefslogtreecommitdiffhomepage
path: root/packages/core/tests/config/watcher.test.ts
blob: 9388c8a0e2aae7535d0923c46dacbc2bb37e1c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { watchDirConfig } from "../../src/config/watcher.js";

const TMP = join("/tmp/opencode", "dispatch-watchdir-test");

beforeEach(() => {
	mkdirSync(TMP, { recursive: true });
});

afterEach(() => {
	rmSync(TMP, { recursive: true, force: true });
});

function wait(ms: number): Promise<void> {
	return new Promise((resolve) => setTimeout(resolve, ms));
}

describe("watchDirConfig", () => {
	it("fires onChange (debounced) when <dir>/dispatch.toml changes", async () => {
		let calls = 0;
		const handle = watchDirConfig(TMP, () => {
			calls++;
		});
		// Let chokidar finish its initial scan before mutating the file.
		await wait(200);

		writeFileSync(join(TMP, "dispatch.toml"), `[permissions]\nread = "allow"\n`, "utf-8");
		// 300ms debounce + chokidar latency.
		await wait(700);

		handle.close();
		expect(calls).toBeGreaterThanOrEqual(1);
	});

	it("does not fire after close()", async () => {
		let calls = 0;
		const handle = watchDirConfig(TMP, () => {
			calls++;
		});
		await wait(200);
		handle.close();

		writeFileSync(join(TMP, "dispatch.toml"), `[permissions]\nedit = "deny"\n`, "utf-8");
		await wait(700);

		expect(calls).toBe(0);
	});
});