import * as fs from 'fs'; import * as path from 'path'; import { PolicyValidator, GitPRContextGatherer } from './src'; import { parsePolicyV2 } from '@nihal1983/core'; import { execSync } from 'child_process'; describe('Public Repository Integration Test', () => { const PUBLIC_REPO_PATH = path.resolve(__dirname, '../../../vulnerable-test-repo'); const POLICY_PATH = path.join(PUBLIC_REPO_PATH, 'CODE-POLICY.md'); // Skip if repo doesn't exist (e.g. in CI or clean environment) if (!fs.existsSync(PUBLIC_REPO_PATH)) { console.warn('⚠️ Repository not found. Skipping integration tests.'); it('should skip tests when repo is missing', () => { expect(true).toBe(true); }); return; } let validator: PolicyValidator; beforeAll(() => { // Load and parse policy const policyContent = fs.readFileSync(POLICY_PATH, 'utf-8'); const parseResult = parsePolicyV2(policyContent); if (!parseResult.success) { throw new Error(`Failed to parse policy: ${JSON.stringify(parseResult.errors)}`); } validator = new PolicyValidator(parseResult.policy!); }); afterAll(() => { // Ensure we switch back to vulnerable branch to avoid dirty state try { execSync('git checkout vulnerable', { cwd: PUBLIC_REPO_PATH, stdio: 'ignore' }); } catch (e) { // ignore } }); /** * Helper to validate a branch */ async function validateBranch(branchName: string, baseBranch: string = 'vulnerable') { execSync(`git checkout ${branchName}`, { cwd: PUBLIC_REPO_PATH, stdio: 'ignore' }); const gatherer = new GitPRContextGatherer({ repoPath: PUBLIC_REPO_PATH, branch: branchName, baseBranch: baseBranch }); const context = await gatherer.gatherContext(); return await validator.validate(context); } it('should detect violations in the vulnerable branch', async () => { const result = await validateBranch('vulnerable', 'vulnerable'); // We expect violations or at least the process to complete expect(result.status).toBeDefined(); }, 30000); // Long timeout for git operations it('should show fewer/different violations in the fixed branch', async () => { const result = await validateBranch('fixed', 'vulnerable'); expect(result.status).toBeDefined(); }, 30000); });