import test from 'node:test'; import assert from 'node:assert/strict'; import {SectionVoxelMap} from '../section-voxel-map.js'; import {TerrainController} from '../terrain-controller.js'; import {TerrainState} from '../terrain-state.js'; import {buildBlockLight} from '../block-light.js'; import {buildSectionMesh} from '../mesh-geometry.js'; test('authoritative columns keep a cave dark when the roof is above the loaded vertical window',()=>{ const world=new SectionVoxelMap();world.setSection('0,0,0',new Uint32Array(4096)); const bounds={min:[0,0,0],max:[15,15,15]},columns=new Map([['0,0',new Int16Array(256).fill(30)]]); assert.equal(buildBlockLight(world,new Map(),bounds,columns).sample([8,8,8]).skyLevel,0); columns.get('0,0').fill(-1); assert.equal(buildBlockLight(world,new Map(),bounds,columns).sample([8,8,8]).skyLevel,15); }); test('compact controller transfers section copies and preserves the main collision buffers',()=>{ const packets=[],worker={postMessage(p,transfers){packets.push(structuredClone(p,{transfer:transfers}));},terminate(){}}; const c=new TerrainController({workerFactory:()=>worker}),world=new SectionVoxelMap(); const cells=new Uint32Array(4096).fill(3);world.setSection('0,0,0',cells); const materials=new Map([[3,{state:'minecraft:stone',color:[128,128,128]}]]); c.reset(world,materials,new Map(),{min:[0,0,0],max:[15,15,15]});c.flush(); assert.equal(cells.byteLength,16384);assert.equal(packets[0].initial,null);assert.equal(packets[0].compact,true);assert.equal(packets[0].sections[0].cells[0],3); const state=new TerrainState(null);state.sync(packets[0]);assert.equal(state.blocks.getAt(0,0,0),3); c.update({unload:[[0,0,0]]});c.update({sections:[{section:[0,0,0],cells:new Uint32Array(4096)}]});c.flush();state.sync(packets[1]); assert.equal(state.blocks.hasSection('0,0,0'),true);assert.equal(state.blocks.size,0);c.dispose(); }); test('section-local mesh coordinates retain fractional geometry near the world border',()=>{ const world=new SectionVoxelMap(),x=29_999_840;world.set(`${x},0,0`,1); const materials=new Map([[1,{state:'custom:partial',color:[128,128,128],render:[{min:[.125,.25,.125],max:[.875,.75,.875]}]}]]); const result=buildSectionMesh({id:`${x/16},0,0`,blocks:world,materials,localCoordinates:true}); assert.deepEqual(result.origin,[x,0,0]); const xs=[];for(let i=0;i