import test from "node:test"; import assert from "node:assert/strict"; import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; /** Mirrors WorldView.#chunkRebuildScore for unit tests. */ function chunkRebuildScore ({ chunkX, chunkY, playerChunkX, playerChunkY, moveDx, moveDy, mode = "urgent" }) { const dx = chunkX - playerChunkX; const dy = chunkY - playerChunkY; const dist = Math.abs(dx) + Math.abs(dy); let ahead = 0; if (moveDx !== 0 || moveDy !== 0) { ahead = dx * moveDx + dy * moveDy; } if (mode === "idle") { return -dist * 10 + (ahead > 0 ? -ahead * 2 : Math.max(ahead, 0)); } return dist * 10 - Math.max(0, ahead) * 3; } test("urgent chunk rebuild prefers nearby chunks ahead of movement", () => { const playerChunkX = 5; const playerChunkY = 5; const ahead = chunkRebuildScore({ chunkX: 6, chunkY: 5, playerChunkX, playerChunkY, moveDx: 1, moveDy: 0, mode: "urgent" }); const behind = chunkRebuildScore({ chunkX: 4, chunkY: 5, playerChunkX, playerChunkY, moveDx: 1, moveDy: 0, mode: "urgent" }); assert.ok(ahead < behind, "chunk in walk direction should outrank chunk behind"); }); test("idle prefetch prefers far chunks ahead of movement", () => { const playerChunkX = 5; const playerChunkY = 5; const farAhead = chunkRebuildScore({ chunkX: 7, chunkY: 5, playerChunkX, playerChunkY, moveDx: 1, moveDy: 0, mode: "idle" }); const nearBehind = chunkRebuildScore({ chunkX: 4, chunkY: 5, playerChunkX, playerChunkY, moveDx: 1, moveDy: 0, mode: "idle" }); assert.ok(farAhead < nearBehind, "idle should target far-ahead chunks before near-behind"); }); test("player chunk coords align with CHUNK_SIZE", () => { assert.equal(Math.floor((5 * CHUNK_SIZE) / CHUNK_SIZE), 5); });