Open History to see the last commit
1import type { GitStatusResult } from "@t3tools/contracts";
2import { afterEach, describe, expect, it, vi } from "vitest";
3
4import {
5 getGitStatusSnapshot,
6 resetGitStatusStateForTests,
7 refreshGitStatus,
8 watchGitStatus,
9} from "./gitStatusState";
10
11function registerListener<T>(listeners: Set<(event: T) => void>, listener: (event: T) => void) {
12 listeners.add(listener);
13 return () => {
14 listeners.delete(listener);
15 };
16}
17
18const gitStatusListeners = new Set<(event: GitStatusResult) => void>();
19
20const BASE_STATUS: GitStatusResult = {
21 isRepo: true,
22 hasOriginRemote: true,
23 isDefaultBranch: false,
24 branch: "feature/push-status",
25 hasWorkingTreeChanges: false,
26 workingTree: { files: [], insertions: 0, deletions: 0 },
27 hasUpstream: true,
28 aheadCount: 0,
29 behindCount: 0,
30 pr: null,
31};
32
33const gitClient = {
34 refreshStatus: vi.fn(async (input: { cwd: string }) => ({
35 ...BASE_STATUS,
36 branch: `${input.cwd}-refreshed`,
37 })),
38 onStatus: vi.fn((input: { cwd: string }, listener: (event: GitStatusResult) => void) =>
39 registerListener(gitStatusListeners, listener),
40 ),
41};
42
43function emitGitStatus(event: GitStatusResult) {
44 for (const listener of gitStatusListeners) {
45 listener(event);
46 }
47}
48
49afterEach(() => {
50 gitStatusListeners.clear();
51 gitClient.onStatus.mockClear();
52 gitClient.refreshStatus.mockClear();
53 resetGitStatusStateForTests();
54});
55
56describe("gitStatusState", () => {
57 it("starts fresh cwd state in a pending state", () => {
58 expect(getGitStatusSnapshot("/fresh")).toEqual({
59 data: null,
60 error: null,
61 cause: null,
62 isPending: true,
63 });
64 });
65
66 it("shares one live subscription per cwd and updates the per-cwd atom snapshot", () => {
67 const releaseA = watchGitStatus("/repo", gitClient);
68 const releaseB = watchGitStatus("/repo", gitClient);
69
70 expect(gitClient.onStatus).toHaveBeenCalledOnce();
71 expect(getGitStatusSnapshot("/repo")).toEqual({
72 data: null,
73 error: null,
74 cause: null,
75 isPending: true,
76 });
77
78 emitGitStatus(BASE_STATUS);
79
80 expect(getGitStatusSnapshot("/repo")).toEqual({
81 data: BASE_STATUS,
82 error: null,
83 cause: null,
84 isPending: false,
85 });
86
87 releaseA();
88 expect(gitStatusListeners.size).toBe(1);
89
90 releaseB();
91 expect(gitStatusListeners.size).toBe(0);
92 });
93
94 it("refreshes git status through the unary RPC without restarting the stream", async () => {
95 const release = watchGitStatus("/repo", gitClient);
96
97 emitGitStatus(BASE_STATUS);
98 const refreshed = await refreshGitStatus("/repo", gitClient);
99
100 expect(gitClient.onStatus).toHaveBeenCalledOnce();
101 expect(gitClient.refreshStatus).toHaveBeenCalledWith({ cwd: "/repo" });
102 expect(refreshed).toEqual({ ...BASE_STATUS, branch: "/repo-refreshed" });
103 expect(getGitStatusSnapshot("/repo")).toEqual({
104 data: BASE_STATUS,
105 error: null,
106 cause: null,
107 isPending: false,
108 });
109
110 release();
111 });
112});