1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
afterEach,
beforeEach,
describe,
expect,
it,
vi,
MockedFunction,
} from 'vitest';
import { act } from 'react';
import { renderHook } from '@testing-library/react';
import { useGitBranchName } from './useGitBranchName.js';
import { fs, vol } from 'memfs'; // For mocking fs
import { EventEmitter } from 'node:events';
import { exec as mockExec, type ChildProcess } from 'node:child_process';
import type { FSWatcher } from 'memfs/lib/volume.js';
// Mock child_process
vi.mock('child_process');
// Mock fs and fs/promises
vi.mock('node:fs', async () => {
const memfs = await vi.importActual<typeof import('memfs')>('memfs');
return memfs.fs;
});
vi.mock('node:fs/promises', async () => {
const memfs = await vi.importActual<typeof import('memfs')>('memfs');
return memfs.fs.promises;
});
const CWD = '/test/project';
const GIT_HEAD_PATH = `${CWD}/.git/HEAD`;
describe('useGitBranchName', () => {
beforeEach(() => {
vol.reset(); // Reset in-memory filesystem
vol.fromJSON({
[GIT_HEAD_PATH]: 'ref: refs/heads/main',
});
vi.useFakeTimers(); // Use fake timers for async operations
});
afterEach(() => {
vi.restoreAllMocks();
vi.clearAllTimers();
});
it('should return branch name', async () => {
(mockExec as MockedFunction<typeof mockExec>).mockImplementation(
(_command, _options, callback) => {
callback?.(null, 'main\n', '');
return new EventEmitter() as ChildProcess;
},
);
const { result, rerender } = renderHook(() => useGitBranchName(CWD));
await act(async () => {
vi.runAllTimers(); // Advance timers to trigger useEffect and exec callback
rerender(); // Rerender to get the updated state
});
expect(result.current).toBe('main');
});
it('should return undefined if git command fails', async () => {
(mockExec as MockedFunction<typeof mockExec>).mockImplementation(
(_command, _options, callback) => {
callback?.(new Error('Git error'), '', 'error output');
return new EventEmitter() as ChildProcess;
},
);
const { result, rerender } = renderHook(() => useGitBranchName(CWD));
expect(result.current).toBeUndefined();
await act(async () => {
vi.runAllTimers();
rerender();
});
expect(result.current).toBeUndefined();
});
it('should return undefined if branch is HEAD (detached state)', async () => {
(mockExec as MockedFunction<typeof mockExec>).mockImplementation(
(_command, _options, callback) => {
callback?.(null, 'HEAD\n', '');
return new EventEmitter() as ChildProcess;
},
);
const { result, rerender } = renderHook(() => useGitBranchName(CWD));
expect(result.current).toBeUndefined();
await act(async () => {
vi.runAllTimers();
rerender();
});
expect(result.current).toBeUndefined();
});
it('should update branch name when .git/HEAD changes', async ({ skip }) => {
skip(); // TODO: fix
(mockExec as MockedFunction<typeof mockExec>).mockImplementationOnce(
(_command, _options, callback) => {
callback?.(null, 'main\n', '');
return new EventEmitter() as ChildProcess;
},
);
const { result, rerender } = renderHook(() => useGitBranchName(CWD));
await act(async () => {
vi.runAllTimers();
rerender();
});
expect(result.current).toBe('main');
// Simulate a branch change
(mockExec as MockedFunction<typeof mockExec>).mockImplementationOnce(
(_command, _options, callback) => {
callback?.(null, 'develop\n', '');
return new EventEmitter() as ChildProcess;
},
);
// Simulate file change event
// Ensure the watcher is set up before triggering the change
await act(async () => {
fs.writeFileSync(GIT_HEAD_PATH, 'ref: refs/heads/develop'); // Trigger watcher
vi.runAllTimers(); // Process timers for watcher and exec
rerender();
});
expect(result.current).toBe('develop');
});
it('should handle watcher setup error silently', async () => {
// Remove .git/HEAD to cause an error in fs.watch setup
vol.unlinkSync(GIT_HEAD_PATH);
(mockExec as MockedFunction<typeof mockExec>).mockImplementation(
(_command, _options, callback) => {
callback?.(null, 'main\n', '');
return new EventEmitter() as ChildProcess;
},
);
const { result, rerender } = renderHook(() => useGitBranchName(CWD));
await act(async () => {
vi.runAllTimers();
rerender();
});
expect(result.current).toBe('main'); // Branch name should still be fetched initially
// Try to trigger a change that would normally be caught by the watcher
(mockExec as MockedFunction<typeof mockExec>).mockImplementationOnce(
(_command, _options, callback) => {
callback?.(null, 'develop\n', '');
return new EventEmitter() as ChildProcess;
},
);
// This write would trigger the watcher if it was set up
// but since it failed, the branch name should not update
// We need to create the file again for writeFileSync to not throw
vol.fromJSON({
[GIT_HEAD_PATH]: 'ref: refs/heads/develop',
});
await act(async () => {
fs.writeFileSync(GIT_HEAD_PATH, 'ref: refs/heads/develop');
vi.runAllTimers();
rerender();
});
// Branch name should not change because watcher setup failed
expect(result.current).toBe('main');
});
it('should cleanup watcher on unmount', async ({ skip }) => {
skip(); // TODO: fix
const closeMock = vi.fn();
const watchMock = vi.spyOn(fs, 'watch').mockReturnValue({
close: closeMock,
} as unknown as FSWatcher);
(mockExec as MockedFunction<typeof mockExec>).mockImplementation(
(_command, _options, callback) => {
callback?.(null, 'main\n', '');
return new EventEmitter() as ChildProcess;
},
);
const { unmount, rerender } = renderHook(() => useGitBranchName(CWD));
await act(async () => {
vi.runAllTimers();
rerender();
});
unmount();
expect(watchMock).toHaveBeenCalledWith(GIT_HEAD_PATH, expect.any(Function));
expect(closeMock).toHaveBeenCalled();
});
});
|