mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 18:22:25 +00:00
Merge main into denis/iss-live-telemetry
This commit is contained in:
commit
050e0a3a7a
38 files changed
+1431
-251
No files matched your search
+155
-54
@@ -12,6 +12,14 @@ import {
|
||||
parseTarkteeDatexLocations,
|
||||
} from '../../server/providers/cctv/sources.js';
|
||||
|
||||
/**
|
||||
* Disarm the 30 s deadline, so a loader that only stops at that deadline
|
||||
* fails at the test timeout instead of passing late.
|
||||
*/
|
||||
function withoutDeadline(t) {
|
||||
t.mock.method(AbortSignal, 'timeout', () => new AbortController().signal);
|
||||
}
|
||||
|
||||
/** Silence the loaders' progress and failure logging. */
|
||||
function quiet(t) {
|
||||
t.mock.method(console, 'log', () => {});
|
||||
@@ -97,6 +105,29 @@ test('Tarktee DATEX image parser rejects non-official hosts', () => {
|
||||
assert.equal(images.size, 0);
|
||||
});
|
||||
|
||||
test('Tarktee DATEX requests wait 30 s, past the slow answers measured live', async (t) => {
|
||||
const timeouts = [];
|
||||
const timeout = AbortSignal.timeout;
|
||||
t.mock.method(AbortSignal, 'timeout', (ms) => {
|
||||
timeouts.push(ms);
|
||||
return timeout.call(AbortSignal, ms);
|
||||
});
|
||||
t.mock.method(console, 'log', () => {});
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async (url) =>
|
||||
new Response(
|
||||
String(url).includes('roadCameraLocations')
|
||||
? SAMPLE_LOCATIONS_XML
|
||||
: SAMPLE_IMAGES_XML,
|
||||
),
|
||||
);
|
||||
const cameras = await loadTarkteeSourcesFromDatex();
|
||||
assert.deepEqual(timeouts, [30_000]);
|
||||
assert.equal(cameras.length, 1);
|
||||
});
|
||||
|
||||
test('Tarktee loader deduplicates cameras sharing an image number before the cap', async (t) => {
|
||||
quiet(t);
|
||||
const dupLocationsXml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
@@ -168,46 +199,6 @@ test('Tarktee loader refuses a redirect and cancels both response bodies', async
|
||||
assert.equal(cancelled.images, true, 'sibling 200 body is also cancelled');
|
||||
});
|
||||
|
||||
test('Tarktee loader cancels fulfilled bodies when either fetch rejects', async (t) => {
|
||||
for (const failedUrl of [TARKTEE_LOCATIONS_URL, TARKTEE_IMAGES_URL]) {
|
||||
for (const delayedSibling of [false, true]) {
|
||||
await t.test(
|
||||
`${failedUrl}, delayed sibling: ${delayedSibling}`,
|
||||
async (t) => {
|
||||
quiet(t);
|
||||
let cancelled = false;
|
||||
const response = new Response(
|
||||
new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new TextEncoder().encode('<xml/>'));
|
||||
},
|
||||
cancel() {
|
||||
cancelled = true;
|
||||
},
|
||||
}),
|
||||
);
|
||||
t.mock.method(globalThis, 'fetch', async (url) => {
|
||||
if (String(url) === failedUrl) throw new TypeError('fetch failed');
|
||||
if (delayedSibling) await new Promise(setImmediate);
|
||||
return response;
|
||||
});
|
||||
|
||||
try {
|
||||
assert.deepEqual(await loadTarkteeSourcesFromDatex(), []);
|
||||
assert.equal(
|
||||
cancelled,
|
||||
true,
|
||||
'fulfilled sibling body is cancelled',
|
||||
);
|
||||
} finally {
|
||||
await response.body.cancel();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('Tarktee loader cancels the sibling body when one feed fails or overflows the byte cap', async (t) => {
|
||||
quiet(t);
|
||||
let siblingCancelled = false;
|
||||
@@ -264,25 +255,135 @@ test('Tarktee loader cancels the sibling body when one feed fails or overflows t
|
||||
);
|
||||
});
|
||||
|
||||
test('Tarktee DATEX requests wait 30 s, past the slow answers measured live', async (t) => {
|
||||
const timeouts = [];
|
||||
const timeout = AbortSignal.timeout;
|
||||
t.mock.method(AbortSignal, 'timeout', (ms) => {
|
||||
timeouts.push(ms);
|
||||
return timeout.call(AbortSignal, ms);
|
||||
});
|
||||
t.mock.method(console, 'log', () => {});
|
||||
for (const failingUrl of [TARKTEE_LOCATIONS_URL, TARKTEE_IMAGES_URL]) {
|
||||
for (const lateSibling of [false, true]) {
|
||||
test(
|
||||
`Tarktee cancels a ${lateSibling ? 'late' : 'received'} sibling when ${failingUrl} rejects before headers`,
|
||||
{ timeout: 2000 },
|
||||
async (t) => {
|
||||
quiet(t);
|
||||
withoutDeadline(t);
|
||||
let cancelled = false;
|
||||
let siblingSignal;
|
||||
let release;
|
||||
const gate = new Promise((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
t.mock.method(globalThis, 'fetch', async (url, init) => {
|
||||
if (String(url) === failingUrl)
|
||||
throw new Error('headers unavailable');
|
||||
siblingSignal = init.signal;
|
||||
// A late transport answers once the sibling is aborted, or on release.
|
||||
if (lateSibling)
|
||||
await Promise.race([
|
||||
gate,
|
||||
new Promise((resolve) =>
|
||||
init.signal.addEventListener('abort', resolve, { once: true }),
|
||||
),
|
||||
]);
|
||||
return new Response(
|
||||
new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new TextEncoder().encode('<xml/>'));
|
||||
},
|
||||
cancel() {
|
||||
cancelled = true;
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
assert.deepEqual(await loadTarkteeSourcesFromDatex(), []);
|
||||
// Also exercise a response arriving after abort, as a queued transport
|
||||
// completion can race the rejection owner even when fetch honors abort.
|
||||
if (lateSibling) {
|
||||
release();
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
}
|
||||
assert.equal(cancelled, true, 'every obtained body must be cancelled');
|
||||
assert.equal(
|
||||
siblingSignal.aborted,
|
||||
true,
|
||||
'a pending transport is aborted immediately',
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
test(
|
||||
`Tarktee aborts a pending sibling when ${failingUrl} rejects`,
|
||||
{ timeout: 2000 },
|
||||
async (t) => {
|
||||
quiet(t);
|
||||
withoutDeadline(t);
|
||||
let aborted = false;
|
||||
t.mock.method(globalThis, 'fetch', async (url, init) => {
|
||||
if (String(url) === failingUrl) throw new Error('headers unavailable');
|
||||
return new Promise((resolve, reject) => {
|
||||
init.signal.addEventListener(
|
||||
'abort',
|
||||
() => {
|
||||
aborted = true;
|
||||
reject(init.signal.reason);
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
});
|
||||
assert.deepEqual(await loadTarkteeSourcesFromDatex(), []);
|
||||
assert.equal(aborted, true, 'do not wait for the 30-second deadline');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
test('Tarktee cancels both lengthless streams when one exceeds the byte cap', async (t) => {
|
||||
quiet(t);
|
||||
const cancelled = [];
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async (url) =>
|
||||
new Response(
|
||||
String(url).includes('roadCameraLocations')
|
||||
? SAMPLE_LOCATIONS_XML
|
||||
: SAMPLE_IMAGES_XML,
|
||||
new ReadableStream({
|
||||
start(controller) {
|
||||
const oversized = String(url) === TARKTEE_LOCATIONS_URL;
|
||||
controller.enqueue(
|
||||
new Uint8Array(oversized ? TARKTEE_MAX_CATALOG_BYTES + 1 : 1),
|
||||
);
|
||||
},
|
||||
cancel() {
|
||||
cancelled.push(String(url));
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
const cameras = await loadTarkteeSourcesFromDatex();
|
||||
assert.deepEqual(timeouts, [30_000, 30_000]);
|
||||
assert.equal(cameras.length, 1);
|
||||
assert.deepEqual(await loadTarkteeSourcesFromDatex(), []);
|
||||
assert.deepEqual(
|
||||
cancelled.sort(),
|
||||
[TARKTEE_LOCATIONS_URL, TARKTEE_IMAGES_URL].sort(),
|
||||
);
|
||||
});
|
||||
|
||||
test(
|
||||
'Tarktee aborts pending headers as soon as its sibling returns an HTTP error',
|
||||
{ timeout: 2000 },
|
||||
async (t) => {
|
||||
quiet(t);
|
||||
withoutDeadline(t);
|
||||
let aborted = false;
|
||||
t.mock.method(globalThis, 'fetch', async (url, init) => {
|
||||
if (String(url) === TARKTEE_LOCATIONS_URL)
|
||||
return new Response('unavailable', { status: 503 });
|
||||
return new Promise((resolve, reject) => {
|
||||
init.signal.addEventListener(
|
||||
'abort',
|
||||
() => {
|
||||
aborted = true;
|
||||
reject(init.signal.reason);
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
});
|
||||
assert.deepEqual(await loadTarkteeSourcesFromDatex(), []);
|
||||
assert.equal(aborted, true);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,112 @@
|
||||
// A body-phase failure of one Tarktee DATEX feed must abort the sibling that is
|
||||
// still waiting for headers instead of leaving it until the 30 s deadline.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import {
|
||||
TARKTEE_IMAGES_URL,
|
||||
TARKTEE_LOCATIONS_URL,
|
||||
TARKTEE_MAX_CATALOG_BYTES,
|
||||
} from '../../server/providers/cctv/constants.js';
|
||||
import { loadTarkteeSourcesFromDatex } from '../../server/providers/cctv/sources.js';
|
||||
|
||||
/** The sibling sends its headers this late; the loader must not wait for it. */
|
||||
const SIBLING_HEADER_DELAY_MS = 2000;
|
||||
/** Generous bound for "at once" on a loaded CI machine. */
|
||||
const FAIL_FAST_MS = 1000;
|
||||
|
||||
function startServer(handlers) {
|
||||
const server = http.createServer((req, res) => handlers[req.url](req, res));
|
||||
return new Promise((resolve) => {
|
||||
server.listen(0, '127.0.0.1', () => resolve(server));
|
||||
});
|
||||
}
|
||||
|
||||
/** Send the two DATEX URLs to the local server through the real fetch. */
|
||||
function routeFetchTo(t, port) {
|
||||
const realFetch = globalThis.fetch;
|
||||
t.mock.method(globalThis, 'fetch', (url, init) => {
|
||||
const path =
|
||||
String(url) === TARKTEE_LOCATIONS_URL
|
||||
? '/loc'
|
||||
: String(url) === TARKTEE_IMAGES_URL
|
||||
? '/img'
|
||||
: null;
|
||||
assert.ok(path, `unexpected url ${url}`);
|
||||
return realFetch(`http://127.0.0.1:${port}${path}`, init);
|
||||
});
|
||||
}
|
||||
|
||||
/** The sibling answers late and reports whether it was cut off before headers. */
|
||||
function slowSibling() {
|
||||
const arrived = Promise.withResolvers();
|
||||
const closed = Promise.withResolvers();
|
||||
return {
|
||||
arrived: arrived.promise,
|
||||
closed: closed.promise,
|
||||
handler(req, res) {
|
||||
arrived.resolve();
|
||||
const timer = setTimeout(() => {
|
||||
res.writeHead(200, { 'content-type': 'application/xml' });
|
||||
res.end('<d2LogicalModel/>');
|
||||
}, SIBLING_HEADER_DELAY_MS);
|
||||
res.on('close', () => {
|
||||
clearTimeout(timer);
|
||||
closed.resolve({ beforeHeaders: !res.headersSent });
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const failingBodies = {
|
||||
'declares a body over the byte cap': (req, res) => {
|
||||
res.writeHead(200, {
|
||||
'content-type': 'application/xml',
|
||||
'content-length': String(TARKTEE_MAX_CATALOG_BYTES + 1),
|
||||
});
|
||||
res.write('<d2LogicalModel>');
|
||||
},
|
||||
'resets the connection mid-body': (req, res) => {
|
||||
res.writeHead(200, { 'content-type': 'application/xml' });
|
||||
res.write('<d2LogicalModel><payloadPublication>');
|
||||
setTimeout(() => res.socket.destroy(), 20);
|
||||
},
|
||||
};
|
||||
|
||||
for (const [label, failingFeed] of Object.entries(failingBodies)) {
|
||||
for (const [failing, pending] of [
|
||||
['/loc', '/img'],
|
||||
['/img', '/loc'],
|
||||
]) {
|
||||
test(`Tarktee aborts ${pending} pending headers when ${failing} ${label}`, { timeout: 10_000 }, async (t) => {
|
||||
t.mock.method(console, 'log', () => {});
|
||||
t.mock.method(console, 'warn', () => {});
|
||||
const sibling = slowSibling();
|
||||
const server = await startServer({
|
||||
[failing]: (req, res) =>
|
||||
sibling.arrived.then(() => failingFeed(req, res)),
|
||||
[pending]: sibling.handler,
|
||||
});
|
||||
t.after(() => {
|
||||
server.closeAllConnections();
|
||||
server.close();
|
||||
});
|
||||
routeFetchTo(t, server.address().port);
|
||||
|
||||
const started = Date.now();
|
||||
const cameras = await loadTarkteeSourcesFromDatex();
|
||||
const elapsed = Date.now() - started;
|
||||
|
||||
assert.deepEqual(cameras, []);
|
||||
assert.ok(
|
||||
elapsed < FAIL_FAST_MS,
|
||||
`loader waited ${elapsed} ms for the sibling's headers instead of aborting it`,
|
||||
);
|
||||
assert.deepEqual(
|
||||
await sibling.closed,
|
||||
{ beforeHeaders: true },
|
||||
`${pending} was not aborted`,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
// An explicit pick of the camera that a settled move just re-opened must end
|
||||
// the opening pick. That pick goes through setActiveCamera's UNCHANGED path
|
||||
// (same camera, already activated); if that path keeps the provisional flag,
|
||||
// the next settled move (a pan, or the landing of NEAREST's own flight)
|
||||
// switches the camera away from the one the user chose.
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import * as Cesium from 'cesium';
|
||||
import cctvLayer, {
|
||||
_setCctvCoverageStateForTest,
|
||||
_setCctvOverlayHostForTest,
|
||||
reopenNearestCamera,
|
||||
} from './cctv.js';
|
||||
import { bindFakeDraw } from '../testSupport/fakeDrawPort.mjs';
|
||||
|
||||
const BASE = {
|
||||
headingDeg: 41,
|
||||
pitchDeg: -5,
|
||||
fovDeg: 56,
|
||||
rangeM: 210,
|
||||
mountHeightM: 100,
|
||||
groundElevationM: 0,
|
||||
city: 'London',
|
||||
};
|
||||
|
||||
function record(id, lat, lon) {
|
||||
return {
|
||||
camera: { ...BASE, id, name: `Camera ${id}`, lat, lon },
|
||||
coverageEntities: [],
|
||||
projection: null,
|
||||
activationDone: true,
|
||||
groundResolved: {},
|
||||
groundSamples: {},
|
||||
groundPrior: 0,
|
||||
frustumGeometry: null,
|
||||
frustumPositions: null,
|
||||
probeClampRangeM: null,
|
||||
position: Cesium.Cartesian3.fromDegrees(lon, lat, 100),
|
||||
};
|
||||
}
|
||||
|
||||
function viewerAt(lat, lon) {
|
||||
const viewer = {
|
||||
isDestroyed: () => false,
|
||||
eye: { lat, lon, height: 300 },
|
||||
scene: {
|
||||
canvas: { clientWidth: 1200, clientHeight: 800 },
|
||||
cartesianToCanvasCoordinates: () => undefined,
|
||||
primitives: { add: (p) => p, remove: () => true },
|
||||
requestRender() {},
|
||||
},
|
||||
};
|
||||
// The layer reads the eye and flies through the renderer's camera port.
|
||||
viewer.draw = bindFakeDraw(viewer, {
|
||||
camera: {
|
||||
pose: () => ({ ...viewer.eye }),
|
||||
on: () => () => {},
|
||||
followed: null,
|
||||
flyToBounds() {},
|
||||
},
|
||||
}).draw;
|
||||
return viewer;
|
||||
}
|
||||
|
||||
function moveViewer(viewer, lat, lon) {
|
||||
viewer.eye = { lat, lon, height: 300 };
|
||||
}
|
||||
|
||||
function withReopenedLayer(fn) {
|
||||
_setCctvOverlayHostForTest({
|
||||
setEntries() {},
|
||||
setVisible() {},
|
||||
clearSource() {},
|
||||
});
|
||||
// Activation builds the projection canvas and frame image.
|
||||
const originalDocument = globalThis.document;
|
||||
const originalImage = globalThis.Image;
|
||||
globalThis.document = { createElement: () => ({ getContext: () => null }) };
|
||||
globalThis.Image = class {};
|
||||
try {
|
||||
const trafalgar = record('a-trafalgar', 51.508, -0.128);
|
||||
const cambridge = record('b-cambridge', 51.513, -0.129);
|
||||
// enable() assigned the opening camera without activating it.
|
||||
trafalgar.activationDone = false;
|
||||
const viewer = viewerAt(51.5081, -0.1281);
|
||||
_setCctvCoverageStateForTest({
|
||||
viewer,
|
||||
records: [trafalgar, cambridge],
|
||||
activeCameraId: 'a-trafalgar',
|
||||
enabled: true,
|
||||
openingCameraProvisional: true,
|
||||
});
|
||||
// A settled move next to Cambridge Circus re-opens there, still as the
|
||||
// opening pick.
|
||||
moveViewer(viewer, 51.5131, -0.1291);
|
||||
assert.equal(reopenNearestCamera(), true);
|
||||
assert.equal(cctvLayer.getUIState().activeCameraId, 'b-cambridge');
|
||||
fn(viewer);
|
||||
} finally {
|
||||
globalThis.document = originalDocument;
|
||||
globalThis.Image = originalImage;
|
||||
_setCctvOverlayHostForTest();
|
||||
_setCctvCoverageStateForTest({ enabled: false });
|
||||
}
|
||||
}
|
||||
|
||||
for (const [name, pick] of [
|
||||
['NEAREST (focusNearest)', () => cctvLayer.focusNearest({ focus: false })],
|
||||
['selectCamera', () => cctvLayer.selectCamera('b-cambridge')],
|
||||
[
|
||||
'setParams({ selectedCameraId })',
|
||||
() => cctvLayer.setParams({ selectedCameraId: 'b-cambridge' }),
|
||||
],
|
||||
[
|
||||
'focusNearestToPoint',
|
||||
() => cctvLayer.focusNearestToPoint(51.513, -0.129, { focus: false }),
|
||||
],
|
||||
]) {
|
||||
test(`an explicit ${name} pick of the re-opened camera ends the opening pick`, () => {
|
||||
withReopenedLayer((viewer) => {
|
||||
pick();
|
||||
assert.equal(cctvLayer.getUIState().activeCameraId, 'b-cambridge');
|
||||
// A later settled move (a pan, or the landing of the pick's own flight)
|
||||
// leaves the explicit choice alone.
|
||||
moveViewer(viewer, 51.5081, -0.1281);
|
||||
assert.equal(reopenNearestCamera(), false);
|
||||
assert.equal(cctvLayer.getUIState().activeCameraId, 'b-cambridge');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test('without a pick, the next settled move still follows the view', () => {
|
||||
withReopenedLayer((viewer) => {
|
||||
moveViewer(viewer, 51.5081, -0.1281);
|
||||
assert.equal(reopenNearestCamera(), true);
|
||||
assert.equal(cctvLayer.getUIState().activeCameraId, 'a-trafalgar');
|
||||
});
|
||||
});
|
||||
@@ -140,25 +140,28 @@ for (const [label, flag] of [
|
||||
});
|
||||
}
|
||||
|
||||
test('a failed catalog cancels the sibling request instead of waiting for the timeout', async (t) => {
|
||||
t.mock.method(console, 'warn', () => {});
|
||||
let siblingSignal;
|
||||
t.mock.method(globalThis, 'fetch', async (url, options) => {
|
||||
if (url === LITHUANIA_CAMERAS_URL) {
|
||||
return new Response('', { status: 503 });
|
||||
}
|
||||
siblingSignal = options.signal;
|
||||
// Never settles on its own; only an abort ends it.
|
||||
return new Promise((_, reject) => {
|
||||
options.signal.addEventListener('abort', () =>
|
||||
reject(options.signal.reason),
|
||||
);
|
||||
for (const [failure, camerasResponse] of [
|
||||
['failed', () => new Response('', { status: 503 })],
|
||||
['malformed', () => new Response('<html>error page</html>')],
|
||||
]) {
|
||||
test(`a ${failure} catalog cancels the sibling request instead of waiting for the timeout`, async (t) => {
|
||||
t.mock.method(console, 'warn', () => {});
|
||||
let siblingSignal;
|
||||
t.mock.method(globalThis, 'fetch', async (url, options) => {
|
||||
if (url === LITHUANIA_CAMERAS_URL) return camerasResponse();
|
||||
siblingSignal = options.signal;
|
||||
// Never settles on its own; only an abort ends it.
|
||||
return new Promise((_, reject) => {
|
||||
options.signal.addEventListener('abort', () =>
|
||||
reject(options.signal.reason),
|
||||
);
|
||||
});
|
||||
});
|
||||
assert.deepEqual(await loadLithuaniaSources(), []);
|
||||
assert.equal(siblingSignal?.aborted, true);
|
||||
assert.notEqual(siblingSignal.reason?.name, 'TimeoutError');
|
||||
});
|
||||
assert.deepEqual(await loadLithuaniaSources(), []);
|
||||
assert.equal(siblingSignal?.aborted, true);
|
||||
assert.notEqual(siblingSignal.reason?.name, 'TimeoutError');
|
||||
});
|
||||
}
|
||||
|
||||
test('a redirected catalog is reported as not followed', async (t) => {
|
||||
const warnings = [];
|
||||
|
||||
@@ -111,6 +111,7 @@ function mount(t, respond) {
|
||||
austin: process.env.CCTV_FORCE_AUSTIN,
|
||||
serverKey: process.env.GOOGLE_MAPS_SERVER_API_KEY,
|
||||
browserKey: process.env.GOOGLE_MAPS_API_KEY,
|
||||
rateLimit: process.env.GEV_RATELIMIT_GOOGLE_PER_MIN,
|
||||
};
|
||||
process.env.CCTV_SOURCES_JSON = JSON.stringify([
|
||||
STILL,
|
||||
@@ -125,6 +126,8 @@ function mount(t, respond) {
|
||||
// than a network call this file does not model.
|
||||
delete process.env.GOOGLE_MAPS_SERVER_API_KEY;
|
||||
delete process.env.GOOGLE_MAPS_API_KEY;
|
||||
// The default Google budget, whatever the shell sets.
|
||||
delete process.env.GEV_RATELIMIT_GOOGLE_PER_MIN;
|
||||
const nativeFetch = globalThis.fetch;
|
||||
const requests = [];
|
||||
globalThis.fetch = async (url, init = {}) => {
|
||||
@@ -139,6 +142,7 @@ function mount(t, respond) {
|
||||
['CCTV_FORCE_AUSTIN', before.austin],
|
||||
['GOOGLE_MAPS_SERVER_API_KEY', before.serverKey],
|
||||
['GOOGLE_MAPS_API_KEY', before.browserKey],
|
||||
['GEV_RATELIMIT_GOOGLE_PER_MIN', before.rateLimit],
|
||||
]) {
|
||||
if (value === undefined) delete process.env[name];
|
||||
else process.env[name] = value;
|
||||
@@ -155,9 +159,9 @@ function mount(t, respond) {
|
||||
});
|
||||
assert.ok(handler, 'the CCTV provider must mount a middleware');
|
||||
|
||||
const call = async (path, headers = {}) => {
|
||||
const call = async (path, headers = {}, socket) => {
|
||||
const { res, done } = recordingResponse();
|
||||
await handler({ url: path, headers, method: 'GET', on() {} }, res);
|
||||
await handler({ url: path, headers, method: 'GET', on() {}, socket }, res);
|
||||
await done;
|
||||
return res;
|
||||
};
|
||||
@@ -528,3 +532,100 @@ test('a forwarded same-origin request reaches every CCTV route but not the Stree
|
||||
assert.equal(direct.body, 'street-view');
|
||||
assert.equal(streetViewCalls(), 1);
|
||||
});
|
||||
|
||||
test('the Street View fallback frames catalog cameras only, from the catalog position', async (t) => {
|
||||
const app = mount(t, ({ url }) =>
|
||||
url.startsWith('https://maps.googleapis.com/')
|
||||
? trackedUpstream({
|
||||
headers: { 'content-type': 'image/jpeg' },
|
||||
body: 'street-view',
|
||||
}).response
|
||||
: upstreamResponse({ status: 503 }),
|
||||
);
|
||||
process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key';
|
||||
const streetView = () =>
|
||||
app.requests.filter(({ url }) =>
|
||||
url.startsWith('https://maps.googleapis.com/'),
|
||||
);
|
||||
|
||||
const unknown = await app.call('/frame/not-a-camera?lat=48.85&lon=2.29');
|
||||
assert.equal(unknown.headers['X-CCTV-Source'], 'synthetic');
|
||||
assert.deepEqual(streetView(), [], 'no Google call for an unknown id');
|
||||
|
||||
const steered = await app.call(
|
||||
`/frame/${STILL.id}?lat=48.85&lon=2.29&heading=90&fov=74&pitch=-17`,
|
||||
);
|
||||
assert.equal(steered.headers['X-CCTV-Source'], 'streetview');
|
||||
const sv = new URL(streetView()[0].url).searchParams;
|
||||
assert.equal(sv.get('location'), `${STILL.lat},${STILL.lon}`);
|
||||
assert.equal(sv.get('heading'), '90');
|
||||
assert.equal(sv.get('fov'), '74');
|
||||
assert.equal(sv.get('pitch'), '-17');
|
||||
});
|
||||
|
||||
test('the Street View fallback spends a per-IP Google budget, and only with a key', async (t) => {
|
||||
const app = mount(t, ({ url }) =>
|
||||
url.startsWith('https://maps.googleapis.com/')
|
||||
? trackedUpstream({
|
||||
headers: { 'content-type': 'image/jpeg' },
|
||||
body: 'street-view',
|
||||
}).response
|
||||
: upstreamResponse({ status: 503 }),
|
||||
);
|
||||
// Read when the first keyed fallback builds the limiter, so after mount().
|
||||
process.env.GEV_RATELIMIT_GOOGLE_PER_MIN = '2';
|
||||
const streetViewCalls = () =>
|
||||
app.requests.filter(({ url }) =>
|
||||
url.startsWith('https://maps.googleapis.com/'),
|
||||
).length;
|
||||
const first = { remoteAddress: '203.0.113.7' };
|
||||
const frameSource = async (socket) =>
|
||||
(await app.call(`/frame/${STILL.id}`, {}, socket)).headers['X-CCTV-Source'];
|
||||
|
||||
// Without a key nothing is spent.
|
||||
for (let i = 0; i < 3; i++)
|
||||
assert.equal(await frameSource(first), 'synthetic');
|
||||
assert.equal(streetViewCalls(), 0);
|
||||
|
||||
process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key';
|
||||
// Neither an unknown id nor a forwarded request spends the window.
|
||||
assert.equal(
|
||||
(await app.call('/frame/not-a-camera', {}, first)).headers['X-CCTV-Source'],
|
||||
'synthetic',
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await app.call(
|
||||
`/frame/${STILL.id}`,
|
||||
{ 'x-forwarded-for': '203.0.113.9' },
|
||||
first,
|
||||
)
|
||||
).headers['X-CCTV-Source'],
|
||||
'synthetic',
|
||||
);
|
||||
assert.deepEqual(
|
||||
[
|
||||
await frameSource(first),
|
||||
await frameSource(first),
|
||||
await frameSource(first),
|
||||
],
|
||||
['streetview', 'streetview', 'synthetic'],
|
||||
);
|
||||
assert.equal(streetViewCalls(), 2, 'no Google call over the budget');
|
||||
|
||||
// Another address has a budget of its own.
|
||||
assert.equal(
|
||||
await frameSource({ remoteAddress: '203.0.113.8' }),
|
||||
'streetview',
|
||||
);
|
||||
assert.equal(streetViewCalls(), 3);
|
||||
|
||||
// Addresses within one IPv6 /64 share a budget.
|
||||
const v6 = (remoteAddress) => frameSource({ remoteAddress });
|
||||
assert.deepEqual(
|
||||
[await v6('2001:db8::1'), await v6('2001:db8::2'), await v6('2001:db8::3')],
|
||||
['streetview', 'streetview', 'synthetic'],
|
||||
);
|
||||
assert.equal(await v6('2001:db8:0:1::1'), 'streetview', 'another /64');
|
||||
assert.equal(streetViewCalls(), 6);
|
||||
});
|
||||
@@ -0,0 +1,87 @@
|
||||
// Real sockets: a client that closes its connection while the camera upstream
|
||||
// is still pending must not get a Street View call outside its own budget.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import { cctvProxy } from '../../server/providers/cctv.js';
|
||||
|
||||
const CAM = {
|
||||
id: 'still-fixture',
|
||||
name: 'Still fixture',
|
||||
lat: 30.27,
|
||||
lon: -97.74,
|
||||
feedType: 'image',
|
||||
url: 'https://cams.upstream.invalid/still.jpg',
|
||||
};
|
||||
|
||||
test('a client that disconnects mid-fetch stays inside its own Street View budget', async (t) => {
|
||||
const saved = { ...process.env };
|
||||
process.env.CCTV_SOURCES_JSON = JSON.stringify([CAM]);
|
||||
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
|
||||
process.env.CCTV_FORCE_AUSTIN = '0';
|
||||
process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key';
|
||||
delete process.env.GOOGLE_MAPS_API_KEY;
|
||||
process.env.GEV_RATELIMIT_GOOGLE_PER_MIN = '1';
|
||||
const nativeFetch = globalThis.fetch;
|
||||
let streetViewCalls = 0;
|
||||
globalThis.fetch = async (url) => {
|
||||
if (String(url).startsWith('https://maps.googleapis.com/')) {
|
||||
streetViewCalls += 1;
|
||||
return new Response('street-view', {
|
||||
headers: { 'content-type': 'image/jpeg' },
|
||||
});
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 400));
|
||||
return new Response(null, { status: 503 });
|
||||
};
|
||||
let handler;
|
||||
cctvProxy().configureServer({
|
||||
middlewares: { use: (_r, fn) => (handler = fn) },
|
||||
});
|
||||
const server = http.createServer((req, res) => {
|
||||
req.url = req.url.replace(/^\/api\/cctv/, '');
|
||||
handler(req, res, () => res.end());
|
||||
});
|
||||
await new Promise((r) => server.listen(0, '127.0.0.1', r));
|
||||
t.after(() => {
|
||||
server.close();
|
||||
globalThis.fetch = nativeFetch;
|
||||
for (const k of Object.keys(process.env))
|
||||
if (!(k in saved)) delete process.env[k];
|
||||
Object.assign(process.env, saved);
|
||||
});
|
||||
const { port } = server.address();
|
||||
const get = (abortAfterMs) =>
|
||||
new Promise((resolve) => {
|
||||
const req = http.get(
|
||||
{
|
||||
host: '127.0.0.1',
|
||||
port,
|
||||
path: `/api/cctv/frame/${CAM.id}`,
|
||||
headers: { host: `localhost:${port}` },
|
||||
agent: false,
|
||||
},
|
||||
(res) => {
|
||||
res.resume();
|
||||
res.on('end', () => resolve(res.headers['x-cctv-source']));
|
||||
},
|
||||
);
|
||||
req.on('error', () => resolve('aborted'));
|
||||
if (abortAfterMs)
|
||||
setTimeout(() => {
|
||||
req.destroy();
|
||||
resolve('aborted');
|
||||
}, abortAfterMs);
|
||||
});
|
||||
|
||||
assert.equal(await get(), 'streetview');
|
||||
assert.equal(await get(), 'synthetic', 'the cap of one is spent');
|
||||
assert.equal(streetViewCalls, 1);
|
||||
await get(50);
|
||||
await new Promise((r) => setTimeout(r, 800));
|
||||
assert.equal(
|
||||
streetViewCalls,
|
||||
1,
|
||||
'an aborted request spent the key outside its own budget',
|
||||
);
|
||||
});
|
||||
@@ -327,9 +327,9 @@ export const DATA_CREDITS = [
|
||||
key: 'ontario-511-cctv',
|
||||
html:
|
||||
'CCTV cameras & frames (Ontario): ' +
|
||||
'<a href="https://511on.ca/" target="_blank" rel="noopener">Ontario 511</a> ' +
|
||||
'(<a href="https://www.ontario.ca/page/open-government-licence-ontario" target="_blank" rel="noopener">Open Government Licence - Ontario</a>) ' +
|
||||
'· provided as is, without warranty; actual road conditions may differ',
|
||||
'<a href="https://511on.ca/" target="_blank" rel="noopener">Ontario 511</a>. ' +
|
||||
'Contains information licensed under the <a href="https://www.ontario.ca/page/open-government-licence-ontario" target="_blank" rel="noopener">Open Government Licence – Ontario</a>. ' +
|
||||
'Provided as is, without warranty; actual road conditions may differ.',
|
||||
},
|
||||
{
|
||||
key: 'fintraffic-cctv',
|
||||
|
||||
@@ -173,6 +173,18 @@ test('the NYC camera credit carries NYC DOT’s requested disclaimer', () => {
|
||||
assert.match(credit.html, /observe posted signage/);
|
||||
});
|
||||
|
||||
test('the Ontario 511 camera credit carries the licence attribution statement', () => {
|
||||
const credit = DATA_CREDITS.find((entry) => entry.key === 'ontario-511-cctv');
|
||||
assert.ok(credit, 'Ontario 511 camera frames must be credited');
|
||||
assert.ok(
|
||||
credit.html.includes(
|
||||
'Contains information licensed under the <a href="https://www.ontario.ca/page/open-government-licence-ontario"',
|
||||
),
|
||||
);
|
||||
assert.match(credit.html, />Open Government Licence – Ontario<\/a>/);
|
||||
assert.match(credit.html, /Provided as is, without warranty/);
|
||||
});
|
||||
|
||||
test("the Houston camera credit names TranStar's terms and the permission they require", () => {
|
||||
const credit = DATA_CREDITS.find((entry) => entry.key === 'houston-cctv');
|
||||
assert.ok(credit, 'Houston TranStar camera frames must be credited');
|
||||
|
||||
Reference in new issue
Block a user