import {spawnSync} from 'node:child_process'; import {hasCompleteFlatTapReport, hasPassingTapContracts} from '../check_tap_contracts.mjs'; // These are seven pre-existing file-level Node scripts, not seven unit cases // or a claim of coverage over the complete DjVu implementation. const contracts = [ ['only', 'PDF and DjVu primary reading, no legacy image fallback, OCR retention passed'], ['png', 'Historical PNG metadata remains verifiable; document image rendering is rejected'], ['range', 'DjVu range bounds, identity, retries, caching and cancellation passed'], ['range_fallback', 'ignored Range falls back to full original, never page images'], ['scheduler', 'DjVu scheduler cancellation, ordering, deduplication, LRU and closure passed'], ['source_integrity', 'DjVu dataset source size, hash, cancellation and map-failure guards passed'], ['worker', 'DjVu Worker bounds, errors, timeout, cancellation and geometry checks passed'] ]; const required = contracts.map(([name]) => ({name: `tests/reader/djvu/test_reader_djvu_${name}.mjs`})); const result = spawnSync(process.execPath, ['--test', '--test-concurrency=1', '--test-reporter=tap', ...required.map(({name}) => name)], {encoding: 'utf8'}); if (result.stdout) process.stdout.write(result.stdout); if (result.stderr) process.stderr.write(result.stderr); if (result.error) throw result.error; const audit = spawnSync(process.execPath, ['--test', '--test-reporter=tap', 'tests/reader/djvu/test_reader_djvu_audit.mjs', 'tests/reader/djvu/test_reader_djvu_text_runtime.mjs', 'tests/reader/djvu/test_reader_djvu_engine_cleanup.mjs'], {encoding: 'utf8'}); if (audit.error) throw audit.error; const auditComplete = audit.status === 0 && hasCompleteFlatTapReport(audit.stdout || ''); if (!auditComplete) process.stderr.write((audit.stdout || '') + (audit.stderr || '')); const lines = (result.stdout || '').split(/\r?\n/); const complete = contracts.every(([name, marker]) => { const subtest = `# Subtest: tests/reader/djvu/test_reader_djvu_${name}.mjs`; const index = lines.indexOf(subtest); return index > 0 && lines[index - 1].startsWith('# ' + marker) && (lines[index - 1] === '# ' + marker || lines[index - 1].startsWith('# ' + marker + ' ')); }); // Coverage is deliberately scoped to the range module, using its original // synthetic transport regression plus the named boundary contract. const minimum = [100, 88, 80]; const boundary = spawnSync(process.execPath, ['--test', '--test-reporter=tap', '--experimental-test-coverage', '--test-coverage-include=**/static/reader/pdf/reader-djvu-range.mjs', ...minimum.map((value, index) => `--test-coverage-${['lines', 'branches', 'functions'][index]}=${value}`), 'tests/reader/djvu/test_reader_djvu_range.mjs', 'tests/reader/djvu/test_reader_djvu_range_boundaries.mjs'], {encoding: 'utf8'}); if (boundary.stdout) process.stdout.write(boundary.stdout); if (boundary.stderr) process.stderr.write(boundary.stderr); if (boundary.error) throw boundary.error; const boundaryName = 'reads reject invalid lengths and offsets without dispatching new windows'; const boundaryLines = (boundary.stdout || '').split(/\r?\n/); const boundaryRequired = [{name: 'tests/reader/djvu/test_reader_djvu_range.mjs', marker: 'DjVu range bounds, identity, retries, caching and cancellation passed'}, {name: boundaryName}, {name:'Content-Range decimal coordinates are bounded and identity-pinned'}]; const coverageRows = boundaryLines.flatMap(line => { const match = /^\s*#\s*reader-djvu-range\.mjs\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|/.exec(line); return match ? [match.slice(1).map(Number)] : []; }); const boundaryComplete = boundary.status === 0 && hasPassingTapContracts(boundary.stdout || '', boundaryRequired) && hasCompleteFlatTapReport(boundary.stdout || '') && ['# start of coverage report', '# end of coverage report'].every(marker => boundaryLines.filter(line => line === marker).length === 1) && coverageRows.length === 1 && coverageRows[0].every((value, index) => value >= minimum[index]); // Independently gate the actual adapter's typed worker/message boundary. This // is a module-level minimum, not coverage of the complete DjVu Reader. const workerMinimum = [100, 93, 100]; const workerName = 'tests/reader/djvu/test_reader_djvu_worker.mjs'; const worker = spawnSync(process.execPath, ['--test', '--test-concurrency=1', '--test-reporter=tap', '--experimental-test-coverage', '--test-coverage-include=**/static/reader/pdf/reader-djvu-worker.mjs', ...workerMinimum.map((value, index) => `--test-coverage-${['lines', 'branches', 'functions'][index]}=${value}`), workerName], {encoding: 'utf8'}); if (worker.stdout) process.stdout.write(worker.stdout); if (worker.stderr) process.stderr.write(worker.stderr); if (worker.error) throw worker.error; const workerLines = (worker.stdout || '').split(/\r?\n/); const workerRows = workerLines.flatMap(line => { const match = /^\s*#\s*reader-djvu-worker\.mjs\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|/.exec(line); return match ? [match.slice(1).map(Number)] : []; }); const workerComplete = worker.status === 0 && hasPassingTapContracts(worker.stdout || '', [{name: workerName, marker:'DjVu Worker bounds, errors, timeout, cancellation and geometry checks passed'}]) && hasCompleteFlatTapReport(worker.stdout || '') && ['# start of coverage report', '# end of coverage report'].every(marker => workerLines.filter(line => line === marker).length === 1) && workerRows.length === 1 && workerRows[0].every((value, index) => value >= workerMinimum[index]); // Gate the scheduler separately: this measures only queue/cache behavior, not // the whole Reader. Keep the original range and worker thresholds unchanged. const schedulerMinimum = [100, 92, 100]; const schedulerName = 'tests/reader/djvu/test_reader_djvu_scheduler.mjs'; const scheduler = spawnSync(process.execPath, ['--test', '--test-concurrency=1', '--test-reporter=tap', '--experimental-test-coverage', '--test-coverage-include=**/static/reader/pdf/reader-djvu-scheduler.mjs', ...schedulerMinimum.map((value, index) => `--test-coverage-${['lines', 'branches', 'functions'][index]}=${value}`), schedulerName], {encoding: 'utf8'}); if (scheduler.stdout) process.stdout.write(scheduler.stdout); if (scheduler.stderr) process.stderr.write(scheduler.stderr); if (scheduler.error) throw scheduler.error; const schedulerLines = (scheduler.stdout || '').split(/\r?\n/); const schedulerRows = schedulerLines.flatMap(line => { const match = /^\s*#\s*reader-djvu-scheduler\.mjs\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|/.exec(line); return match ? [match.slice(1).map(Number)] : []; }); const schedulerComplete = scheduler.status === 0 && hasPassingTapContracts(scheduler.stdout || '', [{name: schedulerName, marker:'DjVu scheduler cancellation, ordering, deduplication, LRU and closure passed'}]) && hasCompleteFlatTapReport(scheduler.stdout || '') && ['# start of coverage report', '# end of coverage report'].every(marker => schedulerLines.filter(line => line === marker).length === 1) && schedulerRows.length === 1 && schedulerRows[0].every((value, index) => value >= schedulerMinimum[index]); // The PDF-shaped document adapter has its own geometry/outline/render gate. // This is adapter-only V8 coverage and does not stand for full Reader coverage. const adapterMinimum = [99, 90, 100]; const adapter = spawnSync(process.execPath, ['--test', '--test-concurrency=1', '--test-reporter=tap', '--experimental-test-coverage', '--test-coverage-include=**/static/reader/pdf/reader-djvu-adapter.mjs', ...adapterMinimum.map((value, index) => `--test-coverage-${['lines', 'branches', 'functions'][index]}=${value}`), workerName], {encoding: 'utf8'}); if (adapter.stdout) process.stdout.write(adapter.stdout); if (adapter.stderr) process.stderr.write(adapter.stderr); if (adapter.error) throw adapter.error; const adapterLines = (adapter.stdout || '').split(/\r?\n/); const adapterRows = adapterLines.flatMap(line => { const match = /^\s*#\s*reader-djvu-adapter\.mjs\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|/.exec(line); return match ? [match.slice(1).map(Number)] : []; }); const adapterComplete = adapter.status === 0 && hasPassingTapContracts(adapter.stdout || '', [{name: workerName, marker:'DjVu Worker bounds, errors, timeout, cancellation and geometry checks passed'}]) && hasCompleteFlatTapReport(adapter.stdout || '') && ['# start of coverage report', '# end of coverage report'].every(marker => adapterLines.filter(line => line === marker).length === 1) && adapterRows.length === 1 && adapterRows[0].every((value, index) => value >= adapterMinimum[index]); // Exact per-module source identity guard: real rejection on malformed inputs // must stay in CI, without claiming coverage for the complete loader. const identityMinimum = [100, 96, 100]; const identityName = 'tests/reader/djvu/test_reader_djvu_source_integrity.mjs'; const identity = spawnSync(process.execPath, ['--test', '--test-concurrency=1', '--test-reporter=tap', '--experimental-test-coverage', '--test-coverage-include=**/static/reader/pdf/reader-djvu-identity.mjs', ...identityMinimum.map((value, index) => `--test-coverage-${['lines', 'branches', 'functions'][index]}=${value}`), identityName], {encoding: 'utf8'}); if (identity.stdout) process.stdout.write(identity.stdout); if (identity.stderr) process.stderr.write(identity.stderr); if (identity.error) throw identity.error; const identityLines = (identity.stdout || '').split(/\r?\n/); const identityRows = identityLines.flatMap(line => { const match = /^\s*#\s*reader-djvu-identity\.mjs\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|/.exec(line); return match ? [match.slice(1).map(Number)] : []; }); const identityComplete = identity.status === 0 && hasPassingTapContracts(identity.stdout || '', [{name: identityName, marker:'DjVu dataset source size, hash, cancellation and map-failure guards passed'}]) && hasCompleteFlatTapReport(identity.stdout || '') && ['# start of coverage report', '# end of coverage report'].every(marker => identityLines.filter(line => line === marker).length === 1) && identityRows.length === 1 && identityRows[0].every((value, index) => value >= identityMinimum[index]); // Loader-only coverage combines its two existing real source/range scripts; // it includes the retained legacy OCR compatibility, not OCR production. const loaderMinimum = [100, 85, 100]; const loaderRequired = [ {name: identityName, marker:'DjVu dataset source size, hash, cancellation and map-failure guards passed'}, {name:'tests/reader/djvu/test_reader_djvu_range_fallback.mjs', marker:'ignored Range falls back to full original, never page images'} ]; const loader = spawnSync(process.execPath, ['--test', '--test-concurrency=1', '--test-reporter=tap', '--experimental-test-coverage', '--test-coverage-include=**/static/reader/pdf/reader-djvu-loader.mjs', ...loaderMinimum.map((value, index) => `--test-coverage-${['lines', 'branches', 'functions'][index]}=${value}`), ...loaderRequired.map(({name}) => name)], {encoding:'utf8'}); if (loader.stdout) process.stdout.write(loader.stdout); if (loader.stderr) process.stderr.write(loader.stderr); if (loader.error) throw loader.error; const loaderLines = (loader.stdout || '').split(/\r?\n/); const loaderRows = loaderLines.flatMap(line => { const match = /^\s*#\s*reader-djvu-loader\.mjs\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|\s*(\d+(?:\.\d+)?)\s*\|/.exec(line); return match ? [match.slice(1).map(Number)] : []; }); const loaderComplete = loader.status === 0 && hasPassingTapContracts(loader.stdout || '', loaderRequired) && hasCompleteFlatTapReport(loader.stdout || '') && ['# start of coverage report', '# end of coverage report'].every(marker => loaderLines.filter(line => line === marker).length === 1) && loaderRows.length === 1 && loaderRows[0].every((value, index) => value >= loaderMinimum[index]); if (result.status !== 0 || !complete || !auditComplete || !hasPassingTapContracts(result.stdout || '', required) || !boundaryComplete || !workerComplete || !schedulerComplete || !adapterComplete || !identityComplete || !loaderComplete || !hasCompleteFlatTapReport(result.stdout || '')) { console.error('DjVu contracts require seven named scripts, completion markers and zero skipped/TODO'); process.exitCode = 1; }