mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
The card never appeared: the Weather panel only passed five fixed ids and only rendered lists and categorical legends for cyclones. Air quality now reaches the panel, the legend style comes from the layer's descriptor, and any layer with list items gets its list. Adds the voice manifest entry main now requires, reads ECCC through the shared capped JSON reader with a 15 s deadline and the row limit enforced, reports stale when every reading was too old, adds source tests, links the ECCC licence and moves the changelog entry into Unreleased.
276 lines
8.9 KiB
JavaScript
276 lines
8.9 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { createAirQualityLayer } from './index.js';
|
|
import { layerFeedState } from '../../data/feedState.js';
|
|
import { STALE_AFTER_MS } from './model.js';
|
|
|
|
function harness(source) {
|
|
const sources = [];
|
|
const events = [];
|
|
const viewer = {
|
|
dataSources: {
|
|
add(value) {
|
|
sources.push(value);
|
|
},
|
|
remove(value) {
|
|
sources.splice(sources.indexOf(value), 1);
|
|
},
|
|
},
|
|
};
|
|
const layer = createAirQualityLayer({
|
|
source,
|
|
overlayHost: {
|
|
setEntries(...args) {
|
|
events.push(args);
|
|
},
|
|
setVisible() {},
|
|
clearSource() {},
|
|
},
|
|
});
|
|
layer.init(viewer);
|
|
layer.enable(viewer);
|
|
return { layer, viewer, sources, events };
|
|
}
|
|
|
|
const reading = (id, value, overrides = {}) => ({
|
|
id: `eccc:${id}`,
|
|
provider: 'eccc',
|
|
scale: 'AQHI',
|
|
value,
|
|
band: 'low',
|
|
stationId: id,
|
|
name: id,
|
|
zone: 'ont',
|
|
lat: 43.65,
|
|
lon: -79.38,
|
|
observedMs: 1_700_000_000_000,
|
|
color: '#4cc9f0',
|
|
label: String(value),
|
|
risk: 'Low health risk',
|
|
note: null,
|
|
...overrides,
|
|
});
|
|
|
|
const staticSource = (rows) => ({ getSnapshot: async () => rows });
|
|
|
|
test('a layer requires both a snapshot source and an overlay host', () => {
|
|
assert.throws(() => createAirQualityLayer(), /snapshot source/);
|
|
assert.throws(
|
|
() => createAirQualityLayer({ source: staticSource([]) }),
|
|
/overlay host/,
|
|
);
|
|
});
|
|
|
|
test('initialization is single-use and owns exactly one data source', () => {
|
|
const { layer, viewer, sources } = harness(staticSource([]));
|
|
assert.equal(sources.length, 1);
|
|
assert.throws(() => layer.init(viewer), /already initialized/);
|
|
layer.destroy(viewer);
|
|
assert.equal(sources.length, 0);
|
|
});
|
|
|
|
test('a refresh renders readings and publishes an overlay cohort', async () => {
|
|
const { layer, events } = harness(
|
|
staticSource([reading('A', 2), reading('B', 7)]),
|
|
);
|
|
assert.equal(await layer.update(), true);
|
|
assert.equal(layer.getStats().count, 2);
|
|
assert.equal(layer.getStats().error, null);
|
|
assert.equal(events.at(-1)[0], 'air-quality');
|
|
});
|
|
|
|
// ── the abort + enabled guard ──────────────────────────────────────────────
|
|
|
|
test('a response landing after disable cannot redraw the scene', async () => {
|
|
// The failure this prevents: disable() clears the overlay, then an in-flight
|
|
// response resolves and repopulates it behind the user's back.
|
|
let release;
|
|
const gate = new Promise((resolve) => {
|
|
release = resolve;
|
|
});
|
|
const { layer, events } = harness({
|
|
getSnapshot: async ({ signal } = {}) => {
|
|
await gate;
|
|
signal?.throwIfAborted();
|
|
return [reading('A', 5)];
|
|
},
|
|
});
|
|
const pending = layer.update();
|
|
layer.disable();
|
|
const publishedBefore = events.length;
|
|
release();
|
|
assert.equal(await pending, false, 'a late response must not be applied');
|
|
assert.equal(
|
|
events.length,
|
|
publishedBefore,
|
|
'no overlay write after disable',
|
|
);
|
|
assert.equal(layer.getStats().count, 0);
|
|
});
|
|
|
|
test('disable aborts the request in flight', async () => {
|
|
let seenSignal = null;
|
|
const { layer } = harness({
|
|
getSnapshot: async ({ signal } = {}) => {
|
|
seenSignal = signal;
|
|
await new Promise((resolve) => setTimeout(resolve, 40));
|
|
signal?.throwIfAborted();
|
|
return [];
|
|
},
|
|
});
|
|
const pending = layer.update();
|
|
layer.disable();
|
|
await pending;
|
|
assert.ok(seenSignal, 'a signal was passed through to the source');
|
|
assert.equal(seenSignal.aborted, true);
|
|
});
|
|
|
|
test('a superseded refresh does not clobber the newer one', async () => {
|
|
let call = 0;
|
|
const { layer } = harness({
|
|
getSnapshot: async ({ signal } = {}) => {
|
|
call += 1;
|
|
const mine = call;
|
|
await new Promise((resolve) => setTimeout(resolve, mine === 1 ? 40 : 1));
|
|
signal?.throwIfAborted();
|
|
return [reading(`call-${mine}`, mine)];
|
|
},
|
|
});
|
|
const first = layer.update();
|
|
const second = layer.update();
|
|
const [firstResult, secondResult] = await Promise.all([first, second]);
|
|
assert.equal(firstResult, false, 'the superseded call reports no update');
|
|
assert.equal(secondResult, true);
|
|
assert.equal(layer.getAnalystRecords()[0].name, 'call-2');
|
|
});
|
|
|
|
test('update is a no-op while the layer is disabled', async () => {
|
|
const { layer } = harness(staticSource([reading('A', 2)]));
|
|
layer.disable();
|
|
assert.equal(await layer.update(), false);
|
|
assert.equal(layer.getStats().count, 0);
|
|
});
|
|
|
|
// ── readout descriptor ─────────────────────────────────────────────────────
|
|
|
|
test('the readout descriptor carries the band legend and coverage', async () => {
|
|
const { layer } = harness(staticSource([reading('A', 2), reading('B', 9)]));
|
|
await layer.update();
|
|
const controls = layer.getRowControls();
|
|
assert.equal(controls.readout, true);
|
|
assert.match(controls.summary.coverage, /Canada only/);
|
|
// The Weather panel's legend shape: one {color, label} per band.
|
|
assert.equal(controls.legendCategorical, true);
|
|
assert.deepEqual(
|
|
controls.legend.map((entry) => entry.label),
|
|
['Low', 'Moderate', 'High', 'Very High'],
|
|
);
|
|
assert.ok(
|
|
controls.legend.every((entry) => /^#[0-9a-f]{6}$/i.test(entry.color)),
|
|
);
|
|
// Worst first, and every item keyed.
|
|
assert.deepEqual(
|
|
controls.list.items.map((i) => i.text),
|
|
['B · Low health risk', 'A · Low health risk'],
|
|
);
|
|
assert.ok(controls.list.items.every((i) => i.id));
|
|
assert.match(controls.summary.status, /2 stations reporting/);
|
|
});
|
|
|
|
test('the readout reports an error rather than a silent empty card', async () => {
|
|
const { layer } = harness({
|
|
getSnapshot: async () => {
|
|
throw new Error('ECCC HTTP 503');
|
|
},
|
|
});
|
|
await layer.update();
|
|
assert.match(layer.getRowControls().summary.status, /503/);
|
|
});
|
|
|
|
test('row-control listeners are notified on refresh and teardown-safe', async () => {
|
|
const { layer, viewer } = harness(staticSource([reading('A', 2)]));
|
|
let calls = 0;
|
|
layer.setRowControlsListener(() => {
|
|
calls += 1;
|
|
});
|
|
await layer.update();
|
|
assert.ok(calls >= 1, 'a refresh notifies the card');
|
|
layer.destroy(viewer);
|
|
const after = calls;
|
|
layer.setRowControlsListener(() => {
|
|
calls += 1;
|
|
});
|
|
assert.equal(calls, after);
|
|
});
|
|
|
|
// ── feed-state honesty ─────────────────────────────────────────────────────
|
|
|
|
test('freshness is judged on observation time, not on when we last fetched', async () => {
|
|
// lastUpdate stays current every time the network answers, even when it
|
|
// answers with nothing new. Judging freshness on it would let the HUD and
|
|
// voice describe hours-old air quality as live.
|
|
const aged = Date.now() - STALE_AFTER_MS - 60_000;
|
|
const { layer } = harness(
|
|
staticSource([reading('A', 2, { observedMs: aged })]),
|
|
);
|
|
await layer.update();
|
|
const stats = layer.getStats();
|
|
assert.ok(
|
|
stats.lastUpdate >= Date.now() - 5_000,
|
|
'we did just receive a response',
|
|
);
|
|
assert.equal(stats.observedAt, aged, 'but the observation itself is old');
|
|
assert.equal(stats.stale, true);
|
|
assert.equal(
|
|
layerFeedState(stats),
|
|
'stale',
|
|
'provenance must not read this as nominal',
|
|
);
|
|
});
|
|
|
|
test('a current reading reports nominal', async () => {
|
|
const { layer } = harness(
|
|
staticSource([reading('A', 2, { observedMs: Date.now() - 10 * 60_000 })]),
|
|
);
|
|
await layer.update();
|
|
const stats = layer.getStats();
|
|
assert.equal(stats.stale, false);
|
|
assert.equal(layerFeedState(stats), 'nominal');
|
|
});
|
|
|
|
test('a refresh that kept no current observation reports stale, not nominal', async () => {
|
|
// Every observation older than OBSERVATION_MAX_AGE_MS is dropped, so a feed
|
|
// that only has hours-old rows yields an empty snapshot. That is the
|
|
// stalest a feed can be; it must not read as a healthy empty layer.
|
|
const { layer } = harness(staticSource([]));
|
|
assert.equal(layer.getStats().stale, false, 'no refresh yet, no verdict');
|
|
await layer.update();
|
|
const stats = layer.getStats();
|
|
assert.equal(stats.count, 0);
|
|
assert.equal(stats.observedAt, null);
|
|
assert.equal(stats.stale, true);
|
|
assert.equal(layerFeedState(stats), 'stale');
|
|
assert.equal(layer.getRowControls().summary.status, 'No current readings');
|
|
});
|
|
|
|
test('a failed refresh over good readings reports degraded, not nominal', async () => {
|
|
let fail = false;
|
|
const { layer } = harness({
|
|
getSnapshot: async () => {
|
|
if (fail) throw new Error('ECCC HTTP 503');
|
|
return [reading('A', 2, { observedMs: Date.now() - 10 * 60_000 })];
|
|
},
|
|
});
|
|
await layer.update();
|
|
assert.equal(layerFeedState(layer.getStats()), 'nominal');
|
|
fail = true;
|
|
await layer.update();
|
|
const stats = layer.getStats();
|
|
assert.ok(stats.count > 0, 'the previous readings are still displayed');
|
|
assert.equal(
|
|
layerFeedState(stats),
|
|
'degraded',
|
|
'showing old data after a failure is degraded',
|
|
);
|
|
});
|