Files
gods-eye-view/src/layers/streetLevel/tileMath.test.mjs
T
Said TurkseverandClaude Opus 5.5 5f98b871e8 fix(street-level): sequence loads, playback and nearest-photo edge cases
From gekh's review of #768, each bug with a test that fails before:

- P2: a failed sequence load clears the previous sequence's cones, resets
  the selection and can be retried; a second click while loading starts no
  second lookup. sequences.js had no tests: sequences.test.mjs now covers
  failure and retry, a superseded late answer, the cache and clearSelection.
- P2: closing a photo stops MapillaryJS sequence playback, which kept the
  hidden viewer stepping and downloading images.
- openNearest without the panel reports it instead of loading forever, and
  stops if the layer (or its provider) went off during the lookup; the
  mesh sampler's misses are bounded and forgotten with its samples; the
  sequence count is the union of ids across tiles; a provider withdraws
  only its own error, so a sequence error cannot mask KEY REJECTED; hover
  stays on (throttled) under an orbiting camera and re-picks when it
  stops; "since N days" follows the clock in a long-open tab.
- A prewarm waits for one in flight and then builds if it gave up.
- New coverage for the layer core (credits, KEY REJECTED vs REQUIRED,
  switching a provider off), the refresh wiring of the tilted-view fix and
  the draped-to-cast swap completion.
- Cleanup: loading is derived from pending; freshStreet(); tile ranking
  computes distances once and shares one path for the date line; shared
  metresBetween / whenIdle / redrawTile; one NEAREST_RADIUS_M and one
  source method list; unused contract fields, exports, getImage and the
  decoder's image branch removed; PROVIDER_COLORS lists only Mapillary.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-05 18:35:44 +01:00

128 lines
5.1 KiB
JavaScript

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);
});