refactor: separate earthquake source and display ownership

This commit is contained in:
Sameh Khamis committed 2026-09-13 12:14:44 -07:00
1 parent 965a44860f
commit d1d938babe
11 files changed
+540 -339

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createEarthquakesLayer } from './index.js';
import { createUsgsEarthquakeSource } from './source.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 = createEarthquakesLayer({
source,
overlayHost: {
setEntries(...args) {
events.push(args);
},
setVisible() {},
clearSource() {},
},
});
layer.init(viewer);
layer.enable(viewer);
return { layer, viewer, sources, events };
}
const row = {
stableId: 'event-a',
usgsId: 'event-a',
lon: 30,
lat: 20,
depthKm: 3,
mag: 4,
place: 'Fixture',
time: 1000,
};
test('late refresh cannot publish after disable, re-enable, or destroy', async () => {
for (const action of ['disable', 'destroy']) {
let resolve, signal;
const h = harness({
getSnapshot(options) {
signal = options.signal;
return new Promise((done) => {
resolve = done;
});
},
});
const pending = h.layer.update(h.viewer);
h.layer[action](h.viewer);
assert.equal(signal.aborted, true);
if (action === 'disable') h.layer.enable(h.viewer);
resolve([row]);
assert.equal(await pending, false);
assert.equal(h.layer.getStats().count, 0);
assert.equal(h.events.length, 0);
h.layer.destroy(h.viewer);
}
});
test('two displays own separate data sources and destruction', async () => {
const a = harness({ getSnapshot: async () => [row] });
const b = harness({ getSnapshot: async () => [] });
await a.layer.update(a.viewer);
await b.layer.update(b.viewer);
assert.equal(a.layer.getStats().count, 1);
assert.equal(b.layer.getStats().count, 0);
a.layer.destroy();
assert.equal(a.sources.length, 0);
assert.equal(b.sources.length, 1);
b.layer.destroy();
});
test('USGS body completion honors cancellation even with an uncooperative transport', async () => {
const abort = new AbortController();
const source = createUsgsEarthquakeSource({
fetchImpl: async () => ({
ok: true,
json: async () => {
abort.abort();
return { features: [] };
},
}),
});
await assert.rejects(source.getSnapshot({ signal: abort.signal }), {
name: 'AbortError',
});
});