Open History to see the last commit
1import {
2 EnvironmentId,
3 WS_METHODS,
4 type VcsListRefsInput,
5 type VcsListRefsResult,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "@effect/vitest";
8import * as Effect from "effect/Effect";
9import * as Fiber from "effect/Fiber";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12import * as Ref from "effect/Ref";
13import * as Stream from "effect/Stream";
14import * as SubscriptionRef from "effect/SubscriptionRef";
15import * as TestClock from "effect/testing/TestClock";
16import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
17
18import {
19 AVAILABLE_CONNECTION_STATE,
20 PrimaryConnectionTarget,
21 type PreparedConnection,
22 type SupervisorConnectionState,
23} from "../connection/model.ts";
24import * as EnvironmentRegistry from "../connection/registry.ts";
25import * as EnvironmentSupervisor from "../connection/supervisor.ts";
26import * as Persistence from "../platform/persistence.ts";
27import { EnvironmentRpcUnavailableError } from "../rpc/client.ts";
28import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
29import type { RpcSession } from "../rpc/session.ts";
30
31import {
32 commitVcsRefsRefresh,
33 createVcsEnvironmentAtoms,
34 makeCachedVcsRefsChanges,
35} from "./vcs.ts";
36import {
37 invalidateCachedVcsRefs,
38 invalidateVcsRefs,
39 vcsRefsCacheStateAtom,
40} from "./vcsRefInvalidation.ts";
41
42const TARGET = new PrimaryConnectionTarget({
43 environmentId: EnvironmentId.make("environment-1"),
44 label: "Test environment",
45 httpBaseUrl: "https://environment.example.test",
46 wsBaseUrl: "wss://environment.example.test",
47});
48
49const CONNECTED_CONNECTION_STATE: SupervisorConnectionState = {
50 ...AVAILABLE_CONNECTION_STATE,
51 desired: true,
52 network: "online",
53 phase: "connected",
54 attempt: 1,
55 generation: 1,
56};
57
58const CACHED_REFS: VcsListRefsResult = {
59 refs: [
60 {
61 name: "main",
62 current: true,
63 isDefault: true,
64 worktreePath: "/repo",
65 },
66 ],
67 isRepo: true,
68 hasPrimaryRemote: true,
69 nextCursor: null,
70 totalCount: 1,
71};
72
73const LIVE_REFS: VcsListRefsResult = {
74 ...CACHED_REFS,
75 refs: [
76 {
77 name: "release",
78 current: true,
79 isDefault: true,
80 worktreePath: "/repo",
81 },
82 ],
83};
84
85function session(client: WsRpcProtocolClient): RpcSession {
86 return {
87 client,
88 initialConfig: Effect.never,
89 subscribeServerConfig: (input) => client.subscribeServerConfig(input),
90 ready: Effect.void,
91 probe: Effect.void,
92 closed: Effect.never,
93 };
94}
95
96function cacheWithRefs(
97 refs: Option.Option<VcsListRefsResult>,
98 overrides: Partial<Persistence.EnvironmentCacheStore["Service"]> = {},
99) {
100 return Persistence.EnvironmentCacheStore.of({
101 loadShell: () => Effect.succeedNone,
102 saveShell: () => Effect.void,
103 loadThread: () => Effect.succeedNone,
104 saveThread: () => Effect.void,
105 removeThread: () => Effect.void,
106 loadServerConfig: () => Effect.succeedNone,
107 saveServerConfig: () => Effect.void,
108 loadVcsRefs: () => Effect.succeed(refs),
109 saveVcsRefs: () => Effect.void,
110 removeVcsRefs: () => Effect.void,
111 clearVcsRefs: () => Effect.void,
112 clear: () => Effect.void,
113 ...overrides,
114 });
115}
116
117describe("cached VCS refs", () => {
118 it("invalidates all ref streams in the mutated environment", () => {
119 const registry = AtomRegistry.make();
120 const environment = {
121 environmentId: TARGET.environmentId,
122 };
123 const otherEnvironment = {
124 environmentId: EnvironmentId.make("environment-2"),
125 };
126
127 expect(registry.get(vcsRefsCacheStateAtom(environment))).toEqual({
128 revision: 0,
129 persistedCacheReadable: true,
130 });
131 expect(registry.get(vcsRefsCacheStateAtom(otherEnvironment))).toEqual({
132 revision: 0,
133 persistedCacheReadable: true,
134 });
135
136 invalidateVcsRefs(registry, environment);
137
138 expect(registry.get(vcsRefsCacheStateAtom(environment))).toEqual({
139 revision: 1,
140 persistedCacheReadable: true,
141 });
142 expect(registry.get(vcsRefsCacheStateAtom(otherEnvironment))).toEqual({
143 revision: 0,
144 persistedCacheReadable: true,
145 });
146 registry.dispose();
147 });
148
149 it.effect("preserves the caller's repository snapshot refresh policy", () =>
150 Effect.scoped(
151 Effect.gen(function* () {
152 const requests = yield* Ref.make<ReadonlyArray<VcsListRefsInput>>([]);
153 const client = {
154 [WS_METHODS.vcsListRefs]: (input: VcsListRefsInput) =>
155 Ref.update(requests, (current) => [...current, input]).pipe(Effect.as(LIVE_REFS)),
156 } as unknown as WsRpcProtocolClient;
157 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
158 target: TARGET,
159 state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
160 session: yield* SubscriptionRef.make(Option.some(session(client))),
161 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
162 connect: Effect.void,
163 disconnect: Effect.void,
164 retryNow: Effect.void,
165 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
166
167 yield* Stream.unwrap(
168 makeCachedVcsRefsChanges({
169 cwd: "/repo",
170 limit: 20,
171 query: "release",
172 refKind: "remote",
173 }).pipe(
174 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
175 Effect.provideService(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())),
176 ),
177 ).pipe(Stream.runHead);
178
179 expect(yield* Ref.get(requests)).toEqual([
180 {
181 cwd: "/repo",
182 limit: 20,
183 query: "release",
184 refKind: "remote",
185 },
186 ]);
187
188 yield* Stream.unwrap(
189 makeCachedVcsRefsChanges({
190 cwd: "/repo",
191 limit: 20,
192 query: "release",
193 refKind: "remote",
194 refresh: true,
195 }).pipe(
196 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
197 Effect.provideService(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())),
198 ),
199 ).pipe(Stream.runHead);
200
201 expect(yield* Ref.get(requests)).toEqual([
202 {
203 cwd: "/repo",
204 limit: 20,
205 query: "release",
206 refKind: "remote",
207 },
208 {
209 cwd: "/repo",
210 limit: 20,
211 query: "release",
212 refKind: "remote",
213 refresh: true,
214 },
215 ]);
216 }),
217 ),
218 );
219
220 it.effect("does not repersist a refresh superseded by ref invalidation", () =>
221 Effect.scoped(
222 Effect.gen(function* () {
223 const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) =>
224 Effect.sync(() => registry.dispose()),
225 );
226 const saved = yield* Ref.make<ReadonlyArray<VcsListRefsResult>>([]);
227 const clears = yield* Ref.make(0);
228 const revisionsObservedDuringClear = yield* Ref.make<ReadonlyArray<number>>([]);
229 const cache = cacheWithRefs(Option.none(), {
230 saveVcsRefs: (_environmentId, _cwd, refs) =>
231 Ref.update(saved, (current) => [...current, refs]),
232 clearVcsRefs: () =>
233 Effect.all([
234 Ref.update(clears, (count) => count + 1),
235 Ref.update(revisionsObservedDuringClear, (current) => [
236 ...current,
237 registry.get(vcsRefsCacheStateAtom(TARGET)).revision,
238 ]),
239 ]).pipe(Effect.asVoid),
240 });
241
242 yield* invalidateCachedVcsRefs(registry, {
243 environmentId: TARGET.environmentId,
244 cwd: "/repo-worktree",
245 }).pipe(Effect.provideService(Persistence.EnvironmentCacheStore, cache));
246
247 expect(
248 yield* commitVcsRefsRefresh(registry, cache, {
249 environmentId: TARGET.environmentId,
250 cwd: "/repo",
251 refs: CACHED_REFS,
252 expectedRevision: 0,
253 persist: true,
254 }),
255 ).toBe(false);
256
257 expect(yield* Ref.get(saved)).toEqual([]);
258 expect(yield* Ref.get(clears)).toBe(1);
259 expect(yield* Ref.get(revisionsObservedDuringClear)).toEqual([0]);
260 expect(registry.get(vcsRefsCacheStateAtom(TARGET))).toEqual({
261 revision: 1,
262 persistedCacheReadable: true,
263 });
264
265 expect(
266 yield* commitVcsRefsRefresh(registry, cache, {
267 environmentId: TARGET.environmentId,
268 cwd: "/repo",
269 refs: LIVE_REFS,
270 expectedRevision: 1,
271 persist: true,
272 }),
273 ).toBe(true);
274 expect(yield* Ref.get(saved)).toEqual([LIVE_REFS]);
275 }),
276 ),
277 );
278
279 it.effect("invalidates persisted refs when ref-affecting commands settle", () =>
280 Effect.scoped(
281 Effect.gen(function* () {
282 const expectedError = new EnvironmentRpcUnavailableError({
283 environmentId: TARGET.environmentId,
284 message: "pull failed after fetching refs",
285 });
286 const client = {
287 [WS_METHODS.vcsPull]: () => Effect.fail(expectedError),
288 [WS_METHODS.vcsRefreshStatus]: () => Effect.void,
289 } as unknown as WsRpcProtocolClient;
290 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
291 target: TARGET,
292 state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
293 session: yield* SubscriptionRef.make(Option.some(session(client))),
294 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
295 connect: Effect.void,
296 disconnect: Effect.void,
297 retryNow: Effect.void,
298 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
299 const run: EnvironmentRegistry.EnvironmentRegistry["Service"]["run"] = (
300 _environmentId,
301 effect,
302 ) => Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor);
303 const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({
304 run,
305 } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);
306 const clears = yield* Ref.make(0);
307 const runtime = Atom.runtime(
308 Layer.merge(
309 Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
310 Layer.succeed(
311 Persistence.EnvironmentCacheStore,
312 cacheWithRefs(Option.none(), {
313 clearVcsRefs: () => Ref.update(clears, (count) => count + 1),
314 }),
315 ),
316 ),
317 );
318 const atoms = createVcsEnvironmentAtoms(runtime);
319 const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) =>
320 Effect.sync(() => registry.dispose()),
321 );
322
323 const result = yield* Effect.promise(() =>
324 atoms.pull.run(registry, {
325 environmentId: TARGET.environmentId,
326 input: { cwd: "/repo" },
327 }),
328 );
329
330 expect(AsyncResult.isFailure(result)).toBe(true);
331 expect(yield* Ref.get(clears)).toBe(1);
332 expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(1);
333
334 const refreshResult = yield* Effect.promise(() =>
335 atoms.refreshStatus.run(registry, {
336 environmentId: TARGET.environmentId,
337 input: { cwd: "/repo" },
338 }),
339 );
340
341 expect(AsyncResult.isSuccess(refreshResult)).toBe(true);
342 expect(yield* Ref.get(clears)).toBe(2);
343 expect(registry.get(vcsRefsCacheStateAtom(TARGET)).revision).toBe(2);
344 }),
345 ),
346 );
347
348 it.effect("suppresses persisted snapshots after an environment-wide clear fails", () =>
349 Effect.scoped(
350 Effect.gen(function* () {
351 const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) =>
352 Effect.sync(() => registry.dispose()),
353 );
354 const persisted = yield* Ref.make<Option.Option<VcsListRefsResult>>(
355 Option.some(CACHED_REFS),
356 );
357 const loads = yield* Ref.make(0);
358 const clearAttempts = yield* Ref.make(0);
359 const cache = cacheWithRefs(Option.none(), {
360 loadVcsRefs: () =>
361 Ref.update(loads, (count) => count + 1).pipe(Effect.andThen(Ref.get(persisted))),
362 saveVcsRefs: (_environmentId, _cwd, refs) => Ref.set(persisted, Option.some(refs)),
363 clearVcsRefs: () =>
364 Ref.updateAndGet(clearAttempts, (count) => count + 1).pipe(
365 Effect.flatMap((attempt) =>
366 attempt === 1
367 ? Effect.fail(
368 new Persistence.ConnectionPersistenceError({
369 operation: "clear-vcs-refs",
370 message: "storage unavailable",
371 }),
372 )
373 : Ref.set(persisted, Option.none()),
374 ),
375 ),
376 });
377
378 yield* invalidateCachedVcsRefs(registry, {
379 environmentId: TARGET.environmentId,
380 cwd: "/repo",
381 }).pipe(Effect.provideService(Persistence.EnvironmentCacheStore, cache));
382
383 const state = registry.get(vcsRefsCacheStateAtom(TARGET));
384 expect(state).toEqual({
385 revision: 1,
386 persistedCacheReadable: false,
387 });
388
389 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
390 target: TARGET,
391 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
392 session: yield* SubscriptionRef.make(Option.none<RpcSession>()),
393 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
394 connect: Effect.void,
395 disconnect: Effect.void,
396 retryNow: Effect.void,
397 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
398 const refs = yield* Stream.unwrap(
399 makeCachedVcsRefsChanges(
400 { cwd: "/repo", limit: 100 },
401 state.revision,
402 registry,
403 state.persistedCacheReadable,
404 ).pipe(
405 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
406 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
407 ),
408 ).pipe(Stream.runHead, Effect.forkChild({ startImmediately: true }));
409
410 yield* Effect.yieldNow;
411 expect(yield* Ref.get(loads)).toBe(0);
412 yield* Fiber.interrupt(refs);
413 expect(registry.get(vcsRefsCacheStateAtom(TARGET))).toEqual(state);
414
415 expect(
416 yield* commitVcsRefsRefresh(registry, cache, {
417 environmentId: TARGET.environmentId,
418 cwd: "/repo",
419 refs: LIVE_REFS,
420 expectedRevision: state.revision,
421 persist: true,
422 }),
423 ).toBe(true);
424 const recoveredState = registry.get(vcsRefsCacheStateAtom(TARGET));
425 expect(recoveredState).toEqual({
426 revision: 1,
427 persistedCacheReadable: true,
428 });
429 expect(yield* Ref.get(clearAttempts)).toBe(2);
430
431 const recoveredRefs = yield* Stream.unwrap(
432 makeCachedVcsRefsChanges(
433 { cwd: "/repo", limit: 100 },
434 recoveredState.revision,
435 registry,
436 recoveredState.persistedCacheReadable,
437 ).pipe(
438 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
439 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
440 ),
441 ).pipe(Stream.runHead);
442
443 expect(Option.getOrThrow(recoveredRefs)).toEqual(LIVE_REFS);
444 expect(yield* Ref.get(loads)).toBe(1);
445 }),
446 ),
447 );
448
449 it.effect("loads an unfiltered branch list without a connection", () =>
450 Effect.scoped(
451 Effect.gen(function* () {
452 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
453 target: TARGET,
454 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
455 session: yield* SubscriptionRef.make(Option.none<RpcSession>()),
456 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
457 connect: Effect.void,
458 disconnect: Effect.void,
459 retryNow: Effect.void,
460 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
461 const refs = yield* Stream.unwrap(
462 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
463 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
464 Effect.provideService(
465 Persistence.EnvironmentCacheStore,
466 cacheWithRefs(Option.some(CACHED_REFS)),
467 ),
468 ),
469 ).pipe(Stream.runHead);
470
471 expect(Option.getOrThrow(refs)).toEqual(CACHED_REFS);
472 }),
473 ),
474 );
475
476 it.effect("retries a transient live failure while connected", () =>
477 Effect.scoped(
478 Effect.gen(function* () {
479 const expectedError = new Error("Could not list Git refs.");
480 const calls = yield* Ref.make(0);
481 const connectionState = yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE);
482 const client = {
483 [WS_METHODS.vcsListRefs]: () =>
484 Ref.updateAndGet(calls, (count) => count + 1).pipe(
485 Effect.flatMap((count) =>
486 count === 1 ? Effect.fail(expectedError) : Effect.succeed(LIVE_REFS),
487 ),
488 ),
489 } as unknown as WsRpcProtocolClient;
490 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
491 target: TARGET,
492 state: connectionState,
493 session: yield* SubscriptionRef.make(Option.some(session(client))),
494 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
495 connect: Effect.void,
496 disconnect: Effect.void,
497 retryNow: Effect.void,
498 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
499
500 const result = Stream.unwrap(
501 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
502 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
503 Effect.provideService(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())),
504 ),
505 ).pipe(Stream.runHead);
506 const fiber = yield* Effect.forkChild(result);
507
508 for (let attempt = 0; attempt < 100 && (yield* Ref.get(calls)) < 1; attempt += 1) {
509 yield* Effect.yieldNow;
510 }
511 expect(yield* Ref.get(calls)).toBe(1);
512
513 yield* TestClock.adjust("1 second");
514 expect(Option.getOrThrow(yield* Fiber.join(fiber))).toEqual(LIVE_REFS);
515 expect(yield* Ref.get(calls)).toBe(2);
516 }).pipe(Effect.provide(TestClock.layer())),
517 ),
518 );
519
520 it.effect("cancels an in-flight refresh when the connection generation changes", () =>
521 Effect.scoped(
522 Effect.gen(function* () {
523 const calls = yield* Ref.make(0);
524 const interruptions = yield* Ref.make(0);
525 const connectionState = yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE);
526 const client = {
527 [WS_METHODS.vcsListRefs]: () =>
528 Ref.updateAndGet(calls, (count) => count + 1).pipe(
529 Effect.flatMap((count) =>
530 count === 1
531 ? Effect.never.pipe(
532 Effect.onInterrupt(() =>
533 Ref.update(interruptions, (interruptions) => interruptions + 1),
534 ),
535 )
536 : Effect.succeed(LIVE_REFS),
537 ),
538 ),
539 } as unknown as WsRpcProtocolClient;
540 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
541 target: TARGET,
542 state: connectionState,
543 session: yield* SubscriptionRef.make(Option.some(session(client))),
544 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
545 connect: Effect.void,
546 disconnect: Effect.void,
547 retryNow: Effect.void,
548 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
549
550 const result = Stream.unwrap(
551 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
552 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
553 Effect.provideService(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())),
554 ),
555 ).pipe(Stream.runHead);
556 const fiber = yield* Effect.forkChild(result);
557
558 for (let attempt = 0; attempt < 100 && (yield* Ref.get(calls)) < 1; attempt += 1) {
559 yield* Effect.yieldNow;
560 }
561 yield* SubscriptionRef.set(connectionState, AVAILABLE_CONNECTION_STATE);
562 for (let attempt = 0; attempt < 100 && (yield* Ref.get(interruptions)) < 1; attempt += 1) {
563 yield* Effect.yieldNow;
564 }
565 expect(yield* Ref.get(interruptions)).toBe(1);
566
567 yield* SubscriptionRef.set(connectionState, {
568 ...CONNECTED_CONNECTION_STATE,
569 generation: 2,
570 });
571 expect(Option.getOrThrow(yield* Fiber.join(fiber))).toEqual(LIVE_REFS);
572 expect(yield* Ref.get(calls)).toBe(2);
573 }),
574 ),
575 );
576
577 it.effect("does not poll refs while the connection remains stable", () =>
578 Effect.scoped(
579 Effect.gen(function* () {
580 const calls = yield* Ref.make(0);
581 const client = {
582 [WS_METHODS.vcsListRefs]: () =>
583 Ref.update(calls, (count) => count + 1).pipe(Effect.as(CACHED_REFS)),
584 } as unknown as WsRpcProtocolClient;
585 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
586 target: TARGET,
587 state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
588 session: yield* SubscriptionRef.make(Option.some(session(client))),
589 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
590 connect: Effect.void,
591 disconnect: Effect.void,
592 retryNow: Effect.void,
593 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
594 const stream = Stream.unwrap(
595 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
596 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
597 Effect.provideService(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())),
598 ),
599 ).pipe(Stream.runDrain);
600 const fiber = yield* Effect.forkChild(stream);
601
602 for (let attempt = 0; attempt < 100 && (yield* Ref.get(calls)) < 1; attempt += 1) {
603 yield* Effect.yieldNow;
604 }
605 expect(yield* Ref.get(calls)).toBe(1);
606
607 yield* TestClock.adjust("1 minute");
608 yield* Effect.yieldNow;
609 expect(yield* Ref.get(calls)).toBe(1);
610 yield* Fiber.interrupt(fiber);
611 }).pipe(Effect.provide(TestClock.layer())),
612 ),
613 );
614
615 it.effect("emits persisted refs before a live refresh", () =>
616 Effect.scoped(
617 Effect.gen(function* () {
618 const client = {
619 [WS_METHODS.vcsListRefs]: () => Effect.succeed(LIVE_REFS),
620 } as unknown as WsRpcProtocolClient;
621 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
622 target: TARGET,
623 state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
624 session: yield* SubscriptionRef.make(Option.some(session(client))),
625 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
626 connect: Effect.void,
627 disconnect: Effect.void,
628 retryNow: Effect.void,
629 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
630
631 const refs = yield* Stream.unwrap(
632 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
633 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
634 Effect.provideService(
635 Persistence.EnvironmentCacheStore,
636 cacheWithRefs(Option.some(CACHED_REFS)),
637 ),
638 ),
639 ).pipe(Stream.take(2), Stream.runCollect);
640
641 expect(refs).toEqual([CACHED_REFS, LIVE_REFS]);
642 }),
643 ),
644 );
645});