import assert from 'node:assert/strict'; import {copyFileSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync} from 'node:fs'; import {spawnSync} from 'node:child_process'; import {createHash} from 'node:crypto'; import {join, relative, resolve} from 'node:path'; assert.equal(process.argv.length, 3, 'usage: node pci-bridges-local.mjs '); const root = process.cwd(); const predecessorPath = resolve(process.argv[2]); const predecessor = JSON.parse(readFileSync(predecessorPath, 'utf8')); assert.equal(predecessor.status, 'K1435_PCI_CAPABILITIES_DEVELOPMENT_MATRIX_VALIDATED'); const dir = mkdtempSync(join(root, 'reports/os-implementation/K1440-pci-bridges-local-')); const sha = path => createHash('sha256').update(readFileSync(path)).digest('hex'); const artifact = path => ({path: relative(root, path), sha256: sha(path)}); const copy = (source, target) => { mkdirSync(join(target, '..'), {recursive: true}); copyFileSync(join(root, source), target); return artifact(target); }; const sourceRoot = join(dir, 'source'); const model = join(sourceRoot, 'os-pci-discovery/src/bridges.rs'); const fixture = join(sourceRoot, 'kernel/tests/os/pci_bridges.rs'); const noStdRoot = join(sourceRoot, 'no-std-root.rs'); mkdirSync(sourceRoot, {recursive: true}); writeFileSync(noStdRoot, '#![no_std]\n#[path = "os-pci-discovery/src/bridges.rs"]\npub mod pci_bridges;\n'); const sources = { model: copy('os-pci-discovery/src/bridges.rs', model), fixture: copy('kernel/tests/os/pci_bridges.rs', fixture), contract: copy('kernel/PCI_BRIDGE_CONTRACT.md', join(sourceRoot, 'kernel/PCI_BRIDGE_CONTRACT.md')), noStdRoot: artifact(noStdRoot), dtbReference: copy('kernel/src/dtb.rs', join(sourceRoot, 'kernel/src/dtb.rs')), harness: artifact(join(root, 'scripts/os-acceptance/pci-bridges-local.mjs')), }; const run = (name, program, args, expected) => { const result = spawnSync(program, args, {cwd: root, encoding: 'utf8'}); const log = join(dir, `${name}.log`); writeFileSync(log, `${result.stdout ?? ''}${result.stderr ?? ''}`); assert.equal(result.status, expected, `${name}: expected exit ${expected}, observed ${result.status}; ${artifact(log).path}`); return {exitCode: result.status, log: artifact(log)}; }; const binary = join(dir, 'pci-bridges-tests'); const tests = { compile: run('host-compile', 'rustc', ['--edition=2024', '--test', fixture, '-o', binary], 0), green: run('host-tests', binary, [], 0), target: run('aarch64-no-std-check', 'rustc', ['--edition=2024', '--crate-type=lib', '--target=aarch64-unknown-none', '--emit=metadata', noStdRoot, '-o', join(dir, 'model.rmeta')], 0), }; const greenText = readFileSync(join(dir, 'host-tests.log'), 'utf8'); assert.match(greenText, /os_pci_discovery_bridges_positive \.\.\. ok/); assert.match(greenText, /os_pci_discovery_bridges_negative \.\.\. ok/); assert.match(greenText, /2 passed; 0 failed/); const mutantRoot = join(dir, 'mutant'); const mutantModel = join(mutantRoot, 'os-pci-discovery/src/bridges.rs'); const mutantFixture = join(mutantRoot, 'kernel/tests/os/pci_bridges.rs'); copy('kernel/tests/os/pci_bridges.rs', mutantFixture); mkdirSync(join(mutantModel, '..'), {recursive: true}); const original = readFileSync(model, 'utf8'); const guard = 'if secondary == upstream.secondary {\n return Err(Error::SecondaryCollision);\n }'; assert.equal(original.split(guard).length, 2, 'parent-secondary guard must exist once'); writeFileSync(mutantModel, original.replace(guard, 'if false {\n return Err(Error::SecondaryCollision);\n }')); const mutant = artifact(mutantModel); const mutantBinary = join(dir, 'pci-bridges-mutant-tests'); tests.mutantCompile = run('mutant-compile', 'rustc', ['--edition=2024', '--test', mutantFixture, '-o', mutantBinary], 0); tests.red = run('mutant-tests', mutantBinary, [], 101); const redText = readFileSync(join(dir, 'mutant-tests.log'), 'utf8'); assert.match(redText, /os_pci_discovery_bridges_negative \.\.\. FAILED/); const report = { schema: 'turkos-k1440-pci-bridges-local/v1', status: 'K1436_K1439_LOCAL_VALIDATED', guestAccepted: false, productAccepted: false, predecessor: artifact(predecessorPath), sources, mutant, tests: {...tests, greenCount: 2, mutationRejected: true}, scope: 'A bounded bus tree accepts one root bridge and one nested bridge, assigns buses 1 and 2 to unique owners, and rejects a child secondary bus that collides with its parent without changing the prior tree. Two host tests and AArch64 no_std compilation passed; removing the collision guard made the negative test fail. This does not prove physical bridge enumeration.', }; const reportPath = join(dir, 'report.json'); writeFileSync(reportPath, JSON.stringify(report, null, 2) + '\n'); console.log(JSON.stringify({status: report.status, report: relative(root, reportPath), tests: report.tests.greenCount, mutationRejected: report.tests.mutationRejected}));