@indigoai-us/hq-cloud 5.25.0 → 5.27.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 (87) hide show
  1. package/.github/workflows/ci.yml +34 -0
  2. package/dist/bin/sync-runner.d.ts +138 -1
  3. package/dist/bin/sync-runner.d.ts.map +1 -1
  4. package/dist/bin/sync-runner.js +288 -16
  5. package/dist/bin/sync-runner.js.map +1 -1
  6. package/dist/bin/sync-runner.test.js +372 -1
  7. package/dist/bin/sync-runner.test.js.map +1 -1
  8. package/dist/index.d.ts +4 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +6 -0
  11. package/dist/index.js.map +1 -1
  12. package/dist/sync/feature-flags.d.ts +136 -0
  13. package/dist/sync/feature-flags.d.ts.map +1 -0
  14. package/dist/sync/feature-flags.js +160 -0
  15. package/dist/sync/feature-flags.js.map +1 -0
  16. package/dist/sync/feature-flags.test.d.ts +24 -0
  17. package/dist/sync/feature-flags.test.d.ts.map +1 -0
  18. package/dist/sync/feature-flags.test.js +330 -0
  19. package/dist/sync/feature-flags.test.js.map +1 -0
  20. package/dist/sync/index.d.ts +19 -0
  21. package/dist/sync/index.d.ts.map +1 -0
  22. package/dist/sync/index.js +13 -0
  23. package/dist/sync/index.js.map +1 -0
  24. package/dist/sync/logger.d.ts +61 -0
  25. package/dist/sync/logger.d.ts.map +1 -0
  26. package/dist/sync/logger.js +51 -0
  27. package/dist/sync/logger.js.map +1 -0
  28. package/dist/sync/logger.test.d.ts +19 -0
  29. package/dist/sync/logger.test.d.ts.map +1 -0
  30. package/dist/sync/logger.test.js +199 -0
  31. package/dist/sync/logger.test.js.map +1 -0
  32. package/dist/sync/metrics.d.ts +89 -0
  33. package/dist/sync/metrics.d.ts.map +1 -0
  34. package/dist/sync/metrics.js +105 -0
  35. package/dist/sync/metrics.js.map +1 -0
  36. package/dist/sync/metrics.test.d.ts +19 -0
  37. package/dist/sync/metrics.test.d.ts.map +1 -0
  38. package/dist/sync/metrics.test.js +280 -0
  39. package/dist/sync/metrics.test.js.map +1 -0
  40. package/dist/sync/push-event.d.ts +110 -0
  41. package/dist/sync/push-event.d.ts.map +1 -0
  42. package/dist/sync/push-event.js +153 -0
  43. package/dist/sync/push-event.js.map +1 -0
  44. package/dist/sync/push-event.test.d.ts +15 -0
  45. package/dist/sync/push-event.test.d.ts.map +1 -0
  46. package/dist/sync/push-event.test.js +188 -0
  47. package/dist/sync/push-event.test.js.map +1 -0
  48. package/dist/sync/push-receiver.d.ts +442 -0
  49. package/dist/sync/push-receiver.d.ts.map +1 -0
  50. package/dist/sync/push-receiver.js +782 -0
  51. package/dist/sync/push-receiver.js.map +1 -0
  52. package/dist/sync/push-receiver.test.d.ts +25 -0
  53. package/dist/sync/push-receiver.test.d.ts.map +1 -0
  54. package/dist/sync/push-receiver.test.js +477 -0
  55. package/dist/sync/push-receiver.test.js.map +1 -0
  56. package/dist/sync/push-transport.d.ts +150 -0
  57. package/dist/sync/push-transport.d.ts.map +1 -0
  58. package/dist/sync/push-transport.js +150 -0
  59. package/dist/sync/push-transport.js.map +1 -0
  60. package/dist/watcher.d.ts +271 -0
  61. package/dist/watcher.d.ts.map +1 -1
  62. package/dist/watcher.js +480 -3
  63. package/dist/watcher.js.map +1 -1
  64. package/dist/watcher.test.d.ts +2 -0
  65. package/dist/watcher.test.d.ts.map +1 -0
  66. package/dist/watcher.test.js +334 -0
  67. package/dist/watcher.test.js.map +1 -0
  68. package/package.json +10 -5
  69. package/src/bin/sync-runner.test.ts +487 -1
  70. package/src/bin/sync-runner.ts +406 -9
  71. package/src/index.ts +38 -0
  72. package/src/sync/feature-flags.test.ts +392 -0
  73. package/src/sync/feature-flags.ts +229 -0
  74. package/src/sync/index.ts +74 -0
  75. package/src/sync/logger.test.ts +241 -0
  76. package/src/sync/logger.ts +79 -0
  77. package/src/sync/metrics.test.ts +380 -0
  78. package/src/sync/metrics.ts +158 -0
  79. package/src/sync/push-event.test.ts +224 -0
  80. package/src/sync/push-event.ts +208 -0
  81. package/src/sync/push-receiver.test.ts +545 -0
  82. package/src/sync/push-receiver.ts +1077 -0
  83. package/src/sync/push-transport.ts +231 -0
  84. package/src/watcher.test.ts +388 -0
  85. package/src/watcher.ts +672 -4
  86. package/test/e2e/sync/cross-tenant-isolation.test.ts +502 -0
  87. package/test/e2e/watcher-real-chokidar.test.ts +105 -0
