mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Merge origin/main into german/navara
Conflict resolutions:
- src/main.js: upstream's PHOTON_URL place-search setting is read once at
module level and passed as geospatial: { endpoints: { photon } } by
createApplication(), so every engine start of the switch flow gets it; the
old single top-level createStandaloneApplication() call stays removed.
- src/layers/bikeshare/render/cesium/rendering.js: upstream's
publishedCapacity fix applied with this branch's
resolveCapacity(model, capacity, status) signature. The same fix is ported
to src/layers/bikeshare/render/navara.js (record.publishedCapacity, status
polls resolve from it), with a Navara regression test; upstream's
proxyRoute test imports the moved render/cesium/rendering.js.
- src/layers/traffic/flow.js: upstream's nextTask() yield and current()
check kept before the scene is taken from this branch's
_trafficScene?.() ?? _viewer?.scene with its missing-scene return.
- scripts/package-boundaries.json, scripts/format-scope.json: union of both
sides' file lists.
- src/voice/actionSchemas.test.mjs: both history comments kept, payload hash
recomputed for the merged schemas (Sentinel-2 map stack enum plus the
shorter layer alias hint).
- src/hudAltitudeDatum.test.mjs: this branch's engine-based IntelHUD
construction with upstream's placeLabels summary context.
This commit is contained in:
commit
1a1eef0e5c
494 files changed
+28358
-1361
No files matched your search
@@ -6,6 +6,12 @@ import { tomtomProxy } from 'gods-eye-view/server/providers/traffic';
|
||||
import { firmsProxy } from 'gods-eye-view/server/providers/firms';
|
||||
import { gbfsProxy } from 'gods-eye-view/server/providers/gbfs';
|
||||
import { localProviderPlugins } from '../../server/providers/local.js';
|
||||
import { makeFixtureRoot } from './fixtureRoot.mjs';
|
||||
|
||||
const nominatimStorageDir = await makeFixtureRoot(
|
||||
'gev-environment-composition-',
|
||||
);
|
||||
test.after(() => fsp.rm(nominatimStorageDir, { recursive: true, force: true }));
|
||||
|
||||
function install(plugin, hook = 'configureServer') {
|
||||
const routes = new Map();
|
||||
@@ -88,7 +94,7 @@ test('standalone composition mounts every extracted provider exactly once withou
|
||||
t.mock.method(globalThis, 'fetch', () => {
|
||||
throw Error('construction must not fetch');
|
||||
});
|
||||
const plugins = localProviderPlugins();
|
||||
const plugins = localProviderPlugins({ nominatimStorageDir });
|
||||
for (const factory of [
|
||||
terrainHeightsProxy,
|
||||
tomtomProxy,
|
||||
|
||||
@@ -6,6 +6,10 @@ import path from 'node:path';
|
||||
import { cctvProxy } from '../../server/providers/cctv.js';
|
||||
import { radioBrowserProxy } from '../../server/providers/radio.js';
|
||||
import { localProviderPlugins } from '../../server/providers/local.js';
|
||||
import { makeFixtureRoot } from './fixtureRoot.mjs';
|
||||
|
||||
const nominatimStorageDir = await makeFixtureRoot('gev-media-composition-');
|
||||
test.after(() => rmSync(nominatimStorageDir, { recursive: true, force: true }));
|
||||
|
||||
function install(plugin, hook = 'configureServer') {
|
||||
let handler;
|
||||
@@ -97,7 +101,7 @@ test('CCTV instances resolve their own application source root and isolate catal
|
||||
|
||||
test('composition creates exactly one CCTV and radio provider without acquisition', (t) => {
|
||||
isolate(t);
|
||||
const plugins = localProviderPlugins();
|
||||
const plugins = localProviderPlugins({ nominatimStorageDir });
|
||||
for (const factory of [cctvProxy, radioBrowserProxy]) {
|
||||
assert.equal(plugins.filter((p) => p.name === factory().name).length, 1);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,994 @@
|
||||
// Review regressions for the Nominatim gate: state that survives restarts and
|
||||
// sweeps, several processes sharing one count, long refusals, stalled bodies,
|
||||
// request-equivalent cache keys, and stricter outline selection.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { spawn } from 'node:child_process';
|
||||
import {
|
||||
createGateStateStore,
|
||||
createNominatimGate,
|
||||
resolveNominatimSettings,
|
||||
PUBLIC_NOMINATIM_SEARCH,
|
||||
} from '../../server/providers/regional/nominatimGate.js';
|
||||
import { createNominatimCache } from '../../server/providers/regional/nominatimCache.js';
|
||||
import {
|
||||
outlineBias,
|
||||
selectOutlineResult,
|
||||
} from '../../server/providers/regional/outlineSelect.js';
|
||||
import {
|
||||
createNominatimOutlineProvider,
|
||||
createNominatimSearchProvider,
|
||||
geocodeProxy,
|
||||
} from '../../server/providers/regional/place.js';
|
||||
|
||||
const PUBLIC = {
|
||||
endpoint: PUBLIC_NOMINATIM_SEARCH,
|
||||
isPublic: true,
|
||||
dailyCap: 50,
|
||||
};
|
||||
const LOCAL = { ...PUBLIC, isPublic: false };
|
||||
const tempDir = (t) => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gev-nominatim-'));
|
||||
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
return dir;
|
||||
};
|
||||
const quick = { sleep: async () => {}, minSpacingMs: 0 };
|
||||
const json = (value) => Response.json(value);
|
||||
const square = (lon, lat, d) => [
|
||||
[
|
||||
[lon, lat],
|
||||
[lon + d, lat],
|
||||
[lon + d, lat + d],
|
||||
[lon, lat + d],
|
||||
[lon, lat],
|
||||
],
|
||||
];
|
||||
|
||||
function mount(options) {
|
||||
let handler;
|
||||
geocodeProxy(options).configureServer({
|
||||
middlewares: { use: (_route, fn) => (handler = fn) },
|
||||
});
|
||||
return (url) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const res = {
|
||||
writableEnded: false,
|
||||
on() {},
|
||||
writeHead(status) {
|
||||
res.status = status;
|
||||
},
|
||||
end(body) {
|
||||
res.writableEnded = true;
|
||||
resolve({ status: res.status, body: JSON.parse(body) });
|
||||
},
|
||||
};
|
||||
Promise.resolve(
|
||||
handler(
|
||||
{
|
||||
method: 'GET',
|
||||
url,
|
||||
headers: {},
|
||||
socket: { remoteAddress: `10.9.${Math.random()}` },
|
||||
on() {},
|
||||
},
|
||||
res,
|
||||
),
|
||||
).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
test('the daily count survives cache sweeps and a restart of the real composition', async (t) => {
|
||||
const storageDir = tempDir(t);
|
||||
const fetchImpl = async () => json([]);
|
||||
const first = createNominatimGate({ settings: PUBLIC, fetchImpl, ...quick });
|
||||
const request = mount({ storageDir, gate: first });
|
||||
for (let i = 0; i < 3; i++)
|
||||
assert.equal((await request(`/?q=place-${i}`)).status, 200);
|
||||
assert.equal(first.stats().usedToday, 3);
|
||||
|
||||
// A foreign file in the answers directory and a full sweep leave the
|
||||
// state alone and the stranger untouched.
|
||||
const answers = path.join(storageDir, 'answers');
|
||||
fs.writeFileSync(path.join(answers, 'notes.json'), '{}');
|
||||
await createNominatimCache({ dir: answers, sweepEvery: 1 }).sweep();
|
||||
assert.ok(fs.existsSync(path.join(answers, 'notes.json')));
|
||||
assert.ok(fs.existsSync(path.join(storageDir, 'state.json')));
|
||||
|
||||
// Restart: a new gate composed on the same directory, which sweeps at start.
|
||||
const second = createNominatimGate({
|
||||
settings: { ...PUBLIC, dailyCap: 4 },
|
||||
fetchImpl,
|
||||
...quick,
|
||||
});
|
||||
const again = mount({ storageDir, gate: second });
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
assert.equal(second.stats().usedToday, 3, 'the count came back');
|
||||
assert.equal((await again('/?q=place-new')).status, 200);
|
||||
const capped = await again('/?q=place-over');
|
||||
assert.equal(
|
||||
capped.status,
|
||||
429,
|
||||
'the cap still counts yesterday-restart usage',
|
||||
);
|
||||
assert.equal(capped.body.code, 'NOMINATIM_DAILY_CAP');
|
||||
});
|
||||
|
||||
test('a sweep removes expired and corrupt entries, never ones it could not read', async (t) => {
|
||||
const dir = tempDir(t);
|
||||
const file = (n) => path.join(dir, `${String(n).repeat(40)}.json`);
|
||||
const live = {
|
||||
key: 'k',
|
||||
payload: null,
|
||||
storedAt: 1,
|
||||
expiresAt: Date.now() + 60_000,
|
||||
};
|
||||
fs.writeFileSync(file(1), JSON.stringify(live));
|
||||
fs.writeFileSync(file(2), JSON.stringify({ ...live, expiresAt: 1 }));
|
||||
fs.writeFileSync(file(3), '{not json');
|
||||
const cache = createNominatimCache({ dir });
|
||||
// Reads failing (a busy disk, a test double) must not empty the cache.
|
||||
const fsp = (await import('node:fs/promises')).default;
|
||||
const readFile = t.mock.method(fsp, 'readFile', async () => {
|
||||
throw Object.assign(new Error('EMFILE'), { code: 'EMFILE' });
|
||||
});
|
||||
await cache.sweep();
|
||||
assert.deepEqual(
|
||||
[1, 2, 3].map((n) => fs.existsSync(file(n))),
|
||||
[true, true, true],
|
||||
);
|
||||
readFile.mock.restore();
|
||||
await cache.sweep();
|
||||
assert.deepEqual(
|
||||
[1, 2, 3].map((n) => fs.existsSync(file(n))),
|
||||
[true, false, false],
|
||||
);
|
||||
});
|
||||
|
||||
test('processes sharing state space public sends and cap atomically', async (t) => {
|
||||
const dir = tempDir(t);
|
||||
const module = new URL(
|
||||
'../../server/providers/regional/nominatimGate.js',
|
||||
import.meta.url,
|
||||
).href;
|
||||
const run = (file, dailyCap) => {
|
||||
const script = `
|
||||
const { createGateStateStore } = await import(${JSON.stringify(module)});
|
||||
const store = createGateStateStore({ file: ${JSON.stringify(file)} });
|
||||
const latestStartAt = Date.now() + 10000;
|
||||
let sentAt = null;
|
||||
for (;;) {
|
||||
const result = store.dispatch(
|
||||
'2026-09-29',
|
||||
{ clock: Date.now, dailyCap: ${dailyCap}, minSpacingMs: 300, latestStartAt },
|
||||
() => {
|
||||
sentAt = Date.now();
|
||||
return Promise.resolve();
|
||||
},
|
||||
);
|
||||
if (result.status === 'wait') {
|
||||
await new Promise((resolve) => setTimeout(resolve, result.waitMs));
|
||||
continue;
|
||||
}
|
||||
console.log(JSON.stringify({ status: result.status, sentAt }));
|
||||
break;
|
||||
}
|
||||
`;
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(process.execPath, [
|
||||
'--input-type=module',
|
||||
'-e',
|
||||
script,
|
||||
]);
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
child.stdout.on('data', (chunk) => (stdout += chunk));
|
||||
child.stderr.on('data', (chunk) => (stderr += chunk));
|
||||
child.on('exit', (code) =>
|
||||
code === 0
|
||||
? resolve(JSON.parse(stdout))
|
||||
: reject(new Error(`exit ${code}: ${stderr}`)),
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const spacedFile = path.join(dir, 'spaced.json');
|
||||
const sends = await Promise.all([
|
||||
run(spacedFile, 3),
|
||||
run(spacedFile, 3),
|
||||
run(spacedFile, 3),
|
||||
]);
|
||||
assert.deepEqual(
|
||||
sends.map((result) => result.status),
|
||||
['started', 'started', 'started'],
|
||||
);
|
||||
// Spacing is measured between actual transport invocations.
|
||||
const sentAt = sends.map((result) => result.sentAt).sort((a, b) => a - b);
|
||||
for (let i = 1; i < sentAt.length; i += 1)
|
||||
assert.ok(
|
||||
sentAt[i] - sentAt[i - 1] >= 300,
|
||||
`${sentAt[i] - sentAt[i - 1]}ms apart`,
|
||||
);
|
||||
assert.equal(
|
||||
createGateStateStore({ file: spacedFile }).count('2026-09-29'),
|
||||
3,
|
||||
);
|
||||
|
||||
const cappedFile = path.join(dir, 'capped.json');
|
||||
const capped = await Promise.all([run(cappedFile, 1), run(cappedFile, 1)]);
|
||||
assert.deepEqual(capped.map((result) => result.status).sort(), [
|
||||
'cap',
|
||||
'started',
|
||||
]);
|
||||
assert.equal(
|
||||
createGateStateStore({ file: cappedFile }).count('2026-09-29'),
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
test('independent public gates sharing state enforce spacing and cap', async (t) => {
|
||||
const dir = tempDir(t);
|
||||
const starts = [];
|
||||
const fetchImpl = async () => {
|
||||
starts.push(Date.now());
|
||||
return json([]);
|
||||
};
|
||||
const file = path.join(dir, 'spacing.json');
|
||||
const make = (options = {}) =>
|
||||
createNominatimGate({
|
||||
settings: PUBLIC,
|
||||
fetchImpl,
|
||||
usage: createGateStateStore({ file: options.file || file }),
|
||||
minSpacingMs: options.minSpacingMs || 40,
|
||||
});
|
||||
await Promise.all([
|
||||
createNominatimSearchProvider({ gate: make() })('one', null),
|
||||
createNominatimSearchProvider({ gate: make() })('two', null),
|
||||
]);
|
||||
starts.sort((a, b) => a - b);
|
||||
assert.ok(starts[1] - starts[0] >= 35, `${starts[1] - starts[0]}ms apart`);
|
||||
|
||||
// A slow durable write must move the next admission relative to the actual
|
||||
// fetch invocation, not the timestamp reserved before that write.
|
||||
const stalledFile = path.join(dir, 'stalled.json');
|
||||
let clock = Date.UTC(2026, 8, 29, 12);
|
||||
const actualStarts = [];
|
||||
const renameSync = fs.renameSync;
|
||||
let stalled = false;
|
||||
const rename = t.mock.method(fs, 'renameSync', (...args) => {
|
||||
if (args[1] === stalledFile && !stalled) {
|
||||
stalled = true;
|
||||
clock += 1500;
|
||||
}
|
||||
return renameSync(...args);
|
||||
});
|
||||
const delayedGate = () =>
|
||||
createNominatimGate({
|
||||
settings: PUBLIC,
|
||||
usage: createGateStateStore({ file: stalledFile }),
|
||||
now: () => clock,
|
||||
sleep: async (ms) => {
|
||||
clock += ms;
|
||||
},
|
||||
minSpacingMs: 1100,
|
||||
fetchImpl: async () => {
|
||||
actualStarts.push(clock);
|
||||
return json([]);
|
||||
},
|
||||
});
|
||||
try {
|
||||
await Promise.all([
|
||||
delayedGate().requestJson((endpoint) => `${endpoint}?q=delayed-one`),
|
||||
delayedGate().requestJson((endpoint) => `${endpoint}?q=delayed-two`),
|
||||
]);
|
||||
} finally {
|
||||
rename.mock.restore();
|
||||
}
|
||||
assert.equal(actualStarts.length, 2);
|
||||
assert.ok(actualStarts[1] - actualStarts[0] >= 1100);
|
||||
assert.equal(
|
||||
createGateStateStore({ file: stalledFile }).count('2026-09-29'),
|
||||
2,
|
||||
);
|
||||
|
||||
const capFile = path.join(dir, 'cap.json');
|
||||
let sent = 0;
|
||||
const capGate = () =>
|
||||
createNominatimGate({
|
||||
settings: { ...PUBLIC, dailyCap: 1 },
|
||||
fetchImpl: async () => {
|
||||
sent += 1;
|
||||
return json([]);
|
||||
},
|
||||
usage: createGateStateStore({ file: capFile }),
|
||||
minSpacingMs: 1,
|
||||
});
|
||||
const results = await Promise.allSettled([
|
||||
createNominatimSearchProvider({ gate: capGate() })('three', null),
|
||||
createNominatimSearchProvider({ gate: capGate() })('four', null),
|
||||
]);
|
||||
assert.equal(
|
||||
results.filter((result) => result.status === 'fulfilled').length,
|
||||
1,
|
||||
);
|
||||
assert.equal(
|
||||
results.find((result) => result.status === 'rejected').reason.code,
|
||||
'NOMINATIM_DAILY_CAP',
|
||||
);
|
||||
assert.equal(sent, 1);
|
||||
});
|
||||
|
||||
test('a persistence failure is reported and never lowers the count', (t) => {
|
||||
const dir = tempDir(t);
|
||||
const blocker = path.join(dir, 'not-a-dir');
|
||||
fs.writeFileSync(blocker, 'x');
|
||||
const errors = [];
|
||||
const store = createGateStateStore({
|
||||
file: path.join(blocker, 'state.json'),
|
||||
onError: (error) => errors.push(error),
|
||||
});
|
||||
store.increment('2026-09-29');
|
||||
store.increment('2026-09-29');
|
||||
assert.equal(store.count('2026-09-29'), 2);
|
||||
assert.equal(store.status().persisted, false);
|
||||
assert.ok(store.status().error);
|
||||
assert.equal(errors.length, 1, 'reported once, not per request');
|
||||
});
|
||||
|
||||
test('a persistence failure after fetch invocation aborts it and blocks restarts', async (t) => {
|
||||
const file = path.join(tempDir(t), 'state.json');
|
||||
let clock = Date.UTC(2026, 8, 29, 12);
|
||||
const markers = () =>
|
||||
fs
|
||||
.readdirSync(path.dirname(file))
|
||||
.filter((name) => name.startsWith('state.json.dispatch.'));
|
||||
const calls = [];
|
||||
const renameSync = fs.renameSync;
|
||||
let failStateWrite = false;
|
||||
const rename = t.mock.method(fs, 'renameSync', (...args) => {
|
||||
if (failStateWrite && args[1] === file) {
|
||||
failStateWrite = false;
|
||||
throw Object.assign(new Error('EIO'), { code: 'EIO' });
|
||||
}
|
||||
return renameSync(...args);
|
||||
});
|
||||
const make = (fetchImpl) =>
|
||||
createNominatimGate({
|
||||
settings: PUBLIC,
|
||||
usage: createGateStateStore({ file, onError: () => {} }),
|
||||
now: () => clock,
|
||||
sleep: async (ms) => {
|
||||
clock += ms;
|
||||
},
|
||||
minSpacingMs: 1100,
|
||||
fetchImpl,
|
||||
});
|
||||
let durableAtInvocation;
|
||||
let requestSignal;
|
||||
const gate = make((url, { signal }) => {
|
||||
calls.push(url);
|
||||
durableAtInvocation = JSON.parse(fs.readFileSync(file, 'utf8')).count;
|
||||
requestSignal = signal;
|
||||
failStateWrite = true;
|
||||
return new Promise((_, reject) =>
|
||||
signal.addEventListener('abort', () => reject(signal.reason), {
|
||||
once: true,
|
||||
}),
|
||||
);
|
||||
});
|
||||
try {
|
||||
await assert.rejects(
|
||||
gate.requestJson((endpoint) => `${endpoint}?q=x`),
|
||||
{
|
||||
code: 'NOMINATIM_STATE_UNAVAILABLE',
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
rename.mock.restore();
|
||||
}
|
||||
assert.equal(durableAtInvocation, 1, 'the count persisted before invocation');
|
||||
assert.ok(requestSignal.aborted);
|
||||
const orphan = markers();
|
||||
assert.equal(orphan.length, 1);
|
||||
assert.equal(gate.stats().persistence.dispatchPending, true);
|
||||
clock += 60_000;
|
||||
const restarted = make(async (url) => {
|
||||
calls.push(url);
|
||||
return json([]);
|
||||
});
|
||||
await assert.rejects(
|
||||
restarted.requestJson((endpoint) => `${endpoint}?q=y`),
|
||||
{
|
||||
code: 'NOMINATIM_STATE_UNAVAILABLE',
|
||||
},
|
||||
);
|
||||
assert.equal(calls.length, 1);
|
||||
assert.deepEqual(markers(), orphan);
|
||||
assert.equal(createGateStateStore({ file }).count('2026-09-29'), 1);
|
||||
// Long past any request's timeout, the next lock owner reclaims it.
|
||||
clock += 10 * 60_000;
|
||||
const resumed = make(async (url) => {
|
||||
calls.push(url);
|
||||
return json([]);
|
||||
});
|
||||
assert.deepEqual(
|
||||
await resumed.requestJson((endpoint) => `${endpoint}?q=z`),
|
||||
[],
|
||||
);
|
||||
assert.equal(calls.length, 2);
|
||||
assert.deepEqual(markers(), []);
|
||||
assert.equal(createGateStateStore({ file }).count('2026-09-29'), 2);
|
||||
});
|
||||
|
||||
test('a dispatch marker that names no start keeps refusing', async (t) => {
|
||||
const file = path.join(tempDir(t), 'state.json');
|
||||
fs.writeFileSync(`${file}.dispatch.123.456`, '');
|
||||
const gate = createNominatimGate({
|
||||
settings: PUBLIC,
|
||||
usage: createGateStateStore({ file, onError: () => {} }),
|
||||
now: () => Date.UTC(2026, 8, 29, 12),
|
||||
sleep: async () => {},
|
||||
fetchImpl: async () =>
|
||||
assert.fail('no request while a dispatch is unfinished'),
|
||||
});
|
||||
await assert.rejects(
|
||||
gate.requestJson((endpoint) => `${endpoint}?q=x`),
|
||||
{
|
||||
code: 'NOMINATIM_STATE_UNAVAILABLE',
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('parseable but invalid public usage state fails closed', async (t) => {
|
||||
const dir = tempDir(t);
|
||||
const file = path.join(dir, 'state.json');
|
||||
fs.writeFileSync(
|
||||
file,
|
||||
JSON.stringify({
|
||||
day: '2026-09-30',
|
||||
count: '50',
|
||||
pausedUntil: '9999999999999',
|
||||
}),
|
||||
);
|
||||
let sent = 0;
|
||||
const gate = createNominatimGate({
|
||||
settings: PUBLIC,
|
||||
fetchImpl: async () => {
|
||||
sent += 1;
|
||||
return json([]);
|
||||
},
|
||||
usage: createGateStateStore({ file, onError: () => {} }),
|
||||
...quick,
|
||||
});
|
||||
await assert.rejects(
|
||||
gate.requestJson((endpoint) => `${endpoint}?q=x`),
|
||||
{ code: 'NOMINATIM_STATE_UNAVAILABLE' },
|
||||
);
|
||||
assert.equal(sent, 0);
|
||||
|
||||
fs.writeFileSync(
|
||||
file,
|
||||
JSON.stringify({ day: '2026-09-30', count: 1, pausedUntil: 0 }),
|
||||
);
|
||||
const migrated = createGateStateStore({ file });
|
||||
assert.equal(
|
||||
migrated.dispatch(
|
||||
'2026-09-30',
|
||||
{ clock: () => 1000, dailyCap: 50, minSpacingMs: 1100 },
|
||||
() => Promise.resolve(),
|
||||
).status,
|
||||
'started',
|
||||
'the original state format without nextStartAt remains supported',
|
||||
);
|
||||
});
|
||||
|
||||
test('a long Retry-After is honoured in full and survives a restart', async (t) => {
|
||||
const file = path.join(tempDir(t), 'state.json');
|
||||
let clock = Date.UTC(2026, 8, 29, 12);
|
||||
const now = () => clock;
|
||||
const refused = createNominatimGate({
|
||||
settings: PUBLIC,
|
||||
fetchImpl: async () =>
|
||||
new Response('', { status: 429, headers: { 'Retry-After': '86400' } }),
|
||||
usage: createGateStateStore({ file }),
|
||||
now,
|
||||
...quick,
|
||||
});
|
||||
await assert.rejects(
|
||||
createNominatimSearchProvider({ gate: refused })('x', null),
|
||||
{
|
||||
code: 'NOMINATIM_REFUSED',
|
||||
},
|
||||
);
|
||||
const calls = [];
|
||||
const restarted = createNominatimGate({
|
||||
settings: PUBLIC,
|
||||
fetchImpl: async (url) => {
|
||||
calls.push(url);
|
||||
return json([]);
|
||||
},
|
||||
usage: createGateStateStore({ file }),
|
||||
now,
|
||||
...quick,
|
||||
});
|
||||
const search = createNominatimSearchProvider({ gate: restarted });
|
||||
clock += 7 * 3_600_000; // past the old six-hour ceiling
|
||||
await assert.rejects(search('y', null), (error) => {
|
||||
assert.equal(error.code, 'NOMINATIM_PAUSED');
|
||||
assert.equal(error.retryAfterMs, 17 * 3_600_000);
|
||||
return true;
|
||||
});
|
||||
assert.equal(calls.length, 0);
|
||||
clock += 17 * 3_600_000 + 1000;
|
||||
await search('y', null);
|
||||
assert.equal(calls.length, 1);
|
||||
});
|
||||
|
||||
test('an operator refusal pauses only the endpoint that refused', async () => {
|
||||
let active = {
|
||||
endpoint: 'https://a.example.test/search',
|
||||
isPublic: false,
|
||||
dailyCap: 50,
|
||||
};
|
||||
const calls = [];
|
||||
const gate = createNominatimGate({
|
||||
settings: () => active,
|
||||
fetchImpl: async (url) => {
|
||||
calls.push(new URL(url).host);
|
||||
return calls.length === 1
|
||||
? new Response('', {
|
||||
status: 429,
|
||||
headers: { 'Retry-After': '60' },
|
||||
})
|
||||
: json([]);
|
||||
},
|
||||
...quick,
|
||||
});
|
||||
const request = () => gate.requestJson((endpoint) => `${endpoint}?q=x`);
|
||||
await assert.rejects(request(), { code: 'NOMINATIM_REFUSED' });
|
||||
active = { ...active, endpoint: 'https://b.example.test/search' };
|
||||
assert.deepEqual(await request(), []);
|
||||
active = { ...active, endpoint: 'https://a.example.test/search' };
|
||||
await assert.rejects(request(), { code: 'NOMINATIM_PAUSED' });
|
||||
assert.deepEqual(calls, ['a.example.test', 'b.example.test']);
|
||||
});
|
||||
|
||||
test('a stalled body times out instead of holding the shared queue', async () => {
|
||||
let cancelled = 0;
|
||||
const stalled = (signal) => {
|
||||
const body = new ReadableStream({
|
||||
pull: () => new Promise(() => {}),
|
||||
cancel: () => {
|
||||
cancelled += 1;
|
||||
},
|
||||
});
|
||||
signal.addEventListener('abort', () => body.cancel().catch(() => {}));
|
||||
return new Response(body, { status: 200 });
|
||||
};
|
||||
let n = 0;
|
||||
const gate = createNominatimGate({
|
||||
settings: { ...PUBLIC, isPublic: false },
|
||||
fetchImpl: async (_url, { signal }) =>
|
||||
++n <= 2 ? stalled(signal) : json([]),
|
||||
timeoutMs: 150,
|
||||
});
|
||||
const search = createNominatimSearchProvider({ gate });
|
||||
const started = Date.now();
|
||||
await assert.rejects(search('stall', null), { code: 'NOMINATIM_TRANSPORT' });
|
||||
assert.ok(Date.now() - started < 3000);
|
||||
assert.ok(cancelled >= 1, 'the body read was cancelled');
|
||||
});
|
||||
|
||||
test('cache keys are request-equivalent: a miss for "Paris???" never answers "Paris"', async () => {
|
||||
const calls = [];
|
||||
const gate = createNominatimGate({
|
||||
settings: LOCAL,
|
||||
fetchImpl: async (url) => {
|
||||
calls.push(new URL(url).searchParams.get('q'));
|
||||
return json(
|
||||
calls.length === 1
|
||||
? []
|
||||
: [
|
||||
{
|
||||
lat: '48.85',
|
||||
lon: '2.35',
|
||||
display_name: 'Paris, France',
|
||||
name: 'Paris',
|
||||
addresstype: 'city',
|
||||
},
|
||||
],
|
||||
);
|
||||
},
|
||||
...quick,
|
||||
});
|
||||
const search = createNominatimSearchProvider({ gate });
|
||||
assert.equal((await search('Paris???', null)).status, 'ZERO_RESULTS');
|
||||
assert.equal((await search('Paris', null)).status, 'OK');
|
||||
assert.deepEqual(calls, ['Paris???', 'Paris']);
|
||||
// Whitespace-only differences are the same upstream request.
|
||||
assert.equal((await search(' Paris ', null)).cached, true);
|
||||
});
|
||||
|
||||
test('a malformed answer is a failure, not a cached "no result"', async () => {
|
||||
let shape = { error: 'Unexpected' };
|
||||
let calls = 0;
|
||||
const gate = createNominatimGate({
|
||||
settings: LOCAL,
|
||||
fetchImpl: async () => {
|
||||
calls += 1;
|
||||
return json(shape);
|
||||
},
|
||||
...quick,
|
||||
});
|
||||
const outline = createNominatimOutlineProvider({ gate });
|
||||
const search = createNominatimSearchProvider({ gate });
|
||||
await assert.rejects(outline({ query: 'Zilker Park', kind: 'landmark' }), {
|
||||
code: 'NOMINATIM_MALFORMED',
|
||||
});
|
||||
await assert.rejects(search('Zilker Park', null), {
|
||||
code: 'NOMINATIM_MALFORMED',
|
||||
});
|
||||
shape = [];
|
||||
assert.equal(
|
||||
(await outline({ query: 'Zilker Park', kind: 'landmark' })).status,
|
||||
'ZERO_RESULTS',
|
||||
);
|
||||
assert.equal(calls, 3, 'the malformed answers were not cached');
|
||||
});
|
||||
|
||||
test('cache entries are namespaced by endpoint', async () => {
|
||||
const cache = createNominatimCache();
|
||||
const hits = [];
|
||||
const gateFor = (endpoint) =>
|
||||
createNominatimGate({
|
||||
settings: { endpoint, isPublic: false, dailyCap: 50 },
|
||||
fetchImpl: async (url) => {
|
||||
hits.push(new URL(url).host);
|
||||
return json([]);
|
||||
},
|
||||
});
|
||||
await createNominatimSearchProvider({
|
||||
gate: gateFor('https://a.example.test/search'),
|
||||
cache,
|
||||
})('x', null);
|
||||
await createNominatimSearchProvider({
|
||||
gate: gateFor('https://b.example.test/search'),
|
||||
cache,
|
||||
})('x', null);
|
||||
assert.deepEqual(hits, ['a.example.test', 'b.example.test']);
|
||||
});
|
||||
|
||||
test('public-host detection ignores a trailing DNS dot', () => {
|
||||
assert.equal(
|
||||
resolveNominatimSettings({
|
||||
NOMINATIM_URL: 'https://nominatim.openstreetmap.org./search',
|
||||
}).isPublic,
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test('missing coordinates are no bias, never (0, 0)', () => {
|
||||
for (const [lat, lon] of [
|
||||
[null, null],
|
||||
['', ''],
|
||||
[undefined, 2],
|
||||
[48.8, null],
|
||||
])
|
||||
assert.equal(outlineBias('city', lat, lon), null, `${lat},${lon}`);
|
||||
assert.ok(outlineBias('city', 0, 0), 'a real (0, 0) is still a position');
|
||||
});
|
||||
|
||||
const boundary = (extra) => ({
|
||||
category: 'boundary',
|
||||
type: 'administrative',
|
||||
lat: '1',
|
||||
lon: '1',
|
||||
geojson: { type: 'Polygon', coordinates: square(1, 1, 0.1) },
|
||||
...extra,
|
||||
});
|
||||
|
||||
test('administrative boundaries must be at the asked level', () => {
|
||||
const county = boundary({
|
||||
name: 'Travis County',
|
||||
addresstype: 'county',
|
||||
place_rank: 12,
|
||||
});
|
||||
const city = boundary({
|
||||
name: 'Austin',
|
||||
addresstype: 'city',
|
||||
place_rank: 16,
|
||||
});
|
||||
const state = boundary({
|
||||
name: 'Texas',
|
||||
addresstype: 'state',
|
||||
place_rank: 8,
|
||||
});
|
||||
assert.equal(
|
||||
selectOutlineResult([state], { kind: 'city', query: 'Texas' }).outline,
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
selectOutlineResult([city], { kind: 'admin', query: 'Austin' }).outline,
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
selectOutlineResult(
|
||||
[boundary({ name: 'Travis', addresstype: 'county', place_rank: 12 })],
|
||||
{ kind: 'city', query: 'Travis' },
|
||||
).outline,
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
selectOutlineResult(
|
||||
[boundary({ name: 'Austin', addresstype: 'city', place_rank: 12 })],
|
||||
{ kind: 'admin', query: 'Austin' },
|
||||
).outline,
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
selectOutlineResult([state], { kind: 'neighborhood', query: 'Texas' })
|
||||
.outline,
|
||||
null,
|
||||
);
|
||||
assert.ok(
|
||||
selectOutlineResult([county], { kind: 'admin', query: 'Travis County' })
|
||||
.outline,
|
||||
);
|
||||
assert.equal(
|
||||
selectOutlineResult([boundary({ name: 'Somewhere', place_rank: 16 })], {
|
||||
kind: 'city',
|
||||
query: 'Somewhere',
|
||||
}).outline,
|
||||
null,
|
||||
'rank alone is not administrative identity',
|
||||
);
|
||||
assert.equal(
|
||||
selectOutlineResult(
|
||||
[
|
||||
boundary({
|
||||
name: 'Somewhere',
|
||||
addresstype: 'locality',
|
||||
place_rank: 16,
|
||||
}),
|
||||
],
|
||||
{ kind: 'city', query: 'Somewhere' },
|
||||
).outline,
|
||||
null,
|
||||
'rank cannot replace an unrecognized address type',
|
||||
);
|
||||
assert.equal(
|
||||
selectOutlineResult([boundary({ name: 'Somewhere' })], {
|
||||
kind: 'city',
|
||||
query: 'Somewhere',
|
||||
}).outline,
|
||||
null,
|
||||
'no level evidence at all',
|
||||
);
|
||||
// An unsuitable early boundary does not block a suitable later one.
|
||||
const picked = selectOutlineResult(
|
||||
[
|
||||
state,
|
||||
boundary({
|
||||
name: 'Texas',
|
||||
addresstype: 'city',
|
||||
osm_type: 'relation',
|
||||
osm_id: 9,
|
||||
}),
|
||||
],
|
||||
{
|
||||
kind: 'city',
|
||||
query: 'Texas',
|
||||
},
|
||||
);
|
||||
assert.equal(picked.outline.osm, 'relation/9');
|
||||
});
|
||||
|
||||
test('outline selection does not publish child-owned admin geometry hooks', () => {
|
||||
for (const [name, addresstype] of [
|
||||
['Nepal', 'country', 'country'],
|
||||
['Bagmati Province', 'province', 'admin1'],
|
||||
['Province of Bagmati', 'province', 'admin1'],
|
||||
['Texas', 'state', 'admin1'],
|
||||
['Travis County', 'county', 'admin2'],
|
||||
['County of Travis', 'county', 'admin2'],
|
||||
['Kathmandu District', 'state_district', 'admin2'],
|
||||
]) {
|
||||
const { outline } = selectOutlineResult([boundary({ name, addresstype })], {
|
||||
kind: 'admin',
|
||||
query: name,
|
||||
});
|
||||
assert.equal('adminArea' in outline, false);
|
||||
assert.equal('adminLevel' in outline, false);
|
||||
}
|
||||
|
||||
for (const row of [
|
||||
boundary({ name: 'Northern Region', addresstype: 'region', place_rank: 8 }),
|
||||
boundary({
|
||||
name: 'Central District',
|
||||
addresstype: 'district',
|
||||
place_rank: 10,
|
||||
}),
|
||||
boundary({ name: 'Example State', addresstype: 'county' }),
|
||||
boundary({ name: 'County of Example', addresstype: 'state' }),
|
||||
boundary({ name: 'Province of Example', addresstype: 'county' }),
|
||||
boundary({ name: 'Example County', addresstype: 'state' }),
|
||||
boundary({ name: 'Example Province', addresstype: 'county' }),
|
||||
boundary({ name: 'County of Example Province', addresstype: 'county' }),
|
||||
]) {
|
||||
const { outline } = selectOutlineResult([row], {
|
||||
kind: 'admin',
|
||||
query: row.name,
|
||||
});
|
||||
assert.ok(outline, row.name);
|
||||
assert.equal(outline.adminArea, undefined, row.name);
|
||||
assert.equal(outline.adminLevel, undefined, row.name);
|
||||
}
|
||||
|
||||
assert.equal(
|
||||
selectOutlineResult(
|
||||
[boundary({ name: 'Mystery Province', place_rank: 8 })],
|
||||
{
|
||||
kind: 'admin',
|
||||
query: 'Mystery Province',
|
||||
},
|
||||
).outline,
|
||||
null,
|
||||
'rank-only metadata never creates an exact administrative outline',
|
||||
);
|
||||
|
||||
const retired = selectOutlineResult(
|
||||
[boundary({ name: 'Bagmati Zone', addresstype: 'state' })],
|
||||
{ kind: 'admin', query: 'Bagmati Zone' },
|
||||
).outline;
|
||||
assert.equal('adminArea' in retired, false);
|
||||
assert.equal('adminLevel' in retired, false);
|
||||
|
||||
const retiredPrefix = selectOutlineResult(
|
||||
[boundary({ name: 'Zone of Bagmati', addresstype: 'state' })],
|
||||
{ kind: 'admin', query: 'Zone of Bagmati' },
|
||||
).outline;
|
||||
assert.equal('adminArea' in retiredPrefix, false);
|
||||
assert.equal('adminLevel' in retiredPrefix, false);
|
||||
});
|
||||
|
||||
test('long admin names do not publish child-owned geometry hooks', () => {
|
||||
const longValidName = `Province of ${'Example '.repeat(24)}`.trim();
|
||||
const valid = selectOutlineResult(
|
||||
[boundary({ name: longValidName, addresstype: 'province' })],
|
||||
{ kind: 'admin', query: longValidName },
|
||||
).outline;
|
||||
assert.equal('adminArea' in valid, false);
|
||||
assert.equal('adminLevel' in valid, false);
|
||||
|
||||
const longContradictoryName = `${'Very Long Administrative Name '.repeat(7)}County`;
|
||||
assert.ok(longContradictoryName.length > 160);
|
||||
const contradictory = selectOutlineResult(
|
||||
[boundary({ name: longContradictoryName, addresstype: 'state' })],
|
||||
{ kind: 'admin', query: longContradictoryName },
|
||||
).outline;
|
||||
assert.ok(contradictory);
|
||||
assert.equal(contradictory.adminArea, undefined);
|
||||
assert.equal(contradictory.adminLevel, undefined);
|
||||
});
|
||||
|
||||
test('non-administrative outline kinds do not publish admin geometry hooks', () => {
|
||||
const city = selectOutlineResult(
|
||||
[
|
||||
boundary({
|
||||
name: 'County of Austin',
|
||||
addresstype: 'city',
|
||||
place_rank: 16,
|
||||
}),
|
||||
],
|
||||
{ kind: 'city', query: 'County of Austin' },
|
||||
).outline;
|
||||
const park = selectOutlineResult(
|
||||
[
|
||||
{
|
||||
category: 'leisure',
|
||||
type: 'park',
|
||||
name: 'Zilker Park',
|
||||
lat: '1',
|
||||
lon: '1',
|
||||
geojson: { type: 'Polygon', coordinates: square(1, 1, 0.01) },
|
||||
},
|
||||
],
|
||||
{ kind: 'landmark', query: 'Zilker Park' },
|
||||
).outline;
|
||||
for (const outline of [city, park]) {
|
||||
assert.equal(outline.adminArea, undefined);
|
||||
assert.equal(outline.adminLevel, undefined);
|
||||
}
|
||||
});
|
||||
|
||||
test('building asks need building evidence', () => {
|
||||
const park = {
|
||||
category: 'tourism',
|
||||
type: 'theme_park',
|
||||
name: 'Big Top',
|
||||
lat: '1',
|
||||
lon: '1',
|
||||
geojson: { type: 'Polygon', coordinates: square(1, 1, 0.0001) },
|
||||
};
|
||||
const camp = { ...park, type: 'camp_site' };
|
||||
const campus = { ...park, category: 'amenity', type: 'university' };
|
||||
for (const row of [park, camp, campus])
|
||||
assert.equal(
|
||||
selectOutlineResult([row], { kind: 'building', query: 'Big Top' })
|
||||
.outline,
|
||||
null,
|
||||
row.type,
|
||||
);
|
||||
const hugeOffice = {
|
||||
...park,
|
||||
category: 'office',
|
||||
type: 'government',
|
||||
geojson: { type: 'Polygon', coordinates: square(1, 1, 0.01) },
|
||||
};
|
||||
assert.equal(
|
||||
selectOutlineResult([hugeOffice], { kind: 'building', query: 'Big Top' })
|
||||
.outline,
|
||||
null,
|
||||
);
|
||||
const office = {
|
||||
...hugeOffice,
|
||||
geojson: { type: 'Polygon', coordinates: square(1, 1, 0.001) },
|
||||
};
|
||||
assert.ok(
|
||||
selectOutlineResult([office], { kind: 'building', query: 'Big Top' })
|
||||
.outline,
|
||||
);
|
||||
const hall = { ...hugeOffice, category: 'building', type: 'yes' };
|
||||
assert.ok(
|
||||
selectOutlineResult([hall], { kind: 'building', query: 'Big Top' }).outline,
|
||||
);
|
||||
});
|
||||
|
||||
test('names match on whole words', () => {
|
||||
const row = (name) => ({
|
||||
category: 'leisure',
|
||||
type: 'park',
|
||||
name,
|
||||
lat: '1',
|
||||
lon: '1',
|
||||
geojson: { type: 'Polygon', coordinates: square(1, 1, 0.01) },
|
||||
});
|
||||
assert.equal(
|
||||
selectOutlineResult([row('Yorkshire Park')], {
|
||||
kind: 'landmark',
|
||||
query: 'York',
|
||||
}).outline,
|
||||
null,
|
||||
);
|
||||
assert.ok(
|
||||
selectOutlineResult([row('York Park')], { kind: 'landmark', query: 'York' })
|
||||
.outline,
|
||||
);
|
||||
assert.ok(
|
||||
selectOutlineResult([row('Zilker Metropolitan Park')], {
|
||||
kind: 'landmark',
|
||||
query: 'Zilker Park',
|
||||
}).outline,
|
||||
);
|
||||
assert.ok(
|
||||
selectOutlineResult(
|
||||
[{ ...row('新宿御苑'), namedetails: { name: '新宿御苑' } }],
|
||||
{ kind: 'landmark', query: '新宿御苑' },
|
||||
).outline,
|
||||
);
|
||||
assert.equal(
|
||||
selectOutlineResult([row('कुल मंदिर')], {
|
||||
kind: 'landmark',
|
||||
query: 'काली मंदिर',
|
||||
}).outline,
|
||||
null,
|
||||
);
|
||||
assert.ok(
|
||||
selectOutlineResult([row('काली मंदिर')], {
|
||||
kind: 'landmark',
|
||||
query: 'काली मंदिर',
|
||||
}).outline,
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,241 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { createGateStateStore } from '../../server/providers/regional/nominatimGate.js';
|
||||
|
||||
const moduleUrl = new URL(
|
||||
'../../server/providers/regional/nominatimGate.js',
|
||||
import.meta.url,
|
||||
).href;
|
||||
|
||||
function startModule(script) {
|
||||
const child = spawn(process.execPath, ['--input-type=module', '-e', script], {
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
});
|
||||
const lines = [];
|
||||
const waiters = [];
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
|
||||
const publish = (line) => {
|
||||
lines.push(line);
|
||||
for (const waiter of [...waiters]) {
|
||||
if (!line.startsWith(waiter.prefix)) continue;
|
||||
waiters.splice(waiters.indexOf(waiter), 1);
|
||||
clearTimeout(waiter.timer);
|
||||
waiter.resolve(line);
|
||||
}
|
||||
};
|
||||
|
||||
child.stdout.on('data', (chunk) => {
|
||||
stdout += chunk;
|
||||
const parts = stdout.split('\n');
|
||||
stdout = parts.pop();
|
||||
for (const line of parts) publish(line);
|
||||
});
|
||||
child.stderr.on('data', (chunk) => {
|
||||
stderr += chunk;
|
||||
});
|
||||
|
||||
return {
|
||||
child,
|
||||
lines,
|
||||
waitFor(prefix, timeoutMs = 3000) {
|
||||
const existing = lines.find((line) => line.startsWith(prefix));
|
||||
if (existing) return Promise.resolve(existing);
|
||||
return new Promise((resolve, reject) => {
|
||||
const waiter = { prefix, resolve, reject, timer: null };
|
||||
waiter.timer = setTimeout(() => {
|
||||
const index = waiters.indexOf(waiter);
|
||||
if (index >= 0) waiters.splice(index, 1);
|
||||
reject(
|
||||
new Error(
|
||||
`timed out waiting for ${prefix}; stderr: ${stderr.trim()}`,
|
||||
),
|
||||
);
|
||||
}, timeoutMs);
|
||||
waiters.push(waiter);
|
||||
});
|
||||
},
|
||||
exited: new Promise((resolve, reject) => {
|
||||
child.once('error', reject);
|
||||
child.once('exit', (code, signal) => resolve({ code, signal }));
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
function holderScript(lockFile) {
|
||||
return `
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
const database = new DatabaseSync(${JSON.stringify(lockFile)});
|
||||
database.exec('BEGIN IMMEDIATE');
|
||||
process.stdout.write('LOCKED\\n');
|
||||
process.stdin.once('data', () => {
|
||||
database.exec('ROLLBACK');
|
||||
database.close();
|
||||
process.stdout.write('RELEASED\\n');
|
||||
});
|
||||
process.stdin.resume();
|
||||
`;
|
||||
}
|
||||
|
||||
function contenderScript(stateFile, lockWaitMs) {
|
||||
return `
|
||||
const { createGateStateStore } = await import(${JSON.stringify(moduleUrl)});
|
||||
const store = createGateStateStore({
|
||||
file: ${JSON.stringify(stateFile)},
|
||||
lockWaitMs: ${lockWaitMs},
|
||||
onError: () => {},
|
||||
});
|
||||
process.stdout.write('ATTEMPTING\\n', () => {
|
||||
const result = store.dispatch(
|
||||
'2026-10-01',
|
||||
{ clock: () => 1000, dailyCap: 50, minSpacingMs: 1100 },
|
||||
() => Promise.resolve(),
|
||||
);
|
||||
delete result.value;
|
||||
process.stdout.write('RESULT:' + JSON.stringify(result) + '\\n');
|
||||
});
|
||||
`;
|
||||
}
|
||||
|
||||
test('a crashed lock owner cannot delete or bypass a successor lock', async (t) => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gev-nominatim-lock-'));
|
||||
const stateFile = path.join(dir, 'state.json');
|
||||
const lockFile = `${stateFile}.lock.sqlite`;
|
||||
const children = [];
|
||||
t.after(() => {
|
||||
for (const process of children) {
|
||||
if (process.exitCode == null && process.signalCode == null)
|
||||
process.kill();
|
||||
}
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const crashedOwner = startModule(holderScript(lockFile));
|
||||
children.push(crashedOwner.child);
|
||||
await crashedOwner.waitFor('LOCKED');
|
||||
const originalLock = fs.statSync(lockFile);
|
||||
|
||||
const blockedByCrashedOwner = startModule(contenderScript(stateFile, 0));
|
||||
children.push(blockedByCrashedOwner.child);
|
||||
const blockedResult = JSON.parse(
|
||||
(await blockedByCrashedOwner.waitFor('RESULT:')).slice('RESULT:'.length),
|
||||
);
|
||||
assert.equal(blockedResult.status, 'unavailable');
|
||||
assert.equal((await blockedByCrashedOwner.exited).code, 0);
|
||||
assert.equal(fs.existsSync(stateFile), false);
|
||||
|
||||
crashedOwner.child.kill();
|
||||
await crashedOwner.exited;
|
||||
|
||||
const successorOwner = startModule(holderScript(lockFile));
|
||||
children.push(successorOwner.child);
|
||||
await successorOwner.waitFor('LOCKED');
|
||||
assert.equal(fs.statSync(lockFile).ino, originalLock.ino);
|
||||
|
||||
const refused = startModule(contenderScript(stateFile, 0));
|
||||
children.push(refused.child);
|
||||
const refusedResult = JSON.parse(
|
||||
(await refused.waitFor('RESULT:')).slice('RESULT:'.length),
|
||||
);
|
||||
assert.equal(refusedResult.status, 'unavailable');
|
||||
assert.equal((await refused.exited).code, 0);
|
||||
assert.equal(
|
||||
createGateStateStore({ file: stateFile }).count('2026-10-01'),
|
||||
0,
|
||||
'a timed-out contender fails closed without spending a public slot',
|
||||
);
|
||||
assert.equal(fs.statSync(lockFile).ino, originalLock.ino);
|
||||
|
||||
successorOwner.child.stdin.end('release\n');
|
||||
await successorOwner.waitFor('RELEASED');
|
||||
assert.equal((await successorOwner.exited).code, 0);
|
||||
const released = createGateStateStore({ file: stateFile });
|
||||
const afterRelease = released.dispatch(
|
||||
'2026-10-01',
|
||||
{ clock: () => 2200, dailyCap: 50, minSpacingMs: 1100 },
|
||||
() => Promise.resolve(),
|
||||
);
|
||||
assert.equal(afterRelease.status, 'started');
|
||||
assert.equal(released.count('2026-10-01'), 1);
|
||||
assert.equal(fs.statSync(lockFile).ino, originalLock.ino);
|
||||
});
|
||||
|
||||
test('a public pause stays authoritative while another process owns the lock', async (t) => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gev-nominatim-pause-'));
|
||||
const stateFile = path.join(dir, 'state.json');
|
||||
const lockFile = `${stateFile}.lock.sqlite`;
|
||||
const children = [];
|
||||
t.after(() => {
|
||||
for (const process of children) {
|
||||
if (process.exitCode == null && process.signalCode == null)
|
||||
process.kill();
|
||||
}
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
fs.writeFileSync(
|
||||
stateFile,
|
||||
JSON.stringify({
|
||||
day: '2026-10-01',
|
||||
count: 1,
|
||||
pausedUntil: 0,
|
||||
nextStartAt: 0,
|
||||
}),
|
||||
);
|
||||
const owner = startModule(holderScript(lockFile));
|
||||
children.push(owner.child);
|
||||
await owner.waitFor('LOCKED');
|
||||
|
||||
const errors = [];
|
||||
const blockedWriter = createGateStateStore({
|
||||
file: stateFile,
|
||||
lockWaitMs: 0,
|
||||
onError: (error) => errors.push(error),
|
||||
});
|
||||
blockedWriter.pauseUntil(9000);
|
||||
assert.equal(errors.length, 1, 'the blocked JSON consolidation is reported');
|
||||
assert.equal(
|
||||
JSON.parse(fs.readFileSync(stateFile, 'utf8')).pausedUntil,
|
||||
0,
|
||||
'the lock owner prevents an unsafe JSON overwrite',
|
||||
);
|
||||
|
||||
owner.child.stdin.end('release\n');
|
||||
await owner.waitFor('RELEASED');
|
||||
assert.equal((await owner.exited).code, 0);
|
||||
|
||||
const successor = createGateStateStore({ file: stateFile });
|
||||
let sent = 0;
|
||||
const send = () => {
|
||||
sent += 1;
|
||||
return Promise.resolve();
|
||||
};
|
||||
assert.deepEqual(
|
||||
successor.dispatch(
|
||||
'2026-10-01',
|
||||
{ clock: () => 1000, dailyCap: 50, minSpacingMs: 1100 },
|
||||
send,
|
||||
),
|
||||
{ status: 'paused', retryAfterMs: 8000 },
|
||||
'a fresh process observes the pending refusal pause',
|
||||
);
|
||||
assert.equal(sent, 0, 'a paused dispatch never starts the transport');
|
||||
const resumed = successor.dispatch(
|
||||
'2026-10-01',
|
||||
{ clock: () => 9001, dailyCap: 50, minSpacingMs: 1100 },
|
||||
send,
|
||||
);
|
||||
assert.equal(resumed.status, 'started');
|
||||
assert.equal(sent, 1);
|
||||
assert.equal(successor.count('2026-10-01'), 2);
|
||||
assert.equal(
|
||||
fs.readdirSync(dir).some((name) => name.startsWith('state.json.pause.')),
|
||||
false,
|
||||
'the next successful transaction consolidates and removes the marker',
|
||||
);
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
@@ -9,16 +9,50 @@
|
||||
// Run with: npm test
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
fetchNominatimSearch,
|
||||
geocodeProxy,
|
||||
NOMINATIM_MAX_PENDING,
|
||||
} from '../../server/providers/regional/place.js';
|
||||
import { fetchRegionalJson } from '../../server/providers/regional/http.js';
|
||||
import {
|
||||
createNominatimGate,
|
||||
resolveNominatimSettings,
|
||||
PUBLIC_NOMINATIM_SEARCH,
|
||||
setSharedNominatimGate,
|
||||
} from '../../server/providers/regional/nominatimGate.js';
|
||||
import { localProviderPlugins } from '../../server/providers/local.js';
|
||||
import { standaloneConfig } from '../../server/standalone/vite.config.js';
|
||||
import { defaultSourceRoot } from '../../server/providers/common/source-root.js';
|
||||
import { createPlaceSearch, createGoogleGeocoder } from '../search/index.js';
|
||||
import { createPhotonGeocoder } from '../keylessGeocoder.js';
|
||||
|
||||
// Each case starts with a fresh shared gate: a pause after one case's
|
||||
// simulated outage must not decide the next case.
|
||||
// Pin the public-policy fixture independently of operator endpoint settings.
|
||||
// Each case's transport fails closed outside its active withUpstream block.
|
||||
let activeUpstream;
|
||||
test.beforeEach(() => {
|
||||
const transport = { fetch: null };
|
||||
activeUpstream = transport;
|
||||
setSharedNominatimGate(
|
||||
createNominatimGate({
|
||||
settings: resolveNominatimSettings({
|
||||
NOMINATIM_URL: PUBLIC_NOMINATIM_SEARCH,
|
||||
}),
|
||||
fetchImpl: (...args) => {
|
||||
if (!transport.fetch) throw new Error('No active test upstream');
|
||||
return transport.fetch(...args);
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
const storageRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'gev-search-route-'));
|
||||
test.after(() => fs.rmSync(storageRoot, { recursive: true, force: true }));
|
||||
|
||||
const HIT = [
|
||||
{
|
||||
lat: '43.1731',
|
||||
@@ -33,13 +67,16 @@ const HIT = [
|
||||
/** Mount the geocode route the way the dev server does. */
|
||||
let clientCounter = 0;
|
||||
|
||||
function mountGeocode(
|
||||
install = (server) => geocodeProxy().configureServer(server),
|
||||
) {
|
||||
function mountGeocode(install = null) {
|
||||
// A fresh client address per mount: the per-client rate limiter is module
|
||||
// state shared by every case in this file, and one case's burst must not
|
||||
// decide another case's outcome.
|
||||
clientCounter += 1;
|
||||
if (!install)
|
||||
install = (server) =>
|
||||
geocodeProxy({
|
||||
storageDir: path.join(storageRoot, `case-${clientCounter}`),
|
||||
}).configureServer(server);
|
||||
const remoteAddress = `10.0.0.${clientCounter % 250}`;
|
||||
const routes = new Map();
|
||||
install({
|
||||
@@ -93,14 +130,17 @@ function mountGeocode(
|
||||
/** Answer the upstream from a stub, counting and optionally stalling calls. */
|
||||
async function withUpstream(respond, run) {
|
||||
const previous = globalThis.fetch;
|
||||
const transport = activeUpstream;
|
||||
const previousUpstream = transport.fetch;
|
||||
const calls = [];
|
||||
globalThis.fetch = async (url, options) => {
|
||||
globalThis.fetch = transport.fetch = async (url, options) => {
|
||||
calls.push({ url: String(url), options });
|
||||
return await respond(String(url), options, calls.length);
|
||||
};
|
||||
try {
|
||||
return await run(calls);
|
||||
} finally {
|
||||
transport.fetch = previousUpstream;
|
||||
globalThis.fetch = previous;
|
||||
}
|
||||
}
|
||||
@@ -111,13 +151,55 @@ const jsonResponse = (value) =>
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
|
||||
test('the route is part of the application on both dev and preview', () => {
|
||||
const plugin = localProviderPlugins().find(
|
||||
test('the real composition binds durable storage before dev or preview installation, independent of CWD', async () => {
|
||||
const cwd = process.cwd();
|
||||
const expected = path.join(defaultSourceRoot, '.gev-cache', 'nominatim');
|
||||
try {
|
||||
for (const suffix of ['one', 'two']) {
|
||||
const dir = path.join(storageRoot, `cwd-${suffix}`);
|
||||
fs.mkdirSync(dir);
|
||||
process.chdir(dir);
|
||||
const { NOMINATIM_CACHE_DIR } = await import(
|
||||
`../../server/providers/regional/place.js?cwd-${suffix}`
|
||||
);
|
||||
assert.equal(NOMINATIM_CACHE_DIR, expected);
|
||||
assert.equal(fs.existsSync(path.join(dir, '.gev-cache')), false);
|
||||
}
|
||||
} finally {
|
||||
process.chdir(cwd);
|
||||
}
|
||||
const bound = [];
|
||||
setSharedNominatimGate({
|
||||
persistUsage: (file) => bound.push(file),
|
||||
});
|
||||
const storage = path.join(storageRoot, 'composition');
|
||||
const plugin = localProviderPlugins({ nominatimStorageDir: storage }).find(
|
||||
(entry) => entry.name === 'geocode-proxy',
|
||||
);
|
||||
assert.ok(plugin, 'the application must compose the place-search route');
|
||||
assert.equal(typeof plugin.configureServer, 'function');
|
||||
assert.equal(typeof plugin.configurePreviewServer, 'function');
|
||||
assert.deepEqual(bound, [path.join(storage, 'state.json')]);
|
||||
const standaloneStorage = path.join(storageRoot, 'standalone');
|
||||
const config = standaloneConfig(
|
||||
{ command: 'serve', mode: 'test' },
|
||||
{ nominatimStorageDir: standaloneStorage },
|
||||
);
|
||||
assert.ok(config.plugins.some((entry) => entry.name === 'geocode-proxy'));
|
||||
assert.deepEqual(bound, [
|
||||
path.join(storage, 'state.json'),
|
||||
path.join(standaloneStorage, 'state.json'),
|
||||
]);
|
||||
for (const value of [null, '', 'relative'])
|
||||
assert.throws(
|
||||
() => localProviderPlugins({ nominatimStorageDir: value }),
|
||||
/absolute directory/,
|
||||
);
|
||||
assert.equal(
|
||||
bound.length,
|
||||
2,
|
||||
'invalid bindings never construct policy state',
|
||||
);
|
||||
});
|
||||
|
||||
test('a search reaches the upstream identified, and the answer is cached', async () => {
|
||||
@@ -126,7 +208,9 @@ test('a search reaches the upstream identified, and the answer is cached', async
|
||||
async () => jsonResponse(HIT),
|
||||
async (calls) => {
|
||||
const query = `niagara-${Date.now()}`;
|
||||
const first = await request(`/api/geocode?q=${query}`);
|
||||
const first = await request(
|
||||
`/api/geocode?q=${query}&bounds=${encodeURIComponent('-20,170|-10,-171')}`,
|
||||
);
|
||||
assert.equal(first.status, 200);
|
||||
assert.equal(first.body.status, 'OK');
|
||||
assert.deepEqual(first.body.results[0].geometry.location, {
|
||||
@@ -134,6 +218,11 @@ test('a search reaches the upstream identified, and the answer is cached', async
|
||||
lng: -79.0384,
|
||||
});
|
||||
assert.equal(calls.length, 1);
|
||||
assert.equal(
|
||||
new URL(calls[0].url).searchParams.has('viewbox'),
|
||||
false,
|
||||
'a wrapped hint must not be coarsened into a Greenwich viewbox',
|
||||
);
|
||||
const headers = calls[0].options.headers;
|
||||
assert.match(
|
||||
String(headers['User-Agent']),
|
||||
@@ -147,7 +236,7 @@ test('a search reaches the upstream identified, and the answer is cached', async
|
||||
'a fixed API endpoint may not be redirected elsewhere',
|
||||
);
|
||||
|
||||
// The policy asks that results be cached; a repeat costs no request.
|
||||
// Dropping the invalid hint also shares the unbiased cache entry.
|
||||
const repeat = await request(`/api/geocode?q=${query}`);
|
||||
assert.equal(repeat.status, 200);
|
||||
assert.equal(
|
||||
|
||||
@@ -7,8 +7,11 @@ import { localProviderPlugins } from '../../server/providers/local.js';
|
||||
import { apiNotFoundPlugin } from '../../server/standalone/api-not-found.js';
|
||||
import { makeFixtureRoot } from './fixtureRoot.mjs';
|
||||
|
||||
const nominatimStorageDir = await makeFixtureRoot('gev-preview-composition-');
|
||||
test.after(() => rm(nominatimStorageDir, { recursive: true, force: true }));
|
||||
|
||||
test('data providers have both hooks; credential editing stays development-only', () => {
|
||||
const plugins = localProviderPlugins();
|
||||
const plugins = localProviderPlugins({ nominatimStorageDir });
|
||||
assert.ok(plugins.some(({ name }) => name === 'fire-perimeters'));
|
||||
assert.ok(plugins.some(({ name }) => name === 'gnss-integrity'));
|
||||
assert.ok(plugins.some(({ name }) => name === 'ghostmaps'));
|
||||
@@ -89,7 +92,10 @@ test('real dev and built-preview servers serve provider JSON and terminate unkno
|
||||
for (const isPreview of [false, true]) {
|
||||
const config = {
|
||||
...base,
|
||||
plugins: [...localProviderPlugins(), apiNotFoundPlugin()],
|
||||
plugins: [
|
||||
...localProviderPlugins({ nominatimStorageDir }),
|
||||
apiNotFoundPlugin(),
|
||||
],
|
||||
server: { host: '127.0.0.1', port: 0, hmr: false },
|
||||
preview: { host: '127.0.0.1', port: 0 },
|
||||
};
|
||||
|
||||
@@ -16,6 +16,9 @@ import * as providers from '../../server/providers/local.js';
|
||||
import { apiNotFoundPlugin } from '../../server/standalone/api-not-found.js';
|
||||
import { makeFixtureRoot } from './fixtureRoot.mjs';
|
||||
|
||||
const nominatimStorageDir = await makeFixtureRoot('gev-build-composition-');
|
||||
test.after(() => rm(nominatimStorageDir, { recursive: true, force: true }));
|
||||
|
||||
test('explicit build inputs preserve browser-only defines, plugin order and loopback protections', () => {
|
||||
const plugin = { name: 'fixture-provider' };
|
||||
const config = createBrowserViteConfig({
|
||||
@@ -73,6 +76,26 @@ test('explicit build inputs preserve browser-only defines, plugin order and loop
|
||||
'import.meta.env.CESIUM_ION_TOKEN': '"ion-fixture"',
|
||||
'import.meta.env.MAPILLARY_CLIENT_TOKEN': '""',
|
||||
});
|
||||
assert.equal(
|
||||
createBrowserViteConfig({ photonUrl: '' }).define[
|
||||
'import.meta.env.PHOTON_URL'
|
||||
],
|
||||
'null',
|
||||
'an empty PHOTON_URL reaches the browser only as the disabled sentinel',
|
||||
);
|
||||
assert.equal(
|
||||
createBrowserViteConfig({ photonUrl: 'https://u:[email protected]/api/' })
|
||||
.define['import.meta.env.PHOTON_URL'],
|
||||
'null',
|
||||
'credentials are never serialized into browser assets',
|
||||
);
|
||||
assert.equal(
|
||||
createBrowserViteConfig({ photonUrl: 'http://localhost:2322/api/' }).define[
|
||||
'import.meta.env.PHOTON_URL'
|
||||
],
|
||||
'null',
|
||||
'plain http would be blocked by connect-src, so it is refused here',
|
||||
);
|
||||
assert.equal(
|
||||
createBrowserViteConfig({ mapillaryToken: 'MLY|1|abc' }).define[
|
||||
'import.meta.env.MAPILLARY_CLIENT_TOKEN'
|
||||
@@ -120,7 +143,7 @@ test('build helper does not discover environment values or construct local provi
|
||||
test('root config retains existing named exports and standalone provider order', () => {
|
||||
for (const [name, value] of Object.entries(providers))
|
||||
assert.equal(compatibility[name], value, name);
|
||||
const config = standaloneConfig({ mode: 'test' });
|
||||
const config = standaloneConfig({ mode: 'test' }, { nominatimStorageDir });
|
||||
const standaloneProviders = config.plugins
|
||||
.slice(4, -6)
|
||||
.map((plugin) => plugin.name);
|
||||
@@ -134,7 +157,9 @@ test('root config retains existing named exports and standalone provider order',
|
||||
);
|
||||
assert.deepEqual(
|
||||
standaloneProviders.filter((name) => name !== 'gemini-live-proxy'),
|
||||
providers.localProviderPlugins().map((plugin) => plugin.name),
|
||||
providers
|
||||
.localProviderPlugins({ nominatimStorageDir })
|
||||
.map((plugin) => plugin.name),
|
||||
);
|
||||
assert.equal(config.plugins.at(-7).name, 'gev-key-setup');
|
||||
// Token fallback follows real providers and local MCP before the API 404.
|
||||
@@ -147,10 +172,26 @@ test('root config retains existing named exports and standalone provider order',
|
||||
assert.equal(config.plugins.at(-1).name, 'panel-build');
|
||||
});
|
||||
|
||||
test('standalone config passes a validated Photon setting into the browser define', () => {
|
||||
const previous = process.env.PHOTON_URL;
|
||||
process.env.PHOTON_URL = 'https://photon.example.test/api/';
|
||||
try {
|
||||
assert.equal(
|
||||
standaloneConfig({ mode: 'test' }, { nominatimStorageDir }).define[
|
||||
'import.meta.env.PHOTON_URL'
|
||||
],
|
||||
'"https://photon.example.test/api/"',
|
||||
);
|
||||
} finally {
|
||||
if (previous === undefined) delete process.env.PHOTON_URL;
|
||||
else process.env.PHOTON_URL = previous;
|
||||
}
|
||||
});
|
||||
|
||||
test('directories the server writes while it runs are kept out of the dev watcher', () => {
|
||||
// Providers write cache files and logs here while the app runs; none of it is
|
||||
// source, so the dev watcher has no reason to stat it.
|
||||
const config = standaloneConfig({ mode: 'test' });
|
||||
const config = standaloneConfig({ mode: 'test' }, { nominatimStorageDir });
|
||||
const plugin = config.plugins.find(
|
||||
({ name }) => name === 'gev-unwatched-runtime-dirs',
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user