mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 08:42:21 +00:00
A wrong or revoked key logged only "download failed: 400"; a 400, 401 or 403 now adds "(check ONTARIO_511_API_KEY)". The README's CCTV row listed Ontario among the default cameras with no mention of the key it now needs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
66 lines
2.2 KiB
JavaScript
66 lines
2.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
|
|
import { loadOntarioSourcesFromOpenData } from './sources.js';
|
|
|
|
const ROW = {
|
|
Id: '42',
|
|
Latitude: 43.6532,
|
|
Longitude: -79.3832,
|
|
Location: 'Hwy 401 near Yonge St',
|
|
Direction: 'Eastbound',
|
|
Views: [
|
|
{
|
|
Status: 'Enabled',
|
|
Url: 'https://511on.ca/map/Cctv/4201',
|
|
Description: 'Eastbound',
|
|
},
|
|
],
|
|
};
|
|
|
|
test('without ONTARIO_511_API_KEY the Ontario pack is skipped, logged once', async (t) => {
|
|
const warnings = [];
|
|
t.mock.method(console, 'warn', (...args) => warnings.push(args.join(' ')));
|
|
t.mock.method(globalThis, 'fetch', async () =>
|
|
assert.fail('no request without a key'),
|
|
);
|
|
assert.deepEqual(await loadOntarioSourcesFromOpenData({}, {}), []);
|
|
assert.deepEqual(await loadOntarioSourcesFromOpenData({}, {}), []);
|
|
assert.equal(warnings.length, 1);
|
|
assert.match(warnings[0], /ONTARIO_511_API_KEY/);
|
|
});
|
|
|
|
test('with a key the camera list is requested with it and parsed', async (t) => {
|
|
const requested = [];
|
|
t.mock.method(globalThis, 'fetch', async (url) => {
|
|
requested.push(String(url));
|
|
return Response.json([ROW]);
|
|
});
|
|
const cameras = await loadOntarioSourcesFromOpenData(
|
|
{ sourceRoot: '/ignored' },
|
|
{ ONTARIO_511_API_KEY: ' test-key ' },
|
|
);
|
|
const url = new URL(requested[0]);
|
|
assert.equal(url.host, '511on.ca');
|
|
assert.equal(url.searchParams.get('key'), 'test-key');
|
|
assert.equal(url.searchParams.get('format'), 'json');
|
|
assert.equal(cameras.length, 1);
|
|
assert.equal(cameras[0].id, 'on-42');
|
|
assert.equal(cameras[0].url, 'https://511on.ca/map/Cctv/4201');
|
|
});
|
|
|
|
test('a rejected key is reported by status, never by the key itself', async (t) => {
|
|
const warnings = [];
|
|
t.mock.method(console, 'warn', (...args) => warnings.push(args.join(' ')));
|
|
t.mock.method(globalThis, 'fetch', async () =>
|
|
Response.json({ Message: 'Invalid Key' }, { status: 400 }),
|
|
);
|
|
const cameras = await loadOntarioSourcesFromOpenData(
|
|
{},
|
|
{ ONTARIO_511_API_KEY: 'secret-key-value' },
|
|
);
|
|
assert.deepEqual(cameras, []);
|
|
assert.match(warnings.join('\n'), /400 \(check ONTARIO_511_API_KEY\)/);
|
|
assert.doesNotMatch(warnings.join('\n'), /secret-key-value/);
|
|
});
|