Files
gods-eye-view/src/layers/cctv/lifecycle.test.mjs
T
German Khokhlov 2a761aa5eb Merge origin/main into german/navara
Main's work since the last merge runs on the globe contract where this
branch draws through it:

- Satellites take main's classes, sensors, ground-track bearings and
  world time through contract points and rings; launches take the world
  timeline. The satellite's cockpit subject dims, copies positions and
  refocuses through the contract.
- Flights, military and vessels keep main's photos, retry cool-downs,
  world-clock motion and the ship bridge. Trail seeding in the shared
  aircraft tracking reads each layer's clock.
- Traffic's flow lines and single build draw as ground sets; transit
  keeps dated credits and the new cities; CCTV opens the nearest camera,
  and the Quebec overlay projects and tests the horizon through the
  contract. Bikeshare's manager cleanup moved into its current lifecycle.
- Draw keeps main's mark actions across engine switches.
- The world clock in src/time is plain numbers again: Julian dates come
  from src/geo, the frame loop is injected, and the binding to Cesium's
  viewer clock lives in src/globe/cesium/worldTime.js. Navara's sky and
  sun follow the world time; live feeds keep the real time.
- The cockpit's orbital view, with its sensor zoom, ground focus and
  satellite switching, stays Cesium-only for now: Navara hides its entry.
  Its Cesium calls live in src/globe/cesium/orbital.js. The ship bridge's
  finest-detail waterline sampling is Cesium-only too; Navara samples
  loaded geometry and detailed terrain. Both are in KNOWN-ISSUES.
- Main's tests that built a Cesium scene by hand run on the fake engine.
- The boundary manifest is regenerated, the coupling baseline records
  the orbital and bridge code, and the main-chunk baseline is raised to
  the merged size: main itself grew its main chunk by 93,959 bytes
  (gzip -9) since the previous merge base.
2026-11-10 13:52:52 +04:00

154 lines
4.8 KiB
JavaScript

import assert from 'node:assert/strict';
import { test } from 'node:test';
import { createLifecycle } from './lifecycle.js';
import { createState } from './state.js';
import { createNavigation } from './navigation.js';
import { createFakeEngine } from '../../globe/testing/fakeEngine.js';
function setup(t, { failMarkers = false } = {}) {
const noop = () => {};
const services = {
overlays: {
clearSource: noop,
hitTest: noop,
setEntries: noop,
setVisible: noop,
},
activation: {},
picking: { unregisterPickOwner: noop },
focus: {},
render: { releaseContinuousRender: noop },
};
const state = createState({ services });
const pending = [];
const renderers = [];
const parts = {
geometryQueue: { stopGeometryLoadQueue: noop },
cards: { teardownAmbientCards: noop, handleVisibilityChange: noop },
projection: { clearProjectionOverlay: noop, stopProjectionLoop: noop },
geometry: { destroyCoverageEntities: noop },
calibration: { loadCalibrationStore: () => new Map() },
catalog: {
loadCameraSources: () => new Promise((resolve) => pending.push(resolve)),
},
};
const rendererFactory = () => {
const renderer = {
owner: null,
released: false,
releases: 0,
hideCredit() {},
attach(owner) {
this.owner = owner;
},
createMarkers() {
if (failMarkers) throw new Error('marker allocation failed');
},
destroy() {
if (!this.released) {
this.released = true;
this.releases++;
}
},
};
renderers.push(renderer);
return renderer;
};
const layer = createLifecycle({
state,
services,
parts,
rendererFactory,
}).methods;
t.after(() => layer.destroy());
return { state, pending, parts, layer, renderers };
}
test('repeated initialization rejects without replacing the current CCTV owner', async (t) => {
const h = setup(t);
const owner = {};
const starting = h.layer.init(owner);
const stopped = assert.rejects(starting, { name: 'AbortError' });
await assert.rejects(h.layer.init({}), /already initialized/);
assert.equal(h.renderers.length, 1);
assert.equal(h.parts.renderer.owner, owner);
assert.equal(h.parts.renderer.released, false);
h.layer.destroy({});
h.pending[0]([]);
await stopped;
assert.equal(h.renderers[0].releases, 1);
});
test('a retired init rejection cannot destroy the newer CCTV owner', async (t) => {
const h = setup(t);
const first = assert.rejects(h.layer.init({}), { name: 'AbortError' });
h.layer.destroy();
const owner = {};
const second = assert.rejects(h.layer.init(owner), { name: 'AbortError' });
const current = h.parts.renderer;
h.pending[0]([]);
await first;
assert.equal(h.parts.renderer, current);
assert.equal(current.owner, owner);
assert.equal(current.released, false);
assert.equal(h.state._sourceAbort.signal.aborted, false);
h.layer.destroy();
h.pending[1]([]);
await second;
assert.deepEqual(
h.renderers.map((renderer) => renderer.releases),
[1, 1],
);
});
test('initial resource failure disposes its owner and leaves initialization retryable', async (t) => {
const h = setup(t, { failMarkers: true });
for (let i = 0; i < 2; i++) {
await assert.rejects(h.layer.init({}), /marker allocation failed/);
assert.equal(h.parts.renderer, null);
assert.equal(h.state._sourceAbort.signal.aborted, true);
}
assert.deepEqual(
h.renderers.map((renderer) => renderer.releases),
[1, 1],
);
});
test('destroy releases focus-appearance callbacks even without prior disable', (t) => {
const h = setup(t);
let released = 0;
h.state._removeFocusAppearListener = () => released++;
h.layer.destroy();
h.layer.destroy();
assert.equal(released, 1);
});
test('enabling CCTV opens nearest to the engine camera and preserves a restored selection', (t) => {
const { state, parts, layer } = setup(t);
const engine = createFakeEngine({ latitude: 0, longitude: 1 });
t.after(() => engine.destroy());
state._records = [
{ camera: { id: 'far', lat: 0, lon: 0 } },
{ camera: { id: 'near', lat: 0, lon: 180 / Math.PI } },
];
parts.renderer = {
cameraCartographic: () => engine.camera.pose().cartographic,
destroy() {},
hideCredit() {},
};
parts.model = {
haversineKm: (_lat, lon, _otherLat, otherLon) => Math.abs(lon - otherLon),
};
parts.navigation = createNavigation({ state, parts });
layer.showRenderer = () => {};
layer.enable();
assert.equal(state._activeCameraId, 'near');
assert.equal(state._openingCameraProvisional, true);
state._activeCameraId = 'far';
state._openingCameraProvisional = false;
layer.enable();
assert.equal(state._activeCameraId, 'far');
assert.equal(state._openingCameraProvisional, false);
state._enabled = false;
});