#!/usr/bin/env node import { readFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { registerHooks } from 'node:module'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); registerHooks({ resolve(specifier, context, nextResolve) { if ( (specifier.startsWith('./') || specifier.startsWith('../')) && !/\.[a-z]+$/i.test(specifier) ) { return nextResolve(`${specifier}.ts`, context); } return nextResolve(specifier, context); }, load(url, context, nextLoad) { if (url.endsWith('.ts') && !url.endsWith('/Types.ts')) { const source = readFileSync(fileURLToPath(url), 'utf8').replace( /import\s*\{[^}]*\}\s*from\s*['"]\.\/Types['"];?/g, '', ); return { format: 'module-typescript', source, shortCircuit: true }; } return nextLoad(url, context); }, }); const { CHAPTERS, LEVELS, YARN_COLORS } = await import(resolve(root, 'assets/scripts/core/LevelData.ts')); const { checkSegmentBlock, createEmptyRoute, distance, findNextHint, interpolate, planStops, validateLevelDemo } = await import(resolve(root, 'assets/scripts/core/RouteEngine.ts')); // Target sawtooth tightness (%) per level across 6 chapters (60 levels) const TARGET_TIGHTNESS = [ // Ch1 繁花集 null, 62, 68, 74, 70, 74, 66, 78, 84, 90, // Ch2 灵禽志 68, 72, 74, 76, 80, 84, 72, 86, 89, 92, // Ch3 萌宠记 70, 74, 78, 82, 84, 86, 76, 88, 90, 92, // Ch4 诗意山水 74, 78, 84, 86, 84, 88, 78, 90, 92, 93, // Ch5 雅致筑影 76, 80, 84, 86, 86, 88, 78, 90, 92, 93, // Ch6 拾光甜物 78, 82, 86, 86, 88, 90, 78, 91, 93, 94, ]; const rows = []; const errors = []; const chapterDemoFingerprints = new Map(); const globalBallLayouts = new Map(); let slackCount = 0; let noCrossCount = 0; let artworkCount = 0; let midRouteSwitchLevels = 0; let causalChainLevels = 0; let multiNestLevels = 0; let topOrSideNestLevels = 0; let gateLevels = 0; function countMidRouteSwitches(lv) { const lines = []; let switches = 0; for (const raw of lv.demo) { const startPt = { x: raw[0][0], y: raw[0][1] }; const matchedNest = lv.nests.find(n => distance(n, startPt) < 4) ?? lv.nests[0]; const route = createEmptyRoute(false, matchedNest); for (let i = 1; i < raw.length; i++) { const fromPt = { x: raw[i - 1][0], y: raw[i - 1][1] }; const toPt = { x: raw[i][0], y: raw[i][1] }; const steps = Math.max(1, Math.ceil(distance(fromPt, toPt) / 6)); for (let s = 1; s <= steps; s++) { const pt = interpolate(fromPt, toPt, s / steps); const prev = route.points[route.points.length - 1]; const segLen = distance(prev, pt); if (segLen < 1.5) continue; route.points.push(pt); const startLen = route.len; route.len += segLen; planStops(lv, lines, route, prev, pt, startLen, segLen); } } const colors = new Set( route.deliveries.map(d => d.target?.color).filter(Boolean), ); if (colors.size >= 2) switches++; lines.push(route); } return switches; } for (let i = 0; i < LEVELS.length; i++) { const lv = LEVELS[i]; const res = validateLevelDemo(lv); if (!res.ok) errors.push(`#${i + 1} ${lv.id}: validateLevelDemo FAILED: ${res.error}`); const spoolTotal = lv.spools.reduce((s, x) => s + x.bonus, 0); const eff = lv.budget !== undefined ? lv.budget + spoolTotal : null; const tight = eff ? +((res.cost / eff) * 100).toFixed(1) : null; const target = TARGET_TIGHTNESS[i]; if (target !== null && tight !== null && Math.abs(tight - target) > 3.5) { errors.push(`#${i + 1} ${lv.id}: tight ${tight}% outside target ${target}%±3.5% (cost=${Math.round(res.cost)}, eff=${eff})`); } const hasOverrideDye = lv.dyes.some(d => d.override); const minTrips = hasOverrideDye ? Math.ceil(lv.targets.length / 2) : YARN_COLORS.reduce( (s, c) => s + Math.ceil(lv.targets.filter(t => t.color === c).length / 2), 0, ); const hasSlack = !lv.maxDispatches || lv.maxDispatches > minTrips; if (hasSlack) slackCount++; if (lv.noCross) noCrossCount++; if (lv.artworkImage) artworkCount++; // Cognitive puzzle variety metrics const switches = countMidRouteSwitches(lv); if (switches > 0) midRouteSwitchLevels++; const hasLock = !!(lv.locks && Object.keys(lv.locks).length > 0); const hasBridge = lv.ponds.some(p => p.bridgeSectionId); if ((hasLock && (hasBridge || lv.solidBalls)) || (hasLock && Object.keys(lv.locks).length >= 2)) { causalChainLevels++; } if (lv.nests.length >= 2) multiNestLevels++; if (lv.nests.some(n => n.y < 420)) topOrSideNestLevels++; if (lv.gates.length > 0) gateLevels++; // Gate #7: Global cross-chapter ball layout uniqueness (0 duplicates across all 60 levels) const layoutSig = lv.balls .map(b => `${Math.round(b.x / 25)},${Math.round(b.y / 25)}`) .sort() .join('|'); if (globalBallLayouts.has(layoutSig)) { const prev = globalBallLayouts.get(layoutSig); errors.push(`#${i + 1} ${lv.id} (Ch${lv.chapter + 1}): global layout duplicate with #${prev.n} ${prev.id} (Ch${prev.ch})`); } else { globalBallLayouts.set(layoutSig, { n: i + 1, id: lv.id, ch: lv.chapter + 1 }); } if (eff !== null && lv.threeStar > eff) { errors.push(`#${i + 1} ${lv.id}: threeStar ${lv.threeStar} > effBudget ${eff}`); } if (lv.threeStar < res.cost) { errors.push(`#${i + 1} ${lv.id}: threeStar ${lv.threeStar} < demo cost ${Math.round(res.cost)}`); } if (lv.id !== 'first-stitch') { const isCasual = ['入门', '基础', '普通', '休闲'].includes(lv.difficulty); const isMid = ['进阶', '挑战'].includes(lv.difficulty); const maxMargin = isCasual ? Math.max(70, res.cost * 0.12) : isMid ? Math.max(55, res.cost * 0.085) : Math.max(45, res.cost * 0.06); if (lv.threeStar - res.cost > maxMargin) { errors.push(`#${i + 1} ${lv.id}: threeStar ${lv.threeStar} too generous vs cost ${Math.round(res.cost)} (max +${Math.round(maxMargin)})`); } } // Gate #8: Tier 1 hint safety — verify Tier 1 hint never cuts across an unbridged pond (start & after demo[0]) const verifyTier1Safe = (linesSoFar, stageTag) => { const hint = findNextHint(lv, linesSoFar, 1); const r = createEmptyRoute(false, hint.points[0] ?? lv.nests[0]); for (let k = 1; k < hint.points.length; k++) { const a = hint.points[k - 1]; const b = hint.points[k]; const seg = distance(a, b); const steps = Math.max(1, Math.ceil(seg / 6)); let prev = a; for (let s = 1; s <= steps; s++) { const pt = interpolate(a, b, s / steps); const block = checkSegmentBlock(lv, linesSoFar, r, prev, pt, distance(prev, pt)); if (block === 'pond') { errors.push(`#${i + 1} ${lv.id}: Tier 1 hint (${stageTag}) crosses unbridged pond`); return; } prev = pt; } } }; verifyTier1Safe([], 'start'); if (lv.demo.length >= 2) { const firstPath = lv.demo[0]; const r0 = createEmptyRoute(false, { x: firstPath[0][0], y: firstPath[0][1] }); for (let k = 1; k < firstPath.length; k++) { const prev = r0.points[r0.points.length - 1]; const pt = { x: firstPath[k][0], y: firstPath[k][1] }; const seg = distance(prev, pt); const startLen = r0.len; r0.points.push(pt); r0.len += seg; r0.cum.push(r0.len); r0.cost += seg; r0.costCum.push(r0.cost); planStops(lv, [], r0, prev, pt, startLen, seg); } verifyTier1Safe([r0], 'after-stroke-1'); } const mechs = (lv.noCross ? 1 : 0) + (lv.locks && Object.keys(lv.locks).length > 0 ? 1 : 0) + (lv.ponds.some(p => p.bridgeSectionId) ? 1 : 0) + (lv.roughZones.length > 0 ? 1 : 0) + (lv.solidBalls ? 1 : 0) + (lv.buttons.length > 0 ? 1 : 0) + (lv.spools.length > 0 ? 1 : 0) + (lv.gates.length > 0 ? 1 : 0) + (lv.nests.length > 1 ? 1 : 0) + (lv.dyes.length > 0 ? 1 : 0); const fp = JSON.stringify(lv.demo); if (!chapterDemoFingerprints.has(lv.chapter)) chapterDemoFingerprints.set(lv.chapter, new Map()); const map = chapterDemoFingerprints.get(lv.chapter); if (map.has(fp)) { errors.push(`#${i + 1} ${lv.id}: duplicate demo fingerprint with #${map.get(fp)} in Ch${lv.chapter + 1}`); } map.set(fp, i + 1); rows.push({ n: i + 1, ch: lv.chapter + 1, id: lv.id, balls: lv.balls.length, cost: Math.round(res.cost), eff, tight, target, three: lv.threeStar, two: lv.twoStar, mechs, art: lv.artworkImage ?? '-', ok: res.ok, }); } // Chapter-start reset check (no inversions across all 6 chapters) for (let c = 1; c < CHAPTERS.length; c++) { const idx = CHAPTERS[c].startIndex; const prevBoss = rows[idx - 1]; const nextStart = rows[idx]; if (nextStart.cost >= prevBoss.cost || nextStart.balls >= prevBoss.balls) { errors.push( `Chapter inversion: Ch${nextStart.ch} #${nextStart.n} (balls=${nextStart.balls}, cost=${nextStart.cost}) vs Boss #${prevBoss.n} (balls=${prevBoss.balls}, cost=${prevBoss.cost})`, ); } } if (slackCount / LEVELS.length < 0.6) { errors.push(`Slack maxDispatches coverage ${slackCount}/${LEVELS.length} < 60%`); } const allT = rows.filter(r => r.tight !== null).map(r => r.tight); const mean = allT.reduce((a, b) => a + b, 0) / allT.length; const sd = Math.sqrt(allT.reduce((a, b) => a + (b - mean) ** 2, 0) / allT.length); console.log(`=== 蚂蚁织画 ${LEVELS.length} 关 CI 质量报告 ===`); console.log(`验关通过: ${rows.filter(r => r.ok).length}/${rows.length} | AI手绣样板: ${artworkCount} 幅 | 全局独有布局: ${globalBallLayouts.size}/${LEVELS.length}`); console.log(`紧度分布: min=${Math.min(...allT)}% max=${Math.max(...allT)}% mean=${mean.toFixed(1)}% sd=${sd.toFixed(2)}`); console.log(`限笔容错覆盖: ${slackCount}/${LEVELS.length} (${Math.round((slackCount / LEVELS.length) * 100)}%) | 不交叉: ${noCrossCount}/${LEVELS.length}`); console.log(`核心烧脑机制: 半途换色=${midRouteSwitchLevels} 关 | 多级因果链=${causalChainLevels} 关 | 多蚁巢=${multiNestLevels} 关 (顶/侧/四巢=${topOrSideNestLevels} 关) | 针门=${gateLevels} 关`); for (let ch = 1; ch <= CHAPTERS.length; ch++) { const list = rows.filter(r => r.ch === ch); const tList = list.filter(r => r.tight !== null).map(r => r.tight); const meanT = (tList.reduce((a, b) => a + b, 0) / tList.length).toFixed(1); const meanM = (list.reduce((a, b) => a + b.mechs, 0) / list.length).toFixed(2); const meanC = Math.round(list.reduce((a, b) => a + b.cost, 0) / list.length); console.log(`Ch${ch} (${CHAPTERS[ch - 1].name}): avgCost=${meanC} avgMechs=${meanM} avgTight=${meanT}% | seq=[${list.map(r => r.tight ?? '-').join(', ')}]`); } if (errors.length > 0) { console.error(`\n❌ 发现 ${errors.length} 项 CI 异常:`); for (const e of errors) console.error(' -', e); process.exit(1); } else { console.log('\n✅ 全部 8 项 CI 质量门禁通过(含全局 60 关零重复拓扑 + 1 级提示不穿水洼校验)!'); }