@incremental/watcher 0.0.0-stage → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,269 @@
1
+ // Integration tests: a scratch git repository with two tasks, run through the real
2
+ // engine and incremental-git (spec/adapters/watcher.md).
3
+ import assert from "node:assert/strict";
4
+ import { spawnSync } from "node:child_process";
5
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
6
+ import os from "node:os";
7
+ import path from "node:path";
8
+ import { test } from "node:test";
9
+ import { connect, defaultEngine } from "@incremental/client";
10
+ import { ConfigError, inputsOf, Session, type TaskDef } from "../src/index.js";
11
+ import { Watcher } from "../src/watch.js";
12
+
13
+ const GIT_ENV = {
14
+ ...process.env,
15
+ GIT_AUTHOR_NAME: "t",
16
+ GIT_AUTHOR_EMAIL: "t@example.com",
17
+ GIT_COMMITTER_NAME: "t",
18
+ GIT_COMMITTER_EMAIL: "t@example.com",
19
+ };
20
+
21
+ function git(cwd: string, ...args: string[]): string {
22
+ const r = spawnSync("git", args, { cwd, encoding: "utf8", env: GIT_ENV });
23
+ if (r.status !== 0) throw new Error(`git ${args.join(" ")} failed: ${r.stderr}`);
24
+ return r.stdout.trim();
25
+ }
26
+
27
+ function write(root: string, rel: string, content: string): void {
28
+ const abs = path.join(root, rel);
29
+ mkdirSync(path.dirname(abs), { recursive: true });
30
+ writeFileSync(abs, content);
31
+ }
32
+
33
+ function commit(root: string, message: string): void {
34
+ git(root, "add", "-A");
35
+ git(root, "commit", "-q", "-m", message);
36
+ }
37
+
38
+ const TASKS: TaskDef[] = ["a", "b"].map((p) => ({
39
+ id: `pkg/${p}`,
40
+ command: [process.execPath, "-e", "process.exit(0)"],
41
+ inputs: [`pkg/${p}`],
42
+ }));
43
+
44
+ // A repository, an initialised store, and the stored baseline set to HEAD.
45
+ async function fixture(): Promise<{ root: string; store: string; base: string }> {
46
+ const base = mkdtempSync(path.join(os.tmpdir(), "incremental-watcher-"));
47
+ const root = path.join(base, "repo");
48
+ mkdirSync(root);
49
+ git(root, "init", "-q", "-b", "main");
50
+ for (const p of ["a", "b"]) write(root, `pkg/${p}/src/index.ts`, `export const ${p} = 1;\n`);
51
+ write(root, "README.md", "fixture\n");
52
+ commit(root, "init");
53
+ const store = path.join(base, "store");
54
+ const init = spawnSync(defaultEngine(), ["init", "--store", store], { encoding: "utf8" });
55
+ assert.equal(init.status, 0, init.stderr);
56
+ const client = await connect({ store, root, command: [defaultEngine(), "serve"] });
57
+ try {
58
+ await client.advance(git(root, "rev-parse", "HEAD"), null);
59
+ } finally {
60
+ await client.close();
61
+ }
62
+ return { root, store, base };
63
+ }
64
+
65
+ function sessionFor(root: string, store: string): Session {
66
+ return new Session({ root, store, tasks: TASKS, timeoutMs: 60_000 });
67
+ }
68
+
69
+ async function waitForEvent(session: Session): Promise<void> {
70
+ const until = Date.now() + 5_000;
71
+ while (session.pending() === 0) {
72
+ if (Date.now() > until) throw new Error("no file-system event arrived");
73
+ await new Promise((r) => setTimeout(r, 50));
74
+ }
75
+ }
76
+
77
+ async function storedBaseline(store: string, root: string): Promise<string | null> {
78
+ const client = await connect({ store, root, command: [defaultEngine(), "serve"] });
79
+ try {
80
+ return (await client.baseline()).baseline;
81
+ } finally {
82
+ await client.close();
83
+ }
84
+ }
85
+
86
+ test("an edit event adds a ChangeSet entry and invalidates only the task that reads it", async () => {
87
+ const { root, store, base } = await fixture();
88
+ const session = sessionFor(root, store);
89
+ try {
90
+ await session.open();
91
+ const first = await session.cycle();
92
+ assert.equal(first.mode, "full");
93
+ assert.equal(first.reason, "no_history");
94
+ assert.deepEqual(first.selected, ["pkg/a", "pkg/b"]);
95
+
96
+ const quiet = await session.cycle();
97
+ assert.equal(quiet.mode, "incremental");
98
+ assert.deepEqual(quiet.selected, []);
99
+
100
+ write(root, "pkg/a/src/index.ts", "export const a = 2;\n");
101
+ await waitForEvent(session);
102
+ const edit = await session.cycle();
103
+ assert.equal(edit.mode, "incremental", `reason: ${edit.reason}`);
104
+ assert.deepEqual(edit.changes, [{ path: "pkg/a/src/index.ts", status: "modified" }]);
105
+ assert.equal(edit.plan?.fallback, null);
106
+ assert.deepEqual(edit.selected, ["pkg/a"]);
107
+ assert.deepEqual(
108
+ edit.results.map((r) => r.outcome),
109
+ ["pass"],
110
+ );
111
+ assert.equal(edit.advanced, false, "an uncommitted edit never advances");
112
+ } finally {
113
+ await session.close();
114
+ rmSync(base, { recursive: true, force: true });
115
+ }
116
+ });
117
+
118
+ test("a committed edit advances the baseline once the run is complete and the tree is clean", async () => {
119
+ const { root, store, base } = await fixture();
120
+ const session = sessionFor(root, store);
121
+ try {
122
+ await session.open();
123
+ await session.cycle();
124
+ write(root, "pkg/b/src/index.ts", "export const b = 2;\n");
125
+ await waitForEvent(session);
126
+ const edit = await session.cycle();
127
+ assert.deepEqual(edit.selected, ["pkg/b"]);
128
+ assert.equal(edit.advanced, false);
129
+
130
+ commit(root, "edit b");
131
+ const committed = await session.cycle();
132
+ assert.equal(committed.mode, "incremental", `reason: ${committed.reason}`);
133
+ assert.deepEqual(committed.selected, ["pkg/b"]);
134
+ assert.equal(committed.advanced, true);
135
+ assert.equal(await storedBaseline(store, root), git(root, "rev-parse", "HEAD"));
136
+
137
+ const quiet = await session.cycle();
138
+ assert.equal(quiet.mode, "incremental");
139
+ assert.deepEqual(quiet.selected, []);
140
+ } finally {
141
+ await session.close();
142
+ rmSync(base, { recursive: true, force: true });
143
+ }
144
+ });
145
+
146
+ test("an event loss produces a full run and no partial plan, then the watch re-arms", async () => {
147
+ const { root, store, base } = await fixture();
148
+ const session = sessionFor(root, store);
149
+ try {
150
+ await session.open();
151
+ await session.cycle();
152
+ session.reportLoss("overflow");
153
+ const lost = await session.cycle();
154
+ assert.equal(lost.mode, "full");
155
+ assert.match(lost.reason ?? "", /^watcher_lost: overflow/);
156
+ assert.equal(lost.plan, null, "no plan is computed from a lost window");
157
+ assert.deepEqual(lost.selected, ["pkg/a", "pkg/b"]);
158
+ assert.equal(lost.advanced, false);
159
+
160
+ const after = await session.cycle();
161
+ assert.equal(after.mode, "incremental", `reason: ${after.reason}`);
162
+ assert.equal(after.reason, null);
163
+ } finally {
164
+ await session.close();
165
+ rmSync(base, { recursive: true, force: true });
166
+ }
167
+ });
168
+
169
+ test("a restart with no in-memory history runs everything", async () => {
170
+ const { root, store, base } = await fixture();
171
+ try {
172
+ const before = await storedBaseline(store, root);
173
+ const first = sessionFor(root, store);
174
+ await first.open();
175
+ await first.cycle();
176
+ await first.close();
177
+
178
+ const restarted = sessionFor(root, store);
179
+ try {
180
+ await restarted.open();
181
+ const r = await restarted.cycle();
182
+ assert.equal(r.mode, "full");
183
+ assert.equal(r.reason, "no_history");
184
+ assert.deepEqual(r.selected, ["pkg/a", "pkg/b"]);
185
+ } finally {
186
+ await restarted.close();
187
+ }
188
+ assert.equal(
189
+ await storedBaseline(store, root),
190
+ before,
191
+ "a watcher run without a complete window does not move the baseline",
192
+ );
193
+ } finally {
194
+ rmSync(base, { recursive: true, force: true });
195
+ }
196
+ });
197
+
198
+ test("no advance when the watcher was not running since the baseline", async () => {
199
+ const { root, store, base } = await fixture();
200
+ const before = await storedBaseline(store, root);
201
+ try {
202
+ // A commit made while no watcher ran: the tree no longer equals the baseline.
203
+ write(root, "pkg/a/src/index.ts", "export const a = 3;\n");
204
+ commit(root, "edit while no watcher ran");
205
+
206
+ const session = sessionFor(root, store);
207
+ try {
208
+ await session.open();
209
+ const r1 = await session.cycle();
210
+ assert.equal(r1.advanced, false);
211
+ assert.deepEqual(r1.selected, ["pkg/a", "pkg/b"]);
212
+ const r2 = await session.cycle();
213
+ assert.equal(r2.mode, "full");
214
+ assert.equal(r2.reason, "not_watching_since_baseline");
215
+ assert.equal(r2.advanced, false);
216
+ } finally {
217
+ await session.close();
218
+ }
219
+ assert.equal(await storedBaseline(store, root), before, "the store baseline did not move");
220
+ } finally {
221
+ rmSync(base, { recursive: true, force: true });
222
+ }
223
+ });
224
+
225
+ test("a task input under an ignored directory is refused, not silently skipped", () => {
226
+ const { root } = { root: mkdtempSync(path.join(os.tmpdir(), "incremental-watcher-cfg-")) };
227
+ try {
228
+ write(root, "node_modules/dep/index.js", "x\n");
229
+ assert.throws(
230
+ () => inputsOf(root, { id: "t", command: ["true"], inputs: ["node_modules/dep"] }),
231
+ ConfigError,
232
+ );
233
+ } finally {
234
+ rmSync(root, { recursive: true, force: true });
235
+ }
236
+ });
237
+
238
+ // Silent drops (spec/adapters/watcher.md): the OS may deliver no event for an
239
+ // edit. The state must still report it, because it is read from a stat walk
240
+ // against the snapshot, never from the event stream.
241
+ test("an edit with no event delivered is still reported, and a rebase clears it", async () => {
242
+ const dir = mkdtempSync(path.join(os.tmpdir(), "incremental-watch-silent-"));
243
+ try {
244
+ writeFileSync(path.join(dir, "a.ts"), "export const a = 1;\n");
245
+ writeFileSync(path.join(dir, "b.ts"), "export const b = 1;\n");
246
+ const w = new Watcher(dir);
247
+ w.rebase();
248
+ // Let the snapshot age past the racy window before a file can be compared by stat.
249
+ await new Promise((r) => setTimeout(r, 2_100));
250
+ const before = w.state();
251
+ assert.deepEqual(before.changes, [], "nothing changed, nothing reported");
252
+
253
+ writeFileSync(path.join(dir, "a.ts"), "export const a = 2;\n");
254
+ rmSync(path.join(dir, "b.ts"));
255
+ // No start(): no watch is open, so no event exists for either change.
256
+ const after = w.state();
257
+ assert.deepEqual(
258
+ after.changes.map((c) => `${c.status} ${c.path}`),
259
+ ["modified a.ts", "deleted b.ts"],
260
+ );
261
+ assert.equal(after.lost, null);
262
+
263
+ w.rebase();
264
+ await new Promise((r) => setTimeout(r, 2_100));
265
+ assert.deepEqual(w.state().changes, []);
266
+ } finally {
267
+ rmSync(dir, { recursive: true, force: true });
268
+ }
269
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": ".",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*.ts", "tests/**/*.ts"]
8
+ }