import assert from 'node:assert/strict' import { existsSync, readdirSync, readFileSync } from 'node:fs' import path from 'node:path' import test from 'node:test' import { fileURLToPath } from 'node:url' import { classifyChangedFiles } from './ci/detect-pr-changes.mjs' import { scanChangedPaths, scanDiffContent, scanWhitelabelSource } from './ci/check-pr-security.mjs' const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') function listWorkflowFiles() { const dirs = ['.github/workflows', '.gitea/workflows'] const files = [] for (const relDir of dirs) { const absDir = path.join(ROOT, relDir) if (!existsSync(absDir)) continue for (const entry of readdirSync(absDir, { withFileTypes: true })) { if (entry.isFile() && /\.(?:yml|yaml)$/u.test(entry.name)) { files.push(path.join(absDir, entry.name)) } } } return files } function validateWorkflowYaml(content, filePath) { const lines = content.split(/\r?\n/u) const topLevelKeys = new Set() const jobKeys = new Set() let inJobs = false let jobsIndent = -1 let blockScalarIndent = null for (let i = 0; i < lines.length; i++) { const raw = lines[i] const lineNo = i + 1 assert.ok(!raw.includes('\t'), `${filePath}:${lineNo} must not contain tabs`) const trimmed = raw.trim() if (!trimmed || trimmed.startsWith('#')) continue const indent = raw.search(/\S/u) if (blockScalarIndent !== null) { if (indent > blockScalarIndent) continue blockScalarIndent = null } if (indent === 0) { const topMatch = trimmed.match(/^([A-Za-z0-9_-]+)\s*:/u) assert.ok(topMatch, `${filePath}:${lineNo} invalid top-level YAML key: ${trimmed}`) const key = topMatch[1] assert.ok(!topLevelKeys.has(key), `${filePath}:${lineNo} duplicate top-level key: ${key}`) topLevelKeys.add(key) inJobs = key === 'jobs' jobsIndent = -1 } else if (inJobs) { if (jobsIndent === -1) jobsIndent = indent if (indent === jobsIndent) { const jobMatch = trimmed.match(/^([A-Za-z0-9_-]+)\s*:/u) assert.ok(jobMatch, `${filePath}:${lineNo} invalid job key: ${trimmed}`) assert.ok(!jobKeys.has(jobMatch[1]), `${filePath}:${lineNo} duplicate job key: ${jobMatch[1]}`) jobKeys.add(jobMatch[1]) } } if (/[|>][+-]?\s*(?:#.*)?$/u.test(trimmed)) { blockScalarIndent = indent } } for (const required of ['name', 'on', 'jobs']) { assert.ok(topLevelKeys.has(required), `${filePath} missing top-level key '${required}'`) } assert.ok(jobKeys.size > 0, `${filePath} must define at least one job`) return { topLevelKeys, jobKeys } } test('all workflow files are valid YAML and pin Node 24.22.1', () => { const files = listWorkflowFiles() assert.ok(files.length > 0, 'Expected at least one CI workflow file') for (const file of files) { const content = readFileSync(file, 'utf8') validateWorkflowYaml(content, path.relative(ROOT, file)) const setupNodeMatches = [...content.matchAll(/uses:\s*actions\/setup-node@[^\n]+[\s\S]*?node-version:\s*['"]?([^'"\s\n]+)['"]?/gu)] assert.ok(setupNodeMatches.length > 0, `${path.relative(ROOT, file)} must configure actions/setup-node`) for (const match of setupNodeMatches) { assert.equal(match[1], '24.22.1', `${path.relative(ROOT, file)} setup-node must pin 24.22.1`) } } }) test('Node 24, verify:node24, and dynamic Dockerfile migrations are aligned', () => { const rootPkg = JSON.parse(readFileSync(path.join(ROOT, 'package.json'), 'utf8')) const agentPkg = JSON.parse(readFileSync(path.join(ROOT, 'apps/detail-agent-pi/package.json'), 'utf8')) const dockerfile = readFileSync(path.join(ROOT, 'Dockerfile'), 'utf8') assert.match(rootPkg.engines?.node ?? '', /24\.22\.1/u) assert.match(agentPkg.engines?.node ?? '', /24\.22\.1/u) assert.equal(rootPkg.scripts?.['verify:node24'], 'node scripts/test-all.mjs') assert.equal(rootPkg.scripts?.['test:all'], 'node scripts/test-all.mjs') assert.match(dockerfile, /node:24\.22\.1-alpine@/u) assert.match(dockerfile, /fs\.readdirSync\(dir,\s*\{withFileTypes:true\}\)/u) assert.match(dockerfile, /migration\.sql/u) }) test('ci.yml defines modular PR gates, Rust disk hygiene, and valid package/script references', () => { const ciPath = path.join(ROOT, '.github/workflows/ci.yml') const ci = readFileSync(ciPath, 'utf8') const { jobKeys } = validateWorkflowYaml(ci, '.github/workflows/ci.yml') for (const job of ['changes', 'security', 'web', 'agent', 'rust']) { assert.ok(jobKeys.has(job), `ci.yml missing required job '${job}'`) } assert.match(ci, /pull_request:/u) assert.match(ci, /workflow_dispatch:/u) assert.match(ci, /CARGO_INCREMENTAL:\s*0\b/u) assert.match(ci, /CARGO_PROFILE_DEV_DEBUG:\s*0\b/u) assert.match(ci, /cargo check --locked --target x86_64-pc-windows-gnu -p photocraft --all-targets/u) assert.match(ci, /cargo test --locked -p photocraft-geom/u) assert.match(ci, /rm -rf target\/debug target\/x86_64-pc-windows-gnu\/debug/u) const rootPkg = JSON.parse(readFileSync(path.join(ROOT, 'package.json'), 'utf8')) const scripts = rootPkg.scripts ?? {} for (const match of ci.matchAll(/\bnpm run ([A-Za-z0-9:_-]+)/gu)) { const scriptName = match[1] assert.ok(Object.hasOwn(scripts, scriptName), `ci.yml references missing npm script '${scriptName}'`) } assert.ok(Object.hasOwn(scripts, 'test'), 'package.json must define test script for npm test') for (const match of ci.matchAll(/\bnode(?:\s+--test)?\s+([A-Za-z0-9_./*-]+\.mjs)\b/gu)) { const target = match[1] if (target.includes('*')) { const dir = path.join(ROOT, path.dirname(target)) const testFiles = readdirSync(dir).filter(name => name.endsWith('.test.mjs')) assert.ok(testFiles.length > 0, `ci.yml glob '${target}' matched no .test.mjs files`) } else { assert.ok(existsSync(path.join(ROOT, target)), `ci.yml references missing script '${target}'`) } } }) test('detect-pr-changes classifies scopes accurately and runs all on workflow_dispatch', () => { assert.deepEqual( classifyChangedFiles(['docs/development/pr-ci-pipeline.md'], 'pull_request'), { web: false, agent: false, rust: false, security: true }, ) assert.deepEqual( classifyChangedFiles(['app/api/health/route.ts'], 'pull_request'), { web: true, agent: false, rust: false, security: true }, ) assert.deepEqual( classifyChangedFiles(['apps/detail-agent-pi/src/agent.mjs'], 'pull_request'), { web: false, agent: true, rust: false, security: true }, ) assert.deepEqual( classifyChangedFiles(['evals/xiaoyi-golden/tests/cli.test.mjs'], 'pull_request'), { web: false, agent: true, rust: false, security: true }, ) assert.deepEqual( classifyChangedFiles(['apps/detail-photocraft/crates/geom/src/lib.rs'], 'pull_request'), { web: false, agent: false, rust: true, security: true }, ) assert.deepEqual( classifyChangedFiles([], 'workflow_dispatch'), { web: true, agent: true, rust: true, security: true }, ) }) test('check-pr-security blocks .env/.private/secrets and passes current repository whitelabel scan', () => { const pathViolations = scanChangedPaths([ '.env', '.env.production', '.env.example', '.private/token.txt', 'certs/server.pem', 'app/page.tsx', ]) assert.deepEqual( pathViolations.map(v => v.file), ['.env', '.env.production', '.private/token.txt', 'certs/server.pem'], ) const diff = [ '+++ b/lib/config.ts', '@@ -1,0 +1,4 @@', '+const ok = process.env.ADMIN_API_KEY', '+const safe = "build-time-key"', '+const leak = "ADMIN_API_KEY=\'supersecretvalue12345\'"', '+const gh = "ghp_1234567890abcdefghij123456"', ].join('\n') const diffViolations = scanDiffContent(diff) assert.deepEqual( diffViolations.map(v => v.type), ['hardcoded-sensitive-env', 'github-token'], ) assert.deepEqual(scanWhitelabelSource(ROOT), []) })