Files
gods-eye-view/src/search/geospatial.test.mjs
T
f4fcf267c6 fix(osm): stop using public Overpass by default (#648) (#742)
Traffic roads come from OpenFreeMap vector tiles, or TomTom flow tiles with a
key (Hybrid by default). Military areas use OpenFreeMap polygons with a bundled
worldwide name index; wide views show named points. ALPR reads an hourly
OpenStreetMap extract. Cockpit place context and country, state and US county
outlines use bundled Natural Earth and US Census data. Overpass is used only
when an operator configures their own endpoint.

Also: camera arrivals no longer land below ground, keyless terrain retries on
429, bounded relation geometry for configured Overpass, one shared
OpenStreetMap credit, and separate tile and API transports for embedders.

Co-authored-by: theGaryLarson <[email protected]>
Co-authored-by: Milan Khanal <[email protected]>
2026-09-28 19:07:57 -05:00

241 lines
7.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
createGeospatialServices,
createHttpGeospatialProvider,
createDefaultPlaceSearch,
} from './index.js';
import { installRouteMiddleware } from '../../server/providers/places/routes.js';
import { createRegionalPlaceProvider } from '../../server/providers/regional/place.js';
const point = { latitude: 30, longitude: -97, radiusM: 250 };
const coordinates = [
[-97, 30],
[-97.001, 30.001],
];
const route = { geometry: coordinates, distanceM: 120, durationS: 90 };
const json = (data) =>
new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' },
});
test('each operation can use an independent provider without coupling forward search', async () => {
const calls = [];
const service = createDefaultPlaceSearch({
providers: {
geocode: [
{
geocode: async () => ({
place: { lat: 30, lng: -97 },
answered: true,
}),
},
],
reverseGeocode: async (lat, lon) => {
calls.push([lat, lon]);
return { locality: 'Town' };
},
textSearch: async (q) => [{ name: q, ...point }],
nearby: async (p) => [p],
route: async () => route,
routeProfiles: ['foot'],
},
});
assert.equal((await service.geocode('Town')).place.lat, 30);
assert.equal((await service.reverseGeocode(30, -97)).locality, 'Town');
assert.equal((await service.textSearch('Library', point))[0].name, 'Library');
assert.deepEqual(await service.nearby(point), [point]);
assert.deepEqual(await service.route(coordinates), {
...route,
ok: true,
profile: 'foot',
});
assert.equal(await service.route(coordinates, 'car'), null);
assert.deepEqual(calls, [[30, -97]]);
});
test('unsupported operations and invalid coordinates never initiate a request', async () => {
let calls = 0;
const service = createGeospatialServices({
providers: {
route: () => {
calls++;
},
},
});
assert.equal(service.capabilities.reverseGeocode, false);
assert.equal(await service.reverseGeocode(30, -97), null);
assert.equal(
await service.route([
[999, 30],
[-97, 30],
]),
null,
);
assert.equal(await service.route(coordinates, 'spaceship'), null);
assert.equal(calls, 0);
const invalid = createGeospatialServices({
providers: { route: async () => ({ ...route, durationS: NaN }) },
});
assert.equal(await invalid.route(coordinates), null);
});
test('application or caller cancellation rejects late provider and body results', async () => {
for (const lifetimeAbort of [true, false]) {
const lifetime = new AbortController();
const caller = new AbortController();
const service = createGeospatialServices({
signal: lifetime.signal,
providers: createHttpGeospatialProvider({
endpoints: { route: '/custom/route' },
fetchImpl: async (_url, { signal }) => ({
ok: true,
async json() {
(lifetimeAbort ? lifetime : caller).abort();
assert.equal(signal.aborted, true);
return { ...route, ok: true };
},
}),
}),
});
await assert.rejects(
service.route(coordinates, 'foot', { signal: caller.signal }),
{ name: 'AbortError' },
);
}
});
test('compatible HTTP endpoints retain units and never place a server credential in browser requests', async () => {
const urls = [];
const service = createGeospatialServices({
providers: createHttpGeospatialProvider({
endpoints: {
reverse: '/geo/reverse',
textSearch: '/geo/search',
nearby: '/geo/nearby',
route: '/geo/route',
},
fetchImpl: async (url) => {
urls.push(new URL(url, 'https://example.test'));
if (url.startsWith('/geo/reverse'))
return json({
status: 'OK',
results: [{ formatted_address: 'Town', address_components: [] }],
});
if (url.startsWith('/geo/route')) return json({ ok: true, ...route });
return json({ places: [{ name: 'Library', ...point }] });
},
}),
});
assert.equal(
(await service.reverseGeocode(30, -97)).formattedAddress,
'Town',
);
await service.textSearch('Library', point);
await service.nearby(point);
assert.equal((await service.route(coordinates)).distanceM, 120);
assert.deepEqual(
urls.map((url) => url.pathname),
['/geo/reverse', '/geo/search', '/geo/nearby', '/geo/route'],
);
assert.ok(urls.every((url) => !url.searchParams.has('key')));
});
test('HTTP refusal cancels its response and does not retry another destination', async () => {
let cancelled = 0,
calls = 0;
const provider = createHttpGeospatialProvider({
fetchImpl: async () => {
calls++;
return {
ok: false,
body: {
async cancel() {
cancelled++;
},
},
};
},
});
await assert.rejects(provider.nearby(point), /unavailable/);
assert.equal(calls, 1);
assert.equal(cancelled, 1);
});
test('Photon configuration changes its endpoint without changing its normalized result', async () => {
const service = createDefaultPlaceSearch({
endpoints: { photon: 'https://search.example/api/' },
fetchImpl: async (url) => {
assert.equal(new URL(url).origin, 'https://search.example');
return json({
features: [
{
type: 'Feature',
geometry: { type: 'Point', coordinates: [-97, 30] },
properties: { name: 'Town' },
},
],
});
},
});
assert.equal((await service.geocode('Town')).place.lat, 30);
});
test('routing middleware uses configured OSRM servers while retaining request bounds and profile', async () => {
let handler,
calls = 0;
installRouteMiddleware(
{
use(_path, fn) {
handler = fn;
},
},
{
endpoints: { foot: 'https://routes.example/walking' },
fetchImpl: async (url, options) => {
calls++;
assert.match(
url,
/^https:\/\/routes\.example\/walking\/route\/v1\/foot\//,
);
assert.equal(options.redirect, 'error');
return json({
code: 'Ok',
routes: [{ geometry: { coordinates }, distance: 120, duration: 90 }],
});
},
},
);
const request = async (query) => {
let payload;
await handler(
{ url: `/?${query}`, socket: { remoteAddress: '127.0.0.1' } },
{
writeHead() {},
end(body) {
payload = JSON.parse(body);
},
},
);
return payload;
};
assert.equal(
(await request('profile=foot&coords=-97,30;-97.001,30.001')).distanceM,
120,
);
assert.equal((await request('profile=foot&coords=-97,30;97,-30')).ok, false);
assert.equal(calls, 1);
});
test('regional place lookup uses bundled Natural Earth with networking unavailable', async (t) => {
t.mock.method(globalThis, 'fetch', () => {
throw new Error('Network forbidden');
});
const lookup = createRegionalPlaceProvider();
assert.equal(
(await lookup({ latitude: 30.2672, longitude: -97.7431 })).region,
'Edwards Plateau',
);
assert.equal((await lookup({ latitude: 26, longitude: -90 })).kind, 'marine');
});