Files
hapi/cli/src/claude/utils/path.test.ts
T

104 lines
4.0 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { getProjectPath } from './path';
import { join, resolve } from 'node:path';
vi.mock('node:os', () => ({
homedir: vi.fn(() => '/home/user')
}));
// Store original env
const originalEnv = process.env;
function expectedProjectPath(workingDir: string, claudeConfigDir = join('/home/user', '.claude')): string {
return join(
claudeConfigDir,
'projects',
resolve(workingDir).replace(/[^a-zA-Z0-9]/g, '-')
);
}
describe('getProjectPath', () => {
beforeEach(() => {
// Reset process.env to a clean state
process.env = { ...originalEnv };
delete process.env.CLAUDE_CONFIG_DIR;
});
afterEach(() => {
// Restore original env
process.env = originalEnv;
});
it('should replace slashes with hyphens in the project path', () => {
const workingDir = '/Users/steve/projects/my-app';
const result = getProjectPath(workingDir);
expect(result).toBe(expectedProjectPath(workingDir));
});
it('should replace dots with hyphens in the project path', () => {
const workingDir = '/Users/steve/projects/app.test.js';
const result = getProjectPath(workingDir);
expect(result).toBe(expectedProjectPath(workingDir));
});
it('should handle paths with both slashes and dots', () => {
const workingDir = '/var/www/my.site.com/public';
const result = getProjectPath(workingDir);
expect(result).toBe(expectedProjectPath(workingDir));
});
it('should replace underscores with hyphens in the project path', () => {
const workingDir = '/data/github/hapi__worktrees/ime';
const result = getProjectPath(workingDir);
expect(result).toBe(expectedProjectPath(workingDir));
});
it('should handle relative paths by resolving them first', () => {
const workingDir = './my-project';
const result = getProjectPath(workingDir);
expect(result).toContain(join('/home/user', '.claude', 'projects'));
expect(result).toContain('my-project');
});
it('should handle empty directory path', () => {
const workingDir = '';
const result = getProjectPath(workingDir);
expect(result).toContain(join('/home/user', '.claude', 'projects'));
});
describe('CLAUDE_CONFIG_DIR support', () => {
it('should use default .claude directory when CLAUDE_CONFIG_DIR is not set', () => {
const workingDir = '/Users/steve/projects/my-app';
const result = getProjectPath(workingDir);
expect(result).toBe(expectedProjectPath(workingDir));
});
it('should use CLAUDE_CONFIG_DIR when set', () => {
process.env.CLAUDE_CONFIG_DIR = '/custom/claude/config';
const workingDir = '/Users/steve/projects/my-app';
const result = getProjectPath(workingDir);
expect(result).toBe(expectedProjectPath(workingDir, '/custom/claude/config'));
});
it('should handle relative CLAUDE_CONFIG_DIR path', () => {
process.env.CLAUDE_CONFIG_DIR = './config/claude';
const workingDir = '/Users/steve/projects/my-app';
const result = getProjectPath(workingDir);
expect(result).toBe(expectedProjectPath(workingDir, './config/claude'));
});
it('should fallback to default when CLAUDE_CONFIG_DIR is empty string', () => {
process.env.CLAUDE_CONFIG_DIR = '';
const workingDir = '/Users/steve/projects/my-app';
const result = getProjectPath(workingDir);
expect(result).toBe(expectedProjectPath(workingDir));
});
it('should handle CLAUDE_CONFIG_DIR with trailing slash', () => {
process.env.CLAUDE_CONFIG_DIR = '/custom/claude/config/';
const workingDir = '/Users/steve/projects/my-app';
const result = getProjectPath(workingDir);
expect(result).toBe(expectedProjectPath(workingDir, '/custom/claude/config/'));
});
});
});