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('PR Verification (Enterprise Repo)', () => { const DEMO_REPO_PATH = path.resolve(__dirname, '../../../demo-enterprise-repo'); const POLICY_PATH = path.join(DEMO_REPO_PATH, 'CODE-POLICY.md'); // Skip if repo not found if (!fs.existsSync(DEMO_REPO_PATH)) { console.warn('⚠️ Demo enterprise repo not found. Skipping tests.'); it('should skip tests', () => { expect(true).toBe(true); }); return; } let validator: PolicyValidator; let branches: string[] = []; beforeAll(() => { const policyContent = fs.readFileSync(POLICY_PATH, 'utf-8'); const parseResult = parsePolicyV2(policyContent); if (!parseResult.success) throw new Error('Failed to parse policy'); validator = new PolicyValidator(parseResult.policy!); // Get branches const branchesOutput = execSync('git branch', { cwd: DEMO_REPO_PATH, encoding: 'utf-8' }); branches = branchesOutput .split('\n') .map(b => b.trim().replace('* ', '')) .filter(b => b && b !== 'master'); }); afterAll(() => { try { execSync('git checkout master', { cwd: DEMO_REPO_PATH, stdio: 'ignore' }); } catch (e) { } }); async function validateBranch(branchName: string) { execSync(`git checkout ${branchName}`, { cwd: DEMO_REPO_PATH, stdio: 'ignore' }); const gatherer = new GitPRContextGatherer({ repoPath: DEMO_REPO_PATH, branch: branchName, baseBranch: 'master' }); const context = await gatherer.gatherContext(); return await validator.validate(context); } it('should validate all extracted branches', async () => { if (branches.length === 0) { console.warn('No branches found to test'); return; } for (const branch of branches) { console.log(`Testing branch: ${branch}`); const result = await validateBranch(branch); expect(result.status).toBeDefined(); } }, 60000); });