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]>
This commit is contained in:
authored and GitHub committed 2026-09-28 19:07:57 -05:00
1 parent 81eb44340d
commit f4fcf267c6
200 files changed
+26665 -1977

No files matched your search

+720 -87
View File
@@ -1,10 +1,14 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { readFileSync } from 'node:fs';
import { tilesForBounds, tileToBBox } from '../../data/tomtomTiles.js';
import { decodeFlowTile } from './flowDecode.js';
import { decodeOpenFreeMapTile } from '../../sources/openFreeMap.js';
import {
RoadRequestError,
createTrafficSource,
roadRequestError,
trafficDetailBounds,
createTrafficSource,
} from './source.js';
import { clampBoundsAroundCenter } from '../../data/trafficBounds.js';
const bounds = { south: 30.267, west: -97.744, north: 30.268, east: -97.743 };
@@ -46,6 +50,7 @@ test('a cancelled flow body cannot refill its source cache', async () => {
const source = createTrafficSource({
fetchImpl: async () => ({
ok: true,
headers: new Headers(),
arrayBuffer: async () => {
calls++;
if (calls === 1) controller.abort();
@@ -64,55 +69,36 @@ test('a cancelled flow body cannot refill its source cache', async () => {
'cancelled bytes were not admitted to the decode cache',
);
});
test('road requests have finite bounds and retain the two-pass query', async () => {
test('road requests validate bounds and select z12/z14 immutable tile passes', async () => {
const calls = [];
const source = createTrafficSource({
fetchImpl: async (...args) => {
calls.push(args);
return new Response('{"elements":[]}');
mapTiles: {
async fetchBounds(box, options) {
calls.push({ box, ...options });
return { tiles: [{ roads: [] }], partial: false };
},
clear() {},
},
});
await assert.rejects(
source.requestRoads({ ...bounds, north: Infinity }),
/bounded road viewport/,
);
await assert.rejects(
source.requestRoads(bounds, { timeoutSec: '25];out;' }),
/bounded road viewport/,
);
assert.equal(calls.length, 0);
await source.requestRoads(bounds, { majorOnly: true, timeoutSec: 8 });
assert.equal(calls[0][0], '/api/overpass');
const query = new URLSearchParams(calls[0][1].body).get('data');
assert.match(query, /\[timeout:8\]/);
assert.doesNotMatch(query, /residential/);
await source.requestRoads(bounds, { majorOnly: true });
await source.requestRoads(bounds);
assert.match(
new URLSearchParams(calls[1][1].body).get('data'),
/residential/,
assert.deepEqual(
calls.map((c) => c.zoom),
[12, 14],
);
});
test('antimeridian clamps produce road bounds accepted on either side', async () => {
const calls = [];
const source = createTrafficSource({
fetchImpl: async (...args) => {
calls.push(args);
return new Response('{"elements":[]}');
},
});
for (const centerLon of [179.99, -179.99]) {
const clamped = clampBoundsAroundCenter(
{
south: -0.02,
north: 0.02,
west: 179.98,
east: -179.98,
},
{ south: -0.02, north: 0.02, west: 179.98, east: -179.98 },
{ lat: 0, lon: centerLon },
);
await source.requestRoads(clamped);
}
assert.equal(calls.length, 2);
assert.equal(calls.length, 4);
});
test('malformed availability is an unavailable source rather than a keyless response', async () => {
const source = createTrafficSource({
@@ -136,91 +122,516 @@ test('traffic construction is inert and parameters belong to each layer', async
assert.equal(b.getParams().uncoveredRoads, 'sim');
});
test('road body parsing retains the source request cancellation signal', async () => {
const controller = new AbortController();
test('road tile replies respect cancellation even when the tile source ignores it', async () => {
const abort = new AbortController();
const source = createTrafficSource({
fetchImpl: async () => ({
ok: true,
status: 200,
headers: new Headers(),
json: async () => {
controller.abort();
return { elements: [] };
mapTiles: {
async fetchBounds() {
abort.abort();
return { tiles: [{ roads: [] }], partial: false };
},
}),
},
});
const response = await source.requestRoads(bounds, {
signal: controller.signal,
await assert.rejects(source.requestRoads(bounds, { signal: abort.signal }), {
name: 'AbortError',
});
await assert.rejects(response.json(), { name: 'AbortError' });
});
test('road sources decode direction and coordinates before scene construction', async () => {
const geometry = [
{ lat: 30, lon: -97 },
{ lat: 30.001, lon: -97.001 },
];
test('keyed and keyless geometry is identical OpenFreeMap data, independent of TomTom failures', async () => {
const tile = decodeOpenFreeMapTile(
readFileSync(
new URL(
'../../data/fixtures/ofm-austin-14-3743-6745.pbf',
import.meta.url,
),
),
14,
3743,
6745,
);
const box = tileToBBox(14, 3743, 6745);
const calls = [];
const source = createTrafficSource({
fetchImpl: async () =>
Response.json({
elements: [
{ type: 'node', id: 1 },
{ type: 'way', geometry, tags: { highway: 'primary', oneway: '-1' } },
{ type: 'way', geometry, tags: { junction: 'roundabout' } },
{ type: 'way', geometry: [geometry[0]] },
],
}),
fetchImpl: async () => {
throw new Error('HTTP 429');
},
mapTiles: {
fetchBounds: async (_, options) => {
calls.push(options.zoom);
return { tiles: [tile], partial: false };
},
},
});
assert.deepEqual(await (await source.requestRoads(bounds)).json(), {
const keyed = await (await source.requestRoads(box, { live: true })).json();
const keyless = await (
await source.requestRoads(box, { live: false })
).json();
assert.deepEqual(keyed, keyless);
assert.equal(keyed.roadSource, 'OpenStreetMap');
assert.ok(keyed.roads.length > 0);
assert.ok(keyed.roads.every((road) => !road.flow));
assert.deepEqual(calls, [14, 14]);
await assert.rejects(source.fetchFlowForBounds(bounds), /HTTP 429/);
assert.deepEqual(await (await source.requestRoads(box)).json(), keyless);
});
test('traffic status is session-cached after settlement, but cancelled discovery can restart', async () => {
const { createFlow } = await import('./flow.js');
let calls = 0;
const state = {};
const flow = createFlow({
state,
services: { credits: { registerDynamicCredit() {} } },
parts: {},
source: {
async getStatus({ signal }) {
calls++;
signal?.throwIfAborted();
return { hasKey: false };
},
},
});
const first = new AbortController();
await flow.ensureFlowStatus(first.signal);
first.abort();
await flow.ensureFlowStatus(new AbortController().signal);
assert.equal(calls, 1);
const retryState = {};
const retryFlow = createFlow({
state: retryState,
services: { credits: {} },
parts: {},
source: {
async getStatus({ signal }) {
calls++;
signal.throwIfAborted();
return { hasKey: false };
},
},
});
await assert.rejects(retryFlow.ensureFlowStatus(first.signal), {
name: 'AbortError',
});
await retryFlow.ensureFlowStatus(new AbortController().signal);
assert.equal(calls, 3);
});
test('road parsing defers surface reads to the cancellable preparation pass', async () => {
const { createModel } = await import('./model.js');
let lookups = 0;
const model = createModel({
state: {
_viewer: {
scene: {
sampleHeightSupported: true,
sampleHeight() {
assert.fail('offscreen 3D height probe on keyless globe');
},
globe: {
show: true,
getHeight() {
lookups++;
return 0;
},
},
},
},
},
services: {},
parts: {},
source: {},
});
const roads = model.parseRoads({
roads: [
{
coordinates: [
[-97, 30],
[-97.001, 30.001],
[-97.74, 30.27],
[-97.741, 30.271],
],
type: 'primary',
oneway: -1,
},
{
coordinates: [
[-97, 30],
[-97.001, 30.001],
],
type: 'unclassified',
oneway: 1,
type: 'residential',
oneway: 0,
},
],
});
assert.equal(roads.length, 2);
assert.deepEqual(
roads.map((r) => r.oneway),
[1, -1],
);
assert.equal(roads[0].waypoints.length, 2);
assert.equal(lookups, 0);
});
test('an Overpass refusal is named by status, and keeps the status beside the words', () => {
// The panel prints this string verbatim, so the mapping is part of the
// layer's contract with the reader, not an implementation detail.
assert.equal(roadRequestError(429).message, 'Overpass rate-limited');
assert.equal(roadRequestError(504).message, 'Overpass timed out');
test('parsed road cache evicts old views by byte budget and never retains partial snapshots', async () => {
const { cacheRoadSnapshot } = await import('./ingestion.js');
const entries = new Map();
const entry = () => ({
major: [
{
coords: [
[1, 2],
[3, 4],
],
},
],
full: null,
});
const first = entry();
cacheRoadSnapshot(entries, 'old', first);
cacheRoadSnapshot(entries, 'new', entry(), {
maxBytes: first.cacheBytes + 1,
});
assert.deepEqual([...entries.keys()], ['new']);
cacheRoadSnapshot(entries, 'new', entry(), { retain: false });
assert.equal(entries.size, 0);
cacheRoadSnapshot(entries, 'oversized', entry(), { maxBytes: 1 });
assert.equal(entries.size, 0);
});
for (const lat of [51.5, 60])
test(`z14 detail at ${lat} stays centered within the sixteen-tile cap`, async () => {
const box = {
south: lat - 0.025,
north: lat + 0.025,
west: -0.155,
east: -0.105,
};
assert.ok(tilesForBounds(box, 14).length > 16);
const detail = trafficDetailBounds(box);
assert.ok(tilesForBounds(detail, 14).length <= 16);
assert.ok(Math.abs((detail.south + detail.north) / 2 - lat) < 1e-10);
const source = createTrafficSource({
mapTiles: { fetchBounds: async () => ({ tiles: [], partial: false }) },
});
const data = await (await source.requestRoads(box)).json();
assert.equal(data.detailLimited, true);
assert.deepEqual(data.detailBounds, detail);
});
test('TomTom buffers are clipped before caching and preserve direction and flow attributes', async () => {
const core = tileToBBox(12, 935, 1686);
const source = createTrafficSource({
fetchImpl: async () => new Response(fixture),
});
const result = await source.fetchFlowForBounds({
...core,
east: core.east - 1e-9,
south: core.south + 1e-9,
});
const decoded = decodeFlowTile(fixture, 12, 935, 1686);
assert.ok(
decoded.some((r) =>
r.coords.some(
([x, y]) =>
x < core.west || x > core.east || y < core.south || y > core.north,
),
),
);
assert.ok(
result.every((r) =>
r.coords.every(
([x, y]) =>
x >= core.west - 1e-10 &&
x <= core.east + 1e-10 &&
y >= core.south - 1e-10 &&
y <= core.north + 1e-10,
),
),
);
assert.ok(result.some((r) => r.closure));
});
test('a failed detail pass keeps major roads and exposes separate degraded status', async () => {
const { createIngestion } = await import('./ingestion.js');
for (const retained of [false, true]) {
const state = {
_loadGeneration: 0,
_tileCache: new Map(),
_enabled: true,
_parseRoads: (data) => data.roads,
};
let paints = 0;
const ingestion = createIngestion({
state,
services: {},
parts: {
viewport: {
clampBounds: (b) => b,
getBoundsCenter: (b) => ({
lat: (b.north + b.south) / 2,
lon: (b.east + b.west) / 2,
}),
},
flow: {
warmFlow: async () => {},
applyFlowThenRender: async () => {
paints++;
if (paints === 1) state._retainedPreview = retained;
return true;
},
},
},
source: {
requestRoads: async (_, opts) => {
if (!opts.majorOnly) throw new Error('tile request failed');
return {
ok: true,
json: async () => ({
roads: [],
roadSource: 'OpenStreetMap tiles',
partial: false,
}),
};
},
},
});
await ingestion.loadRoadsForBounds(bounds, 350);
assert.equal(
paints,
retained ? 2 : 1,
'only an unpainted retained preview needs fallback work',
);
assert.equal(state._roadError, null);
assert.equal(
state._detailError,
'Detailed roads unavailable — OpenFreeMap tiles unavailable',
);
assert.equal(state._roadPartial, true);
assert.equal(state._fetching, false);
clearTimeout(state._retryTimer);
}
});
const ofmDetail = decodeOpenFreeMapTile(
readFileSync(
new URL('../../data/fixtures/ofm-austin-14-3743-6745.pbf', import.meta.url),
),
14,
3743,
6745,
);
async function matchingContext(fetchFlowForBounds) {
const { createState } = await import('./state.js');
const { createModel } = await import('./model.js');
const { createFlow } = await import('./flow.js');
const { createControls } = await import('./controls.js');
const state = createState({ services: {} });
Object.assign(state, {
_liveMode: true,
_enabled: true,
_flowStatusPromise: Promise.resolve(),
});
const source = {
fetchFlowForBounds,
getFlowSessionStats: () => ({ tilesFetched: 1 }),
};
const parts = {
style: {
presetProfileActive: () => false,
jamDensityOn: () => false,
baseDotSize: () => 2,
activeSizeDelta: () => 0,
},
rendering: {
rebuildHeatLines() {},
visibleRoadsForAltitude: (roads) => roads,
},
};
const context = { state, source, parts, services: { credits: {} } };
parts.model = createModel(context);
return {
state,
model: parts.model,
flow: createFlow(context),
controls: createControls(context).methods,
};
}
test('Austin TomTom fixture matches congestion onto OFM detail roads without changing geometry', async () => {
const source = createTrafficSource({
fetchImpl: async () => new Response(fixture),
});
const context = await matchingContext(source.fetchFlowForBounds);
const roads = context.model.parseRoads({ roads: ofmDetail.roads });
const coordinates = structuredClone(roads.map((r) => r.coords));
await context.flow.applyFlowToRoads(roads, tileToBBox(14, 3743, 6745), 0);
const matched = roads.filter((r) => r.flow);
assert.ok(matched.length > 0);
assert.ok(roads.length >= 94);
assert.ok(roads.every((r) => [1, -1].includes(r.oneway)));
assert.ok(matched.some((r) => r.flow.level < 0.8 && r.flow.level > 0));
assert.deepEqual(
roads.map((r) => r.coords),
coordinates,
'flow never substitutes its own polylines',
);
assert.equal(context.state._flowPending, 0);
// Inject closure attributes on the same checked-in TomTom geometry: the
// Austin detail snapshot has no real closures on its matched roads.
const segments = await source.fetchFlowForBounds(tileToBBox(14, 3743, 6745));
const closed = await matchingContext(async () =>
segments.map((s) => ({ ...s, closure: true })),
);
await closed.flow.applyFlowToRoads(roads, bounds, 0);
assert.equal(roads.filter((r) => r.flow?.closure).length, matched.length);
assert.ok(
roads
.filter((r) => r.flow)
.every((r) => closed.model.computeDotCount(r, 350) === 0),
);
});
test('late closures hide and stop dots; coverage counts shown matched dots, and reopening restores them', async () => {
const C = await import('cesium');
const { state, model, controls } = await matchingContext(async () => []);
const matched = { flow: { level: 1, closure: false }, type: 'primary' };
const sim = { flow: null, type: 'residential' };
const closed = { flow: { level: 0, closure: true }, type: 'primary' };
state._roads = [matched, sim, closed];
state._dots = [matched, matched, matched, sim, closed].map((road) => ({
road,
baseMps: 10,
mps: 10,
point: { show: true, color: C.Color.WHITE },
}));
model.recolorDotsInPlace('fixture');
assert.equal(state._dots[4].point.show, false);
assert.equal(state._dots[4].mps, 0);
assert.equal(controls.getStats().count, 4);
assert.equal(controls.getStats().flowCoveragePct, 75);
assert.equal(
state._dots[3].mps,
10,
'unmatched roads retain simulated speed',
);
assert.equal(state._dots[3].bucket, null);
closed.flow = { level: 1, closure: false };
model.recolorDotsInPlace('reopened');
assert.equal(state._dots[4].point.show, true);
assert.equal(state._dots[4].mps, 10);
assert.equal(controls.getStats().count, 5);
assert.equal(controls.getStats().flowCoveragePct, 80);
});
test('refresh failure removes cached matches and reports simulated flow', async () => {
const { flow, state, model, controls } = await matchingContext(async () => {
throw new Error('HTTP 429');
});
const roads = model.parseRoads({ roads: ofmDetail.roads });
roads[0].flow = { level: 0.1, closure: true };
await flow.applyFlowToRoads(roads, bounds, 0);
assert.ok(roads.every((r) => r.flow === null));
assert.equal(controls.getStats().flowCoveragePct, 0);
assert.match(
controls.getStats().loadingLabel,
/SIMULATED — TomTom daily budget reached/,
);
assert.equal(state._flowPending, 0);
});
test('superseded flow cannot apply matches after a camera move or disable', async () => {
let finish;
const { flow, state, model } = await matchingContext(
() =>
new Promise((resolve) => {
finish = resolve;
}),
);
const roads = model.parseRoads({ roads: ofmDetail.roads });
const pending = flow.applyFlowToRoads(roads, bounds, 0);
while (!finish) await new Promise((resolve) => setTimeout(resolve, 0));
state._loadGeneration++;
state._flowPending = 0;
state._enabled = false;
finish(decodeFlowTile(fixture, 12, 935, 1686));
await pending;
assert.ok(roads.every((r) => r.flow === null));
assert.equal(state._flowPending, 0);
});
test('OSM keyed loads run z12 then z14 and reuse road snapshots while refreshing flow', async () => {
const { createIngestion } = await import('./ingestion.js');
const state = {
_loadGeneration: 0,
_tileCache: new Map(),
_enabled: true,
_liveMode: true,
_roadMode: 'osm',
_parseRoads: (data) => data.roads,
};
const passes = [],
paints = [];
const ingestion = createIngestion({
state,
services: {},
parts: {
viewport: {
clampBounds: (b) => b,
getBoundsCenter: () => ({ lat: 30.267, lon: -97.744 }),
},
flow: {
warmFlow: async () => {},
applyFlowThenRender: async (roads) => {
paints.push(roads);
return true;
},
},
},
source: {
requestRoads: async (_, options) => {
passes.push(options.majorOnly);
return {
ok: true,
json: async () => ({
roads: ofmDetail.roads,
roadSource: 'OpenStreetMap',
roadMode: 'osm',
}),
};
},
},
});
await ingestion.loadRoadsForBounds(bounds, 350);
assert.deepEqual(passes, [true, false]);
await ingestion.loadRoadsForBounds(bounds, 350);
assert.deepEqual(
passes,
[true, false],
'repeat view does not fetch road geometry',
);
assert.equal(
paints.length,
3,
'cached roads still pass through flow refresh',
);
});
test('an OpenFreeMap refusal is named by status, and keeps the status beside the words', () => {
assert.equal(roadRequestError(429).message, 'OpenFreeMap tiles rate-limited');
assert.equal(roadRequestError(504).message, 'OpenFreeMap tiles timed out');
assert.equal(
roadRequestError(406).message,
'Overpass refused the road query (HTTP 406)',
'OpenFreeMap tiles unavailable (HTTP 406)',
);
assert.equal(
roadRequestError(500).message,
'Overpass refused the road query (HTTP 500)',
'OpenFreeMap tiles unavailable (HTTP 500)',
);
// 502 and 503 come from the local proxy (mirrors unreachable, local
// limiter busy), so they must not read as a mirror refusing the query.
assert.equal(roadRequestError(502).message, 'Overpass mirrors unreachable');
assert.equal(
roadRequestError(502).message,
'OpenFreeMap tiles unavailable (HTTP 502)',
);
assert.equal(
roadRequestError(503).message,
'Overpass temporarily unavailable',
'OpenFreeMap tiles unavailable (HTTP 503)',
);
assert.equal(roadRequestError(503).status, 503);
// An injected source may refuse without a readable code; a row must never
// print "HTTP undefined" at a reader.
for (const missing of [undefined, null, NaN, 'four-oh-six']) {
const vague = roadRequestError(missing);
assert.equal(vague.message, 'Overpass temporarily unavailable');
assert.equal(vague.message, 'OpenFreeMap tiles unavailable');
assert.equal(vague.status, null, 'an unreadable code is absent, not zero');
}
@@ -234,3 +645,225 @@ test('an Overpass refusal is named by status, and keeps the status beside the wo
'a caller must be able to branch on the code without parsing English',
);
});
for (const phase of ['metadata', 'tile']) {
for (const failure of [406, 429, 503, 504, 'network', 'timeout']) {
test(`OpenFreeMap ${phase} ${failure} retains its reason through the road source`, async () => {
const source = createTrafficSource({
tileFetchImpl: async (url) => {
if (phase === 'tile' && url.endsWith('/planet'))
return Response.json({
tiles: ['https://tiles.openfreemap.org/test/{z}/{x}/{y}.pbf'],
});
if (failure === 'network') throw new TypeError('Failed to fetch');
if (failure === 'timeout')
throw new DOMException('deadline', 'TimeoutError');
return new Response('', { status: failure });
},
});
await assert.rejects(source.requestRoads(bounds), (error) => {
assert.ok(error instanceof RoadRequestError);
assert.equal(
error.status,
typeof failure === 'number' ? failure : null,
);
assert.equal(
error.message,
roadRequestError(
error.status,
failure === 'timeout' ? { name: 'TimeoutError' } : undefined,
).message,
);
assert.equal(error.retryable, true);
return true;
});
});
}
}
test('permanent metadata failures remain non-retryable through the road error wrapper', async () => {
const source = createTrafficSource({
tileFetchImpl: async () => Response.json({}),
});
await assert.rejects(source.requestRoads(bounds), (error) => {
assert.ok(error instanceof RoadRequestError);
assert.equal(error.retryable, false);
assert.equal(error.status, null);
return true;
});
});
test('reticle road requests pass exact bounded tiles and retain distant coarse roads', async () => {
const requests = [];
const source = createTrafficSource({
mapTiles: {
clear() {},
getMetadata: async () => ({}),
async fetchBounds(box, options) {
requests.push(options);
return { tiles: [{ roads: [] }], partial: false };
},
},
});
const box = { south: 30.2, west: -97.8, north: 30.4, east: -97.6 };
const coverage = {
coarse: [{ z: 12, x: 935, y: 1685 }],
detail: [{ z: 14, x: 3743, y: 6745 }],
};
const result = await source.requestOsmRoads(box, { coverage });
assert.deepEqual(
requests.map((r) => r.tiles),
[coverage.detail, coverage.coarse],
);
assert.equal((await result.json()).partial, false);
});
test('footprint requests preserve HTTP failures and cancellation semantics', async () => {
const coverage = { coarse: [{ z: 12, x: 935, y: 1686 }], detail: [] };
const source = createTrafficSource({
mapTiles: {
clear() {},
fetchBounds: async () => {
throw Object.assign(new Error('limited'), { status: 429 });
},
},
});
await assert.rejects(
source.requestOsmRoads(bounds, { coverage }),
(error) => error instanceof RoadRequestError && error.status === 429,
);
const controller = new AbortController();
controller.abort();
await assert.rejects(
source.requestOsmRoads(bounds, { coverage, signal: controller.signal }),
{ name: 'AbortError' },
);
});
test('near detail starts and streams while distant coarse tiles are still pending', async () => {
let releaseCoarse;
const coarse = new Promise((resolve) => {
releaseCoarse = resolve;
});
const order = [],
painted = [];
const source = createTrafficSource({
mapTiles: {
clear() {},
fetchBounds: async (box, options) => {
order.push(options.zoom);
if (options.zoom === 12) return coarse;
const tile = {
roads: [
{
coordinates: [
[-97.744, 30.267],
[-97.743, 30.268],
],
type: 'primary',
},
],
};
options.onTile?.(tile);
return { tiles: [tile], partial: false };
},
},
});
const pending = source.requestOsmRoads(bounds, {
coverage: {
detail: [{ z: 14, x: 3743, y: 6745 }],
coarse: [{ z: 12, x: 935, y: 1686 }],
},
onTile: (tile) => painted.push(tile),
});
await Promise.resolve();
assert.deepEqual(order, [14, 12]);
assert.equal(painted.length, 1);
assert.equal(painted[0].roads.length, 1);
releaseCoarse({ tiles: [], partial: false });
await pending;
});
test('failed detail cores retain real coarse roads while successful cores replace them', async () => {
const detail = [
{ z: 14, x: 3743, y: 6745 },
{ z: 14, x: 3744, y: 6745 },
];
const roads = detail.map((tile, i) => {
const b = tileToBBox(tile.z, tile.x, tile.y);
return {
type: 'primary',
id: i,
coordinates: [
[b.west + 0.002, b.south + 0.002],
[b.east - 0.002, b.north - 0.002],
],
};
});
for (const allFailed of [false, true]) {
const source = createTrafficSource({
mapTiles: {
async fetchBounds(box, { zoom }) {
if (zoom === 12) return { tiles: [{ roads }], partial: false };
if (allFailed) throw new Error('temporary tile failure');
return {
tiles: [{ roads: [] }],
loadedTiles: [detail[0]],
partial: true,
};
},
},
});
const snapshots = [];
const data = await (
await source.requestOsmRoads(bounds, {
coverage: { detail, coarse: [{ z: 12, x: 935, y: 1686 }] },
onTile: (value) => snapshots.push(value),
})
).json();
assert.equal(data.partial, true);
assert.equal(data.replace, true);
assert.deepEqual(data.roads, allFailed ? roads : [roads[1]]);
assert.deepEqual(snapshots.at(-1).roads, data.roads);
}
});
test('traffic separates API and public tile transports, including the default tile fetch', async (t) => {
const publicUrls = [],
apiUrls = [];
const publicFetch = async (url) => {
publicUrls.push(url);
return url.endsWith('/planet')
? Response.json({
tiles: ['https://tiles.openfreemap.org/test/{z}/{x}/{y}.pbf'],
})
: new Response(
readFileSync(
new URL(
'../../data/fixtures/ofm-austin-14-3743-6745.pbf',
import.meta.url,
),
),
);
};
t.mock.method(globalThis, 'fetch', publicFetch);
for (const tileFetchImpl of [undefined, publicFetch]) {
const source = createTrafficSource({
tileFetchImpl,
fetchImpl: async (url) => {
assert.ok(url.startsWith('/api/'));
apiUrls.push(url);
return url.endsWith('/status')
? Response.json({ hasKey: true })
: new Response(fixture);
},
});
await source.getStatus();
await source.fetchFlowForBounds(bounds);
assert.ok(
(await (await source.requestOsmRoads(bounds)).json()).roads.length,
);
}
assert.ok(publicUrls.length >= 4);
assert.ok(apiUrls.length >= 4);
});