File size: 10,754 Bytes
1dbc34b | 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
import { WorktreeResolver, type WorktreeInfo } from '@/services/worktree-resolver.js';
import { exec } from 'child_process';
import path from 'path';
// Mock child_process
vi.mock('child_process', () => ({
exec: vi.fn(),
}));
/**
* Helper to normalize paths for cross-platform test compatibility.
* On Windows, path.resolve('/Users/dev/project') returns 'C:\Users\dev\project' (with current drive).
* This helper ensures test expectations match the actual platform behavior.
*/
const normalizePath = (p: string): string => path.resolve(p);
// Create promisified mock helper
const mockExecAsync = (
impl: (cmd: string, options?: { cwd?: string }) => Promise<{ stdout: string; stderr: string }>
) => {
(exec as unknown as Mock).mockImplementation(
(
cmd: string,
options: { cwd?: string } | undefined,
callback: (error: Error | null, result: { stdout: string; stderr: string }) => void
) => {
impl(cmd, options)
.then((result) => callback(null, result))
.catch((error) => callback(error, { stdout: '', stderr: '' }));
}
);
};
describe('WorktreeResolver', () => {
let resolver: WorktreeResolver;
beforeEach(() => {
vi.clearAllMocks();
resolver = new WorktreeResolver();
});
describe('getCurrentBranch', () => {
it('should return branch name when on a branch', async () => {
mockExecAsync(async () => ({ stdout: 'main\n', stderr: '' }));
const branch = await resolver.getCurrentBranch('/test/project');
expect(branch).toBe('main');
});
it('should return null on detached HEAD (empty output)', async () => {
mockExecAsync(async () => ({ stdout: '', stderr: '' }));
const branch = await resolver.getCurrentBranch('/test/project');
expect(branch).toBeNull();
});
it('should return null when git command fails', async () => {
mockExecAsync(async () => {
throw new Error('Not a git repository');
});
const branch = await resolver.getCurrentBranch('/not/a/git/repo');
expect(branch).toBeNull();
});
it('should trim whitespace from branch name', async () => {
mockExecAsync(async () => ({ stdout: ' feature-branch \n', stderr: '' }));
const branch = await resolver.getCurrentBranch('/test/project');
expect(branch).toBe('feature-branch');
});
it('should use provided projectPath as cwd', async () => {
let capturedCwd: string | undefined;
mockExecAsync(async (cmd, options) => {
capturedCwd = options?.cwd;
return { stdout: 'main\n', stderr: '' };
});
await resolver.getCurrentBranch('/custom/path');
expect(capturedCwd).toBe('/custom/path');
});
});
describe('findWorktreeForBranch', () => {
const porcelainOutput = `worktree /Users/dev/project
branch refs/heads/main
worktree /Users/dev/project/.worktrees/feature-x
branch refs/heads/feature-x
worktree /Users/dev/project/.worktrees/feature-y
branch refs/heads/feature-y
`;
it('should find worktree by branch name', async () => {
mockExecAsync(async () => ({ stdout: porcelainOutput, stderr: '' }));
const result = await resolver.findWorktreeForBranch('/Users/dev/project', 'feature-x');
expect(result).toBe(normalizePath('/Users/dev/project/.worktrees/feature-x'));
});
it('should normalize refs/heads and trim when resolving target branch', async () => {
mockExecAsync(async () => ({ stdout: porcelainOutput, stderr: '' }));
const result = await resolver.findWorktreeForBranch(
'/Users/dev/project',
' refs/heads/feature-x '
);
expect(result).toBe(normalizePath('/Users/dev/project/.worktrees/feature-x'));
});
it('should normalize remote-style target branch names', async () => {
mockExecAsync(async () => ({ stdout: porcelainOutput, stderr: '' }));
const result = await resolver.findWorktreeForBranch('/Users/dev/project', 'origin/feature-x');
expect(result).toBe(normalizePath('/Users/dev/project/.worktrees/feature-x'));
});
it('should return null when branch not found', async () => {
mockExecAsync(async () => ({ stdout: porcelainOutput, stderr: '' }));
const path = await resolver.findWorktreeForBranch('/Users/dev/project', 'non-existent');
expect(path).toBeNull();
});
it('should return null when git command fails', async () => {
mockExecAsync(async () => {
throw new Error('Not a git repository');
});
const path = await resolver.findWorktreeForBranch('/not/a/repo', 'main');
expect(path).toBeNull();
});
it('should find main worktree', async () => {
mockExecAsync(async () => ({ stdout: porcelainOutput, stderr: '' }));
const result = await resolver.findWorktreeForBranch('/Users/dev/project', 'main');
expect(result).toBe(normalizePath('/Users/dev/project'));
});
it('should handle porcelain output without trailing newline', async () => {
const noTrailingNewline = `worktree /Users/dev/project
branch refs/heads/main
worktree /Users/dev/project/.worktrees/feature-x
branch refs/heads/feature-x`;
mockExecAsync(async () => ({ stdout: noTrailingNewline, stderr: '' }));
const result = await resolver.findWorktreeForBranch('/Users/dev/project', 'feature-x');
expect(result).toBe(normalizePath('/Users/dev/project/.worktrees/feature-x'));
});
it('should resolve relative paths to absolute', async () => {
const relativePathOutput = `worktree /Users/dev/project
branch refs/heads/main
worktree .worktrees/feature-relative
branch refs/heads/feature-relative
`;
mockExecAsync(async () => ({ stdout: relativePathOutput, stderr: '' }));
const result = await resolver.findWorktreeForBranch('/Users/dev/project', 'feature-relative');
// Should resolve to absolute path (platform-specific)
expect(result).toBe(normalizePath('/Users/dev/project/.worktrees/feature-relative'));
});
it('should use projectPath as cwd for git command', async () => {
let capturedCwd: string | undefined;
mockExecAsync(async (cmd, options) => {
capturedCwd = options?.cwd;
return { stdout: porcelainOutput, stderr: '' };
});
await resolver.findWorktreeForBranch('/custom/project', 'main');
expect(capturedCwd).toBe('/custom/project');
});
});
describe('listWorktrees', () => {
it('should list all worktrees with metadata', async () => {
const porcelainOutput = `worktree /Users/dev/project
branch refs/heads/main
worktree /Users/dev/project/.worktrees/feature-x
branch refs/heads/feature-x
worktree /Users/dev/project/.worktrees/feature-y
branch refs/heads/feature-y
`;
mockExecAsync(async () => ({ stdout: porcelainOutput, stderr: '' }));
const worktrees = await resolver.listWorktrees('/Users/dev/project');
expect(worktrees).toHaveLength(3);
expect(worktrees[0]).toEqual({
path: normalizePath('/Users/dev/project'),
branch: 'main',
isMain: true,
});
expect(worktrees[1]).toEqual({
path: normalizePath('/Users/dev/project/.worktrees/feature-x'),
branch: 'feature-x',
isMain: false,
});
expect(worktrees[2]).toEqual({
path: normalizePath('/Users/dev/project/.worktrees/feature-y'),
branch: 'feature-y',
isMain: false,
});
});
it('should return empty array when git command fails', async () => {
mockExecAsync(async () => {
throw new Error('Not a git repository');
});
const worktrees = await resolver.listWorktrees('/not/a/repo');
expect(worktrees).toEqual([]);
});
it('should handle detached HEAD worktrees', async () => {
const porcelainWithDetached = `worktree /Users/dev/project
branch refs/heads/main
worktree /Users/dev/project/.worktrees/detached-wt
detached
`;
mockExecAsync(async () => ({ stdout: porcelainWithDetached, stderr: '' }));
const worktrees = await resolver.listWorktrees('/Users/dev/project');
expect(worktrees).toHaveLength(2);
expect(worktrees[1]).toEqual({
path: normalizePath('/Users/dev/project/.worktrees/detached-wt'),
branch: null, // Detached HEAD has no branch
isMain: false,
});
});
it('should mark only first worktree as main', async () => {
const multipleWorktrees = `worktree /Users/dev/project
branch refs/heads/main
worktree /Users/dev/project/.worktrees/wt1
branch refs/heads/branch1
worktree /Users/dev/project/.worktrees/wt2
branch refs/heads/branch2
`;
mockExecAsync(async () => ({ stdout: multipleWorktrees, stderr: '' }));
const worktrees = await resolver.listWorktrees('/Users/dev/project');
expect(worktrees[0].isMain).toBe(true);
expect(worktrees[1].isMain).toBe(false);
expect(worktrees[2].isMain).toBe(false);
});
it('should resolve relative paths to absolute', async () => {
const relativePathOutput = `worktree /Users/dev/project
branch refs/heads/main
worktree .worktrees/relative-wt
branch refs/heads/relative-branch
`;
mockExecAsync(async () => ({ stdout: relativePathOutput, stderr: '' }));
const worktrees = await resolver.listWorktrees('/Users/dev/project');
expect(worktrees[1].path).toBe(normalizePath('/Users/dev/project/.worktrees/relative-wt'));
});
it('should handle single worktree (main only)', async () => {
const singleWorktree = `worktree /Users/dev/project
branch refs/heads/main
`;
mockExecAsync(async () => ({ stdout: singleWorktree, stderr: '' }));
const worktrees = await resolver.listWorktrees('/Users/dev/project');
expect(worktrees).toHaveLength(1);
expect(worktrees[0]).toEqual({
path: normalizePath('/Users/dev/project'),
branch: 'main',
isMain: true,
});
});
it('should handle empty git worktree list output', async () => {
mockExecAsync(async () => ({ stdout: '', stderr: '' }));
const worktrees = await resolver.listWorktrees('/Users/dev/project');
expect(worktrees).toEqual([]);
});
it('should handle output without trailing newline', async () => {
const noTrailingNewline = `worktree /Users/dev/project
branch refs/heads/main
worktree /Users/dev/project/.worktrees/feature-x
branch refs/heads/feature-x`;
mockExecAsync(async () => ({ stdout: noTrailingNewline, stderr: '' }));
const worktrees = await resolver.listWorktrees('/Users/dev/project');
expect(worktrees).toHaveLength(2);
expect(worktrees[1].branch).toBe('feature-x');
});
});
});
|