Files
gods-eye-view/src/layers/flights/renderer.test.mjs
T
German Khokhlov 35e70ec280 Draw the aircraft family, Wildlife Sightings and the Cockpit through the contract
The layers that still had one Cesium and one Navara implementation, or
drew on Cesium only, now draw through engine.movers, engine.lines and
engine.camera on both engines, and application code holds the engine
instead of guessing it from a viewer.

- Aircraft family: flights, military and local ADS-B share one display
  (src/layers/aircraft/render/display.js). The Cesium and Navara
  renderers are gone. Where they differed, the display follows Cesium:
  - attach and visibility tracking, live disableDepthTestDistance
    changes, the tracked glyph's distance scale, model ids that change,
    local ADS-B markers hidden until shown and its UAT ring as a point;
  - the trail head's hidden alpha is 0.45, the contract's
    depthFailAlpha scaled by the head's 0.9.
  The tracked glyph stays over the fleet, as on Navara. The movers
  contract reports readiness per model and reads model axes as the
  glTF's own on every engine. Cesium's follow takes any native target
  through a proxy entity in the ENU frame and mirrors its readout fields.
- Wildlife Sightings draws badges and the density glow as sprite sets
  and its privacy cells as line outlines (no fill yet). It takes clicks
  from engine.input and its card anchors to the engine's canvas and
  camera, so the layer is in both engines' catalogs again and a switch
  keeps the session's layer set.
- Cockpit: cockpitHost takes the engine only. The ridden aircraft is the
  engine follow's target, hidden through its sprite; only the Cesium
  Viewer's stock double-click is still routed around.
- About forty camera, navigation, share-link, HUD, voice, detection and
  layer-lifecycle callers take the engine. isGlobeEngine and
  engineFromViewer calls outside src/globe fall from 51 to 14.

The coupling baseline drops accordingly: layer renderer lines from
5239/12331 to 4546/11106 (Cesium/Navara), `viewer` lines from 2424 to
2209, Cesium-importing files from 69 to 66.
2026-11-07 02:34:34 +04:00

296 lines
10 KiB
JavaScript

