/**
* ProjectContextAnalyzer 테스트
*/
import * as path from 'path';
import { ProjectContextAnalyzer } from '../src/session/ProjectContextAnalyzer';
describe('ProjectContextAnalyzer', () => {
let analyzer: ProjectContextAnalyzer;
beforeEach(() => {
analyzer = new ProjectContextAnalyzer();
});
describe('analyzeProjectContext', () => {
test('Node.js 프로젝트 분석', async () => {
const projectPath = process.cwd(); // 현재 프로젝트 사용
const context = await analyzer.analyzeProjectContext(projectPath);
expect(context).toBeDefined();
expect(context.detection).toBeDefined();
expect(context.workspace).toBeDefined();
expect(context.git).toBeDefined();
expect(context.dependencies).toBeDefined();
expect(context.analysisTimestamp).toBeInstanceOf(Date);
// 프로젝트 감지 검증
expect(context.detection.projectRoot).toBe(projectPath);
expect(context.detection.confidence).toBeGreaterThan(0);
// 워크스페이스 구조 검증
expect(context.workspace.totalFiles).toBeGreaterThan(0);
expect(context.workspace.keyFiles).toContain('package.json');
});
test('package.json이 있는 프로젝트의 의존성 분석', async () => {
const projectPath = process.cwd();
const context = await analyzer.analyzeProjectContext(projectPath);
// Node.js 프로젝트라면 의존성이 있어야 함
if (context.detection.projectType.includes('nodejs')) {
expect(Object.keys(context.dependencies.productionDependencies).length).toBeGreaterThanOrEqual(0);
expect(Object.keys(context.dependencies.developmentDependencies).length).toBeGreaterThanOrEqual(0);
}
});
test('Git 저장소 분석', async () => {
const projectPath = process.cwd();
const context = await analyzer.analyzeProjectContext(projectPath);
// 현재 프로젝트가 Git 저장소라면
if (context.git.isGitRepository) {
expect(context.git.currentBranch).toBeDefined();
expect(Array.isArray(context.git.ignoredPatterns)).toBe(true);
}
});
test('존재하지 않는 경로에서 에러 발생', async () => {
const invalidPath = '/nonexistent/path';
await expect(analyzer.analyzeProjectContext(invalidPath))
.rejects
.toThrow();
});
});
describe('generateProjectSummary', () => {
test('프로젝트 요약 생성', async () => {
const projectPath = process.cwd();
const context = await analyzer.analyzeProjectContext(projectPath);
const summary = analyzer.generateProjectSummary(context);
expect(summary).toContain('🏗️ 프로젝트:');
expect(summary).toContain('📁 경로:');
expect(summary).toContain('🏷️ 타입:');
expect(summary).toContain('🎯 신뢰도:');
expect(summary).toContain('📊 워크스페이스:');
});
});
describe('워크스페이스 구조 분석', () => {
test('소스 디렉토리 식별', async () => {
const projectPath = process.cwd();
const context = await analyzer.analyzeProjectContext(projectPath);
// src 디렉토리가 있다면 소스 디렉토리로 식별되어야 함
if (context.workspace.sourceDirectories.length > 0) {
expect(context.workspace.sourceDirectories.some(dir =>
dir.includes('src') || dir.includes('source')
)).toBe(true);
}
});
test('키 파일들 식별', async () => {
const projectPath = process.cwd();
const context = await analyzer.analyzeProjectContext(projectPath);
// package.json이 있는 프로젝트라면 키 파일로 식별되어야 함
expect(context.workspace.keyFiles).toEqual(
expect.arrayContaining([expect.stringContaining('.json')])
);
});
});
describe('의존성 분석', () => {
test('프레임워크 감지', async () => {
const projectPath = process.cwd();
const context = await analyzer.analyzeProjectContext(projectPath);
// 의존성 분석 결과 구조 검증
expect(Array.isArray(context.dependencies.frameworksDetected)).toBe(true);
expect(Array.isArray(context.dependencies.majorLibraries)).toBe(true);
expect(Array.isArray(context.dependencies.buildTools)).toBe(true);
expect(Array.isArray(context.dependencies.testingFrameworks)).toBe(true);
});
test('빌드 도구 감지', async () => {
const projectPath = process.cwd();
const context = await analyzer.analyzeProjectContext(projectPath);
// TypeScript 프로젝트라면 빌드 도구가 감지되어야 함
if (context.detection.projectType.includes('typescript')) {
expect(context.dependencies.buildTools).toContain('TypeScript');
}
});
});
describe('에러 처리', () => {
test('읽기 권한이 없는 디렉토리 처리', async () => {
// 정상적인 프로젝트 경로로 테스트 (에러 로깅 확인)
const projectPath = process.cwd();
// 에러가 발생해도 분석이 계속되어야 함
const context = await analyzer.analyzeProjectContext(projectPath);
expect(context).toBeDefined();
});
test('빈 프로젝트 디렉토리 처리', async () => {
// 임시 빈 디렉토리 생성하여 테스트할 수 있지만,
// 여기서는 기본 구조 검증만 수행
const projectPath = process.cwd();
const context = await analyzer.analyzeProjectContext(projectPath);
expect(context.workspace).toBeDefined();
expect(context.workspace.totalFiles).toBeGreaterThanOrEqual(0);
expect(context.workspace.totalDirectories).toBeGreaterThanOrEqual(0);
});
});
});
// 통합 테스트
describe('ProjectContextAnalyzer 통합 테스트', () => {
test('전체 분석 파이프라인', async () => {
const analyzer = new ProjectContextAnalyzer();
const projectPath = process.cwd();
// 전체 분석 수행
const startTime = Date.now();
const context = await analyzer.analyzeProjectContext(projectPath);
const endTime = Date.now();
// 성능 검증 (5초 이내)
expect(endTime - startTime).toBeLessThan(5000);
// 결과 구조 완전성 검증
expect(context).toMatchObject({
detection: expect.any(Object),
workspace: expect.any(Object),
git: expect.any(Object),
dependencies: expect.any(Object),
analysisTimestamp: expect.any(Date),
analysisVersion: expect.any(String)
});
// 요약 생성 테스트
const summary = analyzer.generateProjectSummary(context);
expect(summary.length).toBeGreaterThan(100);
console.log('=== 프로젝트 컨텍스트 분석 결과 ===');
console.log(summary);
console.log(`분석 시간: ${endTime - startTime}ms`);
console.log('=====================================');
});
});