import assert from 'node:assert/strict'; import test from 'node:test'; import { coverageZoomForHeight, overviewZoomForHeight, latToTileY, lonToTileX, normalizeBbox, tileBounds, tileLocalToLonLat, tilesForBbox, } from './tileMath.js'; test('lon/lat to tile matches the Sacramento reference tile', () => { assert.equal(lonToTileX(-121.4944, 14), 2662); assert.equal(latToTileY(38.5816, 14), 6286); const bounds = tileBounds(2662, 6286, 14); assert.ok(bounds.west < -121.4944 && -121.4944 < bounds.east); assert.ok(bounds.south < 38.5816 && 38.5816 < bounds.north); }); test('tile-local coordinates round-trip into the tile bounds', () => { const bounds = tileBounds(2662, 6286, 14); const [lon, lat] = tileLocalToLonLat(0, 0, 4096, 2662, 6286, 14); assert.ok(Math.abs(lon - bounds.west) < 1e-9); assert.ok(Math.abs(lat - bounds.north) < 1e-9); const [lon2, lat2] = tileLocalToLonLat(4096, 4096, 4096, 2662, 6286, 14); assert.ok(Math.abs(lon2 - bounds.east) < 1e-9); assert.ok(Math.abs(lat2 - bounds.south) < 1e-9); }); test('normalizeBbox orders and clamps coordinates', () => { assert.deepEqual(normalizeBbox([-121.36, 38.69, -121.56, 38.44]), { west: -121.56, south: 38.44, east: -121.36, north: 38.69, }); assert.equal(normalizeBbox([0, 0, 0, 1]), null); assert.equal(normalizeBbox('nope'), null); assert.equal(normalizeBbox([1, 2, Number.NaN, 3]), null); }); test('city-scale boxes span many z14 tiles', () => { const sacramento = [-121.56, 38.44, -121.36, 38.69]; assert.equal(tilesForBbox(sacramento, 14).total, 160); const detroit = [-83.29, 42.25, -82.91, 42.45]; assert.equal(tilesForBbox(detroit, 14).total, 234); }); test('tilesForBbox orders from the centre outwards and honours the cap', () => { const result = tilesForBbox([-121.56, 38.44, -121.36, 38.69], 14, { limit: 5, }); assert.equal(result.total, 160); assert.equal(result.truncated, true); assert.equal(result.tiles.length, 5); const centreX = lonToTileX(-121.46, 14); const centreY = latToTileY(38.565, 14); assert.ok(Math.abs(result.tiles[0].x - centreX) <= 1); assert.ok(Math.abs(result.tiles[0].y - centreY) <= 1); const full = tilesForBbox([-121.56, 38.44, -121.36, 38.69], 14); assert.equal(full.truncated, false); assert.equal(full.tiles.length, 160); }); test('coverageZoomForHeight steps from coarse to z14 as the camera descends', () => { assert.equal(coverageZoomForHeight(100_000), null); assert.equal(coverageZoomForHeight(30_000), 11); assert.equal(coverageZoomForHeight(8_000), 12); assert.equal(coverageZoomForHeight(3_000), 13); assert.equal(coverageZoomForHeight(500), 14); assert.equal(coverageZoomForHeight(Number.NaN), null); }); test('overviewZoomForHeight covers the globe above the sequence ceiling', () => { assert.equal(overviewZoomForHeight(30_000), null); assert.equal(overviewZoomForHeight(100_000), 5); assert.equal(overviewZoomForHeight(900_000), 4); assert.equal(overviewZoomForHeight(2_000_000), 3); assert.equal(overviewZoomForHeight(5_000_000), 2); assert.equal(overviewZoomForHeight(10_000_000), 1); assert.equal(overviewZoomForHeight(25_000_000), 0); }); test('longitude 180 is the last column, so a world view keeps the eastern hemisphere', () => { assert.equal(lonToTileX(180, 1), 1); assert.equal(lonToTileX(179.99, 1), 1); assert.equal(lonToTileX(-180, 1), 0); const columns = new Set( tilesForBbox([-180, -85, 180, 85], 1).tiles.map((tile) => tile.x), ); assert.deepEqual([...columns].sort(), [0, 1]); }); test('a box across the date line takes tiles on both sides of it, not the far side of the globe', () => { const { tiles } = tilesForBbox([179, -1, -179, 1], 8); const columns = new Set(tiles.map((tile) => tile.x)); assert.ok(columns.has(255), 'west of the line'); assert.ok(columns.has(0), 'east of the line'); assert.ok( [...columns].every((x) => x <= 1 || x >= 254), 'nothing from the middle of the map', ); // The limit keeps the tiles nearest the line. const limited = tilesForBbox([170, -1, -170, 1], 8, { limit: 4 }).tiles; assert.ok(limited.every((tile) => tile.x <= 8 || tile.x >= 247)); }); test('a focus ranks tiles along the line of sight, not from the box centre (review IC8 P1)', () => { // A box that runs far north of the camera, as a tilted view's does: the // box centre is kilometres from both the camera and the screen centre. const from = { lon: -121.4944, lat: 38.5816 }; const to = { lon: -121.4944, lat: 38.596 }; const bbox = [-121.6, 38.57, -121.38, 38.9]; const z = 14; const key = (t) => `${t.x}/${t.y}`; const under = `${lonToTileX(from.lon, z)}/${latToTileY(from.lat, z)}`; const centre = `${lonToTileX(to.lon, z)}/${latToTileY(to.lat, z)}`; const plain = tilesForBbox(bbox, z, { limit: 9 }).tiles.map(key); assert.equal(plain.includes(under), false, 'the old ranking drops them'); const ranked = tilesForBbox(bbox, z, { limit: 9, focus: { from, to } }); const keys = ranked.tiles.map(key); assert.ok(keys.includes(under), 'the tile under the camera'); assert.ok(keys.includes(centre), 'the tile at the centre of the screen'); assert.equal(ranked.total > 9, true); });