| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { mkdtempSync, readFileSync, rmSync, existsSync, unlinkSync } from "node:fs"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { join } from "node:path"; |
| 6 | import { WorldDatabase } from "../src/persistence/worldDb.js"; |
| 7 | import { EditorService } from "../src/editor/service.js"; |
| 8 | import { MinimapCache } from "../src/editor/minimapCache.js"; |
| 9 | import { BlockType, GroundType, installTileAverageColors } from "../../shared/definitions/tiles.js"; |
| 10 | import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; |
| 11 | import { OVERWORLD_ID } from "../../shared/world/dimensions.js"; |
| 12 | |
| 13 | const GRASS = Object.freeze({ r: 10, g: 200, b: 20 }); |
| 14 | const DIRT = Object.freeze({ r: 140, g: 80, b: 30 }); |
| 15 | const WATER = Object.freeze({ r: 20, g: 60, b: 180 }); |
| 16 | const FLOWER = Object.freeze({ r: 200, g: 20, b: 40 }); |
| 17 | |
| 18 | function installFixtureColors () |
| 19 | { |
| 20 | installTileAverageColors({ |
| 21 | grounds: { |
| 22 | grass: { typeId: GroundType.grass, averageColor: GRASS, stateColors: {} }, |
| 23 | dirt: { typeId: GroundType.dirt, averageColor: DIRT, stateColors: {} }, |
| 24 | water: { typeId: GroundType.water, averageColor: WATER, stateColors: {} } |
| 25 | }, |
| 26 | blocks: { |
| 27 | flower: { |
| 28 | typeId: BlockType.flower, |
| 29 | averageColor: FLOWER, |
| 30 | stateColors: { red: FLOWER } |
| 31 | } |
| 32 | } |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | function pixel (buffer, index) |
| 37 | { |
| 38 | const offset = index * 4; |
| 39 | return { r: buffer[offset], g: buffer[offset + 1], b: buffer[offset + 2], a: buffer[offset + 3] }; |
| 40 | } |
| 41 | |
| 42 | test("minimap cache is per dimension, regenerates when missing, and skips chunk blobs when present", () => |
| 43 | { |
| 44 | installFixtureColors(); |
| 45 | const dir = mkdtempSync(join(tmpdir(), "questward-minimap-")); |
| 46 | const worldDb = new WorldDatabase(join(dir, "world.sqlite")); |
| 47 | const cache = new MinimapCache(join(dir, "minimap"), worldDb); |
| 48 | cache.syncColorStamp("stamp-a"); |
| 49 | const editor = new EditorService(worldDb, null, cache); |
| 50 | try |
| 51 | { |
| 52 | const loaded = editor.loadChunk(0, 0, OVERWORLD_ID); |
| 53 | const grounds = loaded.data.grounds.map((ground) => ({ typeId: ground.typeId, state: ground.state })); |
| 54 | grounds[0] = { typeId: GroundType.dirt, state: null }; |
| 55 | const blocks = loaded.data.blocks.slice(); |
| 56 | blocks[1] = { typeId: BlockType.flower, state: "red", config: null }; |
| 57 | editor.saveChunk(0, 0, { |
| 58 | open: true, |
| 59 | grounds, |
| 60 | blocks, |
| 61 | spawners: [] |
| 62 | }, "auth:test", OVERWORLD_ID); |
| 63 | |
| 64 | const overworldFile = cache.pathFor(OVERWORLD_ID, 0, 0); |
| 65 | assert.equal(existsSync(overworldFile), true); |
| 66 | const saved = readFileSync(overworldFile); |
| 67 | assert.equal(saved.length, CHUNK_SIZE * CHUNK_SIZE * 4); |
| 68 | assert.deepEqual(pixel(saved, 0), { ...DIRT, a: 255 }); |
| 69 | assert.deepEqual(pixel(saved, 1), { ...FLOWER, a: 255 }); |
| 70 | assert.deepEqual(pixel(saved, 2), { ...GRASS, a: 255 }); |
| 71 | |
| 72 | const windowed = editor.minimapWindow(0, 0, OVERWORLD_ID); |
| 73 | assert.equal(windowed.span, 25); |
| 74 | assert.equal(windowed.chunkPixels, 16); |
| 75 | assert.equal(windowed.dimensionId, OVERWORLD_ID); |
| 76 | assert.ok(windowed.chunks["0,0"]); |
| 77 | assert.equal(windowed.chunks["4,4"], undefined); |
| 78 | assert.equal(Object.keys(windowed.chunks).length, 1); |
| 79 | |
| 80 | unlinkSync(overworldFile); |
| 81 | const rebuilt = Buffer.from(editor.minimapWindow(0, 0, OVERWORLD_ID).chunks["0,0"], "base64"); |
| 82 | assert.equal(existsSync(overworldFile), true); |
| 83 | assert.deepEqual(pixel(rebuilt, 0), { ...DIRT, a: 255 }); |
| 84 | assert.deepEqual(pixel(rebuilt, 1), { ...FLOWER, a: 255 }); |
| 85 | |
| 86 | editor.createDimension("cave", "Cave", "auth:test"); |
| 87 | const caveLoaded = editor.loadChunk(0, 0, "cave"); |
| 88 | const caveGrounds = caveLoaded.data.grounds.map(() => ({ typeId: GroundType.water, state: null })); |
| 89 | editor.saveChunk(0, 0, { |
| 90 | open: true, |
| 91 | grounds: caveGrounds, |
| 92 | blocks: caveLoaded.data.blocks, |
| 93 | spawners: [] |
| 94 | }, "auth:test", "cave"); |
| 95 | const caveFile = cache.pathFor("cave", 0, 0); |
| 96 | assert.notEqual(caveFile, overworldFile); |
| 97 | assert.deepEqual(pixel(readFileSync(caveFile), 0), { ...WATER, a: 255 }); |
| 98 | const caveWindow = editor.minimapWindow(0, 0, "Cave"); |
| 99 | assert.ok(caveWindow.chunks["0,0"]); |
| 100 | assert.equal(caveWindow.chunks["4,4"], undefined); |
| 101 | assert.ok(editor.minimapWindow(0, 0, OVERWORLD_ID).chunks["0,0"]); |
| 102 | |
| 103 | worldDb.db.prepare(` |
| 104 | UPDATE world_chunk_versions SET data_blob = ? WHERE dimension_id = ? AND chunk_x = 0 AND chunk_y = 0 |
| 105 | `).run("not-json", OVERWORLD_ID); |
| 106 | const fromCache = Buffer.from(editor.minimapWindow(0, 0, OVERWORLD_ID).chunks["0,0"], "base64"); |
| 107 | assert.deepEqual(pixel(fromCache, 0), { ...DIRT, a: 255 }); |
| 108 | |
| 109 | cache.syncColorStamp("stamp-b"); |
| 110 | assert.equal(existsSync(caveFile), false); |
| 111 | assert.equal(existsSync(join(dir, "minimap", "color-stamp.txt")), true); |
| 112 | assert.equal(readFileSync(join(dir, "minimap", "color-stamp.txt"), "utf8"), "stamp-b"); |
| 113 | } |
| 114 | finally |
| 115 | { |
| 116 | worldDb.close(); |
| 117 | rmSync(dir, { recursive: true, force: true }); |
| 118 | } |
| 119 | }); |