@indigoai-us/hq-cloud 5.25.0 → 5.26.0

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.
Files changed (44) hide show
  1. package/dist/bin/sync-runner.d.ts +100 -1
  2. package/dist/bin/sync-runner.d.ts.map +1 -1
  3. package/dist/bin/sync-runner.js +214 -16
  4. package/dist/bin/sync-runner.js.map +1 -1
  5. package/dist/bin/sync-runner.test.js +372 -1
  6. package/dist/bin/sync-runner.test.js.map +1 -1
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +3 -0
  10. package/dist/index.js.map +1 -1
  11. package/dist/sync/index.d.ts +11 -0
  12. package/dist/sync/index.d.ts.map +1 -0
  13. package/dist/sync/index.js +9 -0
  14. package/dist/sync/index.js.map +1 -0
  15. package/dist/sync/push-event.d.ts +110 -0
  16. package/dist/sync/push-event.d.ts.map +1 -0
  17. package/dist/sync/push-event.js +153 -0
  18. package/dist/sync/push-event.js.map +1 -0
  19. package/dist/sync/push-event.test.d.ts +15 -0
  20. package/dist/sync/push-event.test.d.ts.map +1 -0
  21. package/dist/sync/push-event.test.js +188 -0
  22. package/dist/sync/push-event.test.js.map +1 -0
  23. package/dist/sync/push-transport.d.ts +67 -0
  24. package/dist/sync/push-transport.d.ts.map +1 -0
  25. package/dist/sync/push-transport.js +66 -0
  26. package/dist/sync/push-transport.js.map +1 -0
  27. package/dist/watcher.d.ts +160 -0
  28. package/dist/watcher.d.ts.map +1 -1
  29. package/dist/watcher.js +298 -0
  30. package/dist/watcher.js.map +1 -1
  31. package/dist/watcher.test.d.ts +2 -0
  32. package/dist/watcher.test.d.ts.map +1 -0
  33. package/dist/watcher.test.js +334 -0
  34. package/dist/watcher.test.js.map +1 -0
  35. package/package.json +3 -2
  36. package/src/bin/sync-runner.test.ts +487 -1
  37. package/src/bin/sync-runner.ts +305 -9
  38. package/src/index.ts +17 -0
  39. package/src/sync/index.ts +19 -0
  40. package/src/sync/push-event.test.ts +224 -0
  41. package/src/sync/push-event.ts +208 -0
  42. package/src/sync/push-transport.ts +84 -0
  43. package/src/watcher.test.ts +388 -0
  44. package/src/watcher.ts +386 -0
