import test from "node:test"; import assert from "node:assert/strict"; import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; import { GroundType } from "../../shared/definitions/tiles.js"; import { computeChunkAtlasKeys, EMPTY_BLOCK_ROW_KEY, EMPTY_BLOOM_KEY } from "../../shared/render/chunkAtlasKeys.js"; const catalog = { grounds: { grass: { states: ["summer"] } }, blocks: {} }; function grassSnapshot (chunkX = 0, chunkY = 0) { return { chunkX, chunkY, grounds: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => ({ typeId: GroundType.grass })), blocks: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => null) }; } test("uniform grass chunks share atlas keys regardless of chunk coords", () => { const a = computeChunkAtlasKeys({ snapshot: grassSnapshot(0, 0), catalog, season: "summer", softTerrain: false }); const b = computeChunkAtlasKeys({ snapshot: grassSnapshot(3, 7), catalog, season: "summer", softTerrain: false }); assert.equal(a.groundKey, b.groundKey); assert.equal(a.bloomKey, b.bloomKey); assert.equal(a.blockRowKeys.length, CHUNK_SIZE); assert.ok(a.blockRowKeys.every((key) => key === EMPTY_BLOCK_ROW_KEY)); assert.equal(a.bloomKey, EMPTY_BLOOM_KEY); }); test("soft border fingerprint differs when neighbor ground changes", () => { const snapshot = grassSnapshot(1, 1); const flat = computeChunkAtlasKeys({ snapshot, catalog, season: "summer", softTerrain: true, resolveGround: () => ({ typeId: GroundType.grass }) }); const edgeDirt = computeChunkAtlasKeys({ snapshot, catalog, season: "summer", softTerrain: true, resolveGround: (tileX, tileY) => { if (tileX < snapshot.chunkX * CHUNK_SIZE) { return { typeId: GroundType.dirt }; } return { typeId: GroundType.grass }; } }); assert.notEqual(flat.groundKey, edgeDirt.groundKey); }); test("wet sand border fingerprint differs for liquid water neighbors", () => { const catalogSand = { grounds: { sand: { states: ["sand", "sand-wet"] }, water: { states: ["summer"] }, "deep-water": { states: ["summer"] } }, blocks: {} }; const snapshot = { chunkX: 1, chunkY: 1, grounds: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => ({ typeId: GroundType.sand })), blocks: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => null) }; const dry = computeChunkAtlasKeys({ snapshot, catalog: catalogSand, season: "summer", softTerrain: false, resolveGround: () => ({ typeId: GroundType.sand }) }); const besideWater = computeChunkAtlasKeys({ snapshot, catalog: catalogSand, season: "summer", softTerrain: false, resolveGround: (tileX, tileY) => { if (tileX < snapshot.chunkX * CHUNK_SIZE) { return { typeId: GroundType.water }; } return { typeId: GroundType.sand }; } }); assert.notEqual(dry.groundKey, besideWater.groundKey); const winterShallow = computeChunkAtlasKeys({ snapshot, catalog: catalogSand, season: "winter", softTerrain: false, resolveGround: (tileX, tileY) => { if (tileX < snapshot.chunkX * CHUNK_SIZE) { return { typeId: GroundType.water }; } return { typeId: GroundType.sand }; } }); const winterDeep = computeChunkAtlasKeys({ snapshot, catalog: catalogSand, season: "winter", softTerrain: false, resolveGround: (tileX, tileY) => { if (tileX < snapshot.chunkX * CHUNK_SIZE) { return { typeId: GroundType.deepWater }; } return { typeId: GroundType.sand }; } }); // Frozen shallow water should not wet the shoreline; deep still does. assert.equal(winterShallow.groundKey.includes("wet:flat") || winterShallow.groundKey.includes("wetBand:0.6"), true); assert.ok(winterShallow.groundKey.includes("wet:flat")); assert.notEqual(winterShallow.groundKey, winterDeep.groundKey); }); test("wet sand band fraction is part of the ground atlas key", () => { const catalogSand = { grounds: { sand: { states: ["sand", "sand-wet"] } }, blocks: {} }; const snapshot = { chunkX: 0, chunkY: 0, grounds: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => ({ typeId: GroundType.sand })), blocks: Array.from({ length: CHUNK_SIZE * CHUNK_SIZE }, () => null) }; const medium = computeChunkAtlasKeys({ snapshot, catalog: catalogSand, season: "summer", wetSandBandFraction: 0.6, resolveGround: () => ({ typeId: GroundType.sand }) }); const off = computeChunkAtlasKeys({ snapshot, catalog: catalogSand, season: "summer", wetSandBandFraction: 0, resolveGround: () => ({ typeId: GroundType.sand }) }); assert.notEqual(medium.groundKey, off.groundKey); assert.ok(medium.groundKey.includes("wetBand:0.6")); assert.ok(off.groundKey.includes("wetBand:0")); });