import { test } from 'node:test'; import assert from 'node:assert/strict'; import { mkdtemp, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { execFileSync } from 'node:child_process'; import { getGitBranch, getGitStatus } from '../dist/git.js'; test('getGitBranch returns null when cwd is undefined', async () => { const result = await getGitBranch(undefined); assert.equal(result, null); }); test('getGitBranch returns null for non-git directory', async () => { const dir = await mkdtemp(path.join(tmpdir(), 'claude-hud-nogit-')); try { const result = await getGitBranch(dir); assert.equal(result, null); } finally { await rm(dir, { recursive: true, force: true }); } }); test('getGitBranch returns branch name for git directory', async () => { const dir = await mkdtemp(path.join(tmpdir(), 'claude-hud-git-')); try { execFileSync('git', ['init'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['config', 'user.email', 'test@test.com'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['config', 'user.name', 'Test'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['commit', '--allow-empty', '-m', 'init'], { cwd: dir, stdio: 'ignore' }); const result = await getGitBranch(dir); assert.ok(result === 'main' || result === 'master', `Expected main or master, got ${result}`); } finally { await rm(dir, { recursive: true, force: true }); } }); test('getGitBranch returns custom branch name', async () => { const dir = await mkdtemp(path.join(tmpdir(), 'claude-hud-git-')); try { execFileSync('git', ['init'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['config', 'user.email', 'test@test.com'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['config', 'user.name', 'Test'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['commit', '--allow-empty', '-m', 'init'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['checkout', '-b', 'feature/test-branch'], { cwd: dir, stdio: 'ignore' }); const result = await getGitBranch(dir); assert.equal(result, 'feature/test-branch'); } finally { await rm(dir, { recursive: true, force: true }); } }); // getGitStatus tests test('getGitStatus returns null when cwd is undefined', async () => { const result = await getGitStatus(undefined); assert.equal(result, null); }); test('getGitStatus returns null for non-git directory', async () => { const dir = await mkdtemp(path.join(tmpdir(), 'claude-hud-nogit-')); try { const result = await getGitStatus(dir); assert.equal(result, null); } finally { await rm(dir, { recursive: true, force: true }); } }); test('getGitStatus returns clean state for clean repo', async () => { const dir = await mkdtemp(path.join(tmpdir(), 'claude-hud-git-')); try { execFileSync('git', ['init'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['config', 'user.email', 'test@test.com'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['config', 'user.name', 'Test'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['commit', '--allow-empty', '-m', 'init'], { cwd: dir, stdio: 'ignore' }); const result = await getGitStatus(dir); assert.ok(result?.branch === 'main' || result?.branch === 'master'); assert.equal(result?.isDirty, false); assert.equal(result?.ahead, 0); assert.equal(result?.behind, 0); } finally { await rm(dir, { recursive: true, force: true }); } }); test('getGitStatus detects dirty state', async () => { const dir = await mkdtemp(path.join(tmpdir(), 'claude-hud-git-')); try { execFileSync('git', ['init'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['config', 'user.email', 'test@test.com'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['config', 'user.name', 'Test'], { cwd: dir, stdio: 'ignore' }); execFileSync('git', ['commit', '--allow-empty', '-m', 'init'], { cwd: dir, stdio: 'ignore' }); // Create uncommitted file await writeFile(path.join(dir, 'dirty.txt'), 'uncommitted change'); const result = await getGitStatus(dir); assert.equal(result?.isDirty, true); } finally { await rm(dir, { recursive: true, force: true }); } });