Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { Deferred, Effect, Exit, Layer, Option, Scope, Stream } from "effect";
3import type {
4 GitStatusLocalResult,
5 GitStatusRemoteResult,
6 GitStatusResult,
7 GitStatusStreamEvent,
8} from "@t3tools/contracts";
9import { describe } from "vitest";
10
11import { GitStatusBroadcaster } from "../Services/GitStatusBroadcaster.ts";
12import { GitStatusBroadcasterLive } from "./GitStatusBroadcaster.ts";
13import { type GitManagerShape, GitManager } from "../Services/GitManager.ts";
14
15const baseLocalStatus: GitStatusLocalResult = {
16 isRepo: true,
17 hostingProvider: {
18 kind: "github",
19 name: "GitHub",
20 baseUrl: "https://github.com",
21 },
22 hasOriginRemote: true,
23 isDefaultBranch: false,
24 branch: "feature/status-broadcast",
25 hasWorkingTreeChanges: false,
26 workingTree: { files: [], insertions: 0, deletions: 0 },
27};
28
29const baseRemoteStatus: GitStatusRemoteResult = {
30 hasUpstream: true,
31 aheadCount: 0,
32 behindCount: 0,
33 pr: null,
34};
35
36const baseStatus: GitStatusResult = {
37 ...baseLocalStatus,
38 ...baseRemoteStatus,
39};
40
41function makeTestLayer(state: {
42 currentLocalStatus: GitStatusLocalResult;
43 currentRemoteStatus: GitStatusRemoteResult | null;
44 localStatusCalls: number;
45 remoteStatusCalls: number;
46 localInvalidationCalls: number;
47 remoteInvalidationCalls: number;
48}) {
49 const gitManager: GitManagerShape = {
50 localStatus: () =>
51 Effect.sync(() => {
52 state.localStatusCalls += 1;
53 return state.currentLocalStatus;
54 }),
55 remoteStatus: () =>
56 Effect.sync(() => {
57 state.remoteStatusCalls += 1;
58 return state.currentRemoteStatus;
59 }),
60 status: () => Effect.die("status should not be called in this test"),
61 invalidateLocalStatus: () =>
62 Effect.sync(() => {
63 state.localInvalidationCalls += 1;
64 }),
65 invalidateRemoteStatus: () =>
66 Effect.sync(() => {
67 state.remoteInvalidationCalls += 1;
68 }),
69 invalidateStatus: () => Effect.die("invalidateStatus should not be called in this test"),
70 resolvePullRequest: () => Effect.die("resolvePullRequest should not be called in this test"),
71 preparePullRequestThread: () =>
72 Effect.die("preparePullRequestThread should not be called in this test"),
73 runStackedAction: () => Effect.die("runStackedAction should not be called in this test"),
74 };
75
76 return GitStatusBroadcasterLive.pipe(Layer.provide(Layer.succeed(GitManager, gitManager)));
77}
78
79describe("GitStatusBroadcasterLive", () => {
80 it.effect("reuses the cached git status across repeated reads", () => {
81 const state = {
82 currentLocalStatus: baseLocalStatus,
83 currentRemoteStatus: baseRemoteStatus,
84 localStatusCalls: 0,
85 remoteStatusCalls: 0,
86 localInvalidationCalls: 0,
87 remoteInvalidationCalls: 0,
88 };
89
90 return Effect.gen(function* () {
91 const broadcaster = yield* GitStatusBroadcaster;
92
93 const first = yield* broadcaster.getStatus({ cwd: "/repo" });
94 const second = yield* broadcaster.getStatus({ cwd: "/repo" });
95
96 assert.deepStrictEqual(first, baseStatus);
97 assert.deepStrictEqual(second, baseStatus);
98 assert.equal(state.localStatusCalls, 1);
99 assert.equal(state.remoteStatusCalls, 1);
100 assert.equal(state.localInvalidationCalls, 0);
101 assert.equal(state.remoteInvalidationCalls, 0);
102 }).pipe(Effect.provide(makeTestLayer(state)));
103 });
104
105 it.effect("refreshes the cached snapshot after explicit invalidation", () => {
106 const state = {
107 currentLocalStatus: baseLocalStatus,
108 currentRemoteStatus: baseRemoteStatus,
109 localStatusCalls: 0,
110 remoteStatusCalls: 0,
111 localInvalidationCalls: 0,
112 remoteInvalidationCalls: 0,
113 };
114
115 return Effect.gen(function* () {
116 const broadcaster = yield* GitStatusBroadcaster;
117 const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
118
119 state.currentLocalStatus = {
120 ...baseLocalStatus,
121 branch: "feature/updated-status",
122 };
123 state.currentRemoteStatus = {
124 ...baseRemoteStatus,
125 aheadCount: 2,
126 };
127 const refreshed = yield* broadcaster.refreshStatus("/repo");
128 const cached = yield* broadcaster.getStatus({ cwd: "/repo" });
129
130 assert.deepStrictEqual(initial, baseStatus);
131 assert.deepStrictEqual(refreshed, {
132 ...state.currentLocalStatus,
133 ...state.currentRemoteStatus,
134 });
135 assert.deepStrictEqual(cached, {
136 ...state.currentLocalStatus,
137 ...state.currentRemoteStatus,
138 });
139 assert.equal(state.localStatusCalls, 2);
140 assert.equal(state.remoteStatusCalls, 2);
141 assert.equal(state.localInvalidationCalls, 1);
142 assert.equal(state.remoteInvalidationCalls, 1);
143 }).pipe(Effect.provide(makeTestLayer(state)));
144 });
145
146 it.effect("refreshes only the cached local snapshot when requested", () => {
147 const state = {
148 currentLocalStatus: baseLocalStatus,
149 currentRemoteStatus: baseRemoteStatus,
150 localStatusCalls: 0,
151 remoteStatusCalls: 0,
152 localInvalidationCalls: 0,
153 remoteInvalidationCalls: 0,
154 };
155
156 return Effect.gen(function* () {
157 const broadcaster = yield* GitStatusBroadcaster;
158 const initial = yield* broadcaster.getStatus({ cwd: "/repo" });
159
160 state.currentLocalStatus = {
161 ...baseLocalStatus,
162 branch: "feature/local-only-refresh",
163 hasWorkingTreeChanges: true,
164 };
165
166 const refreshedLocal = yield* broadcaster.refreshLocalStatus("/repo");
167 const cached = yield* broadcaster.getStatus({ cwd: "/repo" });
168
169 assert.deepStrictEqual(initial, baseStatus);
170 assert.deepStrictEqual(refreshedLocal, state.currentLocalStatus);
171 assert.deepStrictEqual(cached, {
172 ...state.currentLocalStatus,
173 ...baseRemoteStatus,
174 });
175 assert.equal(state.localStatusCalls, 2);
176 assert.equal(state.remoteStatusCalls, 1);
177 assert.equal(state.localInvalidationCalls, 1);
178 assert.equal(state.remoteInvalidationCalls, 0);
179 }).pipe(Effect.provide(makeTestLayer(state)));
180 });
181
182 it.effect("streams a local snapshot first and remote updates later", () => {
183 const state = {
184 currentLocalStatus: baseLocalStatus,
185 currentRemoteStatus: baseRemoteStatus,
186 localStatusCalls: 0,
187 remoteStatusCalls: 0,
188 localInvalidationCalls: 0,
189 remoteInvalidationCalls: 0,
190 };
191
192 return Effect.gen(function* () {
193 const broadcaster = yield* GitStatusBroadcaster;
194 const snapshotDeferred = yield* Deferred.make<GitStatusStreamEvent>();
195 const remoteUpdatedDeferred = yield* Deferred.make<GitStatusStreamEvent>();
196 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) => {
197 if (event._tag === "snapshot") {
198 return Deferred.succeed(snapshotDeferred, event).pipe(Effect.ignore);
199 }
200 if (event._tag === "remoteUpdated") {
201 return Deferred.succeed(remoteUpdatedDeferred, event).pipe(Effect.ignore);
202 }
203 return Effect.void;
204 }).pipe(Effect.forkScoped);
205
206 const snapshot = yield* Deferred.await(snapshotDeferred);
207 yield* broadcaster.refreshStatus("/repo");
208 const remoteUpdated = yield* Deferred.await(remoteUpdatedDeferred);
209
210 assert.deepStrictEqual(snapshot, {
211 _tag: "snapshot",
212 local: baseLocalStatus,
213 remote: null,
214 } satisfies GitStatusStreamEvent);
215 assert.deepStrictEqual(remoteUpdated, {
216 _tag: "remoteUpdated",
217 remote: baseRemoteStatus,
218 } satisfies GitStatusStreamEvent);
219 }).pipe(Effect.provide(makeTestLayer(state)));
220 });
221
222 it.effect("stops the remote poller after the last stream subscriber disconnects", () => {
223 const state = {
224 currentLocalStatus: baseLocalStatus,
225 currentRemoteStatus: baseRemoteStatus,
226 localStatusCalls: 0,
227 remoteStatusCalls: 0,
228 localInvalidationCalls: 0,
229 remoteInvalidationCalls: 0,
230 };
231 let remoteInterruptedDeferred: Deferred.Deferred<void, never> | null = null;
232 let remoteStartedDeferred: Deferred.Deferred<void, never> | null = null;
233 const testLayer = GitStatusBroadcasterLive.pipe(
234 Layer.provide(
235 Layer.succeed(GitManager, {
236 localStatus: () =>
237 Effect.sync(() => {
238 state.localStatusCalls += 1;
239 return state.currentLocalStatus;
240 }),
241 remoteStatus: () =>
242 Effect.sync(() => {
243 state.remoteStatusCalls += 1;
244 }).pipe(
245 Effect.andThen(
246 remoteStartedDeferred
247 ? Deferred.succeed(remoteStartedDeferred, undefined).pipe(Effect.ignore)
248 : Effect.void,
249 ),
250 Effect.andThen(Effect.never as Effect.Effect<GitStatusRemoteResult | null, never>),
251 Effect.onInterrupt(() =>
252 remoteInterruptedDeferred
253 ? Deferred.succeed(remoteInterruptedDeferred, undefined).pipe(Effect.ignore)
254 : Effect.void,
255 ),
256 ),
257 status: () => Effect.die("status should not be called in this test"),
258 invalidateLocalStatus: () =>
259 Effect.sync(() => {
260 state.localInvalidationCalls += 1;
261 }),
262 invalidateRemoteStatus: () =>
263 Effect.sync(() => {
264 state.remoteInvalidationCalls += 1;
265 }),
266 invalidateStatus: () => Effect.die("invalidateStatus should not be called in this test"),
267 resolvePullRequest: () =>
268 Effect.die("resolvePullRequest should not be called in this test"),
269 preparePullRequestThread: () =>
270 Effect.die("preparePullRequestThread should not be called in this test"),
271 runStackedAction: () => Effect.die("runStackedAction should not be called in this test"),
272 } satisfies GitManagerShape),
273 ),
274 );
275
276 return Effect.gen(function* () {
277 const remoteInterrupted = yield* Deferred.make<void>();
278 const remoteStarted = yield* Deferred.make<void>();
279 remoteInterruptedDeferred = remoteInterrupted;
280 remoteStartedDeferred = remoteStarted;
281
282 const broadcaster = yield* GitStatusBroadcaster;
283 const firstSnapshot = yield* Deferred.make<GitStatusStreamEvent>();
284 const secondSnapshot = yield* Deferred.make<GitStatusStreamEvent>();
285 const firstScope = yield* Scope.make();
286 const secondScope = yield* Scope.make();
287 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
288 event._tag === "snapshot"
289 ? Deferred.succeed(firstSnapshot, event).pipe(Effect.ignore)
290 : Effect.void,
291 ).pipe(Effect.forkIn(firstScope));
292 yield* Stream.runForEach(broadcaster.streamStatus({ cwd: "/repo" }), (event) =>
293 event._tag === "snapshot"
294 ? Deferred.succeed(secondSnapshot, event).pipe(Effect.ignore)
295 : Effect.void,
296 ).pipe(Effect.forkIn(secondScope));
297
298 yield* Deferred.await(firstSnapshot);
299 yield* Deferred.await(secondSnapshot);
300 yield* Deferred.await(remoteStarted);
301
302 assert.equal(state.remoteStatusCalls, 1);
303
304 yield* Scope.close(firstScope, Exit.void);
305 assert.equal(Option.isNone(yield* Deferred.poll(remoteInterrupted)), true);
306
307 yield* Scope.close(secondScope, Exit.void).pipe(Effect.forkScoped);
308 yield* Deferred.await(remoteInterrupted);
309 assert.equal(Option.isSome(yield* Deferred.poll(remoteInterrupted)), true);
310 }).pipe(Effect.provide(testLayer));
311 });
312});