// The flights logic draws only through its renderer: with a renderer that
// is plain data (no Cesium), the layer builds its fleet, selects a contact
// on a click through the engine's selection dispatcher, tracks and follows
// it, and releases it on an empty click; then the same through its display
// on the fake engine and on the Cesium engine.
import test from 'node:test';
import assert from 'node:assert/strict';
import * as geo from '../../geo/index.js';
import { createFakeEngine } from '../../globe/testing/fakeEngine.js';
import { createApplicationFlights } from '../../app/layers/flights.js';
import { defaultSurface } from '../../data/surfaceServices.js';
import * as militaryRegistry from '../../data/militaryRegistry.js';
import { openSkySnapshot } from '../../sources/live/aircraft.js';
import { Rgba } from '../shared/color.js';
import { recordingAircraftRenderer as recordingRenderer } from '../../testSupport/aircraftRecordingRenderer.mjs';
globalThis.window ||= {};
globalThis.window.dispatchEvent ||= () => true;
globalThis.window.addEventListener ||= () => {};
globalThis.window.removeEventListener ||= () => {};
globalThis.CustomEvent ||= class CustomEvent extends Event {
constructor(type, init) {
super(type);
this.detail = init?.detail;
}
};
globalThis.document ||= {
body: { classList: { contains: () => false } },
addEventListener() {},
removeEventListener() {},
};
function snapshotOf(ids) {
const nowSec = Math.floor(Date.now() / 1000);
return openSkySnapshot({
time: nowSec,
states: ids.map((id, index) => [
id,
`TST${index} `,
'United States',
nowSec,
nowSec,
-97.6 + index * 0.1,
30.3,
10_668,
false,
250,
95,
5,
null,
10_700,
null,
null,
null,
5,
]),
});
}
test('the flights logic draws, selects and follows only through its renderer', async () => {
const engine = createFakeEngine();
let renderer = null;
const layer = createApplicationFlights({
surface: defaultSurface,
source: {
label: 'Fixture aircraft',
getSnapshot: async () => snapshotOf(['abc123', 'def456']),
},
militaryRegistry,
renderer: {
create: ({ viewer, engine: given }) => {
assert.equal(viewer, null, 'no viewer on an engine without one');
renderer = recordingRenderer(given);
return renderer;
},
},
});
layer.init(null, { engine });
layer.enable();
try {
await layer.update(null);
assert.equal(renderer.sprites.size, 2, 'one sprite per contact');
const [sprite] = renderer.sprites;
assert.ok(sprite.color instanceof Rgba, 'colours are engine-neutral');
assert.equal(sprite.id, 'abc123');
assert.equal(layer.getAllPositions().length, 2);
// A click on a contact: the engine's pick, through the dispatcher.
engine.fake.script('pick', () => ({ id: 'def456' }));
engine.fake.script('drillPick', () => [{ id: 'def456' }]);
const click = () => {
engine.fake.input('down', { position: { x: 10, y: 10 } });
engine.fake.input('up', { position: { x: 10, y: 10 } });
engine.fake.input('click', { position: { x: 10, y: 10 } });
};
click();
assert.equal(layer.getTrackedInfo()?.icao24, 'def456');
assert.deepEqual(
renderer.calls.filter(
([name]) => name.startsWith('tracked') || name.startsWith('follow'),
),
[
['tracked.create', 'tracked-1'],
['follow.start', 'tracked-1'],
],
);
// Clicking the tracked visual itself is a no-op.
engine.fake.script('pick', () => ({ id: 'tracked-1' }));
engine.fake.script('drillPick', () => [{ id: 'tracked-1' }]);
click();
assert.equal(layer.getTrackedInfo()?.icao24, 'def456');
// Empty space releases the follow and removes the tracked visual.
engine.fake.script('pick', () => undefined);
engine.fake.script('drillPick', () => []);
click();
assert.equal(layer.getTrackedInfo(), null);
assert.ok(
renderer.calls.some(([name]) => name === 'follow.release') &&
renderer.calls.some(
([name, id]) => name === 'tracked.remove' && id === 'tracked-1',
),
);
} finally {
layer.disable();
layer.destroy(null);
}
assert.ok(renderer.calls.some(([name]) => name === 'destroy'));
});
test('the flights logic draws and follows on the aircraft display (fake engine)', async () => {
const { createAircraftDisplay, aircraftAtlasLayout } =
await import('../aircraft/render/display.js');
const layout = aircraftAtlasLayout();
const engine = createFakeEngine();
let renderer = null;
const layer = createApplicationFlights({
surface: defaultSurface,
source: {
label: 'Fixture aircraft',
getSnapshot: async () => snapshotOf(['abc123', 'def456']),
},
militaryRegistry,
renderer: {
create: ({ engine: given }) =>
(renderer = createAircraftDisplay(given, {
layerId: 'flights',
order: 8,
headingOffsetDeg: 180,
drawAtlas: async () => ({
canvas: { width: layout.width, height: layout.height },
cells: layout.cells,
}),
})),
},
});
layer.init(null, { engine });
layer.enable();
try {
await renderer.whenReady();
await layer.update(null);
await Promise.resolve();
const sprites = engine.movers
.list()
.filter(({ id }) => id.startsWith('flights-sprites'));
assert.equal(sprites.length, 1);
assert.equal(sprites[0].count, 2);
const { store } = engine.fake.moverSet(sprites[0].id);
assert.deepEqual([...store.pickIds.slice(0, 2)].sort(), [
'abc123',
'def456',
]);
// What it draws, as the QA probe reads it (the same on every engine).
const drawn = layer.qaDrawn();
assert.deepEqual(drawn.map((row) => row.id).sort(), ['abc123', 'def456']);
assert.ok(drawn.every((row) => row.show && row.image.startsWith('data:')));
// A click on a contact selects it; the engine follows with its label.
engine.fake.script('pick', () => ({ id: 'def456' }));
engine.fake.script('drillPick', () => [{ id: 'def456' }]);
engine.fake.input('down', { position: { x: 10, y: 10 } });
engine.fake.input('up', { position: { x: 10, y: 10 } });
engine.fake.input('click', { position: { x: 10, y: 10 } });
assert.equal(layer.getTrackedInfo()?.icao24, 'def456');
const following = engine.camera.following();
assert.equal(following?.label?.id, 'flights:def456');
engine.fake.frame();
const tracked = engine.movers
.list()
.find(({ id }) => id.startsWith('flights-tracked'));
assert.equal(tracked.count, 1);
assert.equal(layer.getAllPositions().length, 2);
// A camera command ends the engine's follow; the contact stays selected
// (as on Cesium after a search flight), and tracking it again follows.
engine.camera.setView({
destination: geo.cartesian3.fromDegrees(-97.7, 30.2, 50_000),
});
assert.equal(engine.camera.following(), null);
assert.equal(layer.getTrackedInfo()?.icao24, 'def456');
assert.equal(layer.trackById('def456', { origin: 'voice' }), true);
assert.equal(engine.camera.following()?.label?.id, 'flights:def456');
assert.equal(layer.stopTracking(), true);
assert.equal(engine.camera.following(), null);
assert.equal(
engine.movers.list().find(({ id }) => id.startsWith('flights-tracked'))
.count,
0,
);
} finally {
layer.disable();
layer.destroy(null);
}
assert.equal(engine.movers.list().length, 0, 'no mover set left behind');
engine.destroy();
});
test('the flights logic draws, tracks and follows on the Cesium engine through the same display', async () => {
const { createCesiumMoversEngine } =
await import('../../testSupport/cesiumMoversEngine.mjs');
const { createFlightsDisplay } = await import('./render/display.js');
const { aircraftAtlasLayout } = await import('../aircraft/render/display.js');
const layout = aircraftAtlasLayout();
const { engine, viewer, frame, primitive } = createCesiumMoversEngine();
engine.camera.setView({
destination: geo.cartesian3.fromDegrees(-97.5, 30.3, 400_000),
});
let display = null;
const layer = createApplicationFlights({
surface: defaultSurface,
source: {
label: 'Fixture aircraft',
getSnapshot: async () => snapshotOf(['abc123', 'def456']),
},
militaryRegistry,
renderer: {
// The registry's factory, with the test's atlas.
create: ({ engine: given }) =>
(display = createFlightsDisplay(given, {
drawAtlas: async () => ({
canvas: { width: layout.width, height: layout.height },
cells: layout.cells,
}),
})),
},
});
layer.init(viewer, { engine });
layer.enable();
try {
await display.whenReady();
await layer.update(null);
await new Promise((resolve) => setTimeout(resolve, 0));
const [sprites] = engine.movers
.list()
.filter(({ id }) => id.startsWith('flights-sprites'));
assert.equal(sprites.count, 2);
const billboards = primitive(sprites.id);
assert.equal(billboards.length, 2, 'one Cesium billboard per contact');
assert.deepEqual([billboards.get(0).id, billboards.get(1).id].sort(), [
'abc123',
'def456',
]);
assert.deepEqual(
layer
.qaDrawn()
.map((row) => row.id)
.sort(),
['abc123', 'def456'],
);
// Tracking follows through Cesium's trackedEntity, with the readout.
assert.equal(layer.trackById('def456', { origin: 'voice' }), true);
const following = engine.camera.following();
assert.equal(following?.label?.id, 'flights:def456');
assert.equal(viewer.trackedEntity?.gevTrackedId, 'flights:def456');
frame();
await new Promise((resolve) => setTimeout(resolve, 0));
const tracked = engine.movers
.list()
.find(({ id }) => id.startsWith('flights-tracked'));
assert.equal(tracked.count, 1);
assert.equal(
primitive(tracked.id).get(0).disableDepthTestDistance,
Number.POSITIVE_INFINITY,
);
assert.equal(layer.stopTracking(), true);
assert.equal(engine.camera.following(), null);
assert.equal(viewer.trackedEntity, undefined);
} finally {
layer.disable();
layer.destroy(viewer);
}
assert.equal(engine.movers.list().length, 0, 'no mover set left behind');
assert.equal(engine.lines.list().length, 0, 'no line left behind');
engine.destroy();
});