Files
gods-eye-view/src/testSupport/cctvProviderHarness.mjs
T
German Khokhlov ed38a1ab3f fix(cctv): keep an HLS camera's health through the refused viewer's frame
A viewer refused for capacity falls back to /frame after hls.js gives up.
For a pullable HLS camera without a snapshot, /frame only has a
placeholder or a Street View frame, which says nothing about the stream,
yet it wrote degraded over the health that /media owns. /frame now
leaves that health as it is; a dead upstream is still reported through
/media.

Tests: the middleware suite covers both fallbacks, a real upstream
failure that still degrades, and cross-site segment and release refusals
on a live HLS lease; capacity checks use HLS_LIMITS and HlsCapacityError;
the three CCTV suites share one provider harness in src/testSupport.
CURRENT-STATE and the changelog fragment describe the refusal.
2026-11-20 11:48:22 +04:00

92 lines
2.6 KiB
JavaScript

import { cctvProxy } from '../../server/providers/cctv.js';
/** Collects what a handler wrote, the way a Node ServerResponse would. */
export function recordingResponse() {
const chunks = [];
let finished;
const done = new Promise((resolve) => {
finished = resolve;
});
const res = {
statusCode: 0,
headers: {},
writableEnded: false,
body: '',
writeHead(status, headers) {
res.statusCode = status;
res.headers = { ...res.headers, ...headers };
},
setHeader(name, value) {
res.headers[name] = value;
},
write(chunk) {
chunks.push(Buffer.from(chunk));
return true;
},
end(chunk) {
if (chunk) chunks.push(Buffer.from(chunk));
res.writableEnded = true;
res.body = Buffer.concat(chunks).toString('utf8');
finished();
},
on() {},
once() {},
emit() {},
destroy() {},
};
return { res, done };
}
export function mountCctvProvider(t, { cameras, fetch }) {
const saved = {};
for (const name of [
'CCTV_SOURCES_JSON',
'CCTV_SOURCES_FILE',
'CCTV_FORCE_AUSTIN',
'GOOGLE_MAPS_SERVER_API_KEY',
'GOOGLE_MAPS_API_KEY',
])
saved[name] = process.env[name];
const nativeFetch = globalThis.fetch;
const closers = [];
t.after(() => {
for (const close of closers) close();
globalThis.fetch = nativeFetch;
for (const [name, value] of Object.entries(saved)) {
if (value === undefined) delete process.env[name];
else process.env[name] = value;
}
});
process.env.CCTV_SOURCES_JSON = JSON.stringify(cameras);
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
process.env.CCTV_FORCE_AUSTIN = '0';
// No Street View key: a /frame miss lands on the synthetic billboard.
delete process.env.GOOGLE_MAPS_SERVER_API_KEY;
delete process.env.GOOGLE_MAPS_API_KEY;
// The HLS puller binds fetch when cctvProxy() creates it, so mock first.
globalThis.fetch = fetch;
let handler;
cctvProxy().configureServer({
httpServer: { on: (_event, fn) => closers.push(fn) },
middlewares: { use: (_route, fn) => (handler = fn) },
});
if (!handler) throw new Error('the CCTV provider must mount a middleware');
const call = async (path, headers = {}, method = 'GET', remoteAddress) => {
const { res, done } = recordingResponse();
await handler(
{ url: path, headers, method, socket: { remoteAddress }, on() {} },
res,
);
await done;
return res;
};
const health = async (id) => {
const res = await call('/health');
return JSON.parse(res.body).cameras.find((entry) => entry.id === id);
};
return { call, health, handler };
}