mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 16:52:20 +00:00
Add the Data Library: live regional datasets, tiles and packs
DATA LAYERS keeps its curated core layers; regional and niche sources live in the Data Library, opened from Browse data library at the foot of the panel. Each entry is a JSON manifest in src/library/entries/ pointing at a live upstream (query API, provider-hosted file or tile endpoint), so nothing is bundled and a self-hosted instance hosts no data. Sources without CORS go through one allowlisted /api/library route. Kinds: dataset (GeoJSON, CSV, JSON records, ArcGIS, partitioned GeoParquet), tiles (XYZ, WMS/ArcGIS bbox export, terrarium relief) and pack. Selections persist and travel in share links as lib=, separate from the core layer tokens. Library rows get Zoom and Remove icon chips that work while the layer is off, rendered through RailChips. Strings are translated in every locale.
This commit is contained in:
1 parent
10a788db78
commit
db80d82b8d
182 files changed
+15805
-44
No files matched your search
@@ -0,0 +1,365 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { LayerLifecycle } from '../data/lifecycle.js';
|
||||
import { createDataLibrary } from './controller.js';
|
||||
import { LIBRARY_ENTRIES, LIBRARY_LEDGER } from './catalog.js';
|
||||
import { validateLibraryCatalog } from './manifest.js';
|
||||
import { LIBRARY_STORAGE_KEY } from './selection.js';
|
||||
|
||||
const base = {
|
||||
family: 'imagery',
|
||||
provider: 'Fixture',
|
||||
description: 'Fixture entry.',
|
||||
maintainer: 'test',
|
||||
coverage: { global: true },
|
||||
licence: { name: 'Public domain', url: 'https://example.org/licence' },
|
||||
attribution: { text: 'Fixture' },
|
||||
status: 'community',
|
||||
};
|
||||
const tiles = (id) => ({
|
||||
...base,
|
||||
id,
|
||||
kind: 'tiles',
|
||||
title: id,
|
||||
source: {
|
||||
format: 'xyz',
|
||||
url: 'https://tiles.example.org/{z}/{x}/{y}.png',
|
||||
fetch: 'browser-direct',
|
||||
},
|
||||
});
|
||||
const { entries } = validateLibraryCatalog([
|
||||
tiles('night-lights'),
|
||||
tiles('sea-marks'),
|
||||
{
|
||||
...base,
|
||||
id: 'fixture-pack',
|
||||
kind: 'pack',
|
||||
family: 'cameras',
|
||||
title: 'Pack',
|
||||
source: { format: 'host', url: 'https://cams.example.org/' },
|
||||
pack: { host: 'cctv', env: 'CCTV_FIXTURE_ENABLED' },
|
||||
licence: { ...base.licence, operatorOptIn: true },
|
||||
},
|
||||
]);
|
||||
const ledger = [
|
||||
['night-lights', '00'],
|
||||
['sea-marks', '01'],
|
||||
];
|
||||
|
||||
function sealedManager(t) {
|
||||
const manager = new LayerLifecycle({});
|
||||
manager.finalizeRegistrations([]);
|
||||
// Enabled layers arm refresh timers; release them with the test.
|
||||
t.after(() => manager.destroyAll());
|
||||
return manager;
|
||||
}
|
||||
|
||||
function memoryStorage(initial) {
|
||||
const store = new Map(initial ? [[LIBRARY_STORAGE_KEY, initial]] : []);
|
||||
return {
|
||||
store,
|
||||
getItem: (key) => store.get(key) ?? null,
|
||||
setItem: (key, value) => store.set(key, value),
|
||||
};
|
||||
}
|
||||
|
||||
const source = (availability) => ({
|
||||
async getAvailability() {
|
||||
return {
|
||||
server: true,
|
||||
availability: new Map(
|
||||
entries.map(({ id }) => [
|
||||
id,
|
||||
availability[id] ?? { available: true, reason: null },
|
||||
]),
|
||||
),
|
||||
};
|
||||
},
|
||||
async loadDataset() {
|
||||
return { features: [], truncated: false };
|
||||
},
|
||||
tileTemplate: (entry) => entry.source.url,
|
||||
});
|
||||
|
||||
function shareLinkDouble() {
|
||||
return {
|
||||
encoder: null,
|
||||
changes: 0,
|
||||
setLibraryParamsEncoder(encoder) {
|
||||
this.encoder = encoder;
|
||||
},
|
||||
onLibraryStateChange() {
|
||||
this.changes += 1;
|
||||
},
|
||||
encode() {
|
||||
const params = new URLSearchParams();
|
||||
this.encoder?.(params);
|
||||
return params.get('lib');
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('nothing registers until a viewer adds an entry; add turns it on and persists', async (t) => {
|
||||
const manager = sealedManager(t);
|
||||
const storage = memoryStorage();
|
||||
const shareLink = shareLinkDouble();
|
||||
const library = createDataLibrary({
|
||||
entries,
|
||||
ledger,
|
||||
dataManager: manager,
|
||||
services: {},
|
||||
storage,
|
||||
shareLink,
|
||||
initialHash: '',
|
||||
source: source({}),
|
||||
});
|
||||
await library.start();
|
||||
assert.equal(manager.layers.size, 0);
|
||||
assert.equal(await library.add('night-lights'), true);
|
||||
assert.equal(manager.isEnabled('lib-night-lights'), true);
|
||||
assert.equal(manager.getAll()[0].libraryEntryId, 'night-lights');
|
||||
assert.deepEqual(JSON.parse(storage.store.get(LIBRARY_STORAGE_KEY)), {
|
||||
added: ['night-lights'],
|
||||
on: ['night-lights'],
|
||||
});
|
||||
assert.equal(shareLink.encode(), '00');
|
||||
await manager.setEnabled('lib-night-lights', false, { origin: 'user' });
|
||||
assert.deepEqual(JSON.parse(storage.store.get(LIBRARY_STORAGE_KEY)).on, []);
|
||||
assert.equal(shareLink.encode(), null);
|
||||
assert.equal(await library.remove('night-lights'), true);
|
||||
assert.equal(manager.layers.has('lib-night-lights'), false);
|
||||
assert.deepEqual(JSON.parse(storage.store.get(LIBRARY_STORAGE_KEY)), {
|
||||
added: [],
|
||||
on: [],
|
||||
});
|
||||
});
|
||||
|
||||
test('packs and unavailable entries cannot be added', async (t) => {
|
||||
const manager = sealedManager(t);
|
||||
const library = createDataLibrary({
|
||||
entries,
|
||||
ledger,
|
||||
dataManager: manager,
|
||||
services: {},
|
||||
storage: memoryStorage(),
|
||||
initialHash: '',
|
||||
source: source({
|
||||
'sea-marks': { available: false, reason: 'operator-opt-in' },
|
||||
}),
|
||||
});
|
||||
await library.start();
|
||||
assert.equal(await library.add('fixture-pack'), false);
|
||||
assert.equal(await library.add('sea-marks'), false);
|
||||
assert.equal(manager.layers.size, 0);
|
||||
const row = library.list().find(({ entry }) => entry.id === 'sea-marks');
|
||||
assert.deepEqual(
|
||||
{ available: row.available, reason: row.reason, added: row.added },
|
||||
{ available: false, reason: 'operator-opt-in', added: false },
|
||||
);
|
||||
});
|
||||
|
||||
test('a stored selection restores; a non-explicit change is not remembered', async (t) => {
|
||||
const manager = sealedManager(t);
|
||||
const storage = memoryStorage(
|
||||
JSON.stringify({ added: ['night-lights', 'sea-marks'], on: ['sea-marks'] }),
|
||||
);
|
||||
const library = createDataLibrary({
|
||||
entries,
|
||||
ledger,
|
||||
dataManager: manager,
|
||||
services: {},
|
||||
storage,
|
||||
initialHash: '',
|
||||
source: source({}),
|
||||
});
|
||||
await library.start();
|
||||
await manager.waitForLayerSettled?.('lib-sea-marks');
|
||||
assert.equal(manager.layers.has('lib-night-lights'), true);
|
||||
assert.equal(manager.isEnabled('lib-night-lights'), false);
|
||||
assert.equal(manager.isEnabled('lib-sea-marks'), true);
|
||||
await manager.setEnabled('lib-sea-marks', false, { origin: 'context' });
|
||||
assert.deepEqual(JSON.parse(storage.store.get(LIBRARY_STORAGE_KEY)).on, [
|
||||
'sea-marks',
|
||||
]);
|
||||
});
|
||||
|
||||
test('a link decides what is on and adds its entries to the library', async (t) => {
|
||||
const manager = sealedManager(t);
|
||||
const storage = memoryStorage(
|
||||
JSON.stringify({ added: ['night-lights'], on: ['night-lights'] }),
|
||||
);
|
||||
const library = createDataLibrary({
|
||||
entries,
|
||||
ledger,
|
||||
dataManager: manager,
|
||||
services: {},
|
||||
storage,
|
||||
initialHash: '#lat=1&lon=2&lib=01.zz',
|
||||
source: source({}),
|
||||
});
|
||||
await library.start();
|
||||
await manager.waitForLayerSettled?.('lib-sea-marks');
|
||||
assert.equal(manager.isEnabled('lib-sea-marks'), true);
|
||||
assert.equal(manager.isEnabled('lib-night-lights'), false);
|
||||
assert.deepEqual(JSON.parse(storage.store.get(LIBRARY_STORAGE_KEY)), {
|
||||
added: ['night-lights', 'sea-marks'],
|
||||
on: ['sea-marks'],
|
||||
});
|
||||
});
|
||||
|
||||
test('zoom flies to an added layer through the caller, reset removes everything', async (t) => {
|
||||
const manager = sealedManager(t);
|
||||
const storage = memoryStorage();
|
||||
const flights = [];
|
||||
const library = createDataLibrary({
|
||||
entries,
|
||||
ledger,
|
||||
dataManager: manager,
|
||||
services: {},
|
||||
storage,
|
||||
initialHash: '',
|
||||
source: source({}),
|
||||
zoomTo: (extent) => {
|
||||
flights.push(extent);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
await library.start();
|
||||
assert.equal(await library.zoom('night-lights'), false, 'not added yet');
|
||||
await library.add('night-lights');
|
||||
await library.add('sea-marks');
|
||||
const row = library.list().find(({ entry }) => entry.id === 'night-lights');
|
||||
assert.equal(row.canZoom, true);
|
||||
// Off: zoom switches the layer on, then flies.
|
||||
await manager.setEnabled('lib-night-lights', false, { origin: 'user' });
|
||||
assert.equal(await library.zoom('night-lights'), true);
|
||||
assert.equal(manager.isEnabled('lib-night-lights'), true);
|
||||
assert.deepEqual(flights[0], {
|
||||
west: -180,
|
||||
south: -85,
|
||||
east: 180,
|
||||
north: 85,
|
||||
});
|
||||
const chips = manager.layers
|
||||
.get('lib-night-lights')
|
||||
.module.getRowControls().chips;
|
||||
assert.equal(chips[0].id, 'zoom');
|
||||
chips[0].onClick();
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
assert.equal(flights.length, 2);
|
||||
assert.deepEqual(
|
||||
chips.map(({ id, icon, action, whileOff }) => [id, icon, action, whileOff]),
|
||||
[
|
||||
['zoom', 'zoom_in_map', true, true],
|
||||
['remove', 'delete', true, true],
|
||||
],
|
||||
);
|
||||
// The row's delete icon removes that entry from the library.
|
||||
chips[1].onClick();
|
||||
await manager.waitForLayerSettled?.('lib-night-lights');
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
for (
|
||||
let tries = 0;
|
||||
manager.layers.has('lib-night-lights') && tries < 50;
|
||||
tries++
|
||||
)
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
assert.equal(manager.layers.has('lib-night-lights'), false);
|
||||
assert.deepEqual(JSON.parse(storage.store.get(LIBRARY_STORAGE_KEY)).added, [
|
||||
'sea-marks',
|
||||
]);
|
||||
assert.equal(await library.reset(), 1);
|
||||
assert.equal(manager.layers.size, 0);
|
||||
assert.deepEqual(JSON.parse(storage.store.get(LIBRARY_STORAGE_KEY)), {
|
||||
added: [],
|
||||
on: [],
|
||||
});
|
||||
});
|
||||
|
||||
test('without a navigation owner the zoom control is disabled', async (t) => {
|
||||
const manager = sealedManager(t);
|
||||
const library = createDataLibrary({
|
||||
entries,
|
||||
ledger,
|
||||
dataManager: manager,
|
||||
services: {},
|
||||
storage: memoryStorage(),
|
||||
initialHash: '',
|
||||
source: source({}),
|
||||
});
|
||||
await library.start();
|
||||
await library.add('night-lights');
|
||||
assert.equal(await library.zoom('night-lights'), false);
|
||||
assert.deepEqual(
|
||||
manager.layers
|
||||
.get('lib-night-lights')
|
||||
.module.getRowControls()
|
||||
.chips.map(({ id }) => id),
|
||||
['remove'],
|
||||
);
|
||||
});
|
||||
|
||||
test('the summary counts entries by kind, availability and what is added and on', async (t) => {
|
||||
const manager = sealedManager(t);
|
||||
const library = createDataLibrary({
|
||||
entries,
|
||||
ledger,
|
||||
dataManager: manager,
|
||||
services: {},
|
||||
storage: memoryStorage(),
|
||||
initialHash: '',
|
||||
source: source({
|
||||
'sea-marks': { available: false, reason: 'operator-opt-in' },
|
||||
}),
|
||||
});
|
||||
await library.start();
|
||||
await library.add('night-lights');
|
||||
assert.deepEqual(library.summary(), {
|
||||
entries: 3,
|
||||
kinds: { dataset: 0, tiles: 2, pack: 1 },
|
||||
themes: ['cameras', 'imagery'],
|
||||
// The pack is available because the fixture source says so.
|
||||
available: 2,
|
||||
unavailable: 1,
|
||||
added: 1,
|
||||
on: 1,
|
||||
// Tiles are drapes: no feature count.
|
||||
features: 0,
|
||||
featureLayers: 0,
|
||||
});
|
||||
});
|
||||
|
||||
test('every shipped entry kind registers, view-bounded datasets included', async (t) => {
|
||||
const manager = sealedManager(t);
|
||||
const library = createDataLibrary({
|
||||
entries: LIBRARY_ENTRIES,
|
||||
ledger: LIBRARY_LEDGER,
|
||||
dataManager: manager,
|
||||
services: {},
|
||||
storage: memoryStorage(),
|
||||
initialHash: '',
|
||||
source: {
|
||||
async getAvailability() {
|
||||
return {
|
||||
server: true,
|
||||
availability: new Map(
|
||||
LIBRARY_ENTRIES.map(({ id }) => [
|
||||
id,
|
||||
{ available: true, reason: null },
|
||||
]),
|
||||
),
|
||||
};
|
||||
},
|
||||
async loadDataset() {
|
||||
return { features: [], truncated: false };
|
||||
},
|
||||
tileTemplate: (entry) => entry.source.url,
|
||||
},
|
||||
});
|
||||
await library.start();
|
||||
assert.equal(await library.add('fema-usa-structures'), true);
|
||||
const module = manager.layers.get('lib-fema-usa-structures').module;
|
||||
// No camera to read: the layer waits for one rather than loading.
|
||||
assert.equal(module.getDiagnostics().tooHigh, true);
|
||||
assert.equal(await library.add('nasa-black-marble'), true);
|
||||
});
|
||||
Reference in new issue
Block a user