/* eslint-disable no-console */ import process from 'node:process'; import { discoverCatalogueCandidates, formatCatalogueIntegrityReport, validateCatalogueIntegrity, type CatalogueCandidate, } from '../src/modules/machines/catalogueIntegrity'; import * as blueprintModule from '../src/modules/machines/data/catalogueBlueprint'; import * as coreMachinesModule from '../src/modules/machines/data/coreMachines'; import * as dataIndexModule from '../src/modules/machines/data/index'; import * as registryModule from '../src/modules/machines/registry'; const MODULES: Array<{ name: string; exports: Record }> = [ { name: 'registry', exports: registryModule as unknown as Record }, { name: 'dataIndex', exports: dataIndexModule as unknown as Record }, { name: 'coreMachines', exports: coreMachinesModule as unknown as Record }, { name: 'catalogueBlueprint', exports: blueprintModule as unknown as Record }, ]; const candidates = dedupeCandidates( MODULES.flatMap((moduleInfo) => discoverCatalogueCandidates(moduleInfo.exports, moduleInfo.name, { aggregateRootSiblings: false, }), ), ); if (candidates.length === 0) { console.error('No machine catalogue candidates were discovered in registry/data modules.'); process.exitCode = 1; } else { console.log(`Discovered ${candidates.length} machine catalogue candidate(s).`); const failures: string[] = []; candidates.forEach((candidate) => { const summary = validateCatalogueIntegrity(candidate.machines, { requireAtLeastOneMachine: true, allowPlaceholderAssets: true, warnOnMissingCameraPresets: false, warnOnMissingAnimationMetadata: false, warnOnMissingExplodedMetadata: false, }); const label = `${candidate.name} (${candidate.machines.length} machine${ candidate.machines.length === 1 ? '' : 's' })`; if (summary.passed) { console.log( `✓ ${label}: ${summary.errors} errors, ${summary.warnings} warnings, ${summary.componentCount} components`, ); return; } failures.push(`${label}\n${formatCatalogueIntegrityReport(summary, { includeWarnings: true })}`); }); const blueprintCandidates = discoverCatalogueCandidates( blueprintModule as unknown as Record, 'catalogueBlueprint', { aggregateRootSiblings: true, }, ); const largestBlueprint = blueprintCandidates.toSorted( (a, b) => b.machines.length - a.machines.length, )[0]; if (!largestBlueprint) { failures.push( 'catalogueBlueprint\nNo machine-like entries were discovered in the 28-machine implementation blueprint.', ); } else if (largestBlueprint.machines.length < 28) { failures.push( `catalogueBlueprint\nExpected at least 28 blueprint entries, found ${largestBlueprint.machines.length} in ${largestBlueprint.name}.`, ); } else { console.log( `✓ catalogueBlueprint scale: ${largestBlueprint.machines.length} planned machine entries discovered in ${largestBlueprint.name}`, ); } if (failures.length > 0) { console.error('\nCatalogue validation failed:\n'); failures.forEach((failure, index) => { console.error(`${index + 1}. ${failure}\n`); }); process.exitCode = 1; } else { console.log('Catalogue validation completed successfully.'); } } function dedupeCandidates(candidatesToDedupe: CatalogueCandidate[]): CatalogueCandidate[] { const candidateByArray = new Map(); candidatesToDedupe.forEach((candidate) => { const existing = candidateByArray.get(candidate.machines); if ( !existing || candidate.machines.length > existing.machines.length || candidate.name.length < existing.name.length ) { candidateByArray.set(candidate.machines, candidate); } }); return Array.from(candidateByArray.values()).sort((a, b) => { if (b.machines.length !== a.machines.length) { return b.machines.length - a.machines.length; } return a.name.localeCompare(b.name); }); }