Open History to see the last commit
1import { assert, it, describe } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { Deferred, Effect, Exit, FileSystem, Layer, Option, Path, Scope, Stream } from "effect";
4import type {
5 VcsStatusLocalResult,
6 VcsStatusRemoteResult,
7 VcsStatusResult,
8 VcsStatusStreamEvent,
9} from "@t3tools/contracts";
10
11import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts";
12import * as GitWorkflowService from "../git/GitWorkflowService.ts";
13
14const baseLocalStatus: VcsStatusLocalResult = {
15 isRepo: true,
16 sourceControlProvider: {
17 kind: "github",
18 name: "GitHub",
19 baseUrl: "https://github.com",
20 },
21 hasPrimaryRemote: true,
22 isDefaultRef: false,
23 refName: "feature/status-broadcast",
24 hasWorkingTreeChanges: false,
25 workingTree: { files: [], insertions: 0, deletions: 0 },
26};
27
28const baseRemoteStatus: VcsStatusRemoteResult = {
29 hasUpstream: true,
30 aheadCount: 0,
31 behindCount: 0,
32 pr: null,
33};
34
35const baseStatus: VcsStatusResult = {
36 ...baseLocalStatus,
37 ...baseRemoteStatus,
38};
39
40function makeTestLayer(state: {
41 currentLocalStatus: VcsStatusLocalResult;
42 currentRemoteStatus: VcsStatusRemoteResult | null;
43 localStatusCalls: number;
44 remoteStatusCalls: number;
45 localInvalidationCalls: number;
46 remoteInvalidationCalls: number;
47}) {
48 return VcsStatusBroadcaster.layer.pipe(
49 Layer.provide(
50 Layer.mock(GitWorkflowService.GitWorkflowService)({
51 localStatus: () =>
52 Effect.sync(() => {
53 state.localStatusCalls += 1;
54 return state.currentLocalStatus;
55 }),
56 remoteStatus: () =>
57 Effect.sync(() => {
58 state.remoteStatusCalls += 1;
59 return state.currentRemoteStatus;
60 }),
61 invalidateLocalStatus: () =>
62 Effect.sync(() => {
63 state.localInvalidationCalls += 1;
64 }),
65 invalidateRemoteStatus: () =>
66 Effect.sync(() => {
67 state.remoteInvalidationCalls += 1;
68 }),
69 }),
70 ),
71 );
72}
73
74describe("VcsStatusBroadcaster", () => {
75 it.effect("reuses the cached VCS status across repeated reads", () => {
76 const state = {
77 currentLocalStatus: baseLocalStatus,
78 currentRemoteStatus: baseRemoteStatus,
79 localStatusCalls: 0,
80 remoteStatusCalls: 0,
81 localInvalidationCalls: 0,
82 remoteInvalidationCalls: 0,
83 };
84
85 return Effect.gen(function* () {
86 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
87
88 const first = yield* broadcaster.getStatus({ cwd: "/repo" });
89 const second = yield* broadcaster.getStatus({ cwd: "/repo" });
90
91 assert.deepStrictEqual(first, baseStatus);
92 assert.deepStrictEqual(second, baseStatus);
93 assert.equal(state.localStatusCalls, 1);
94 assert.equal(state.remoteStatusCalls, 1);
95 assert.equal(state.localInvalidationCalls, 0);
96 assert.equal(state.remoteInvalidationCalls, 0);
97 }).pipe(Effect.provide(makeTestLayer(state)));
98 });
99
100 it.effect("refreshes the cached snapshot after explicit invalidation", () => {
101 const state = {
102 currentLocalStatus: baseLocalStatus,
103 currentRemoteStatus: baseRemoteStatus,
104 localStatusCalls: 0,
105 remoteStatusCalls: 0,
106 localInvalidationCalls: 0,
107 remoteInvalidationCalls: 0,
108 };
109
110 return Effect.gen(function* () {
111 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
112 const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
113
114 state.currentLocalStatus = {
115 ...baseLocalStatus,
116 refName: "feature/updated-status",
117 };
118 state.currentRemoteStatus = {
119 ...baseRemoteStatus,
120 aheadCount: 2,
121 };
122 const refreshed = yield* broadcaster.refreshStatus("/repo");
123 const cached = yield* broadcaster.getStatus({ cwd: "/repo" });
124
125 assert.deepStrictEqual(initial, baseStatus);
126 assert.deepStrictEqual(refreshed, {
127 ...state.currentLocalStatus,
128 ...state.currentRemoteStatus,
129 });
130 assert.deepStrictEqual(cached, {
131 ...state.currentLocalStatus,
132 ...state.currentRemoteStatus,
133 });
134 assert.equal(state.localStatusCalls, 2);
135 assert.equal(state.remoteStatusCalls, 2);
136 assert.equal(state.localInvalidationCalls, 1);
137 assert.equal(state.remoteInvalidationCalls, 1);
138 }).pipe(Effect.provide(makeTestLayer(state)));
139 });
140
141 it.effect("refreshes only the cached local snapshot when requested", () => {
142 const state = {
143 currentLocalStatus: baseLocalStatus,
144 currentRemoteStatus: baseRemoteStatus,
145 localStatusCalls: 0,
146 remoteStatusCalls: 0,
147 localInvalidationCalls: 0,
148 remoteInvalidationCalls: 0,
149 };
150
151 return Effect.gen(function* () {
152 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
153 const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
154
155 state.currentLocalStatus = {
156 ...baseLocalStatus,
157 refName: "feature/local-only-refresh",
158 hasWorkingTreeChanges: true,
159 };
160
161 const refreshedLocal = yield* broadcaster.refreshLocalStatus("/repo");
162 const cached = yield* broadcaster.getStatus({ cwd: "/repo" });
163
164 assert.deepStrictEqual(initial, baseStatus);
165 assert.deepStrictEqual(refreshedLocal, state.currentLocalStatus);
166 assert.deepStrictEqual(cached, {
167 ...state.currentLocalStatus,
168 ...baseRemoteStatus,
169 });
170 assert.equal(state.localStatusCalls, 2);
171 assert.equal(state.remoteStatusCalls, 1);
172 assert.equal(state.localInvalidationCalls, 1);
173 assert.equal(state.remoteInvalidationCalls, 0);
174 }).pipe(Effect.provide(makeTestLayer(state)));
175 });
176
177 it.effect("normalizes symlinked CWDs before cache lookup and workflow calls", () => {
178 const seenCwds: string[] = [];
179 const state = {
180 currentLocalStatus: baseLocalStatus,
181 currentRemoteStatus: baseRemoteStatus,
182 localStatusCalls: 0,
183 remoteStatusCalls: 0,
184 localInvalidationCalls: 0,
185 remoteInvalidationCalls: 0,
186 };
187 const testLayer = VcsStatusBroadcaster.layer.pipe(
188 Layer.provide(
189 Layer.mock(GitWorkflowService.GitWorkflowService)({
190 localStatus: (input) =>
191 Effect.sync(() => {
192 seenCwds.push(input.cwd);
193 state.localStatusCalls += 1;
194 return state.currentLocalStatus;
195 }),
196 remoteStatus: (input) =>
197 Effect.sync(() => {
198 seenCwds.push(input.cwd);
199 state.remoteStatusCalls += 1;
200 return state.currentRemoteStatus;
201 }),
202 invalidateLocalStatus: () =>
203 Effect.sync(() => {
204 state.localInvalidationCalls += 1;
205 }),
206 invalidateRemoteStatus: () =>
207 Effect.sync(() => {
208 state.remoteInvalidationCalls += 1;
209 }),
210 } satisfies Partial<GitWorkflowService.GitWorkflowServiceShape>),
211 ),
212 );
213
214 return Effect.gen(function* () {
215 const fileSystem = yield* FileSystem.FileSystem;
216 const path = yield* Path.Path;
217 const realDir = yield* fileSystem.makeTempDirectoryScoped({
218 prefix: "t3-vcs-status-real-",
219 });
220 const linkParent = yield* fileSystem.makeTempDirectoryScoped({
221 prefix: "t3-vcs-status-link-",
222 });
223 const linkDir = path.join(linkParent, "repo-link");
224 yield* fileSystem.symlink(realDir, linkDir);
225 const realPath = yield* fileSystem.realPath(realDir);
226
227 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
228 yield* broadcaster.getStatus({ cwd: linkDir });
229 yield* broadcaster.getStatus({ cwd: realDir });
230
231 assert.deepStrictEqual(seenCwds, [realPath, realPath]);
232 assert.equal(state.localStatusCalls, 1);
233 assert.equal(state.remoteStatusCalls, 1);
234 }).pipe(Effect.provide(Layer.mergeAll(testLayer, NodeServices.layer)));
235 });
236
237 it.effect("streams a local snapshot first and remote updates later", () => {
238 const state = {
239 currentLocalStatus: baseLocalStatus,
240 currentRemoteStatus: baseRemoteStatus,
241 localStatusCalls: 0,
242 remoteStatusCalls: 0,
243 localInvalidationCalls: 0,
244 remoteInvalidationCalls: 0,
245 };
246
247 return Effect.gen(function* () {
248 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
249 const snapshotDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
250 const remoteUpdatedDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
251 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => {
252 if (event._tag === "snapshot") {
253 return Deferred.succeed(snapshotDeferred, event).pipe(Effect.ignore);
254 }
255 if (event._tag === "remoteUpdated") {
256 return Deferred.succeed(remoteUpdatedDeferred, event).pipe(Effect.ignore);
257 }
258 return Effect.void;
259 }).pipe(Effect.forkScoped);
260
261 const snapshot = yield* Deferred.await(snapshotDeferred);
262 yield* broadcaster.refreshStatus("/repo");
263 const remoteUpdated = yield* Deferred.await(remoteUpdatedDeferred);
264
265 assert.deepStrictEqual(snapshot, {
266 _tag: "snapshot",
267 local: baseLocalStatus,
268 remote: null,
269 } satisfies VcsStatusStreamEvent);
270 assert.deepStrictEqual(remoteUpdated, {
271 _tag: "remoteUpdated",
272 remote: baseRemoteStatus,
273 } satisfies VcsStatusStreamEvent);
274 }).pipe(Effect.provide(makeTestLayer(state)));
275 });
276
277 it.effect("stops the remote poller after the last stream subscriber disconnects", () => {
278 const state = {
279 currentLocalStatus: baseLocalStatus,
280 currentRemoteStatus: baseRemoteStatus,
281 localStatusCalls: 0,
282 remoteStatusCalls: 0,
283 localInvalidationCalls: 0,
284 remoteInvalidationCalls: 0,
285 };
286 let remoteInterruptedDeferred: Deferred.Deferred<void, never> | null = null;
287 let remoteStartedDeferred: Deferred.Deferred<void, never> | null = null;
288 const testLayer = VcsStatusBroadcaster.layer.pipe(
289 Layer.provide(
290 Layer.mock(GitWorkflowService.GitWorkflowService)({
291 localStatus: () =>
292 Effect.sync(() => {
293 state.localStatusCalls += 1;
294 return state.currentLocalStatus;
295 }),
296 remoteStatus: () =>
297 Effect.sync(() => {
298 state.remoteStatusCalls += 1;
299 }).pipe(
300 Effect.andThen(
301 remoteStartedDeferred
302 ? Deferred.succeed(remoteStartedDeferred, undefined).pipe(Effect.ignore)
303 : Effect.void,
304 ),
305 Effect.andThen(Effect.never as Effect.Effect<VcsStatusRemoteResult | null, never>),
306 Effect.onInterrupt(() =>
307 remoteInterruptedDeferred
308 ? Deferred.succeed(remoteInterruptedDeferred, undefined).pipe(Effect.ignore)
309 : Effect.void,
310 ),
311 ),
312 invalidateLocalStatus: () =>
313 Effect.sync(() => {
314 state.localInvalidationCalls += 1;
315 }),
316 invalidateRemoteStatus: () =>
317 Effect.sync(() => {
318 state.remoteInvalidationCalls += 1;
319 }),
320 } satisfies Partial<GitWorkflowService.GitWorkflowServiceShape>),
321 ),
322 );
323
324 return Effect.gen(function* () {
325 const remoteInterrupted = yield* Deferred.make<void>();
326 const remoteStarted = yield* Deferred.make<void>();
327 remoteInterruptedDeferred = remoteInterrupted;
328 remoteStartedDeferred = remoteStarted;
329
330 const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
331 const firstSnapshot = yield* Deferred.make<VcsStatusStreamEvent>();
332 const secondSnapshot = yield* Deferred.make<VcsStatusStreamEvent>();
333 const firstScope = yield* Scope.make();
334 const secondScope = yield* Scope.make();
335 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
336 event._tag === "snapshot"
337 ? Deferred.succeed(firstSnapshot, event).pipe(Effect.ignore)
338 : Effect.void,
339 ).pipe(Effect.forkIn(firstScope));
340 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
341 event._tag === "snapshot"
342 ? Deferred.succeed(secondSnapshot, event).pipe(Effect.ignore)
343 : Effect.void,
344 ).pipe(Effect.forkIn(secondScope));
345
346 yield* Deferred.await(firstSnapshot);
347 yield* Deferred.await(secondSnapshot);
348 yield* Deferred.await(remoteStarted);
349
350 assert.equal(state.remoteStatusCalls, 1);
351
352 yield* Scope.close(firstScope, Exit.void);
353 assert.isTrue(Option.isNone(yield* Deferred.poll(remoteInterrupted)));
354
355 yield* Scope.close(secondScope, Exit.void).pipe(Effect.forkScoped);
356 yield* Deferred.await(remoteInterrupted);
357 assert.isTrue(Option.isSome(yield* Deferred.poll(remoteInterrupted)));
358 }).pipe(Effect.provide(testLayer));
359 });
360});