Open History to see the last commit
1import { EnvironmentId, WS_METHODS, type VcsListRefsResult } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Fiber from "effect/Fiber";
5import * as Option from "effect/Option";
6import * as Ref from "effect/Ref";
7import * as Stream from "effect/Stream";
8import * as SubscriptionRef from "effect/SubscriptionRef";
9import * as TestClock from "effect/testing/TestClock";
10
11import {
12 AVAILABLE_CONNECTION_STATE,
13 PrimaryConnectionTarget,
14 type PreparedConnection,
15 type SupervisorConnectionState,
16} from "../connection/model.ts";
17import * as EnvironmentSupervisor from "../connection/supervisor.ts";
18import * as Persistence from "../platform/persistence.ts";
19import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
20import type { RpcSession } from "../rpc/session.ts";
21import { makeCachedVcsRefsChanges } from "./vcs.ts";
22
23const TARGET = new PrimaryConnectionTarget({
24 environmentId: EnvironmentId.make("environment-1"),
25 label: "Test environment",
26 httpBaseUrl: "https://environment.example.test",
27 wsBaseUrl: "wss://environment.example.test",
28});
29
30const CONNECTED_CONNECTION_STATE: SupervisorConnectionState = {
31 ...AVAILABLE_CONNECTION_STATE,
32 desired: true,
33 network: "online",
34 phase: "connected",
35 attempt: 1,
36 generation: 1,
37};
38
39const CACHED_REFS: VcsListRefsResult = {
40 refs: [
41 {
42 name: "main",
43 current: true,
44 isDefault: true,
45 worktreePath: "/repo",
46 },
47 ],
48 isRepo: true,
49 hasPrimaryRemote: true,
50 nextCursor: null,
51 totalCount: 1,
52};
53
54const LIVE_REFS: VcsListRefsResult = {
55 ...CACHED_REFS,
56 refs: [
57 {
58 name: "release",
59 current: true,
60 isDefault: true,
61 worktreePath: "/repo",
62 },
63 ],
64};
65
66function session(client: WsRpcProtocolClient): RpcSession {
67 return {
68 client,
69 initialConfig: Effect.never,
70 ready: Effect.void,
71 probe: Effect.void,
72 closed: Effect.never,
73 };
74}
75
76function cacheWithRefs(refs: Option.Option<VcsListRefsResult>) {
77 return Persistence.EnvironmentCacheStore.of({
78 loadShell: () => Effect.succeed(Option.none()),
79 saveShell: () => Effect.void,
80 loadThread: () => Effect.succeed(Option.none()),
81 saveThread: () => Effect.void,
82 removeThread: () => Effect.void,
83 loadServerConfig: () => Effect.succeed(Option.none()),
84 saveServerConfig: () => Effect.void,
85 loadVcsRefs: () => Effect.succeed(refs),
86 saveVcsRefs: () => Effect.void,
87 clear: () => Effect.void,
88 });
89}
90
91describe("cached VCS refs", () => {
92 it.effect("loads an unfiltered branch list without a connection", () =>
93 Effect.scoped(
94 Effect.gen(function* () {
95 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
96 target: TARGET,
97 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
98 session: yield* SubscriptionRef.make(Option.none<RpcSession>()),
99 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
100 connect: Effect.void,
101 disconnect: Effect.void,
102 retryNow: Effect.void,
103 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
104 const refs = yield* Stream.unwrap(
105 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
106 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
107 Effect.provideService(
108 Persistence.EnvironmentCacheStore,
109 cacheWithRefs(Option.some(CACHED_REFS)),
110 ),
111 ),
112 ).pipe(Stream.runHead);
113
114 expect(Option.getOrThrow(refs)).toEqual(CACHED_REFS);
115 }),
116 ),
117 );
118
119 it.effect("continues polling after a transient live failure", () =>
120 Effect.scoped(
121 Effect.gen(function* () {
122 const expectedError = new Error("Could not list Git refs.");
123 const calls = yield* Ref.make(0);
124 const client = {
125 [WS_METHODS.vcsListRefs]: () =>
126 Ref.updateAndGet(calls, (count) => count + 1).pipe(
127 Effect.flatMap((count) =>
128 count === 1 ? Effect.fail(expectedError) : Effect.succeed(LIVE_REFS),
129 ),
130 ),
131 } as unknown as WsRpcProtocolClient;
132 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
133 target: TARGET,
134 state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
135 session: yield* SubscriptionRef.make(Option.some(session(client))),
136 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
137 connect: Effect.void,
138 disconnect: Effect.void,
139 retryNow: Effect.void,
140 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
141
142 const result = Stream.unwrap(
143 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
144 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
145 Effect.provideService(
146 Persistence.EnvironmentCacheStore,
147 cacheWithRefs(Option.some(CACHED_REFS)),
148 ),
149 ),
150 ).pipe(Stream.runHead);
151 const fiber = yield* Effect.forkChild(result);
152
153 for (let attempt = 0; attempt < 100 && (yield* Ref.get(calls)) < 1; attempt += 1) {
154 yield* Effect.yieldNow;
155 }
156 expect(yield* Ref.get(calls)).toBe(1);
157
158 yield* TestClock.adjust("5 seconds");
159 expect(Option.getOrThrow(yield* Fiber.join(fiber))).toEqual(LIVE_REFS);
160 }).pipe(Effect.provide(TestClock.layer())),
161 ),
162 );
163
164 it.effect("revalidates connected refs every five seconds", () =>
165 Effect.scoped(
166 Effect.gen(function* () {
167 const calls = yield* Ref.make(0);
168 const client = {
169 [WS_METHODS.vcsListRefs]: () =>
170 Ref.updateAndGet(calls, (count) => count + 1).pipe(
171 Effect.map((count) => (count === 1 ? CACHED_REFS : LIVE_REFS)),
172 ),
173 } as unknown as WsRpcProtocolClient;
174 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
175 target: TARGET,
176 state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
177 session: yield* SubscriptionRef.make(Option.some(session(client))),
178 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
179 connect: Effect.void,
180 disconnect: Effect.void,
181 retryNow: Effect.void,
182 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
183 const results = Stream.unwrap(
184 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
185 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
186 Effect.provideService(Persistence.EnvironmentCacheStore, cacheWithRefs(Option.none())),
187 ),
188 ).pipe(Stream.take(2), Stream.runCollect);
189 const fiber = yield* Effect.forkChild(results);
190
191 for (let attempt = 0; attempt < 100 && (yield* Ref.get(calls)) < 1; attempt += 1) {
192 yield* Effect.yieldNow;
193 }
194 expect(yield* Ref.get(calls)).toBe(1);
195
196 yield* TestClock.adjust("5 seconds");
197 expect(Array.from(yield* Fiber.join(fiber))).toEqual([CACHED_REFS, LIVE_REFS]);
198 }).pipe(Effect.provide(TestClock.layer())),
199 ),
200 );
201
202 it.effect("does not emit persisted refs before a live refresh", () =>
203 Effect.scoped(
204 Effect.gen(function* () {
205 const client = {
206 [WS_METHODS.vcsListRefs]: () => Effect.succeed(LIVE_REFS),
207 } as unknown as WsRpcProtocolClient;
208 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
209 target: TARGET,
210 state: yield* SubscriptionRef.make(CONNECTED_CONNECTION_STATE),
211 session: yield* SubscriptionRef.make(Option.some(session(client))),
212 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
213 connect: Effect.void,
214 disconnect: Effect.void,
215 retryNow: Effect.void,
216 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
217
218 const refs = yield* Stream.unwrap(
219 makeCachedVcsRefsChanges({ cwd: "/repo", limit: 100 }).pipe(
220 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
221 Effect.provideService(
222 Persistence.EnvironmentCacheStore,
223 cacheWithRefs(Option.some(CACHED_REFS)),
224 ),
225 ),
226 ).pipe(Stream.runHead);
227
228 expect(Option.getOrThrow(refs)).toEqual(LIVE_REFS);
229 }),
230 ),
231 );
232});