Open History to see the last commit
1import { assert, it, describe } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Cause from "effect/Cause";
4import * as Deferred from "effect/Deferred";
5import * as Duration from "effect/Duration";
6import * as Effect from "effect/Effect";
7import * as Exit from "effect/Exit";
8import * as FileSystem from "effect/FileSystem";
9import * as Layer from "effect/Layer";
10import * as Logger from "effect/Logger";
11import * as Option from "effect/Option";
12import * as Path from "effect/Path";
13import * as Scope from "effect/Scope";
14import * as Stream from "effect/Stream";
15import * as TestClock from "effect/testing/TestClock";
16import type {
17 VcsStatusLocalResult,
18 VcsStatusRemoteResult,
19 VcsStatusResult,
20 VcsStatusStreamEvent,
21} from "@t3tools/contracts";
22import { GitManagerError } from "@t3tools/contracts";
23
24import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts";
25import * as GitWorkflowService from "../git/GitWorkflowService.ts";
26
27const baseLocalStatus: VcsStatusLocalResult = {
28 isRepo: true,
29 sourceControlProvider: {
30 kind: "github",
31 name: "GitHub",
32 baseUrl: "https://github.com",
33 },
34 hasPrimaryRemote: true,
35 isDefaultRef: false,
36 refName: "feature/status-broadcast",
37 hasWorkingTreeChanges: false,
38 workingTree: { files: [], insertions: 0, deletions: 0 },
39};
40
41const baseRemoteStatus: VcsStatusRemoteResult = {
42 hasUpstream: true,
43 aheadCount: 0,
44 behindCount: 0,
45 pr: null,
46};
47
48const remoteStatusWithPr: VcsStatusRemoteResult = {
49 ...baseRemoteStatus,
50 pr: {
51 number: 2978,
52 title: "[codex] Rewrite client connection architecture",
53 url: "https://github.com/pingdotgg/t3code/pull/2978",
54 baseRef: "main",
55 headRef: "codex/connection-state-audit",
56 state: "open",
57 },
58};
59
60const baseStatus: VcsStatusResult = {
61 ...baseLocalStatus,
62 ...baseRemoteStatus,
63};
64
65function makeTestLayer(state: {
66 currentLocalStatus: VcsStatusLocalResult;
67 currentRemoteStatus: VcsStatusRemoteResult | null;
68 localStatusCalls: number;
69 remoteStatusCalls: number;
70 localInvalidationCalls: number;
71 remoteInvalidationCalls: number;
72 remoteStatusRefreshUpstreamValues?: Array<boolean | undefined>;
73}) {
74 return VcsStatusBroadcaster.layer.pipe(
75 Layer.provideMerge(NodeServices.layer),
76 Layer.provide(
77 Layer.mock(GitWorkflowService.GitWorkflowService)({
78 localStatus: () =>
79 Effect.sync(() => {
80 state.localStatusCalls += 1;
81 return state.currentLocalStatus;
82 }),
83 remoteStatus: (_input, options) =>
84 Effect.sync(() => {
85 state.remoteStatusCalls += 1;
86 state.remoteStatusRefreshUpstreamValues?.push(options?.refreshUpstream);
87 return state.currentRemoteStatus;
88 }),
89 invalidateLocalStatus: () =>
90 Effect.sync(() => {
91 state.localInvalidationCalls += 1;
92 }),
93 invalidateRemoteStatus: () =>
94 Effect.sync(() => {
95 state.remoteInvalidationCalls += 1;
96 }),
97 invalidateStatus: () =>
98 Effect.sync(() => {
99 state.localInvalidationCalls += 1;
100 state.remoteInvalidationCalls += 1;
101 }),
102 }),
103 ),
104 );
105}
106
107describe("VcsStatusBroadcaster", () => {
108 it.effect("reuses the cached VCS status across repeated reads", () => {
109 const state = {
110 currentLocalStatus: baseLocalStatus,
111 currentRemoteStatus: baseRemoteStatus,
112 localStatusCalls: 0,
113 remoteStatusCalls: 0,
114 localInvalidationCalls: 0,
115 remoteInvalidationCalls: 0,
116 };
117
118 return Effect.gen(function* () {
119 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
120
121 const first = yield* broadcaster.getStatus({ cwd: "/repo" });
122 const second = yield* broadcaster.getStatus({ cwd: "/repo" });
123
124 assert.deepStrictEqual(first, baseStatus);
125 assert.deepStrictEqual(second, baseStatus);
126 assert.equal(state.localStatusCalls, 1);
127 assert.equal(state.remoteStatusCalls, 1);
128 assert.equal(state.localInvalidationCalls, 0);
129 assert.equal(state.remoteInvalidationCalls, 0);
130 }).pipe(Effect.provide(makeTestLayer(state)));
131 });
132
133 it.effect("refreshes the cached snapshot after explicit invalidation", () => {
134 const state = {
135 currentLocalStatus: baseLocalStatus,
136 currentRemoteStatus: baseRemoteStatus,
137 localStatusCalls: 0,
138 remoteStatusCalls: 0,
139 localInvalidationCalls: 0,
140 remoteInvalidationCalls: 0,
141 };
142
143 return Effect.gen(function* () {
144 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
145 const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
146
147 state.currentLocalStatus = {
148 ...baseLocalStatus,
149 refName: "feature/updated-status",
150 };
151 state.currentRemoteStatus = {
152 ...baseRemoteStatus,
153 aheadCount: 2,
154 };
155 const refreshed = yield* broadcaster.refreshStatus("/repo");
156 const cached = yield* broadcaster.getStatus({ cwd: "/repo" });
157
158 assert.deepStrictEqual(initial, baseStatus);
159 assert.deepStrictEqual(refreshed, {
160 ...state.currentLocalStatus,
161 ...state.currentRemoteStatus,
162 });
163 assert.deepStrictEqual(cached, {
164 ...state.currentLocalStatus,
165 ...state.currentRemoteStatus,
166 });
167 assert.equal(state.localStatusCalls, 2);
168 assert.equal(state.remoteStatusCalls, 2);
169 assert.equal(state.localInvalidationCalls, 1);
170 assert.equal(state.remoteInvalidationCalls, 1);
171 }).pipe(Effect.provide(makeTestLayer(state)));
172 });
173
174 it.effect("keeps the cached snapshot unchanged when a refresh branch fails", () => {
175 const state = {
176 currentLocalStatus: baseLocalStatus,
177 currentRemoteStatus: baseRemoteStatus,
178 localStatusCalls: 0,
179 remoteStatusCalls: 0,
180 localInvalidationCalls: 0,
181 remoteInvalidationCalls: 0,
182 failRemoteStatus: false,
183 };
184 const testLayer = VcsStatusBroadcaster.layer.pipe(
185 Layer.provideMerge(NodeServices.layer),
186 Layer.provide(
187 Layer.mock(GitWorkflowService.GitWorkflowService)({
188 localStatus: () =>
189 Effect.sync(() => {
190 state.localStatusCalls += 1;
191 return state.currentLocalStatus;
192 }),
193 remoteStatus: () =>
194 Effect.suspend(() => {
195 state.remoteStatusCalls += 1;
196 return state.failRemoteStatus
197 ? Effect.fail(
198 new GitManagerError({
199 operation: "VcsStatusBroadcaster.test",
200 cwd: "/repo",
201 detail: "remote status failed",
202 }),
203 )
204 : Effect.succeed(state.currentRemoteStatus);
205 }),
206 invalidateLocalStatus: () =>
207 Effect.sync(() => {
208 state.localInvalidationCalls += 1;
209 }),
210 invalidateRemoteStatus: () =>
211 Effect.sync(() => {
212 state.remoteInvalidationCalls += 1;
213 }),
214 invalidateStatus: () =>
215 Effect.sync(() => {
216 state.localInvalidationCalls += 1;
217 state.remoteInvalidationCalls += 1;
218 }),
219 }),
220 ),
221 );
222
223 return Effect.gen(function* () {
224 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
225 yield* broadcaster.getStatus({ cwd: "/repo" });
226
227 state.currentLocalStatus = {
228 ...baseLocalStatus,
229 refName: "feature/partial-refresh",
230 };
231 state.currentRemoteStatus = {
232 ...baseRemoteStatus,
233 aheadCount: 3,
234 };
235 state.failRemoteStatus = true;
236
237 const refreshExit = yield* broadcaster.refreshStatus("/repo").pipe(Effect.exit);
238 const cached = yield* broadcaster.getStatus({ cwd: "/repo" });
239
240 assert.isTrue(Exit.isFailure(refreshExit));
241 assert.deepStrictEqual(cached, baseStatus);
242 }).pipe(Effect.provide(testLayer));
243 });
244
245 it.effect("refreshes only the cached local snapshot when requested", () => {
246 const state = {
247 currentLocalStatus: baseLocalStatus,
248 currentRemoteStatus: baseRemoteStatus,
249 localStatusCalls: 0,
250 remoteStatusCalls: 0,
251 localInvalidationCalls: 0,
252 remoteInvalidationCalls: 0,
253 };
254
255 return Effect.gen(function* () {
256 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
257 const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
258
259 state.currentLocalStatus = {
260 ...baseLocalStatus,
261 refName: "feature/local-only-refresh",
262 hasWorkingTreeChanges: true,
263 };
264
265 const refreshedLocal = yield* broadcaster.refreshLocalStatus("/repo");
266 const cached = yield* broadcaster.getStatus({ cwd: "/repo" });
267
268 assert.deepStrictEqual(initial, baseStatus);
269 assert.deepStrictEqual(refreshedLocal, state.currentLocalStatus);
270 assert.deepStrictEqual(cached, {
271 ...state.currentLocalStatus,
272 ...baseRemoteStatus,
273 });
274 assert.equal(state.localStatusCalls, 2);
275 assert.equal(state.remoteStatusCalls, 1);
276 assert.equal(state.localInvalidationCalls, 1);
277 assert.equal(state.remoteInvalidationCalls, 0);
278 }).pipe(Effect.provide(makeTestLayer(state)));
279 });
280
281 it.effect("normalizes symlinked CWDs before cache lookup and workflow calls", () => {
282 const seenCwds: string[] = [];
283 const state = {
284 currentLocalStatus: baseLocalStatus,
285 currentRemoteStatus: baseRemoteStatus,
286 localStatusCalls: 0,
287 remoteStatusCalls: 0,
288 localInvalidationCalls: 0,
289 remoteInvalidationCalls: 0,
290 };
291 const testLayer = VcsStatusBroadcaster.layer.pipe(
292 Layer.provideMerge(NodeServices.layer),
293 Layer.provide(
294 Layer.mock(GitWorkflowService.GitWorkflowService)({
295 localStatus: (input) =>
296 Effect.sync(() => {
297 seenCwds.push(input.cwd);
298 state.localStatusCalls += 1;
299 return state.currentLocalStatus;
300 }),
301 remoteStatus: (input) =>
302 Effect.sync(() => {
303 seenCwds.push(input.cwd);
304 state.remoteStatusCalls += 1;
305 return state.currentRemoteStatus;
306 }),
307 invalidateLocalStatus: () =>
308 Effect.sync(() => {
309 state.localInvalidationCalls += 1;
310 }),
311 invalidateRemoteStatus: () =>
312 Effect.sync(() => {
313 state.remoteInvalidationCalls += 1;
314 }),
315 } satisfies Partial<GitWorkflowService.GitWorkflowService["Service"]>),
316 ),
317 );
318
319 return Effect.gen(function* () {
320 const fileSystem = yield* FileSystem.FileSystem;
321 const path = yield* Path.Path;
322 const realDir = yield* fileSystem.makeTempDirectoryScoped({
323 prefix: "t3-vcs-status-real-",
324 });
325 const linkParent = yield* fileSystem.makeTempDirectoryScoped({
326 prefix: "t3-vcs-status-link-",
327 });
328 const linkDir = path.join(linkParent, "repo-link");
329 yield* fileSystem.symlink(realDir, linkDir);
330 const realPath = yield* fileSystem.realPath(realDir);
331
332 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
333 yield* broadcaster.getStatus({ cwd: linkDir });
334 yield* broadcaster.getStatus({ cwd: realDir });
335
336 assert.deepStrictEqual(seenCwds, [realPath, realPath]);
337 assert.equal(state.localStatusCalls, 1);
338 assert.equal(state.remoteStatusCalls, 1);
339 }).pipe(Effect.provide(testLayer));
340 });
341
342 it.effect("streams a local snapshot first and remote updates later", () => {
343 const state = {
344 currentLocalStatus: baseLocalStatus,
345 currentRemoteStatus: baseRemoteStatus,
346 localStatusCalls: 0,
347 remoteStatusCalls: 0,
348 localInvalidationCalls: 0,
349 remoteInvalidationCalls: 0,
350 };
351
352 return Effect.gen(function* () {
353 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
354 const snapshotDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
355 const remoteUpdatedDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
356 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => {
357 if (event._tag === "snapshot") {
358 return Deferred.succeed(snapshotDeferred, event).pipe(Effect.ignore);
359 }
360 if (event._tag === "remoteUpdated") {
361 return Deferred.succeed(remoteUpdatedDeferred, event).pipe(Effect.ignore);
362 }
363 return Effect.void;
364 }).pipe(Effect.forkScoped);
365
366 const snapshot = yield* Deferred.await(snapshotDeferred);
367 yield* broadcaster.refreshStatus("/repo");
368 const remoteUpdated = yield* Deferred.await(remoteUpdatedDeferred);
369
370 assert.deepStrictEqual(snapshot, {
371 _tag: "snapshot",
372 local: baseLocalStatus,
373 remote: null,
374 } satisfies VcsStatusStreamEvent);
375 assert.deepStrictEqual(remoteUpdated, {
376 _tag: "remoteUpdated",
377 remote: baseRemoteStatus,
378 } satisfies VcsStatusStreamEvent);
379 }).pipe(Effect.provide(makeTestLayer(state)));
380 });
381
382 it.effect("loads remote status once when periodic refreshes are disabled", () => {
383 const state = {
384 currentLocalStatus: baseLocalStatus,
385 currentRemoteStatus: remoteStatusWithPr,
386 localStatusCalls: 0,
387 remoteStatusCalls: 0,
388 localInvalidationCalls: 0,
389 remoteInvalidationCalls: 0,
390 remoteStatusRefreshUpstreamValues: [] as Array<boolean | undefined>,
391 };
392
393 return Effect.gen(function* () {
394 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
395 const scope = yield* Scope.make();
396 const snapshotDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
397 const remoteUpdatedDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
398 yield* Stream.runForEach(
399 broadcaster.streamStatus(
400 { cwd: "/repo" },
401 { automaticRemoteRefreshInterval: Effect.succeed(Duration.zero) },
402 ),
403 (event) => {
404 if (event._tag === "snapshot") {
405 return Deferred.succeed(snapshotDeferred, event).pipe(Effect.ignore);
406 }
407 if (event._tag === "remoteUpdated") {
408 return Deferred.succeed(remoteUpdatedDeferred, event).pipe(Effect.ignore);
409 }
410 return Effect.void;
411 },
412 ).pipe(Effect.forkIn(scope));
413
414 const snapshot = yield* Deferred.await(snapshotDeferred);
415 const remoteUpdated = yield* Deferred.await(remoteUpdatedDeferred);
416
417 assert.deepStrictEqual(snapshot, {
418 _tag: "snapshot",
419 local: baseLocalStatus,
420 remote: null,
421 } satisfies VcsStatusStreamEvent);
422 assert.deepStrictEqual(remoteUpdated, {
423 _tag: "remoteUpdated",
424 remote: remoteStatusWithPr,
425 } satisfies VcsStatusStreamEvent);
426 assert.equal(state.remoteStatusCalls, 1);
427 assert.equal(state.remoteInvalidationCalls, 0);
428 assert.deepStrictEqual(state.remoteStatusRefreshUpstreamValues, [false]);
429
430 yield* TestClock.adjust(Duration.minutes(2));
431 assert.equal(state.remoteStatusCalls, 1);
432 assert.equal(state.remoteInvalidationCalls, 0);
433
434 yield* Scope.close(scope, Exit.void);
435 }).pipe(Effect.provide(Layer.merge(makeTestLayer(state), TestClock.layer())));
436 });
437
438 it.effect("retries the initial remote load when periodic refreshes are disabled", () => {
439 const state = {
440 currentLocalStatus: baseLocalStatus,
441 localStatusCalls: 0,
442 remoteStatusCalls: 0,
443 localInvalidationCalls: 0,
444 remoteInvalidationCalls: 0,
445 remoteStatusRefreshUpstreamValues: [] as Array<boolean | undefined>,
446 };
447 const privateCwd = "/private/user/workspace/repo";
448 const nestedCause = new Error("private nested VCS failure");
449 const messages: Array<ReadonlyArray<unknown>> = [];
450 const logger = Logger.make<unknown, void>(({ message }) => {
451 messages.push(message as ReadonlyArray<unknown>);
452 });
453 let firstRemoteAttemptDeferred: Deferred.Deferred<void> | null = null;
454 const testLayer = VcsStatusBroadcaster.layer.pipe(
455 Layer.provideMerge(NodeServices.layer),
456 Layer.provide(
457 Layer.mock(GitWorkflowService.GitWorkflowService)({
458 localStatus: () =>
459 Effect.sync(() => {
460 state.localStatusCalls += 1;
461 return state.currentLocalStatus;
462 }),
463 remoteStatus: (_input, options) =>
464 Effect.suspend(() => {
465 state.remoteStatusCalls += 1;
466 state.remoteStatusRefreshUpstreamValues.push(options?.refreshUpstream);
467 if (state.remoteStatusCalls === 1) {
468 return Effect.fail(
469 new GitManagerError({
470 operation: "VcsStatusBroadcaster.test",
471 cwd: privateCwd,
472 detail: "private initial remote status failure",
473 cause: nestedCause,
474 }),
475 ).pipe(
476 Effect.ensuring(
477 firstRemoteAttemptDeferred
478 ? Deferred.succeed(firstRemoteAttemptDeferred, undefined).pipe(Effect.ignore)
479 : Effect.void,
480 ),
481 );
482 }
483 return Effect.succeed(remoteStatusWithPr);
484 }),
485 invalidateLocalStatus: () =>
486 Effect.sync(() => {
487 state.localInvalidationCalls += 1;
488 }),
489 invalidateRemoteStatus: () =>
490 Effect.sync(() => {
491 state.remoteInvalidationCalls += 1;
492 }),
493 }),
494 ),
495 );
496
497 return Effect.gen(function* () {
498 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
499 const scope = yield* Scope.make();
500 firstRemoteAttemptDeferred = yield* Deferred.make<void>();
501 const remoteUpdatedDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
502 yield* Stream.runForEach(
503 broadcaster.streamStatus(
504 { cwd: privateCwd },
505 { automaticRemoteRefreshInterval: Effect.succeed(Duration.zero) },
506 ),
507 (event) =>
508 event._tag === "remoteUpdated"
509 ? Deferred.succeed(remoteUpdatedDeferred, event).pipe(Effect.ignore)
510 : Effect.void,
511 ).pipe(Effect.forkIn(scope));
512
513 yield* Deferred.await(firstRemoteAttemptDeferred);
514 yield* Effect.yieldNow;
515 assert.equal(state.remoteStatusCalls, 1);
516 assert.deepStrictEqual(
517 messages.find((message) => message[0] === "VCS remote status refresh failed"),
518 [
519 "VCS remote status refresh failed",
520 {
521 cwdLength: privateCwd.length,
522 reasonCount: 1,
523 failureCount: 1,
524 failureTags: ["GitManagerError"],
525 failureOperations: ["VcsStatusBroadcaster.test"],
526 defectCount: 0,
527 defectTags: [],
528 interruptionCount: 0,
529 consecutiveFailures: 1,
530 nextDelayMs: 30_000,
531 },
532 ],
533 );
534
535 yield* TestClock.adjust(Duration.seconds(30));
536 const remoteUpdated = yield* Deferred.await(remoteUpdatedDeferred);
537
538 assert.deepStrictEqual(remoteUpdated, {
539 _tag: "remoteUpdated",
540 remote: remoteStatusWithPr,
541 } satisfies VcsStatusStreamEvent);
542 assert.equal(state.remoteStatusCalls, 2);
543 assert.equal(state.remoteInvalidationCalls, 0);
544 assert.deepStrictEqual(state.remoteStatusRefreshUpstreamValues, [false, false]);
545
546 yield* Scope.close(scope, Exit.void);
547 }).pipe(
548 Effect.provide(
549 Layer.mergeAll(
550 testLayer,
551 TestClock.layer(),
552 Logger.layer([logger], { mergeWithExisting: false }),
553 ),
554 ),
555 );
556 });
557
558 it.effect("delays automatic refresh when a cached remote snapshot is available", () => {
559 const state = {
560 currentLocalStatus: baseLocalStatus,
561 currentRemoteStatus: baseRemoteStatus,
562 localStatusCalls: 0,
563 remoteStatusCalls: 0,
564 localInvalidationCalls: 0,
565 remoteInvalidationCalls: 0,
566 };
567
568 return Effect.gen(function* () {
569 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
570 yield* broadcaster.getStatus({ cwd: "/repo" });
571 const scope = yield* Scope.make();
572 const snapshotDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
573 yield* Stream.runForEach(
574 broadcaster.streamStatus(
575 { cwd: "/repo" },
576 { automaticRemoteRefreshInterval: Effect.succeed(Duration.minutes(1)) },
577 ),
578 (event) =>
579 event._tag === "snapshot"
580 ? Deferred.succeed(snapshotDeferred, event).pipe(Effect.ignore)
581 : Effect.void,
582 ).pipe(Effect.forkIn(scope));
583
584 yield* Deferred.await(snapshotDeferred);
585 assert.equal(state.remoteStatusCalls, 1);
586 assert.equal(state.remoteInvalidationCalls, 0);
587
588 yield* TestClock.adjust(Duration.seconds(59));
589 assert.equal(state.remoteStatusCalls, 1);
590
591 yield* TestClock.adjust(Duration.seconds(1));
592 yield* Effect.yieldNow;
593 assert.equal(state.remoteStatusCalls, 2);
594 assert.equal(state.remoteInvalidationCalls, 1);
595
596 yield* Scope.close(scope, Exit.void);
597 }).pipe(Effect.provide(Layer.merge(makeTestLayer(state), TestClock.layer())));
598 });
599
600 it("backs off remote refresh failures exponentially and honors larger configured intervals", () => {
601 assert.equal(
602 Duration.toMillis(VcsStatusBroadcaster.remoteRefreshFailureDelay(1, Duration.seconds(1))),
603 30_000,
604 );
605 assert.equal(
606 Duration.toMillis(VcsStatusBroadcaster.remoteRefreshFailureDelay(2, Duration.seconds(1))),
607 60_000,
608 );
609 assert.equal(
610 Duration.toMillis(VcsStatusBroadcaster.remoteRefreshFailureDelay(3, Duration.seconds(1))),
611 120_000,
612 );
613 assert.equal(
614 Duration.toMillis(VcsStatusBroadcaster.remoteRefreshFailureDelay(1, Duration.minutes(5))),
615 300_000,
616 );
617 assert.equal(
618 Duration.toMillis(VcsStatusBroadcaster.remoteRefreshFailureDelay(20, Duration.seconds(1))),
619 900_000,
620 );
621 });
622
623 it("summarizes refresh causes without exposing nested failure details", () => {
624 const nestedCause = new Error("private nested failure detail");
625 const failure = new GitManagerError({
626 operation: "VcsStatusBroadcaster.remoteStatus",
627 cwd: "/private/user/workspace/repo",
628 detail: "private Git failure detail",
629 cause: nestedCause,
630 });
631 const cause = Cause.combine(Cause.fail(failure), Cause.die(new TypeError("private defect")));
632
633 assert.deepStrictEqual(VcsStatusBroadcaster.remoteRefreshFailureDiagnostics(cause), {
634 reasonCount: 2,
635 failureCount: 1,
636 failureTags: ["GitManagerError"],
637 failureOperations: ["VcsStatusBroadcaster.remoteStatus"],
638 defectCount: 1,
639 defectTags: ["TypeError"],
640 interruptionCount: 0,
641 });
642 });
643
644 it.effect("stops the remote poller after the last stream subscriber disconnects", () => {
645 const state = {
646 currentLocalStatus: baseLocalStatus,
647 currentRemoteStatus: baseRemoteStatus,
648 localStatusCalls: 0,
649 remoteStatusCalls: 0,
650 localInvalidationCalls: 0,
651 remoteInvalidationCalls: 0,
652 };
653 let remoteInterruptedDeferred: Deferred.Deferred<void, never> | null = null;
654 let remoteStartedDeferred: Deferred.Deferred<void, never> | null = null;
655 const testLayer = VcsStatusBroadcaster.layer.pipe(
656 Layer.provideMerge(NodeServices.layer),
657 Layer.provide(
658 Layer.mock(GitWorkflowService.GitWorkflowService)({
659 localStatus: () =>
660 Effect.sync(() => {
661 state.localStatusCalls += 1;
662 return state.currentLocalStatus;
663 }),
664 remoteStatus: () =>
665 Effect.sync(() => {
666 state.remoteStatusCalls += 1;
667 }).pipe(
668 Effect.andThen(
669 remoteStartedDeferred
670 ? Deferred.succeed(remoteStartedDeferred, undefined).pipe(Effect.ignore)
671 : Effect.void,
672 ),
673 Effect.andThen(Effect.never as Effect.Effect<VcsStatusRemoteResult | null, never>),
674 Effect.onInterrupt(() =>
675 remoteInterruptedDeferred
676 ? Deferred.succeed(remoteInterruptedDeferred, undefined).pipe(Effect.ignore)
677 : Effect.void,
678 ),
679 ),
680 invalidateLocalStatus: () =>
681 Effect.sync(() => {
682 state.localInvalidationCalls += 1;
683 }),
684 invalidateRemoteStatus: () =>
685 Effect.sync(() => {
686 state.remoteInvalidationCalls += 1;
687 }),
688 } satisfies Partial<GitWorkflowService.GitWorkflowService["Service"]>),
689 ),
690 );
691
692 return Effect.gen(function* () {
693 const remoteInterrupted = yield* Deferred.make<void>();
694 const remoteStarted = yield* Deferred.make<void>();
695 remoteInterruptedDeferred = remoteInterrupted;
696 remoteStartedDeferred = remoteStarted;
697
698 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
699 const firstSnapshot = yield* Deferred.make<VcsStatusStreamEvent>();
700 const secondSnapshot = yield* Deferred.make<VcsStatusStreamEvent>();
701 const firstScope = yield* Scope.make();
702 const secondScope = yield* Scope.make();
703 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
704 event._tag === "snapshot"
705 ? Deferred.succeed(firstSnapshot, event).pipe(Effect.ignore)
706 : Effect.void,
707 ).pipe(Effect.forkIn(firstScope));
708 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
709 event._tag === "snapshot"
710 ? Deferred.succeed(secondSnapshot, event).pipe(Effect.ignore)
711 : Effect.void,
712 ).pipe(Effect.forkIn(secondScope));
713
714 yield* Deferred.await(firstSnapshot);
715 yield* Deferred.await(secondSnapshot);
716 yield* Deferred.await(remoteStarted);
717
718 assert.equal(state.remoteStatusCalls, 1);
719
720 yield* Scope.close(firstScope, Exit.void);
721 assert.isTrue(Option.isNone(yield* Deferred.poll(remoteInterrupted)));
722
723 yield* Scope.close(secondScope, Exit.void).pipe(Effect.forkScoped);
724 yield* Deferred.await(remoteInterrupted);
725 assert.isTrue(Option.isSome(yield* Deferred.poll(remoteInterrupted)));
726 }).pipe(Effect.provide(testLayer));
727 });
728});