@@ -0,0 +1,231 @@
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 { encodePushEvent, 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
+ }
85
+
86
+ // ─── HttpPushTransport ───────────────────────────────────────────────────────
87
+
88
+ /**
89
+ * Minimal fetch surface so tests can inject a mocked `fetch` without pulling
90
+ * in DOM/undici types. Matches the subset of the global `fetch` we use.
91
+ */
92
+ export type FetchLike = (
93
+ input: string,
94
+ init?: {
95
+ method?: string;
96
+ headers?: Record<string, string>;
97
+ body?: string;
98
+ signal?: AbortSignal;
99
+ },
100
+ ) => Promise<{ ok: boolean; status: number; text(): Promise<string> }>;
101
+
102
+ /**
103
+ * Async getter for the current Cognito access token. Long-running daemons MUST
104
+ * pass a getter (not a captured string) so each publish resolves the freshest
105
+ * token — mirrors {@link VaultServiceConfig.authToken} semantics in types.ts.
106
+ * A static string is also accepted for short-lived tools and tests.
107
+ */
108
+ export type AuthTokenSource = string | (() => string | Promise<string>);
109
+
110
+ export interface HttpPushTransportOptions {
111
+ /**
112
+ * Vault API base URL (e.g. `https://vault-api.example.com`). The same
113
+ * `apiUrl` the runner already resolves for VaultClient. Trailing slashes
114
+ * are stripped. Required — config/env-driven, no hard-coded default.
115
+ */
116
+ apiUrl: string;
117
+ /**
118
+ * Endpoint path the PushEvent is POSTed to. Defaults to `/sync/push`
119
+ * (matches the deployed hq-pro endpoint from US-006). Override for testing
120
+ * or future endpoint moves.
121
+ */
122
+ pushPath?: string;
123
+ /** Cognito JWT — static string OR async getter. See {@link AuthTokenSource}. */
124
+ authToken: AuthTokenSource;
125
+ /**
126
+ * Optional extra headers (e.g. client identification) merged onto every
127
+ * request. Authorization + Content-Type are always set by the transport.
128
+ */
129
+ headers?: Record<string, string>;
130
+ /**
131
+ * Per-request timeout in milliseconds. Default 10_000. On timeout the
132
+ * publish rejects — the daemon treats a rejected publish as a transient
133
+ * miss (the cadence poll still covers it) and MUST NOT crash.
134
+ */
135
+ timeoutMs?: number;
136
+ /** Injectable fetch (tests). Defaults to the global `fetch`. */
137
+ fetchImpl?: FetchLike;
138
+ }
139
+
140
+ /**
141
+ * Real client `PushTransport` that POSTs encoded PushEvents to the deployed
142
+ * `/sync/push` endpoint, authenticating with the menubar's existing Cognito
143
+ * bearer token — the same auth path VaultClient uses (Authorization: Bearer
144
+ * <accessToken>, token resolved per-request via the supplied getter so it
145
+ * self-heals across refreshes).
146
+ *
147
+ * Failure posture
148
+ * ───────────────
149
+ * `publish()` rejects on a network error, a non-2xx response, or a timeout.
150
+ * The DAEMON is responsible for not letting that rejection crash it — the
151
+ * watcher's emit path catches publish errors and logs them; the periodic
152
+ * cadence poll remains the safety net that eventually ships the change. This
153
+ * transport never swallows errors itself, so callers retain full visibility.
154
+ *
155
+ * `connected` flips true on `start()` and false on `dispose()`. It is purely
156
+ * advisory (HTTP is connectionless); the health endpoint may read it but it
157
+ * carries no delivery guarantee.
158
+ */
159
+ export class HttpPushTransport implements PushTransport {
160
+ private readonly apiUrl: string;
161
+ private readonly pushPath: string;
162
+ private readonly getAuthToken: () => Promise<string>;
163
+ private readonly extraHeaders: Record<string, string>;
164
+ private readonly timeoutMs: number;
165
+ private readonly fetchImpl: FetchLike;
166
+ private _connected = false;
167
+
168
+ constructor(opts: HttpPushTransportOptions) {
169
+ if (!opts.apiUrl || opts.apiUrl.trim() === "") {
170
+ throw new Error("HttpPushTransport: apiUrl is required");
171
+ }
172
+ this.apiUrl = opts.apiUrl.replace(/\/+$/, "");
173
+ const path = opts.pushPath ?? "/sync/push";
174
+ this.pushPath = path.startsWith("/") ? path : `/${path}`;
175
+ const tok = opts.authToken;
176
+ // Normalize string|getter into a single async getter (mirrors VaultClient).
177
+ this.getAuthToken =
178
+ typeof tok === "function" ? async () => tok() : async () => tok;
179
+ this.extraHeaders = opts.headers ?? {};
180
+ this.timeoutMs = opts.timeoutMs ?? 10_000;
181
+ // Bind so a destructured global fetch keeps its receiver.
182
+ this.fetchImpl =
183
+ opts.fetchImpl ??
184
+ ((input, init) => (globalThis.fetch as unknown as FetchLike)(input, init));
185
+ }
186
+
187
+ get connected(): boolean {
188
+ return this._connected;
189
+ }
190
+
191
+ async start(): Promise<void> {
192
+ this._connected = true;
193
+ }
194
+
195
+ async publish(event: PushEvent): Promise<void> {
196
+ // Validate-on-encode (US-007 contract) — a malformed event throws a typed
197
+ // PushEventDecodeError BEFORE we hit the network.
198
+ const body = encodePushEvent(event);
199
+ const token = await this.getAuthToken();
200
+
201
+ const controller = new AbortController();
202
+ const timer = setTimeout(() => controller.abort(), this.timeoutMs);
203
+ try {
204
+ const res = await this.fetchImpl(`${this.apiUrl}${this.pushPath}`, {
205
+ method: "POST",
206
+ headers: {
207
+ ...this.extraHeaders,
208
+ Authorization: `Bearer ${token}`,
209
+ "Content-Type": "application/json",
210
+ Accept: "application/json",
211
+ },
212
+ body,
213
+ signal: controller.signal,
214
+ });
215
+ if (!res.ok) {
216
+ const text = await res.text().catch(() => "");
217
+ throw new Error(
218
+ `HttpPushTransport: POST ${this.pushPath} failed (${res.status})${
219
+ text ? `: ${text}` : ""
220
+ }`,
221
+ );
222
+ }
223
+ } finally {
224
+ clearTimeout(timer);
225
+ }
226
+ }
227
+
228
+ async dispose(): Promise<void> {
229
+ this._connected = false;
230
+ }
231
+ }
@@ -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
+ });