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

vcs.test.ts

644 lines23.6 KB

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