|
| 1 | +/** |
| 2 | + * Tests for worktree project-ID mismatch fix |
| 3 | + * |
| 4 | + * Validates that detect-project.sh uses -e (not -d) for .git existence |
| 5 | + * checks, so that git worktrees (where .git is a file) are detected |
| 6 | + * correctly. |
| 7 | + * |
| 8 | + * Run with: node tests/hooks/detect-project-worktree.test.js |
| 9 | + */ |
| 10 | + |
| 11 | +const assert = require('assert'); |
| 12 | +const path = require('path'); |
| 13 | +const fs = require('fs'); |
| 14 | +const os = require('os'); |
| 15 | +const { execSync } = require('child_process'); |
| 16 | + |
| 17 | +let passed = 0; |
| 18 | +let failed = 0; |
| 19 | + |
| 20 | +function test(name, fn) { |
| 21 | + try { |
| 22 | + fn(); |
| 23 | + console.log(` \u2713 ${name}`); |
| 24 | + passed++; |
| 25 | + } catch (err) { |
| 26 | + console.log(` \u2717 ${name}`); |
| 27 | + console.log(` Error: ${err.message}`); |
| 28 | + failed++; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function createTempDir() { |
| 33 | + return fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-worktree-test-')); |
| 34 | +} |
| 35 | + |
| 36 | +function cleanupDir(dir) { |
| 37 | + try { |
| 38 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 39 | + } catch { |
| 40 | + // ignore cleanup errors |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +const repoRoot = path.resolve(__dirname, '..', '..'); |
| 45 | +const detectProjectPath = path.join( |
| 46 | + repoRoot, |
| 47 | + 'skills', |
| 48 | + 'continuous-learning-v2', |
| 49 | + 'scripts', |
| 50 | + 'detect-project.sh' |
| 51 | +); |
| 52 | + |
| 53 | +console.log('\n=== Worktree Project-ID Mismatch Tests ===\n'); |
| 54 | + |
| 55 | +// ────────────────────────────────────────────────────── |
| 56 | +// Group 1: Content checks on detect-project.sh |
| 57 | +// ────────────────────────────────────────────────────── |
| 58 | + |
| 59 | +console.log('--- Content checks on detect-project.sh ---'); |
| 60 | + |
| 61 | +test('uses -e (not -d) for .git existence check', () => { |
| 62 | + const content = fs.readFileSync(detectProjectPath, 'utf8'); |
| 63 | + assert.ok( |
| 64 | + content.includes('[ -e "${project_root}/.git" ]'), |
| 65 | + 'detect-project.sh should use -e for .git check' |
| 66 | + ); |
| 67 | + assert.ok( |
| 68 | + !content.includes('[ -d "${project_root}/.git" ]'), |
| 69 | + 'detect-project.sh should NOT use -d for .git check' |
| 70 | + ); |
| 71 | +}); |
| 72 | + |
| 73 | +test('has command -v git fallback check', () => { |
| 74 | + const content = fs.readFileSync(detectProjectPath, 'utf8'); |
| 75 | + assert.ok( |
| 76 | + content.includes('command -v git'), |
| 77 | + 'detect-project.sh should check for git availability with command -v' |
| 78 | + ); |
| 79 | +}); |
| 80 | + |
| 81 | +test('uses git -C for safe directory operations', () => { |
| 82 | + const content = fs.readFileSync(detectProjectPath, 'utf8'); |
| 83 | + assert.ok( |
| 84 | + content.includes('git -C'), |
| 85 | + 'detect-project.sh should use git -C for directory-scoped operations' |
| 86 | + ); |
| 87 | +}); |
| 88 | + |
| 89 | +// ────────────────────────────────────────────────────── |
| 90 | +// Group 2: Behavior test — -e vs -d |
| 91 | +// ────────────────────────────────────────────────────── |
| 92 | + |
| 93 | +console.log('\n--- Behavior test: -e vs -d ---'); |
| 94 | + |
| 95 | +const behaviorDir = createTempDir(); |
| 96 | + |
| 97 | +test('[ -d ] returns true for .git directory', () => { |
| 98 | + const dir = path.join(behaviorDir, 'test-d-dir'); |
| 99 | + fs.mkdirSync(dir, { recursive: true }); |
| 100 | + fs.mkdirSync(path.join(dir, '.git')); |
| 101 | + const result = execSync(`bash -c '[ -d "${dir}/.git" ] && echo yes || echo no'`).toString().trim(); |
| 102 | + assert.strictEqual(result, 'yes'); |
| 103 | +}); |
| 104 | + |
| 105 | +test('[ -d ] returns false for .git file', () => { |
| 106 | + const dir = path.join(behaviorDir, 'test-d-file'); |
| 107 | + fs.mkdirSync(dir, { recursive: true }); |
| 108 | + fs.writeFileSync(path.join(dir, '.git'), 'gitdir: /some/path\n'); |
| 109 | + const result = execSync(`bash -c '[ -d "${dir}/.git" ] && echo yes || echo no'`).toString().trim(); |
| 110 | + assert.strictEqual(result, 'no'); |
| 111 | +}); |
| 112 | + |
| 113 | +test('[ -e ] returns true for .git directory', () => { |
| 114 | + const dir = path.join(behaviorDir, 'test-e-dir'); |
| 115 | + fs.mkdirSync(dir, { recursive: true }); |
| 116 | + fs.mkdirSync(path.join(dir, '.git')); |
| 117 | + const result = execSync(`bash -c '[ -e "${dir}/.git" ] && echo yes || echo no'`).toString().trim(); |
| 118 | + assert.strictEqual(result, 'yes'); |
| 119 | +}); |
| 120 | + |
| 121 | +test('[ -e ] returns true for .git file', () => { |
| 122 | + const dir = path.join(behaviorDir, 'test-e-file'); |
| 123 | + fs.mkdirSync(dir, { recursive: true }); |
| 124 | + fs.writeFileSync(path.join(dir, '.git'), 'gitdir: /some/path\n'); |
| 125 | + const result = execSync(`bash -c '[ -e "${dir}/.git" ] && echo yes || echo no'`).toString().trim(); |
| 126 | + assert.strictEqual(result, 'yes'); |
| 127 | +}); |
| 128 | + |
| 129 | +test('[ -e ] returns false when .git does not exist', () => { |
| 130 | + const dir = path.join(behaviorDir, 'test-e-none'); |
| 131 | + fs.mkdirSync(dir, { recursive: true }); |
| 132 | + const result = execSync(`bash -c '[ -e "${dir}/.git" ] && echo yes || echo no'`).toString().trim(); |
| 133 | + assert.strictEqual(result, 'no'); |
| 134 | +}); |
| 135 | + |
| 136 | +cleanupDir(behaviorDir); |
| 137 | + |
| 138 | +// ────────────────────────────────────────────────────── |
| 139 | +// Group 3: E2E test — detect-project.sh with worktree .git file |
| 140 | +// ────────────────────────────────────────────────────── |
| 141 | + |
| 142 | +console.log('\n--- E2E: detect-project.sh with worktree .git file ---'); |
| 143 | + |
| 144 | +test('detect-project.sh sets PROJECT_NAME and non-global PROJECT_ID for worktree', () => { |
| 145 | + const testDir = createTempDir(); |
| 146 | + |
| 147 | + try { |
| 148 | + // Create a "main" repo with git init so we have real git structures |
| 149 | + const mainRepo = path.join(testDir, 'main-repo'); |
| 150 | + fs.mkdirSync(mainRepo, { recursive: true }); |
| 151 | + execSync('git init', { cwd: mainRepo, stdio: 'pipe' }); |
| 152 | + execSync('git commit --allow-empty -m "init"', { |
| 153 | + cwd: mainRepo, |
| 154 | + stdio: 'pipe', |
| 155 | + env: { |
| 156 | + ...process.env, |
| 157 | + GIT_AUTHOR_NAME: 'Test', |
| 158 | + GIT_AUTHOR_EMAIL: '[email protected]', |
| 159 | + GIT_COMMITTER_NAME: 'Test', |
| 160 | + GIT_COMMITTER_EMAIL: '[email protected]' |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + // Create a worktree-like directory with .git as a file |
| 165 | + const worktreeDir = path.join(testDir, 'my-worktree'); |
| 166 | + fs.mkdirSync(worktreeDir, { recursive: true }); |
| 167 | + |
| 168 | + // Set up the worktree directory structure in the main repo |
| 169 | + const worktreesDir = path.join(mainRepo, '.git', 'worktrees', 'my-worktree'); |
| 170 | + fs.mkdirSync(worktreesDir, { recursive: true }); |
| 171 | + |
| 172 | + // Create the gitdir file and commondir in the worktree metadata |
| 173 | + const mainGitDir = path.join(mainRepo, '.git'); |
| 174 | + fs.writeFileSync( |
| 175 | + path.join(worktreesDir, 'commondir'), |
| 176 | + '../..\n' |
| 177 | + ); |
| 178 | + fs.writeFileSync( |
| 179 | + path.join(worktreesDir, 'HEAD'), |
| 180 | + fs.readFileSync(path.join(mainGitDir, 'HEAD'), 'utf8') |
| 181 | + ); |
| 182 | + |
| 183 | + // Write .git file in the worktree directory (this is what git worktree creates) |
| 184 | + fs.writeFileSync( |
| 185 | + path.join(worktreeDir, '.git'), |
| 186 | + `gitdir: ${worktreesDir}\n` |
| 187 | + ); |
| 188 | + |
| 189 | + // Source detect-project.sh from the worktree directory and capture results |
| 190 | + const script = ` |
| 191 | + export CLAUDE_PROJECT_DIR="${worktreeDir}" |
| 192 | + export HOME="${testDir}" |
| 193 | + source "${detectProjectPath}" |
| 194 | + echo "PROJECT_NAME=\${PROJECT_NAME}" |
| 195 | + echo "PROJECT_ID=\${PROJECT_ID}" |
| 196 | + `; |
| 197 | + |
| 198 | + const result = execSync(`bash -c '${script.replace(/'/g, "'\\''")}'`, { |
| 199 | + cwd: worktreeDir, |
| 200 | + timeout: 10000, |
| 201 | + env: { |
| 202 | + ...process.env, |
| 203 | + HOME: testDir, |
| 204 | + CLAUDE_PROJECT_DIR: worktreeDir |
| 205 | + } |
| 206 | + }).toString(); |
| 207 | + |
| 208 | + const lines = result.trim().split('\n'); |
| 209 | + const vars = {}; |
| 210 | + for (const line of lines) { |
| 211 | + const match = line.match(/^(PROJECT_NAME|PROJECT_ID)=(.*)$/); |
| 212 | + if (match) { |
| 213 | + vars[match[1]] = match[2]; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + assert.ok( |
| 218 | + vars.PROJECT_NAME && vars.PROJECT_NAME.length > 0, |
| 219 | + `PROJECT_NAME should be set, got: "${vars.PROJECT_NAME || ''}"` |
| 220 | + ); |
| 221 | + assert.ok( |
| 222 | + vars.PROJECT_ID && vars.PROJECT_ID !== 'global', |
| 223 | + `PROJECT_ID should not be "global", got: "${vars.PROJECT_ID || ''}"` |
| 224 | + ); |
| 225 | + } finally { |
| 226 | + cleanupDir(testDir); |
| 227 | + } |
| 228 | +}); |
| 229 | + |
| 230 | +// ────────────────────────────────────────────────────── |
| 231 | +// Summary |
| 232 | +// ────────────────────────────────────────────────────── |
| 233 | + |
| 234 | +console.log('\n=== Test Results ==='); |
| 235 | +console.log(`Passed: ${passed}`); |
| 236 | +console.log(`Failed: ${failed}`); |
| 237 | +console.log(`Total: ${passed + failed}\n`); |
| 238 | + |
| 239 | +process.exit(failed > 0 ? 1 : 0); |
0 commit comments