mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-05 02:44:02 +00:00
fix(cctv): release snapshot readers and verify provider error paths
This commit is contained in:
1 parent
3d956177ab
commit
24d2582516
5 files changed
+147
-28
No files matched your search
@@ -212,3 +212,50 @@ test('CCTV upstream frame fetch refuses a body it cannot stream instead of buffe
|
||||
});
|
||||
assert.equal(result, null);
|
||||
});
|
||||
|
||||
test('reader-only snapshots release their lock on success, overflow and read failure', async () => {
|
||||
for (const mode of ['success', 'overflow', 'failure']) {
|
||||
const body = new ReadableStream({
|
||||
pull(controller) {
|
||||
if (mode === 'failure') controller.error(new Error('read failed'));
|
||||
else { controller.enqueue(new Uint8Array(4)); controller.close(); }
|
||||
},
|
||||
});
|
||||
Object.defineProperty(body, Symbol.asyncIterator, { value: undefined });
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
maxBytes: mode === 'overflow' ? 2 : 8,
|
||||
fetchImpl: async () => ({ ok: true, headers: new Headers({ 'content-type': 'image/jpeg' }), body }),
|
||||
});
|
||||
assert.equal(body.locked, false, mode);
|
||||
assert.equal(result?.body.length ?? null, mode === 'success' ? 4 : null);
|
||||
}
|
||||
});
|
||||
|
||||
test('the snapshot deadline remains active after headers while reading a stalled body', async () => {
|
||||
let signal;
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
timeoutMs: 20,
|
||||
fetchImpl: async (_url, init) => {
|
||||
signal = init.signal;
|
||||
return new Response(new ReadableStream({ start(controller) {
|
||||
signal.addEventListener('abort', () => controller.error(signal.reason), { once: true });
|
||||
}}), { headers: { 'content-type': 'image/jpeg' } });
|
||||
},
|
||||
});
|
||||
assert.equal(result, null);
|
||||
assert.equal(signal.aborted, true);
|
||||
});
|
||||
|
||||
test('rejected snapshot responses abort the upstream download', async () => {
|
||||
for (const [status, contentType] of [[503, 'image/jpeg'], [200, 'text/html']]) {
|
||||
let signal;
|
||||
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
|
||||
fetchImpl: async (_url, init) => {
|
||||
signal = init.signal;
|
||||
return new Response('rejected', { status, headers: { 'content-type': contentType } });
|
||||
},
|
||||
});
|
||||
assert.equal(result, null);
|
||||
assert.equal(signal.aborted, true);
|
||||
}
|
||||
});
|
||||
@@ -89,3 +89,18 @@ test('both Places routes answer bad coordinates with a 400 before the limiter an
|
||||
else process.env.GEV_RATELIMIT_GOOGLE_PER_MIN = previousLimit;
|
||||
}
|
||||
});
|
||||
|
||||
test('preview validates both Places routes and keeps the keyless response', async () => {
|
||||
for (const key of ['', 'test-key']) {
|
||||
const routes = new Map();
|
||||
googlePlacesContextProxy({ resolveApiKey: () => key }).configurePreviewServer({
|
||||
middlewares: { use: (path, handler) => routes.set(path, handler) },
|
||||
});
|
||||
for (const name of ['nearby-places', 'text-search']) {
|
||||
const result = await invokeRoute(routes.get(`/api/google/${name}`), { url: '/?q=capitol&lat=&lon=', remoteAddress: 'preview-test' });
|
||||
assert.equal(result.statusCode, key ? 400 : 200);
|
||||
assert.deepEqual(result.body.places, []);
|
||||
if (!key) assert.equal(result.body.configured, false);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -7,9 +7,9 @@ import { cctvProxy } from '../../server/providers/cctv.js';
|
||||
import { radioBrowserProxy } from '../../server/providers/radio.js';
|
||||
import { localProviderPlugins } from '../../server/providers/local.js';
|
||||
|
||||
function install(plugin) {
|
||||
function install(plugin, hook = 'configureServer') {
|
||||
let handler;
|
||||
plugin.configureServer({
|
||||
plugin[hook]({
|
||||
middlewares: {
|
||||
use(_route, fn) {
|
||||
handler = fn;
|
||||
@@ -102,3 +102,50 @@ test('composition creates exactly one CCTV and radio provider without acquisitio
|
||||
assert.equal(plugins.filter((p) => p.name === factory().name).length, 1);
|
||||
}
|
||||
});
|
||||
|
||||
for (const hook of ['configureServer', 'configurePreviewServer']) {
|
||||
test(`CCTV ${hook} maps header timeouts and cancels upstream error bodies`, async (t) => {
|
||||
isolate(t);
|
||||
process.env.CCTV_SOURCES_JSON = JSON.stringify([
|
||||
{
|
||||
id: 'bounded',
|
||||
name: 'Test',
|
||||
lat: 30,
|
||||
lon: -97,
|
||||
feedType: 'video',
|
||||
url: 'https://camera.example.org/live.mp4',
|
||||
},
|
||||
]);
|
||||
const request = install(
|
||||
cctvProxy({ sourceRoot: fixture(t, 'unused') }),
|
||||
hook,
|
||||
);
|
||||
// Resolve the catalog before substituting transport behavior.
|
||||
await request('/sources');
|
||||
t.mock.method(globalThis, 'fetch', async () => {
|
||||
throw new DOMException('timeout', 'AbortError');
|
||||
});
|
||||
const timeout = await request('/media/bounded');
|
||||
assert.equal(timeout.status, 504);
|
||||
assert.deepEqual(JSON.parse(timeout.body), {
|
||||
error: 'Upstream media timeout',
|
||||
});
|
||||
let cancelled = false;
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async () =>
|
||||
new Response(
|
||||
new ReadableStream({
|
||||
cancel() {
|
||||
cancelled = true;
|
||||
},
|
||||
}),
|
||||
{ status: 503 },
|
||||
),
|
||||
);
|
||||
const error = await request('/media/bounded');
|
||||
assert.equal(error.status, 503);
|
||||
assert.equal(cancelled, true);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user