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

gitStatusState.test.ts

325 lines9.9 KB

1import { EnvironmentId, type GitStatusResult } from "@t3tools/contracts";
2import { afterEach, describe, expect, it, vi } from "vitest";
3​
4import type { WsRpcClient } from "../rpc/wsRpcClient";
5import { resetAppAtomRegistryForTests } from "../rpc/atomRegistry";
6import {
7 getGitStatusSnapshot,
8 resetGitStatusStateForTests,
9 refreshGitStatus,
10 watchGitStatus,
11} from "./gitStatusState";
12​
13const serviceHarness = vi.hoisted(() => ({
14 connections: new Map<string, any>(),
15 listeners: new Set<() => void>(),
16}));
17​
18vi.mock("../environments/runtime/service", () => ({
19 readEnvironmentConnection: (environmentId: string) =>
20 serviceHarness.connections.get(environmentId) ?? null,
21 subscribeEnvironmentConnections: (listener: () => void) => {
22 serviceHarness.listeners.add(listener);
23 return () => {
24 serviceHarness.listeners.delete(listener);
25 };
26 },
27}));
28​
29function registerListener<T>(listeners: Set<(event: T) => void>, listener: (event: T) => void) {
30 listeners.add(listener);
31 return () => {
32 listeners.delete(listener);
33 };
35​
36const gitStatusListeners = new Set<(event: GitStatusResult) => void>();
37const ENVIRONMENT_ID = EnvironmentId.make("environment-local");
38const OTHER_ENVIRONMENT_ID = EnvironmentId.make("environment-remote");
39const TARGET = { environmentId: ENVIRONMENT_ID, cwd: "/repo" } as const;
40const FRESH_TARGET = { environmentId: ENVIRONMENT_ID, cwd: "/fresh" } as const;
41​
42const BASE_STATUS: GitStatusResult = {
43 isRepo: true,
44 hasOriginRemote: true,
45 isDefaultBranch: false,
46 branch: "feature/push-status",
47 hasWorkingTreeChanges: false,
48 workingTree: { files: [], insertions: 0, deletions: 0 },
49 hasUpstream: true,
50 aheadCount: 0,
51 behindCount: 0,
52 pr: null,
53};
54​
55const gitClient = {
56 refreshStatus: vi.fn(async (input: { cwd: string }) => ({
57 ...BASE_STATUS,
58 branch: `${input.cwd}-refreshed`,
59 })),
60 onStatus: vi.fn((input: { cwd: string }, listener: (event: GitStatusResult) => void) =>
61 registerListener(gitStatusListeners, listener),
62 ),
63};
64​
65function emitGitStatus(event: GitStatusResult) {
66 for (const listener of gitStatusListeners) {
67 listener(event);
68 }
70​
71function createRegisteredGitStatusClient(environmentId: EnvironmentId) {
72 const listeners = new Set<(event: GitStatusResult) => void>();
73 const client = {
74 dispose: vi.fn(async () => undefined),
75 reconnect: vi.fn(async () => undefined),
76 terminal: {
77 open: vi.fn(async () => undefined),
78 write: vi.fn(async () => undefined),
79 resize: vi.fn(async () => undefined),
80 clear: vi.fn(async () => undefined),
81 restart: vi.fn(async () => undefined),
82 close: vi.fn(async () => undefined),
83 onEvent: vi.fn(() => () => undefined),
84 },
85 projects: {
86 searchEntries: vi.fn(async () => []),
87 writeFile: vi.fn(async () => undefined),
88 },
89 shell: {
90 openInEditor: vi.fn(async () => undefined),
91 },
92 git: {
93 pull: vi.fn(async () => undefined),
94 refreshStatus: vi.fn(async (input: { cwd: string }) => ({
95 ...BASE_STATUS,
96 branch: `${input.cwd}-refreshed`,
97 })),
98 onStatus: vi.fn((_: { cwd: string }, listener: (event: GitStatusResult) => void) =>
99 registerListener(listeners, listener),
100 ),
101 runStackedAction: vi.fn(async () => ({}) as any),
102 listBranches: vi.fn(async () => []),
103 createWorktree: vi.fn(async () => undefined),
104 removeWorktree: vi.fn(async () => undefined),
105 createBranch: vi.fn(async () => undefined),
106 checkout: vi.fn(async () => undefined),
107 init: vi.fn(async () => undefined),
108 resolvePullRequest: vi.fn(async () => undefined),
109 preparePullRequestThread: vi.fn(async () => undefined),
110 },
111 server: {
112 getConfig: vi.fn(async () => ({
113 environment: {
114 environmentId,
115 },
116 })),
117 refreshProviders: vi.fn(async () => undefined),
118 upsertKeybinding: vi.fn(async () => undefined),
119 getSettings: vi.fn(async () => undefined),
120 updateSettings: vi.fn(async () => undefined),
121 subscribeConfig: vi.fn(() => () => undefined),
122 subscribeLifecycle: vi.fn(() => () => undefined),
123 subscribeAuthAccess: vi.fn(() => () => undefined),
124 },
125 orchestration: {
126 dispatchCommand: vi.fn(async () => undefined),
127 getTurnDiff: vi.fn(async () => undefined),
128 getFullThreadDiff: vi.fn(async () => undefined),
129 subscribeShell: vi.fn(() => () => undefined),
130 subscribeThread: vi.fn(() => () => undefined),
131 },
132 } as unknown as WsRpcClient;
133​
134 serviceHarness.connections.set(environmentId, {
135 kind: "saved" as const,
136 knownEnvironment: {
137 id: environmentId,
138 label: `Environment ${environmentId}`,
139 source: "manual" as const,
140 environmentId,
141 target: {
142 httpBaseUrl: "http://example.test",
143 wsBaseUrl: "ws://example.test",
144 },
145 },
146 client,
147 environmentId,
148 ensureBootstrapped: async () => undefined,
149 reconnect: async () => undefined,
150 dispose: async () => undefined,
151 });
152 for (const listener of serviceHarness.listeners) {
153 listener();
154 }
155​
156 return {
157 client,
158 emit: (event: GitStatusResult) => {
159 for (const listener of listeners) {
160 listener(event);
161 }
162 },
163 };
165​
166afterEach(async () => {
167 gitStatusListeners.clear();
168 serviceHarness.connections.clear();
169 serviceHarness.listeners.clear();
170 gitClient.onStatus.mockClear();
171 gitClient.refreshStatus.mockClear();
172 resetGitStatusStateForTests();
173 resetAppAtomRegistryForTests();
174});
175​
176describe("gitStatusState", () => {
177 it("starts fresh cwd state in a pending state", () => {
178 expect(getGitStatusSnapshot(FRESH_TARGET)).toEqual({
179 data: null,
180 error: null,
181 cause: null,
182 isPending: true,
183 });
184 });
185​
186 it("shares one live subscription per cwd and updates the per-cwd atom snapshot", () => {
187 const releaseA = watchGitStatus(TARGET, gitClient);
188 const releaseB = watchGitStatus(TARGET, gitClient);
189​
190 expect(gitClient.onStatus).toHaveBeenCalledOnce();
191 expect(getGitStatusSnapshot(TARGET)).toEqual({
192 data: null,
193 error: null,
194 cause: null,
195 isPending: true,
196 });
197​
198 emitGitStatus(BASE_STATUS);
199​
200 expect(getGitStatusSnapshot(TARGET)).toEqual({
201 data: BASE_STATUS,
202 error: null,
203 cause: null,
204 isPending: false,
205 });
206​
207 releaseA();
208 expect(gitStatusListeners.size).toBe(1);
209​
210 releaseB();
211 expect(gitStatusListeners.size).toBe(0);
212 });
213​
214 it("refreshes git status through the unary RPC without restarting the stream", async () => {
215 const release = watchGitStatus(TARGET, gitClient);
216​
217 emitGitStatus(BASE_STATUS);
218 const refreshed = await refreshGitStatus(TARGET, gitClient);
219​
220 expect(gitClient.onStatus).toHaveBeenCalledOnce();
221 expect(gitClient.refreshStatus).toHaveBeenCalledWith({ cwd: "/repo" });
222 expect(refreshed).toEqual({ ...BASE_STATUS, branch: "/repo-refreshed" });
223 expect(getGitStatusSnapshot(TARGET)).toEqual({
224 data: BASE_STATUS,
225 error: null,
226 cause: null,
227 isPending: false,
228 });
229​
230 release();
231 });
232​
233 it("keeps git status subscriptions isolated by environment when cwds match", () => {
234 const localListeners = new Set<(event: GitStatusResult) => void>();
235 const remoteListeners = new Set<(event: GitStatusResult) => void>();
236 const localClient = {
237 refreshStatus: vi.fn(),
238 onStatus: vi.fn((_: { cwd: string }, listener: (event: GitStatusResult) => void) =>
239 registerListener(localListeners, listener),
240 ),
241 };
242 const remoteClient = {
243 refreshStatus: vi.fn(),
244 onStatus: vi.fn((_: { cwd: string }, listener: (event: GitStatusResult) => void) =>
245 registerListener(remoteListeners, listener),
246 ),
247 };
248 const remoteTarget = { environmentId: OTHER_ENVIRONMENT_ID, cwd: "/repo" } as const;
249​
250 const releaseLocal = watchGitStatus(TARGET, localClient);
251 const releaseRemote = watchGitStatus(remoteTarget, remoteClient);
252​
253 for (const listener of localListeners) {
254 listener(BASE_STATUS);
255 }
256 for (const listener of remoteListeners) {
257 listener({ ...BASE_STATUS, branch: "remote-branch" });
258 }
259​
260 expect(getGitStatusSnapshot(TARGET).data?.branch).toBe("feature/push-status");
261 expect(getGitStatusSnapshot(remoteTarget).data?.branch).toBe("remote-branch");
262​
263 releaseLocal();
264 releaseRemote();
265 });
266​
267 it("waits for a delayed environment client registration instead of throwing", () => {
268 const release = watchGitStatus(TARGET);
269​
270 expect(getGitStatusSnapshot(TARGET)).toEqual({
271 data: null,
272 error: null,
273 cause: null,
274 isPending: true,
275 });
276​
277 const registered = createRegisteredGitStatusClient(ENVIRONMENT_ID);
278 registered.emit(BASE_STATUS);
279​
280 expect(getGitStatusSnapshot(TARGET)).toEqual({
281 data: BASE_STATUS,
282 error: null,
283 cause: null,
284 isPending: false,
285 });
286​
287 release();
288 });
289​
290 it("resubscribes after the environment client is removed and re-registered", async () => {
291 const firstClient = createRegisteredGitStatusClient(ENVIRONMENT_ID);
292 const release = watchGitStatus(TARGET);
293​
294 firstClient.emit(BASE_STATUS);
295 expect(getGitStatusSnapshot(TARGET).data?.branch).toBe("feature/push-status");
296​
297 serviceHarness.connections.delete(ENVIRONMENT_ID);
298 for (const listener of serviceHarness.listeners) {
299 listener();
300 }
301​
302 expect(getGitStatusSnapshot(TARGET)).toEqual({
303 data: BASE_STATUS,
304 error: null,
305 cause: null,
306 isPending: true,
307 });
308​
309 const secondClient = createRegisteredGitStatusClient(ENVIRONMENT_ID);
310 secondClient.emit({ ...BASE_STATUS, branch: "reconnected-branch" });
311​
312 expect(getGitStatusSnapshot(TARGET)).toEqual({
313 data: { ...BASE_STATUS, branch: "reconnected-branch" },
314 error: null,
315 cause: null,
316 isPending: false,
317 });
318​
319 release();
320 });
321​
322 it("returns the cached snapshot when refresh is requested before the client is registered", async () => {
323 await expect(refreshGitStatus(TARGET)).resolves.toBeNull();
324 });
325});