server/map_test.js (11760B)
1 const assert = require('node:assert/strict'); 2 const fs = require('node:fs'); 3 const test = require('node:test'); 4 const vm = require('node:vm'); 5 6 const mapSource = fs.readFileSync('server/assets/map.js', 'utf8') + '\nAOMap;'; 7 const AOMap = vm.runInNewContext(mapSource, { 8 document: {querySelectorAll: () => []}, 9 }); 10 11 function mapWithTiles(tileSize, count, fetch) { 12 const MapClass = vm.runInNewContext(mapSource, { 13 document: {querySelectorAll: () => []}, fetch, setTimeout, clearTimeout, URLSearchParams, 14 createImageBitmap: async () => ({width: tileSize, height: tileSize, close() {}}), 15 }); 16 const elements = { 17 canvas: {getContext: () => ({fillRect() {}, drawImage() {}, strokeRect() {}})}, 18 '[data-map-status]': {}, '[data-map-location]': {}, '[data-map-view]': {value: '950'}, 19 }; 20 const map = new MapClass({dataset: {character: '1'}, querySelector: selector => elements[selector] || null}); 21 map.view = '950'; 22 map.width = tileSize; 23 map.height = tileSize; 24 map.scale = 1; 25 map.manifest = {}; 26 map.zones = new Map([[950, {width: tileSize * count, height: tileSize, levels: [{ 27 tile_size: tileSize, width: tileSize * count, height: tileSize, 28 rect: [0, 0, tileSize * count, tileSize], columns: count, rows: 1, 29 tiles: Array.from({length: count}, (_, index) => `${index}.png`), 30 }]}]]); 31 map.renderSoon = () => {}; 32 return map; 33 } 34 35 test('detail layers require their checkbox, sufficient zoom and a bounded viewport', () => { 36 const map = mapWithTiles(1000, 1, () => {}); 37 const npcs = {checked: false}, warps = {checked: true}; 38 map.element.querySelector = selector => selector === '[data-map-npcs]' ? npcs : warps; 39 map.scale = 0.2; 40 let query = new URLSearchParams(map.entityQuery()); 41 assert.equal(query.get('npcs'), '0'); 42 assert.equal(query.get('warps'), '0'); 43 44 map.scale = 0.3; 45 query = new URLSearchParams(map.entityQuery()); 46 assert.equal(query.get('npcs'), '0'); 47 assert.equal(query.get('warps'), '1'); 48 npcs.checked = true; 49 assert.equal(new URLSearchParams(map.entityQuery()).get('npcs'), '0'); 50 map.scale = 1.2; 51 assert.equal(new URLSearchParams(map.entityQuery()).get('npcs'), '1'); 52 warps.checked = false; 53 assert.equal(new URLSearchParams(map.entityQuery()).get('warps'), '0'); 54 55 warps.checked = true; 56 map.width = 8000; 57 query = new URLSearchParams(map.entityQuery()); 58 assert.equal(query.get('npcs'), '0'); 59 assert.equal(query.get('warps'), '0'); 60 }); 61 62 test('the main map does not select a teleport destination on a background tap', () => { 63 const map = mapWithTiles(1000, 1, () => {}); 64 map.location.textContent = ''; 65 map.pick(500, 500); 66 assert.equal(map.selection, null); 67 assert.equal(map.location.textContent, ''); 68 }); 69 70 test('finding a zone focuses its boundary within the world map', () => { 71 const map = mapWithTiles(128, 1, () => { throw new Error('Navigation must not make a request'); }); 72 map.width = 1000; 73 map.height = 500; 74 map.selection = {point: [1, 2]}; 75 const zone = {origin: [1000, 2000], boundary: [[100, 100], [500, 100], [500, 900], [100, 900]]}; 76 77 map.focusZone(zone); 78 79 assert.equal(map.view, 'world'); 80 assert.deepEqual(Array.from(map.center), [1300, 2500]); 81 assert.equal(map.scale, 0.5625); 82 assert.equal(map.selection, null); 83 }); 84 85 test('panning back reuses more than 128 small cached tiles without new requests', async () => { 86 let requests = 0; 87 const map = mapWithTiles(128, 200, async () => { 88 requests++; 89 return {ok: true, blob: async () => null}; 90 }); 91 92 for (let index = 0; index < 200; index++) { 93 map.center = [index * 128 + 64, 64]; 94 map.render(); 95 await new Promise(resolve => setImmediate(resolve)); 96 } 97 map.center = [64, 64]; 98 map.render(); 99 await new Promise(resolve => setImmediate(resolve)); 100 101 assert.equal(requests, 200); 102 assert.equal(map.tileBytes, 13107200); 103 }); 104 105 test('a large view starts at most six tile requests at once', () => { 106 let requests = 0; 107 const map = mapWithTiles(128, 200, () => { requests++; return new Promise(() => {}); }); 108 map.width = 25600; 109 map.center = [12800, 64]; 110 111 map.render(); 112 113 assert.equal(requests, 6); 114 }); 115 116 test('zooming defers requests until the zoom settles', async () => { 117 let requests = 0; 118 const map = mapWithTiles(128, 200, () => { requests++; return new Promise(() => {}); }); 119 map.center = [64, 64]; 120 121 map.zoom(0.8, 64, 64); 122 map.render(); 123 const duringZoom = requests; 124 await new Promise(resolve => setTimeout(resolve, 160)); 125 map.render(); 126 127 assert.equal(duringZoom, 0); 128 assert.equal(requests, 2); 129 }); 130 131 test('the memory budget evicts the least recently viewed tile', async () => { 132 let requests = 0; 133 const map = mapWithTiles(1024, 65, async () => { 134 requests++; 135 return {ok: true, blob: async () => null}; 136 }); 137 138 for (let index = 0; index < 64; index++) { 139 map.center = [index * 1024 + 512, 512]; 140 map.render(); 141 await new Promise(resolve => setImmediate(resolve)); 142 } 143 map.center = [512, 512]; 144 map.render(); 145 map.center = [66048, 512]; 146 map.render(); 147 await new Promise(resolve => setImmediate(resolve)); 148 map.render(); 149 150 assert.equal(requests, 65); 151 assert.equal(map.tileBytes, 268435456); 152 assert.equal(map.tiles.has('0.png'), true); 153 assert.equal(map.tiles.has('1.png'), false); 154 assert.equal(map.tiles.has('64.png'), true); 155 }); 156 157 test('panning past queued tiles loads the current view next', async () => { 158 const requests = []; 159 const responses = []; 160 const map = mapWithTiles(128, 20, path => { 161 requests.push(path); 162 return new Promise(resolve => responses.push(resolve)); 163 }); 164 map.width = 2560; 165 map.center = [1280, 64]; 166 167 map.render(); 168 map.width = 128; 169 map.center = [2496, 64]; 170 map.render(); 171 for (const resolve of responses) resolve({ok: true, blob: async () => null}); 172 await new Promise(resolve => setImmediate(resolve)); 173 map.render(); 174 175 assert.deepEqual(requests, ['admin/map-assets/0.png', 'admin/map-assets/1.png', 'admin/map-assets/2.png', 176 'admin/map-assets/3.png', 'admin/map-assets/4.png', 'admin/map-assets/5.png', 'admin/map-assets/19.png']); 177 }); 178 179 test('unused terrain margins do not request tiles behind polygon clipping', () => { 180 const map = Object.create(AOMap.prototype); 181 map.center = [2048, 2048]; 182 map.width = 4096; 183 map.height = 4096; 184 map.scale = 1; 185 const requests = []; 186 map.tile = path => { requests.push(path); return null; }; 187 const levels = [{tile_size: 1024, width: 4096, height: 4096, rect: [0, 0, 4096, 4096], 188 columns: 4, rows: 4, order: 'row', tiles: ['0', '1', '2', '3', '4', '5', '6', '7', 189 '8', '9', '10', '11', '12', '13', '14', '15']}]; 190 191 map.layer(levels, [0, 0, 4096, 4096], [[1500, 1500], [2000, 1500], [2000, 2000], [1500, 2000]]); 192 193 assert.deepEqual(requests, ['9']); 194 }); 195 196 test('panning beyond the polygon bounds requests no terrain tiles', () => { 197 const map = Object.create(AOMap.prototype); 198 map.center = [3500, 3500]; 199 map.width = 1000; 200 map.height = 1000; 201 map.scale = 1; 202 const requests = []; 203 map.tile = path => { requests.push(path); return null; }; 204 const levels = [{tile_size: 1024, width: 4096, height: 4096, rect: [0, 0, 4096, 4096], columns: 4, rows: 4}]; 205 206 map.layer(levels, [0, 0, 4096, 4096], [[1500, 1500], [2000, 1500], [2000, 2000], [1500, 2000]]); 207 208 assert.deepEqual(requests, []); 209 }); 210 211 test('pinching anchors the midpoint and does not turn into a teleport tap', () => { 212 const map = mapWithTiles(128, 1, () => { throw new Error('Unexpected tile request'); }); 213 map.canvas.getBoundingClientRect = () => ({left: 0, top: 0}); 214 map.canvas.setPointerCapture = () => {}; 215 map.canvas.hasPointerCapture = () => false; 216 map.pick = () => { throw new Error('Pinch must not select a destination'); }; 217 const pointer = (pointerId, clientX, clientY) => ({pointerId, clientX, clientY, button: 0}); 218 219 map.pointerDown(pointer(1, 32, 64)); 220 map.pointerDown(pointer(2, 96, 64)); 221 map.pointerMove(pointer(2, 160, 64)); 222 map.pointerEnd(pointer(2, 160, 64)); 223 map.pointerMove(pointer(1, 42, 64)); 224 map.pointerEnd(pointer(1, 42, 64)); 225 clearTimeout(map.loadTimer); 226 227 assert.equal(map.scale, 2); 228 assert.deepEqual(Array.from(map.center), [-21, 0]); 229 assert.equal(map.pointers.size, 0); 230 }); 231 232 test('cancelled touch releases the gesture without selecting a destination', () => { 233 const map = mapWithTiles(128, 1, () => {}); 234 map.canvas.getBoundingClientRect = () => ({left: 0, top: 0}); 235 map.canvas.setPointerCapture = () => {}; 236 map.canvas.hasPointerCapture = () => false; 237 map.pick = () => { throw new Error('Cancelled touch must not select'); }; 238 const event = {pointerId: 1, clientX: 30, clientY: 30, button: 0}; 239 240 map.pointerDown(event); 241 map.pointerEnd(event, true); 242 243 assert.equal(map.drag, null); 244 assert.equal(map.pointers.size, 0); 245 }); 246 247 test('acquisition locations focus outdoor and interior coordinates and label taps', () => { 248 const map = mapWithTiles(1000, 1, () => {}); 249 map.zones.set(705, {id: 705, name: 'Trade', origin: [1000, 2000]}); 250 map.zones.set(1183, {id: 1183, name: 'Shop', width: 500, height: 500, levels: []}); 251 map.sources = [{name: 'Shop: Supplies', playfield: 705, position: [100, 0, 200]}]; 252 253 map.focusSource(map.sources[0]); 254 assert.equal(map.view, 'world'); 255 assert.deepEqual(Array.from(map.center), [1100, 2200]); 256 map.pick(500, 500); 257 assert.match(map.location.textContent, /Shop: Supplies/); 258 assert.equal(map.selection, null); 259 260 map.focusSource({name: 'Shop: Armor', playfield: 1183, position: [180, 5, 135]}); 261 assert.equal(map.view, '1183'); 262 assert.deepEqual(Array.from(map.center), [180, 135]); 263 assert.equal(map.initialized, true); 264 }); 265 266 test('source overview fits every overworld marker into the viewport', () => { 267 const map = mapWithTiles(1000, 1, () => {}); 268 map.height = 500; 269 map.zones.set(505, {origin: [35214, 44566]}); 270 map.zones.set(705, {origin: [43756, 26526]}); 271 map.sources = [ 272 {playfield: 505, position: [2092, 0, 3822]}, 273 {playfield: 705, position: [960, 0, 510]}, 274 ]; 275 276 map.focusSources(); 277 278 assert.equal(map.view, 'world'); 279 assert.equal(map.initialized, true); 280 assert.deepEqual(Array.from(map.center), [41011, 37712]); 281 for (const source of map.sources) { 282 const [x, y] = map.toScreen(...map.playerPoint(source)); 283 assert(x > 6 && x < 994 && y > 6 && y < 494); 284 } 285 }); 286 287 test('a single overview location has a finite useful zoom', () => { 288 const map = mapWithTiles(1000, 1, () => {}); 289 map.zones.set(505, {origin: [35214, 44566]}); 290 map.sources = [{playfield: 505, position: [2092, 0, 3822]}]; 291 292 map.focusSources(); 293 294 assert.deepEqual(Array.from(map.center), [37306, 48388]); 295 assert.equal(map.scale, 1); 296 }); 297 298 test('loot and vending markers use distinct colors and shared markers show both', () => { 299 const map = mapWithTiles(500, 1, () => {}); 300 map.zones.get(950).levels = []; 301 map.sources = [ 302 {playfield: 950, position: [100, 0, 100], loot: true}, 303 {playfield: 950, position: [200, 0, 100], vending: true}, 304 {playfield: 950, position: [300, 0, 100], loot: true, vending: true}, 305 ]; 306 const fills = []; 307 Object.assign(map.context, { 308 beginPath() {}, arc() {}, stroke() {}, fillText() {}, 309 fill() { fills.push(this.fillStyle); }, 310 }); 311 312 map.render(); 313 314 assert.deepEqual(fills, ['#f4b942', '#55b8ff', '#f4b942', '#55b8ff']); 315 });