import assert from 'node:assert/strict'; import { Worker } from 'node:worker_threads'; import { generateMap } from '../src/mapPipeline.js'; import { createWorldMap } from '../src/worldMap.js'; import { buildMaximumProductionLasso, derivePatchSeed } from './production-fixtures.mjs'; const initial = generateMap(114514); const world = createWorldMap(initial); const rect = buildMaximumProductionLasso(world); const terrainType = initial.terrainTemplate?.terrainType || initial.terrainDebug?.terrainType || 'auto'; const candidatePlan = [0, 1, 2].map((variant, index) => ({ candidateId: `r11.6-large:${variant}`, candidateOrdinal: index + 1, variant, seed: derivePatchSeed(world.seed, terrainType, variant), })); const worker = new Worker(new URL('./browser-worker-node-shim.mjs', import.meta.url), { type: 'module' }); const startedAt = performance.now(); const result = await new Promise((resolve, reject) => { const timeoutMs = 540_000; const timer = setTimeout(() => { reject(new Error(`r11.6 max-range best-of timed out after ${timeoutMs / 1000} seconds`)); void worker.terminate().catch(() => {}); }, timeoutMs); worker.on('message', (message) => { if (message.id !== 1 || message.type === 'progress') return; clearTimeout(timer); if (!message.ok) reject(new Error(message.error || message.code || 'worker failed')); else resolve(message.result); }); worker.on('error', (error) => { clearTimeout(timer); reject(error); }); worker.postMessage({ id: 1, world, rect, options: { patchMode: 'expansion', terrainType, variant: 0, seed: candidatePlan[0].seed, maxQualityRetries: 0, qualityTerrainAttempts: 1, acceptBestAvailableQuality: false, includeSeamVisualization: false, }, search: { searchId: 'r11.6-large-bestof-quality', operationId: 'r11.6-large-bestof-quality', committedRevision: 1, workerEpoch: 1, executionAttempt: 1, totalCandidateCount: 3, selectBestCandidate: true, draftSelection: true, parallelDrafts: false, candidatePlan, }, }); }); const elapsedMs = performance.now() - startedAt; assert.equal(result?.ok, true, 'max-range best-of must produce a publishable full-production candidate'); assert.equal(result?.bestOfCandidates, true, 'multiple-candidate highest-quality selection remains enabled'); assert.equal(result?.candidateQuality?.hardPass, true, 'selected candidate must pass full quality'); assert.equal(result?.candidateQuality?.finalMerge?.hardPass, true, 'selected whole-selection merge must pass'); assert.equal(result?.seamDiagnostics?.hardPass, true, 'selected candidate must pass seam audit'); const attempts = result.searchAttempts || []; const fullAttempts = attempts.filter((a) => ['evaluated', 'success'].includes(a.status)); assert.equal(fullAttempts.length, 3, 'overlapping admissible bounds compare all three complete candidates'); assert.equal(result?.draftSelection?.fullGenerationPassCount, 3, 'winner publication must not perform a fourth rematerialization generation'); const maxScore = Math.max(...fullAttempts.map((a) => Number(a.candidateQuality?.finalMerge?.score ?? -Infinity))); assert(Math.abs(Number(result.candidateQuality.finalMerge.score) - maxScore) < 1e-12, 'selected candidate must be the highest final-production quality among full candidates'); assert(['admissible-bound-cached-winner', 'admissible-bound-winner-live'].includes(result?.draftSelection?.selectionReason), 'winner must publish either directly from the live exact Production state or from retained exact replay state, never by a fourth full regeneration'); const req = result.candidateQuality.finalMerge.transportRequirements || {}; const cls = result.candidateQuality.finalMerge.transportClasses || {}; for (const key of ['national', 'expressway', 'railTrunk']) { if (!req[key]?.demand) continue; assert((cls[key]?.cells || 0) >= (req[key]?.minCells || 0), `${key}: routed coverage floor must be met`); assert((cls[key]?.paths || 0) > 0, `${key}: demanded class must exist`); } assert(Object.entries(req).some(([key, value]) => value?.demand && (cls[key]?.paths || 0) < (value?.minPaths || 0) && (cls[key]?.cells || 0) >= (value?.minCells || 0)), 'fixture must exercise representation-independent quality: fewer merged paths than diagnostic minPaths while routed coverage still passes'); const compactReq = fullAttempts.find((a) => a.candidateQuality?.finalMerge?.transportRequirements)?.candidateQuality?.finalMerge?.transportRequirements || {}; for (const value of Object.values(compactReq)) { assert.equal(value.required, value.demand, 'compact Worker diagnostics must mirror the real demand field'); } console.log(JSON.stringify({ ok: true, elapsedMs: Math.round(elapsedMs), selectedVariant: result.actualVariant, selectedScore: result.candidateQuality.finalMerge.score, scores: fullAttempts.map((a) => ({ variant: a.variant, score: a.candidateQuality?.finalMerge?.score })), fullGenerationPassCount: result.draftSelection.fullGenerationPassCount, selectionReason: result.draftSelection.selectionReason, transportClasses: cls, transportRequirements: req, }, null, 2)); await worker.terminate(); process.exit(0);