Repository navigation
Expand file tree
/
Copy pathWorktreeManager.test.ts
More file actions
79 lines (67 loc) · 3.08 KB
/
Copy pathWorktreeManager.test.ts
File metadata and controls
79 lines (67 loc) · 3.08 KB
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
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { execSync } from 'child_process';
import { WorktreeManager } from '../src/services/WorktreeManager.js';
describe('WorktreeManager', () => {
let tmpDir: string;
let manager: WorktreeManager;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'worktree-test-'));
manager = new WorktreeManager();
// Initialize a git repo
execSync('git init', { cwd: tmpDir, stdio: 'pipe' });
execSync('git config user.email "test@test.com"', { cwd: tmpDir, stdio: 'pipe' });
execSync('git config user.name "Test"', { cwd: tmpDir, stdio: 'pipe' });
fs.writeFileSync(path.join(tmpDir, 'README.md'), '# Test');
execSync('git add . && git commit -m "init"', { cwd: tmpDir, stdio: 'pipe' });
});
afterEach(() => {
// Clean up worktrees first
try {
execSync('git worktree prune', { cwd: tmpDir, stdio: 'pipe' });
} catch {}
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('creates a worktree', () => {
const result = manager.createWorktree(tmpDir, 'test-branch');
expect(result.worktreePath).toContain('test-branch');
expect(result.branch).toBe('test-branch');
expect(fs.existsSync(result.worktreePath)).toBe(true);
});
it('creates a worktree with CLAUDE.md', () => {
const result = manager.createWorktree(tmpDir, 'test-branch-md', '# My Config');
const claudeMd = fs.readFileSync(path.join(result.worktreePath, 'CLAUDE.md'), 'utf-8');
expect(claudeMd).toBe('# My Config');
});
it('removes a worktree', () => {
const result = manager.createWorktree(tmpDir, 'to-remove');
expect(fs.existsSync(result.worktreePath)).toBe(true);
manager.removeWorktree(tmpDir, result.worktreePath, 'to-remove');
// Worktree should be removed
expect(fs.existsSync(result.worktreePath)).toBe(false);
});
it('updates CLAUDE.md in a worktree', () => {
const result = manager.createWorktree(tmpDir, 'update-md', '# Old');
manager.updateClaudeMd(result.worktreePath, '# New');
const content = manager.getClaudeMd(result.worktreePath);
expect(content).toBe('# New');
});
it('creates and updates AGENTS.md for codex worktrees', () => {
const result = manager.createWorktree(tmpDir, 'codex-md', '# Codex Config', 'codex');
const agentsMd = fs.readFileSync(path.join(result.worktreePath, 'AGENTS.md'), 'utf-8');
expect(agentsMd).toBe('# Codex Config');
manager.updateClaudeMd(result.worktreePath, '# Updated Codex Config', 'codex');
const content = manager.getClaudeMd(result.worktreePath, 'codex');
expect(content).toBe('# Updated Codex Config');
});
it('getClaudeMd returns null if no file', () => {
const result = manager.createWorktree(tmpDir, 'no-md');
// No CLAUDE.md was written
const content = manager.getClaudeMd(result.worktreePath);
// README.md exists from init, but CLAUDE.md may or may not
// Just check it returns string or null
expect(content === null || typeof content === 'string').toBe(true);
});
});