Files
gods-eye-view/src/tools/queries/share.test.mjs
T
Said Turksever 30e6cd39c7 fix(street-level): re-check after every await; close the old photo; fresh panel views
Address samehkhamis's review on #1113:

- show_street_level checks the turn, the camera's owner (navigation stamp,
  re-taken where its own flight lands, or a press/wheel on the globe) and
  the hiding modes after the flight, the map switch, the layer switch-on and
  the lookup; a photo that opened after either is closed again. The hidden
  panel is also refused before the flight.
- An open photo closes before a new request arms or looks up, so a place
  with no imagery shows its own error.
- The panel posts every view with `fresh`; the inline app opens a billed
  photo only for a fresh one.
- A passed-through view's street_level point moves with a new camera
  position; revealPanel does nothing above 720px, scrolling included.
- Document that the app's own links do not carry `sl`.
2026-11-06 00:40:20 +00:00

302 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from 'node:assert/strict';
import test from 'node:test';
import { composeCatalog, coreTools } from '../index.js';
test('links open the app looking straight down on the area', async () => {
const catalog = composeCatalog({
tools: coreTools,
services: { app: { baseUrl: 'https://maps.example/view?x=1' } },
});
const result = await catalog.call('show_in_gods_eye_view', {
area: { lat: 30.2672, lon: -97.7431, radius_km: 5 },
});
const url = new URL(result.data.url);
assert.equal(
url.origin + url.pathname + url.search,
'https://maps.example/view?x=1',
);
assert.deepEqual(Object.fromEntries(new URLSearchParams(url.hash.slice(1))), {
v: '2',
lat: '30.2672',
lon: '-97.7431',
alt: '9091',
heading: '0',
pitch: '-90',
roll: '0',
});
assert.equal(
result.summary,
`Open 5 km around 30.267, -97.743 in God's Eye View: ${url.href}`,
);
});
test('altitude is bounded for tiny and planet-sized areas', async () => {
const catalog = composeCatalog({
tools: coreTools,
services: { app: { baseUrl: 'http://localhost:4173/' } },
});
const tiny = await catalog.call('show_in_gods_eye_view', {
area: { lat: 0, lon: 0, radius_km: 0.1 },
});
assert.equal(tiny.data.view.camera.altitude_m, 500);
const world = await catalog.call('show_in_gods_eye_view', {
area: { bbox: [-180, -85, 180, 85] },
});
assert.equal(world.data.view.camera.altitude_m, 15000000);
const broken = composeCatalog({
tools: coreTools,
services: { app: { baseUrl: 'not a url' } },
});
await assert.rejects(
broken.call('show_in_gods_eye_view', {
area: { lat: 0, lon: 0, radius_km: 1 },
}),
(error) => error.code === 'unavailable',
);
});
test('links turn on the requested layers with the app share-link codec', async () => {
const { decodeLayerStateParams } = await import('../../data/layerState.js');
const catalog = composeCatalog({
tools: coreTools,
services: { app: { baseUrl: 'http://localhost:4173/' } },
});
const result = await catalog.call('show_in_gods_eye_view', {
area: { lat: 30.27, lon: -97.74, radius_km: 10 },
layers: ['earthquakes', 'flights', 'flights'],
});
assert.deepEqual(result.data.view.layers, ['earthquakes', 'flights']);
const params = new URLSearchParams(new URL(result.data.url).hash.slice(1));
assert.deepEqual(decodeLayerStateParams(params).enabledLayerIds.sort(), [
'earthquakes',
'flights',
]);
await assert.rejects(
catalog.call('show_in_gods_eye_view', {
area: { lat: 0, lon: 0, radius_km: 1 },
layers: ['not-a-layer'],
}),
/must be one of/,
);
});
test('links carry a camera, style, map and an entity to follow', async () => {
const { viewFromParams } = await import('../../view/index.js');
const catalog = composeCatalog({
tools: coreTools,
services: { app: { baseUrl: 'http://localhost:4173/' } },
});
const result = await catalog.call('show_in_gods_eye_view', {
area: { lat: 25, lon: 121, radius_km: 200 },
camera: { pitch_deg: -40, heading_deg: 350 },
layers: ['ais-live-vessels'],
style: 'thermal',
map: 'esri-imagery',
follow: { kind: 'military_aircraft', id: 'AE1234' },
});
const params = new URLSearchParams(new URL(result.data.url).hash.slice(1));
assert.equal(params.get('style'), 'flir');
assert.deepEqual(viewFromParams(params), result.data.view);
assert.deepEqual(result.data.view.layers, ['ais-live-vessels', 'military']);
assert.deepEqual(result.data.view.follow, {
kind: 'military_aircraft',
id: 'ae1234',
});
assert.equal(result.data.view.camera.pitch_deg, -40);
const camera = await catalog.call('show_in_gods_eye_view', {
camera: { lat: 48.8584, lon: 2.2945, altitude_m: 1200, pitch_deg: -30 },
});
assert.equal(camera.data.view.camera.altitude_m, 1200);
assert.match(camera.summary, /^Open 48\.858, 2\.295 in God's Eye View: /);
await assert.rejects(
catalog.call('show_in_gods_eye_view', { layers: ['flights'] }),
/Give a view, an area, or a camera with lat and lon/,
);
});
test('links can open street-level photos at the area, on the map the source needs', async () => {
const { viewFromParams } = await import('../../view/index.js');
const catalog = composeCatalog({
tools: coreTools,
services: { app: { baseUrl: 'http://localhost:4173/' } },
});
const result = await catalog.call('show_in_gods_eye_view', {
area: { lat: 38.5799, lon: -121.4937, radius_km: 1 },
street_level: { provider: 'google' },
});
const { view } = result.data;
assert.equal(view.map, 'photoreal');
assert.deepEqual(view.layers, ['street-level']);
assert.deepEqual(view.street_level, {
provider: 'google',
lat: 38.5799,
lon: -121.4937,
});
const params = new URLSearchParams(new URL(result.data.url).hash.slice(1));
assert.equal(params.get('sl'), 'google,38.57990,-121.49370');
assert.equal(params.get('map'), 'photoreal');
assert.deepEqual(viewFromParams(params), view);
// The panel opens a billed photo only for a result it gets fresh.
assert.ok(Math.abs(Date.now() - result.data.issued_at) < 60_000);
const point = await catalog.call('show_in_gods_eye_view', {
view,
street_level: { provider: 'mapillary', lat: 38.58, lon: -121.49 },
});
assert.deepEqual(point.data.view.street_level, {
provider: 'mapillary',
lat: 38.58,
lon: -121.49,
});
await assert.rejects(
catalog.call('show_in_gods_eye_view', {
area: { lat: 0, lon: 0, radius_km: 1 },
map: 'osm',
street_level: { provider: 'google' },
}),
/photoreal/,
);
});
test('a new area or camera position moves a passed-through view’s photos, and a tilted area still opens them at its centre', async () => {
const catalog = composeCatalog({
tools: coreTools,
services: { app: { baseUrl: 'http://localhost:4173/' } },
});
const first = await catalog.call('show_in_gods_eye_view', {
area: { lat: 38.5799, lon: -121.4937, radius_km: 1 },
street_level: { provider: 'google' },
});
const second = await catalog.call('show_in_gods_eye_view', {
view: first.data.view,
area: { lat: 42.3314, lon: -83.0458, radius_km: 1 },
});
assert.deepEqual(second.data.view.street_level, {
provider: 'google',
lat: 42.3314,
lon: -83.0458,
});
// A new camera position moves them too; turning the camera keeps them.
const moved = await catalog.call('show_in_gods_eye_view', {
view: first.data.view,
camera: { lat: 40.758, lon: -73.9855, altitude_m: 800, pitch_deg: -90 },
});
assert.deepEqual(moved.data.view.street_level, {
provider: 'google',
lat: 40.758,
lon: -73.9855,
});
const turned = await catalog.call('show_in_gods_eye_view', {
view: first.data.view,
camera: { heading_deg: 90 },
});
assert.deepEqual(turned.data.view.street_level, first.data.view.street_level);
const tilted = await catalog.call('show_in_gods_eye_view', {
area: { lat: 48.8584, lon: 2.2945, radius_km: 1 },
camera: { pitch_deg: -30 },
street_level: { provider: 'mapillary' },
});
assert.notEqual(
tilted.data.view.camera.lat,
48.8584,
'the camera sits behind the target',
);
assert.deepEqual(tilted.data.view.street_level, {
provider: 'mapillary',
lat: 48.8584,
lon: 2.2945,
});
// A wide area frames the camera tens of km up, past where a camera's
// look-at point is trusted: the photos still open at the area's centre.
const wide = await catalog.call('show_in_gods_eye_view', {
area: { lat: 48.8584, lon: 2.2945, radius_km: 15 },
camera: { pitch_deg: -30 },
street_level: { provider: 'mapillary' },
});
assert.ok(wide.data.view.camera.altitude_m > 20_000);
assert.deepEqual(wide.data.view.street_level, {
provider: 'mapillary',
lat: 48.8584,
lon: 2.2945,
});
await assert.rejects(
catalog.call('show_in_gods_eye_view', {
area: { lat: 0, lon: 0, radius_km: 1 },
follow: { kind: 'aircraft', id: 'AE1234' },
street_level: {},
}),
/cannot both follow/,
);
});
test('a camera with no photo point opens photos where it looks, measured from its ground', async () => {
const { lookAtPoint } = await import('../../view/index.js');
const asked = [];
const catalog = composeCatalog({
tools: coreTools,
services: {
app: { baseUrl: 'http://localhost:4173/' },
terrain: {
getHeights: async (points) => {
asked.push(points);
return [{ ellipsoid: 1590 }];
},
},
},
});
// Denver: 1,909 m above the ellipsoid is about 320 m above its streets.
const camera = {
lat: 39.7392,
lon: -104.9903,
altitude_m: 1909,
pitch_deg: -30,
heading_deg: 0,
};
const result = await catalog.call('show_in_gods_eye_view', {
camera,
street_level: { provider: 'mapillary' },
});
assert.deepEqual(asked, [[{ lat: 39.7392, lon: -104.9903 }]]);
const grounded = lookAtPoint({ ...camera, altitude_m: 319 });
const { street_level: street } = result.data.view;
assert.ok(Math.abs(street.lat - grounded.lat) < 1e-5, 'about 550 m ahead');
assert.ok(
street.lat - camera.lat < 0.006,
'not the 3.3 km a sea-level guess gives',
);
// No terrain answer: the view's own estimate, as before.
const offline = composeCatalog({
tools: coreTools,
services: {
app: { baseUrl: 'http://localhost:4173/' },
terrain: {
getHeights: async () => {
throw new Error('offline');
},
},
},
});
const fallback = await offline.call('show_in_gods_eye_view', {
camera,
street_level: { provider: 'mapillary' },
});
const guess = lookAtPoint(camera);
assert.ok(Math.abs(fallback.data.view.street_level.lat - guess.lat) < 1e-5);
});
test('a view passed through alone keeps its street-level photos', async () => {
const catalog = composeCatalog({
tools: coreTools,
services: { app: { baseUrl: 'http://localhost:4173/' } },
});
const first = await catalog.call('show_in_gods_eye_view', {
area: { lat: 38.5799, lon: -121.4937, radius_km: 1 },
street_level: { provider: 'mapillary' },
});
const again = await catalog.call('show_in_gods_eye_view', {
view: first.data.view,
});
assert.deepEqual(again.data.view.street_level, first.data.view.street_level);
assert.deepEqual(again.data.view, first.data.view);
});