@@ -0,0 +1,208 @@
1
+ /**
2
+ * PushEvent — the wire-shared payload exchanged between every link in the
3
+ * event-driven-hq-cloud-sync pipeline.
4
+ *
5
+ * Producers (the watcher) emit one PushEvent per local content change.
6
+ * Consumers (the push endpoint / receiver / coalescer) decode the payload,
7
+ * validate it, and act on it. Because the same shape crosses a network
8
+ * boundary, it has its own dedicated module.
9
+ *
10
+ * Conventions
11
+ * ───────────
12
+ * - `contentHash` is `sha256:<64-lowercase-hex>`. The `<algorithm>:<hex>`
13
+ * prefix lets a future hash migration ship without breaking the wire
14
+ * format — consumers can branch on the prefix and fall back to refusing
15
+ * unknown algorithms.
16
+ * - `mtime` and `eventTimestamp` are strict ISO-8601 datetime strings.
17
+ * `mtime` is the filesystem modification time of the source file at the
18
+ * moment of capture; `eventTimestamp` is when the watcher emitted the
19
+ * event. They diverge whenever the watcher coalesces or retries.
20
+ * - `sequenceNumber` is a non-negative safe integer. Monotonicity per
21
+ * `originDeviceId` is a producer-side invariant — a single PushEvent
22
+ * can't be self-monotonic, so the schema only validates the bounds.
23
+ * - Unknown extra fields are dropped silently by `decodePushEvent` to
24
+ * permit forwards-compatible additions on the producer side without
25
+ * forcing every consumer to upgrade in lockstep.
26
+ *
27
+ * Ported from indigoai-us/hq-pro PR #112 (src/sync/push-event.ts) into
28
+ * @indigoai-us/hq-cloud (Path B) per project event-driven-sync-menubar US-007.
29
+ */
30
+
31
+ import { z } from "zod";
32
+
33
+ // ─── Constants ─────────────────────────────────────────────────────────────
34
+
35
+ /**
36
+ * `sha256:` + 64 lowercase hex chars. The algorithm prefix is mandatory so
37
+ * future hash migrations stay non-breaking; consumers can switch on the
38
+ * prefix and reject unknown algorithms explicitly.
39
+ */
40
+ export const CONTENT_HASH_PATTERN = /^sha256:[0-9a-f]{64}$/;
41
+
42
+ /**
43
+ * Strict ISO-8601 datetime regex matching what `z.iso.datetime()` produces.
44
+ *
45
+ * Format: `YYYY-MM-DDTHH:MM:SS(.fff)?(Z|±HH:MM)`
46
+ */
47
+ export const ISO8601_DATETIME_PATTERN =
48
+ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;
49
+
50
+ // ─── Zod schema ────────────────────────────────────────────────────────────
51
+
52
+ /**
53
+ * Runtime schema for PushEvent. Unknown extra keys are dropped via the zod
54
+ * default `.strip()` behavior — that is load-bearing for forwards
55
+ * compatibility (see module JSDoc).
56
+ */
57
+ export const PushEventSchema = z
58
+ .object({
59
+ relativePath: z
60
+ .string()
61
+ .min(1, "relativePath must be a non-empty string"),
62
+ contentHash: z
63
+ .string()
64
+ .regex(
65
+ CONTENT_HASH_PATTERN,
66
+ "contentHash must match `sha256:<64 lowercase hex>`",
67
+ ),
68
+ mtime: z
69
+ .string()
70
+ .regex(ISO8601_DATETIME_PATTERN, "mtime must be an ISO-8601 datetime"),
71
+ originDeviceId: z
72
+ .string()
73
+ .min(1, "originDeviceId must be a non-empty string"),
74
+ originTenantId: z
75
+ .string()
76
+ .min(1, "originTenantId must be a non-empty string"),
77
+ sequenceNumber: z
78
+ .number()
79
+ .int("sequenceNumber must be an integer")
80
+ .min(0, "sequenceNumber must be non-negative")
81
+ .max(
82
+ Number.MAX_SAFE_INTEGER,
83
+ "sequenceNumber must be <= Number.MAX_SAFE_INTEGER",
84
+ ),
85
+ eventTimestamp: z
86
+ .string()
87
+ .regex(
88
+ ISO8601_DATETIME_PATTERN,
89
+ "eventTimestamp must be an ISO-8601 datetime",
90
+ ),
91
+ })
92
+ // `.strip()` is the zod 4 default; called explicitly here so the intent is
93
+ // obvious to future readers. Unknown keys MUST NOT throw — see module JSDoc.
94
+ .strip();
95
+
96
+ /**
97
+ * The canonical PushEvent type. All fields are required; producers and
98
+ * consumers share this exact shape.
99
+ */
100
+ export type PushEvent = z.infer<typeof PushEventSchema>;
101
+
102
+ // ─── Errors ────────────────────────────────────────────────────────────────
103
+
104
+ /**
105
+ * Public alias for a single decode issue. Aliased here (rather than re-exporting
106
+ * the `$`-prefixed zod internal symbol directly) so consumers can annotate
107
+ * against `PushEventDecodeIssue` without importing zod internals. The alias is
108
+ * structurally identical to `z.core.$ZodIssue`, so this is a non-breaking
109
+ * narrowing of the public surface.
110
+ */
111
+ export type PushEventDecodeIssue = z.core.$ZodIssue;
112
+
113
+ /**
114
+ * Thrown by `decodePushEvent` (and `encodePushEvent` on invalid input) when
115
+ * the payload fails validation. `.issues` carries the underlying zod issues
116
+ * so callers can render structured diagnostics — see the test suite for an
117
+ * example of asserting on the issue path.
118
+ */
119
+ export class PushEventDecodeError extends Error {
120
+ readonly issues: readonly PushEventDecodeIssue[];
121
+ readonly stage: "json-parse" | "schema-validation";
122
+
123
+ constructor(
124
+ message: string,
125
+ args: {
126
+ issues: readonly PushEventDecodeIssue[];
127
+ stage: "json-parse" | "schema-validation";
128
+ cause?: unknown;
129
+ },
130
+ ) {
131
+ super(message, args.cause === undefined ? undefined : { cause: args.cause });
132
+ this.name = "PushEventDecodeError";
133
+ this.issues = args.issues;
134
+ this.stage = args.stage;
135
+ }
136
+ }
137
+
138
+ // ─── Encode / Decode ───────────────────────────────────────────────────────
139
+
140
+ /**
141
+ * Validate `event` against `PushEventSchema` and return the canonical JSON
142
+ * serialization. Validating on encode catches producer-side mistakes early
143
+ * (and ensures the output always round-trips through `decodePushEvent`).
144
+ *
145
+ * Extra keys on `event` are dropped — the returned JSON string contains
146
+ * only the declared PushEvent fields.
147
+ */
148
+ export function encodePushEvent(event: PushEvent): string {
149
+ const parsed = PushEventSchema.safeParse(event);
150
+ if (!parsed.success) {
151
+ throw new PushEventDecodeError(
152
+ "encodePushEvent: input failed PushEvent schema validation",
153
+ { issues: parsed.error.issues, stage: "schema-validation" },
154
+ );
155
+ }
156
+ return JSON.stringify(parsed.data);
157
+ }
158
+
159
+ /**
160
+ * Parse and validate an incoming PushEvent. Accepts either a raw JSON string
161
+ * (the on-the-wire form) or an already-parsed object (handy for in-process
162
+ * wiring and tests).
163
+ *
164
+ * - Unknown extra fields are dropped silently — see module JSDoc.
165
+ * - Missing required fields throw `PushEventDecodeError` whose `.issues`
166
+ * exposes the underlying zod issues.
167
+ * - Malformed JSON throws `PushEventDecodeError` with `stage:"json-parse"`
168
+ * and a synthetic issue at the root path.
169
+ * - A JSON string that parses to a non-object value (e.g. `'42'`, `'"text"'`,
170
+ * `'null'`) surfaces as `stage: 'schema-validation'` — the JSON itself was
171
+ * syntactically valid, so it clears the parse stage before failing the
172
+ * object-shape check.
173
+ */
174
+ export function decodePushEvent(input: unknown): PushEvent {
175
+ let candidate: unknown = input;
176
+
177
+ if (typeof input === "string") {
178
+ try {
179
+ candidate = JSON.parse(input);
180
+ } catch (err) {
181
+ throw new PushEventDecodeError(
182
+ "decodePushEvent: input string is not valid JSON",
183
+ {
184
+ issues: [
185
+ {
186
+ code: "custom",
187
+ message:
188
+ err instanceof Error ? err.message : "invalid JSON input",
189
+ path: [],
190
+ input,
191
+ } as PushEventDecodeIssue,
192
+ ],
193
+ stage: "json-parse",
194
+ cause: err,
195
+ },
196
+ );
197
+ }
198
+ }
199
+
200
+ const parsed = PushEventSchema.safeParse(candidate);
201
+ if (!parsed.success) {
202
+ throw new PushEventDecodeError(
203
+ "decodePushEvent: payload failed PushEvent schema validation",
204
+ { issues: parsed.error.issues, stage: "schema-validation" },
205
+ );
206
+ }
207
+ return parsed.data;
208
+ }
@@ -0,0 +1,84 @@
1
+ /**
2
+ * PushTransport — outbound shipping seam for the watcher daemon.
3
+ *
4
+ * The daemon wires file-watcher events into a transport that ships each
5
+ * PushEvent to the cloud. Defining the interface here lets the daemon ship
6
+ * with a swappable boundary — tests inject a fake, a later story swaps in a
7
+ * concrete WebSocket/HTTP implementation, and the daemon entry point never
8
+ * changes.
9
+ *
10
+ * Lifecycle
11
+ * ─────────
12
+ * - `start()` is awaited BEFORE the watcher is started. It's the place
13
+ * to open sockets, refresh tokens, etc.
14
+ * - `publish(event)` is called for every coalesced PushEvent the watcher
15
+ * emits. Implementations decide whether to buffer, batch, or send
16
+ * inline; the daemon awaits the returned promise so back-pressure can
17
+ * be honored.
18
+ * - `dispose()` is awaited DURING shutdown, AFTER the watcher has been
19
+ * torn down. Implementations should drain in-flight publishes (with
20
+ * their own internal timeout) and close any sockets.
21
+ * - `connected` is a passive boolean used by the health endpoint. It MAY
22
+ * flap during reconnect attempts — that's fine; consumers treat it as
23
+ * advisory, not a contract.
24
+ *
25
+ * The `NoopPushTransport` shipped here is the default when no transport
26
+ * is wired in: it counts publishes (so unit tests can assert delivery)
27
+ * and logs nothing — observers should rely on the watcher's `onEvent`
28
+ * counter on the daemon side, not the transport's internals.
29
+ *
30
+ * Ported from indigoai-us/hq-pro PR #112 (src/sync/push-transport.ts) into
31
+ * @indigoai-us/hq-cloud (Path B) per project event-driven-sync-menubar US-007.
32
+ */
33
+
34
+ import type { PushEvent } from "./push-event.js";
35
+
36
+ export interface PushTransport {
37
+ /** Open sockets, refresh tokens. Awaited before the watcher starts. */
38
+ start(): Promise<void>;
39
+ /** Ship one coalesced PushEvent. Awaited per event for back-pressure. */
40
+ publish(event: PushEvent): Promise<void>;
41
+ /** Drain + close. Awaited during daemon shutdown after watcher.dispose. */
42
+ dispose(): Promise<void>;
43
+ /** Advisory: is the transport currently believed to be connected? */
44
+ readonly connected: boolean;
45
+ }
46
+
47
+ /**
48
+ * Default `PushTransport` used until a real implementation lands.
49
+ *
50
+ * Behavior:
51
+ * - `start()` flips `connected` to true.
52
+ * - `publish()` increments a counter (visible via `publishedCount`).
53
+ * - `dispose()` flips `connected` back to false.
54
+ *
55
+ * Deliberately silent: when the daemon runs with this default, the
56
+ * watcher's per-event log line (emitted by the daemon itself, not the
57
+ * transport) is the only observability. That keeps the noop from drowning
58
+ * the log when high-rate writes hit a dev machine.
59
+ */
60
+ export class NoopPushTransport implements PushTransport {
61
+ private _connected = false;
62
+ private _count = 0;
63
+
64
+ get connected(): boolean {
65
+ return this._connected;
66
+ }
67
+
68
+ /** Test/observability hook: how many events have been published. */
69
+ get publishedCount(): number {
70
+ return this._count;
71
+ }
72
+
73
+ async start(): Promise<void> {
74
+ this._connected = true;
75
+ }
76
+
77
+ async publish(_event: PushEvent): Promise<void> {
78
+ this._count += 1;
79
+ }
80
+
81
+ async dispose(): Promise<void> {
82
+ this._connected = false;
83
+ }
84
+ }
@@ -0,0 +1,388 @@
1
+ import { afterEach, beforeEach, describe, it, expect, vi } from "vitest";
2
+ import * as fs from "fs";
3
+ import * as os from "os";
4
+ import * as path from "path";
5
+ import {
6
+ FakeClock,
7
+ WatchPushDriver,
8
+ TreeWatcher,
9
+ createWatchPathFilter,
10
+ } from "./watcher.js";
11
+
12
+ /**
13
+ * US-001 — Phase 1 test harness: watch-triggered push seam + latency assertion.
14
+ *
15
+ * These tests drive the debounce/coalesce core ({@link WatchPushDriver}) with an
16
+ * injected {@link FakeClock} and an injected spy push fn — no real chokidar
17
+ * watcher, no real S3, no real 10-minute sleep. US-002/US-003 build the real
18
+ * watcher + targeted-push fn on top of this same seam, so the tests here also
19
+ * serve as regression guards for the contract those stories depend on.
20
+ */
21
+
22
+ const DEBOUNCE = 2000;
23
+
24
+ /**
25
+ * Build a driver wired to a FakeClock and a spy push fn. Returns everything a
26
+ * test needs to drive and assert against the seam. This helper is the reusable
27
+ * "test harness" the story asks for.
28
+ */
29
+ function makeHarness(opts?: { debounceMs?: number }) {
30
+ const clock = new FakeClock();
31
+ const push = vi.fn(() => {});
32
+ const driver = new WatchPushDriver({
33
+ debounceMs: opts?.debounceMs ?? DEBOUNCE,
34
+ clock,
35
+ push,
36
+ });
37
+ return {
38
+ clock,
39
+ push,
40
+ driver,
41
+ /** Emit a synthetic file-change event into the driver. */
42
+ emitChange: () => driver.notifyChange(),
43
+ /** Advance virtual time. */
44
+ advance: (ms: number) => clock.advance(ms),
45
+ };
46
+ }
47
+
48
+ describe("FakeClock", () => {
49
+ it("fires a timer exactly when its deadline is reached", () => {
50
+ const clock = new FakeClock();
51
+ const fn = vi.fn();
52
+ clock.setTimeout(fn, 100);
53
+ clock.advance(99);
54
+ expect(fn).not.toHaveBeenCalled();
55
+ clock.advance(1);
56
+ expect(fn).toHaveBeenCalledTimes(1);
57
+ });
58
+
59
+ it("does not fire a cleared timer", () => {
60
+ const clock = new FakeClock();
61
+ const fn = vi.fn();
62
+ const handle = clock.setTimeout(fn, 100);
63
+ clock.clearTimeout(handle);
64
+ clock.advance(1000);
65
+ expect(fn).not.toHaveBeenCalled();
66
+ expect(clock.pendingTimerCount()).toBe(0);
67
+ });
68
+
69
+ it("reports pending timer count for leak checks", () => {
70
+ const clock = new FakeClock();
71
+ clock.setTimeout(() => {}, 100);
72
+ clock.setTimeout(() => {}, 200);
73
+ expect(clock.pendingTimerCount()).toBe(2);
74
+ clock.advance(100);
75
+ expect(clock.pendingTimerCount()).toBe(1);
76
+ });
77
+ });
78
+
79
+ describe("US-001: WatchPushDriver — debounced push seam", () => {
80
+ it("calls the injected push fn exactly once within debounce+grace after a synthetic change", async () => {
81
+ const h = makeHarness();
82
+
83
+ h.emitChange();
84
+ // Before the window elapses, no push.
85
+ h.advance(DEBOUNCE - 1);
86
+ expect(h.push).not.toHaveBeenCalled();
87
+
88
+ // Crossing the debounce boundary fires the push exactly once.
89
+ h.advance(1);
90
+ await Promise.resolve();
91
+ expect(h.push).toHaveBeenCalledTimes(1);
92
+ });
93
+
94
+ it("coalesces N rapid changes within the window to exactly 1 push", async () => {
95
+ const h = makeHarness();
96
+
97
+ // 5 synthetic changes spread across less than one debounce window.
98
+ for (let i = 0; i < 5; i++) {
99
+ h.emitChange();
100
+ h.advance(100); // total 500ms << 2000ms debounce
101
+ }
102
+ // Window has not fully elapsed since the LAST change yet.
103
+ expect(h.push).not.toHaveBeenCalled();
104
+
105
+ // Let the quiet window after the final change elapse.
106
+ h.advance(DEBOUNCE);
107
+ await Promise.resolve();
108
+ expect(h.push).toHaveBeenCalledTimes(1);
109
+ });
110
+
111
+ it("treats two separate bursts as two pushes", async () => {
112
+ const h = makeHarness();
113
+
114
+ h.emitChange();
115
+ h.advance(DEBOUNCE);
116
+ await Promise.resolve();
117
+ expect(h.push).toHaveBeenCalledTimes(1);
118
+
119
+ h.emitChange();
120
+ h.advance(DEBOUNCE);
121
+ await Promise.resolve();
122
+ expect(h.push).toHaveBeenCalledTimes(2);
123
+ });
124
+
125
+ it("never overlaps an in-flight push; a mid-push change re-triggers one follow-up pass", async () => {
126
+ const clock = new FakeClock();
127
+ let release!: () => void;
128
+ const inFlight = new Promise<void>((r) => {
129
+ release = r;
130
+ });
131
+ let calls = 0;
132
+ const push = vi.fn(() => {
133
+ calls += 1;
134
+ // First push hangs until released; later pushes resolve immediately.
135
+ return calls === 1 ? inFlight : Promise.resolve();
136
+ });
137
+ const driver = new WatchPushDriver({ debounceMs: DEBOUNCE, clock, push });
138
+
139
+ // First burst -> first push starts and hangs.
140
+ driver.notifyChange();
141
+ clock.advance(DEBOUNCE);
142
+ await Promise.resolve();
143
+ expect(push).toHaveBeenCalledTimes(1);
144
+ expect(driver.isPushing()).toBe(true);
145
+
146
+ // A change arrives while the first push is in flight: must NOT start a
147
+ // concurrent push.
148
+ driver.notifyChange();
149
+ clock.advance(DEBOUNCE);
150
+ await Promise.resolve();
151
+ expect(push).toHaveBeenCalledTimes(1);
152
+
153
+ // Release the first push; the collapsed mid-push change re-arms.
154
+ release();
155
+ await inFlight;
156
+ await Promise.resolve();
157
+ // Re-armed window must elapse before the follow-up push fires.
158
+ clock.advance(DEBOUNCE);
159
+ await Promise.resolve();
160
+ expect(push).toHaveBeenCalledTimes(2);
161
+
162
+ driver.dispose();
163
+ });
164
+
165
+ it("respects a custom debounce window", async () => {
166
+ const h = makeHarness({ debounceMs: 500 });
167
+ h.emitChange();
168
+ h.advance(499);
169
+ expect(h.push).not.toHaveBeenCalled();
170
+ h.advance(1);
171
+ await Promise.resolve();
172
+ expect(h.push).toHaveBeenCalledTimes(1);
173
+ });
174
+ });
175
+
176
+ describe("US-001: WatchPushDriver — teardown leaves no leaked timers", () => {
177
+ it("dispose() cancels a pending debounce timer", () => {
178
+ const h = makeHarness();
179
+ h.emitChange();
180
+ expect(h.clock.pendingTimerCount()).toBe(1);
181
+
182
+ h.driver.dispose();
183
+ expect(h.clock.pendingTimerCount()).toBe(0);
184
+
185
+ // A push must not fire after dispose, even if time advances.
186
+ h.advance(DEBOUNCE * 10);
187
+ expect(h.push).not.toHaveBeenCalled();
188
+ });
189
+
190
+ it("notifyChange() after dispose is a no-op and schedules nothing", () => {
191
+ const h = makeHarness();
192
+ h.driver.dispose();
193
+ h.emitChange();
194
+ expect(h.clock.pendingTimerCount()).toBe(0);
195
+ h.advance(DEBOUNCE * 10);
196
+ expect(h.push).not.toHaveBeenCalled();
197
+ });
198
+
199
+ it("dispose() is idempotent", () => {
200
+ const h = makeHarness();
201
+ h.emitChange();
202
+ h.driver.dispose();
203
+ h.driver.dispose();
204
+ expect(h.clock.pendingTimerCount()).toBe(0);
205
+ });
206
+
207
+ it("a fully drained burst leaves zero pending timers", async () => {
208
+ const h = makeHarness();
209
+ for (let i = 0; i < 5; i++) h.emitChange();
210
+ h.advance(DEBOUNCE);
211
+ await Promise.resolve();
212
+ expect(h.push).toHaveBeenCalledTimes(1);
213
+ expect(h.clock.pendingTimerCount()).toBe(0);
214
+ });
215
+ });
216
+
217
+ /**
218
+ * US-002 — file-watcher module (debounced, ignore-aware, exclusion-aware).
219
+ *
220
+ * `createWatchPathFilter` is pure (no chokidar) so the exclusion logic is
221
+ * tested directly against absolute paths. `TreeWatcher` is then exercised via
222
+ * its `handleEvent` seam + FakeClock so debounce/coalesce/lifecycle assert
223
+ * deterministically without spinning a real chokidar instance.
224
+ */
225
+
226
+ const ROOT = "/tmp/hq-root";
227
+
228
+ describe("US-002: createWatchPathFilter — ignore-list matching", () => {
229
+ const filter = createWatchPathFilter(ROOT, false);
230
+
231
+ it("emits for an ordinary tracked file", () => {
232
+ expect(filter(path.join(ROOT, "personal/notes.md"))).toBe(true);
233
+ });
234
+
235
+ it("does NOT emit for a .env file (DEFAULT_IGNORES secret)", () => {
236
+ expect(filter(path.join(ROOT, ".env"))).toBe(false);
237
+ expect(filter(path.join(ROOT, "personal/.env.local"))).toBe(false);
238
+ });
239
+
240
+ it("does NOT emit for node_modules / dist build artifacts", () => {
241
+ expect(filter(path.join(ROOT, "node_modules/foo/index.js"))).toBe(false);
242
+ expect(filter(path.join(ROOT, "dist/bundle.js"))).toBe(false);
243
+ });
244
+
245
+ it("does NOT emit for the watched root itself or paths outside it", () => {
246
+ expect(filter(ROOT)).toBe(false);
247
+ expect(filter("/some/other/place/file.txt")).toBe(false);
248
+ });
249
+ });
250
+
251
+ describe("US-002: createWatchPathFilter — personal-vault exclusions", () => {
252
+ const personal = createWatchPathFilter(ROOT, true);
253
+ const nonPersonal = createWatchPathFilter(ROOT, false);
254
+
255
+ it("does NOT emit for PERSONAL_VAULT_DEFAULT_EXCLUSIONS in personalMode (output/, .beads/)", () => {
256
+ expect(personal(path.join(ROOT, "personal/output/x.txt"))).toBe(false);
257
+ expect(personal(path.join(ROOT, "personal/.beads/issues.db"))).toBe(false);
258
+ });
259
+
260
+ it("does NOT emit for PERSONAL_VAULT_EXCLUDED_TOP_LEVEL in personalMode (.git/companies/repos/workspace)", () => {
261
+ expect(personal(path.join(ROOT, "companies/indigo/board.json"))).toBe(false);
262
+ expect(personal(path.join(ROOT, "workspace/threads/x.json"))).toBe(false);
263
+ expect(personal(path.join(ROOT, "repos/private/foo/file.ts"))).toBe(false);
264
+ });
265
+
266
+ it("DOES emit for an included top-level personal path in personalMode", () => {
267
+ expect(personal(path.join(ROOT, "personal/notes.md"))).toBe(true);
268
+ expect(personal(path.join(ROOT, "core/policies/x.md"))).toBe(true);
269
+ });
270
+
271
+ it("non-personal mode does NOT apply the excluded-top-level buckets", () => {
272
+ // companies/ is only excluded by the personal-vault layer; in non-personal
273
+ // mode the ignore stack alone governs and lets it through.
274
+ expect(nonPersonal(path.join(ROOT, "companies/indigo/board.json"))).toBe(true);
275
+ });
276
+ });
277
+
278
+ describe("US-002: TreeWatcher — debounce coalesce (FakeClock seam)", () => {
279
+ function makeWatcher(opts?: { debounceMs?: number; personalMode?: boolean }) {
280
+ const clock = new FakeClock();
281
+ const changed = vi.fn();
282
+ const watcher = new TreeWatcher({
283
+ hqRoot: ROOT,
284
+ debounceMs: opts?.debounceMs ?? DEBOUNCE,
285
+ personalMode: opts?.personalMode ?? false,
286
+ clock,
287
+ });
288
+ watcher.onChange(changed);
289
+ return { clock, changed, watcher };
290
+ }
291
+
292
+ it("coalesces 5 rapid changes within the window into exactly 1 changed signal", () => {
293
+ const { clock, changed, watcher } = makeWatcher();
294
+ for (let i = 0; i < 5; i++) {
295
+ watcher.handleEvent(path.join(ROOT, `personal/file-${i}.md`));
296
+ clock.advance(20); // 100ms total << 2000ms debounce
297
+ }
298
+ expect(changed).not.toHaveBeenCalled();
299
+ clock.advance(DEBOUNCE);
300
+ expect(changed).toHaveBeenCalledTimes(1);
301
+ expect(watcher.pendingTimerCount()).toBe(0);
302
+ });
303
+
304
+ it("fires once after the quiet window for a single change", () => {
305
+ const { clock, changed, watcher } = makeWatcher();
306
+ watcher.handleEvent(path.join(ROOT, "personal/a.md"));
307
+ clock.advance(DEBOUNCE - 1);
308
+ expect(changed).not.toHaveBeenCalled();
309
+ clock.advance(1);
310
+ expect(changed).toHaveBeenCalledTimes(1);
311
+ });
312
+
313
+ it("does NOT emit for an ignored / excluded path", () => {
314
+ const { clock, changed, watcher } = makeWatcher({ personalMode: true });
315
+ watcher.handleEvent(path.join(ROOT, ".env"));
316
+ watcher.handleEvent(path.join(ROOT, "personal/output/big.bin"));
317
+ watcher.handleEvent(path.join(ROOT, "companies/indigo/board.json"));
318
+ clock.advance(DEBOUNCE * 2);
319
+ expect(changed).not.toHaveBeenCalled();
320
+ expect(watcher.pendingTimerCount()).toBe(0);
321
+ });
322
+
323
+ it("treats two separate bursts as two changed signals", () => {
324
+ const { clock, changed, watcher } = makeWatcher();
325
+ watcher.handleEvent(path.join(ROOT, "personal/a.md"));
326
+ clock.advance(DEBOUNCE);
327
+ expect(changed).toHaveBeenCalledTimes(1);
328
+ watcher.handleEvent(path.join(ROOT, "personal/b.md"));
329
+ clock.advance(DEBOUNCE);
330
+ expect(changed).toHaveBeenCalledTimes(2);
331
+ });
332
+ });
333
+
334
+ describe("US-002: TreeWatcher — lifecycle (real chokidar over a temp dir)", () => {
335
+ let dir: string;
336
+
337
+ beforeEach(() => {
338
+ // realpathSync resolves macOS's /var -> /private/var symlink so chokidar's
339
+ // canonicalized event paths stay inside hqRoot (path.relative would
340
+ // otherwise yield a `..`-prefixed path and the filter would reject them).
341
+ dir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "treewatcher-")));
342
+ });
343
+
344
+ afterEach(() => {
345
+ fs.rmSync(dir, { recursive: true, force: true });
346
+ });
347
+
348
+ it("start() is idempotent — a second start does not open a second watcher", () => {
349
+ const w = new TreeWatcher({ hqRoot: dir });
350
+ w.start();
351
+ expect(w.isWatching()).toBe(true);
352
+ w.start(); // no throw, no second instance
353
+ expect(w.isWatching()).toBe(true);
354
+ w.stop();
355
+ expect(w.isWatching()).toBe(false);
356
+ });
357
+
358
+ it("stop() closes the watcher and clears pending timers; dispose() is permanent", () => {
359
+ const clock = new FakeClock();
360
+ const w = new TreeWatcher({ hqRoot: dir, clock });
361
+ w.start();
362
+ w.handleEvent(path.join(dir, "x.md"));
363
+ expect(w.pendingTimerCount()).toBe(1);
364
+ w.stop();
365
+ expect(w.pendingTimerCount()).toBe(0);
366
+ expect(w.isWatching()).toBe(false);
367
+
368
+ // After dispose, events are inert and start is a no-op.
369
+ w.dispose();
370
+ w.handleEvent(path.join(dir, "y.md"));
371
+ expect(w.pendingTimerCount()).toBe(0);
372
+ w.start();
373
+ expect(w.isWatching()).toBe(false);
374
+ });
375
+
376
+ it("emits a debounced changed signal for a real file write", async () => {
377
+ const w = new TreeWatcher({ hqRoot: dir, debounceMs: 50 });
378
+ const changed = vi.fn();
379
+ w.onChange(changed);
380
+ w.start();
381
+ // Give chokidar a beat to set up its watch before writing.
382
+ await new Promise((r) => setTimeout(r, 200));
383
+ fs.writeFileSync(path.join(dir, "hello.md"), "hi");
384
+ await new Promise((r) => setTimeout(r, 1500));
385
+ expect(changed).toHaveBeenCalled();
386
+ w.dispose();
387
+ }, 10_000);
388
+ });