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

vcs.test.ts

666 lines23.9 KB

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