Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

VcsStatusBroadcaster.test.ts

718 lines25.6 KB

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