for(constlevelof[5,6]){constschedule=AppLogic.specialSchedule(level,100,level,['warp','lock','crossing','internalGate'],[]);assert(schedule.types.length===1&&schedule.setCount===1,`Level ${level} did not schedule exactly one minimum set`)}
assert(logic.addCrossingSpecial(cross,context.rngFrom(0x47c205ed),newSet()),'A one-section crossing template could not be generated within the obstacle cap');
assert((cross.obstacles||[]).length<=Math.floor((cross.valid.length+(cross.obstacles||[]).length)*.2),'Crossing template exceeded the 20% obstacle cap');
constadded=logic.addWarpSpecial(cached,context.rngFrom(0x9e3779b9),newSet());assert(added,'Could not add a warp for stale-cache regression');
assert(!Object.prototype.hasOwnProperty.call(cached,'_warpMap'),'Warp mutation did not invalidate the stale warp map');constaddedPair=cached.specialCells.warps[0];assert(logic.isWarpTransition(cached,addedPair.a,addedPair.b),'Warp lookup did not rebuild after cache invalidation');
constafter=cached.solution.map(path=>logic.turnAnalysis(path,cached).count);assert(cached._warpMap&&typeofcached._warpMap.get==='function'&&cached._warpMap.size===2,'Warp map was not rebuilt after adding a warp');
constwarpedIndex=cached.solution.findIndex(path=>cached.specialCells.warps.some(pair=>path.cells.some(cell=>context.sameCell(cell,pair.a))));assert(warpedIndex>=0&&Number.isFinite(after[warpedIndex]),'Warp turn analysis did not run');
for(letlevel=1;level<=4;level++){constlow=logic.addSpecialCellPattern(base,0x700000+level,level),special=logic.specialCellSet(low);assert(low===base&&!special.internalGates.length&&!special.warps.length&&!special.locks.length&&!special.crossings.length,`Level ${level} received a special mechanic before the production threshold`)}
assert(special.internalGates.length===0&&present.length===1&&puzzle.specialSchedule?.introduction===true&&puzzle.specialSchedule?.setCount===1,'Level-5 first encounter did not schedule exactly one production special');
for(const[type,count]ofObject.entries({warp:counts.warp,lock:counts.lock}))assert(count>0,`${type} was never selected by deterministic first-encounter scheduling`);
assert(counts.boards>0,'Level-5 scheduling generated no viable special boards');
assert((puzzle.obstacles||[]).length<=Math.floor((puzzle.valid.length+(puzzle.obstacles||[]).length)*.2),'Special-cell conversion exceeded the 20% obstacle cap');
for(constpathofpuzzle.solution){
for(leti=0;i<path.cells.length;i++){
constcell=path.cells[i],key=context.ckey(...cell),visits=covered.get(key)||0;assert(valid.has(key),'Special solution leaves the valid area');assert(visits===0||visits===1&&crossingKeys.has(key),'Special solution overlaps a non-crossing cell or crosses more than twice');covered.set(key,visits+1);
if(i){constprev=path.cells[i-1];assert(context.manhattan(prev,cell)===1||logic.isWarpTransition(puzzle,prev,cell),'Special solution has an invalid transition')}
}
assert(logic.pathValid(path,puzzle),'Special solution no longer satisfies its clue');
}
assert(covered.size===valid.size,'Special solution does not cover every valid cell');
for(constpairofspecial.warps){letfound=false;for(constpathofpuzzle.solution){constai=path.cells.findIndex(cell=>context.sameCell(cell,pair.a)),bi=path.cells.findIndex(cell=>context.sameCell(cell,pair.b));if(ai>=0||bi>=0){assert(Math.abs(ai-bi)===1,'Warp endpoints are not consecutive');found=true}}assert(found,'Warp pair is absent from the solution')}
for(constlockofspecial.locks){letfound=false;for(constpathofpuzzle.solution){constki=path.cells.findIndex(cell=>context.sameCell(cell,lock.key)),di=path.cells.findIndex(cell=>context.sameCell(cell,lock.door));if(ki>=0||di>=0){assert(ki>=0&&di>ki,'Key is not before its door on the solution line');found=true}}assert(found,'Key/door pair is absent from the solution')}
for(constpairofspecial.internalGates||[]){consta=puzzle.g[pair.a],b=puzzle.g[pair.b],da=context.SIDE_D[a[2]],db=context.SIDE_D[b[2]];assert(context.manhattan(a,b)===1&&a[0]+da[0]===b[0]&&a[1]+da[1]===b[1]&&b[0]+db[0]===a[0]&&b[1]+db[1]===a[1],'Internal gate pair is not adjacent and mutually facing');assert(puzzle.solution.some(path=>path.startGate===pair.a||path.endGate===pair.a)&&puzzle.solution.some(path=>path.startGate===pair.b||path.endGate===pair.b),'Internal gate pair is not used as solution endpoints')}
conststate={paths:context.deepClone(puzzle.solution),specialProgress:{crossings:special.crossings.map(cell=>context.ckey(...cell))}};assert(logic.isSolved(state,puzzle),'Generated special-cell solution does not solve its board');
for(constcellofspecial.crossings){constkey=context.ckey(...cell),validSet=newSet(puzzle.valid.map(candidate=>context.ckey(...candidate))),gateSet=newSet(puzzle.g.map(g=>context.ckey(g[0],g[1])));assert(covered.get(key)===2,'Crossing solution does not use the crossing cell exactly twice');assert(!gateSet.has(key),'Crossing cell was placed on a gate cell');for(constneighborof[[cell[0]-1,cell[1]],[cell[0]+1,cell[1]],[cell[0],cell[1]-1],[cell[0],cell[1]+1]])assert(validSet.has(context.ckey(...neighbor)),'Crossing cell was placed on the board edge')}
assert(logic.isSolved(solvedState,solvedPuzzle),'A live perpendicular crossing did not permit normal board clear');
solvedState.paths[1].cells=[[0,2],[1,2],[1,1],[2,1],[3,1]];solvedState.specialProgress.crossings=['2,2'];assert(!logic.isSolved(solvedState,solvedPuzzle),'Crossing history permitted clear after the crossing state was removed');