Follow through the camera port in the remaining tests

- The tests that faked a follow by setting the viewer's tracked entity,
  or by raising its change event, follow through the camera port
  (followEntity, or a port with follow and followChanged), and check
  followedOf: camera handoffs, globe rotation, tilt, ground guard,
  vessels, ALPR, CCTV, Contacts, radio, orientation, share links and
  follow offsets, the tracked readout and the shell's camera listeners.
- The fake Cesium camera port delivers followChanged from the port itself.
- A ride handed to another aircraft is adopted by the follow's change, as
  the application does, not by the test calling the adoption.
- The unbound-factory record lists the scene, the map defaults, share
  restoration and the World Packet as entries whose caller loads the
  factories; the map startup test stubs the default renderer load.
This commit is contained in:
Sameh Khamis committed 2026-11-10 18:10:14 -08:00
1 parent 9a13aeab48
commit 1fbe546531
21 files changed
+476 -148

No files matched your search

+31 -10
View File
@@ -17,7 +17,12 @@ import {
createCesiumQuery,
queryCapabilities,
} from '../render/cesium/query.js';
import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs';
import {
bindFakeCesiumCamera,
followEntity,
followedEntity,
} from '../testSupport/fakeCameraPort.mjs';
import { entityFollowTarget } from '../data/followed.js';
import { fakeSceneFrame } from '../testSupport/fakeCesiumScene.mjs';
import { entityRecord } from '../data/entityRecords.js';
@@ -139,7 +144,15 @@ function createRealViewer(target, { frames = true } = {}) {
// `camera.changed`, so the fixture has to carry one.
scene.preRender = new Cesium.Event();
camera.getPickRay = () => new Cesium.Ray();
return { viewer: withPorts({ camera, scene }, { frames }), target };
// Cesium's entity tracking, without entities of its own: the camera port
// follows on a tracking widget of the fake's.
const viewer = {
camera,
scene,
trackedEntityChanged: new Cesium.Event(),
entities: {},
};
return { viewer: withPorts(viewer, { frames }), target };
}
/** Place a real camera in an orbit around `target` and release the frame. */
@@ -556,7 +569,7 @@ for (const [label, altitude, range] of [
trackingReferenceFrame: Cesium.TrackingReferenceFrame.ENU,
});
entityRecord(entity).displayPosition = () => target;
viewer.trackedEntity = entity;
followEntity(viewer, entity);
const follow = new Cesium.EntityView(entity, viewer.scene);
follow.update(viewer.clock.currentTime);
viewer.camera.lookAt(
@@ -585,7 +598,7 @@ for (const [label, altitude, range] of [
follow.update(viewer.clock.currentTime);
const transform = Cesium.Matrix4.clone(viewer.camera.transform);
viewer.scene.preUpdate.raiseEvent();
assert.equal(viewer.trackedEntity, entity);
assert.equal(followedEntity(viewer), entity);
assert.ok(
Cesium.Matrix4.equalsEpsilon(
viewer.camera.transform,
@@ -605,7 +618,7 @@ for (const [label, altitude, range] of [
}
target = Cesium.Cartesian3.fromDegrees(-96, 31, altitude);
follow.update(viewer.clock.currentTime);
assert.equal(viewer.trackedEntity, entity);
assert.equal(followedEntity(viewer), entity);
assert.ok(Math.abs(readCameraTargetFrame(viewer).range - range) < 1e-5);
});
}
@@ -619,10 +632,10 @@ test('orientation animation cancels on replacement and explicit cancellation', (
animator.animate(frame, { heading: 0, pitch: STRAIGHT_DOWN_PITCH });
animator.animate(frame, { heading: 0, pitch: OBLIQUE_PITCH });
assert.equal(viewer.scene.preUpdate.numberOfListeners, 1);
viewer.trackedEntity = {};
followEntity(viewer, {});
viewer.scene.preUpdate.raiseEvent();
assert.equal(viewer.scene.preUpdate.numberOfListeners, 0);
viewer.trackedEntity = undefined;
followEntity(viewer, null);
animator.animate(frame, { heading: 0, pitch: STRAIGHT_DOWN_PITCH });
animator.cancel();
assert.equal(viewer.scene.preUpdate.numberOfListeners, 0);
@@ -631,9 +644,17 @@ test('orientation animation cancels on replacement and explicit cancellation', (
test('orientation navigation preserves follow and selection; new navigation cancels animation', () => {
const calls = [];
const entity = {};
const viewer = { trackedEntity: entity };
const viewer = {};
bindRenderer(viewer, {
camera: { cancelFlight: () => calls.push('cancel-flight') },
camera: {
cancelFlight: () => calls.push('cancel-flight'),
// The camera follows `entity`.
followed: entityFollowTarget(entity, {}),
follow(target) {
this.followed = target;
},
on: () => () => {},
},
});
let cockpit = false;
const navigation = new NavigationController({
@@ -651,7 +672,7 @@ test('orientation navigation preserves follow and selection; new navigation canc
navigation.runOrientation('camera', () => 'animated'),
'animated',
);
assert.equal(viewer.trackedEntity, entity);
assert.equal(followedEntity(viewer), entity);
assert.deepEqual(calls, [
'cancel-orientation',
'interrupt-motion',
+1 -1
View File
@@ -509,8 +509,8 @@ test('a ride handed to an aircraft on a layer Contacts turned on keeps that laye
await settle();
assert.equal(cockpit.enter(), true);
// NEXT hands the view to a military aircraft; Contacts turned its layer on.
// The follow's change adopts it as the ridden aircraft.
retrack({ layerId: 'military', info: MILITARY });
assert.equal(cockpit._adoptTrackedEntity(performance.now()), true);
assert.equal(cockpit.readSubjectLayerId(), 'military');
await manager.setEnabled('ais-live-vessels', false, { origin: 'user' });
await settle();
+3 -3
View File
@@ -15,8 +15,8 @@ import * as Cesium from 'cesium';
import {
bindFakeCesiumCamera,
followEntity,
followedEntity,
} from '../testSupport/fakeCameraPort.mjs';
import { followedOf } from '../data/followed.js';
import { fakeScenePorts } from '../testSupport/fakeCesiumScene.mjs';
import {
FAKE_EVENT_TYPES,
@@ -640,7 +640,7 @@ test('a double-click on a site focuses it and keeps the orbital view', async (t)
const { cockpit, viewer, handler } = scene;
scene.doubleClick(SITES.alpha.at);
assert.equal(handler.stockDoubleClicks, 0, 'Cesium does not track the site');
assert.equal(followedEntity(viewer), undefined);
assert.equal(followedOf(viewer), null);
scene.frame();
assert.equal(cockpit.active, true, 'still in orbit');
assert.equal(cockpit.orbital.focus.key, SITES.alpha.id);
@@ -726,7 +726,7 @@ test('a satellite row rides it, staying in orbit; RETURN goes back', async (t) =
assert.equal(newTrack.length, 1);
assert.ok(newTrack[0].timestamp > firstTrack.timestamp, 'the new ride’s own time');
assert.deepEqual(satellites.calls.slice(-2), [['track', 49260], ['subject', 49260]]);
assert.equal(followedEntity(viewer), undefined, 'the Cockpit keeps the camera');
assert.equal(followedOf(viewer), null, 'the Cockpit keeps the camera');
assert.equal(cockpit.subjectId, 49260);
assert.equal(cockpit.orbital.focus, null, 'the focus is reset');
assert.equal(modeEvents.at(-1)?.active, true);
+2 -1
View File
@@ -29,6 +29,7 @@ import {
followEntity,
followedEntity,
} from '../testSupport/fakeCameraPort.mjs';
import { followedOf } from '../data/followed.js';
import { fakeScenePorts } from '../testSupport/fakeCesiumScene.mjs';
import { fakeInput } from '../input/testDoubles.mjs';
@@ -412,7 +413,7 @@ test('entering takes the camera and shows only the orbital HUD', async (t) => {
assert.equal(cockpit.subjectKind, 'satellite');
assert.equal(cockpit.subjectId, ISS.noradId);
assert.deepEqual(satellites.calls, [['subject', ISS.noradId]]);
assert.equal(followedEntity(viewer), undefined);
assert.equal(followedOf(viewer), null);
assert.equal(viewer.scene.screenSpaceCameraController.enableInputs, false);
assert.ok(document.body.classList.contains('cockpit-mode'));
assert.ok(document.body.classList.contains('cockpit-orbit'));
+31 -6
View File
@@ -7,9 +7,9 @@ import * as Cesium from 'cesium';
import { applyShareFollowOffset, placeFollowEye } from './shareFollow.js';
import { followOffsetToEnu } from '../share/viewCodec.js';
import { withEntityRecord } from '../data/entityRecords.js';
import { followedOf } from '../data/followed.js';
import { entityFollowTarget, followedOf } from '../data/followed.js';
import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs';
import { findCamera } from '../render/rendererOf.js';
import { findCamera, rendererOf } from '../render/rendererOf.js';
function realCamera() {
const scene = {
@@ -58,10 +58,6 @@ function following(start = START) {
camera,
scene,
clock: { currentTime: Cesium.JulianDate.now() },
trackedEntity: withEntityRecord(
{},
{ trackedId: 'satellites:25544', displayPosition: () => TARGET },
),
};
bindFakeCesiumCamera(viewer, {
frame: {
@@ -69,6 +65,35 @@ function following(start = START) {
phase === 'preUpdate' ? scene.preUpdate.addEventListener(cb) : () => {},
},
});
// The port follows the satellite, already framed in its orbit frame; its
// follow offset is the eye in that frame, as the Cesium follow's is.
const renderer = rendererOf(viewer);
const port = renderer.camera;
const followed = entityFollowTarget(
withEntityRecord(
{},
{ trackedId: 'satellites:25544', displayPosition: () => TARGET },
),
{ position: () => TARGET, frame: 'autodetect' },
);
renderer.camera = Object.create(port, {
followed: { get: () => followed },
followFramed: { get: () => true },
followOffset: {
value: Object.freeze({
get() {
const p = camera.position;
return { x: p.x, y: p.y, z: p.z };
},
set(offset) {
camera.lookAtTransform(
camera.transform,
new Cesium.Cartesian3(offset.x, offset.y, offset.z),
);
},
}),
},
});
return viewer;
}
+73 -9
View File
@@ -20,7 +20,8 @@ import {
} from '../share/viewCodec.js';
import { entityRecord, withEntityRecord } from '../data/entityRecords.js';
import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs';
import { followedOf } from '../data/followed.js';
import { entityFollowTarget, followedOf } from '../data/followed.js';
import { rendererOf } from '../render/rendererOf.js';
const NOW_MS = Date.now();
const FIX = { lat: 37.6213, lon: -122.379, alt: 3200 };
@@ -131,6 +132,58 @@ const preUpdateFrame = (viewer) => ({
: () => {},
});
/**
* Give `viewer`'s bound camera port a follow, as the Cesium renderer's is:
* `follow(target)` sets `followed` and raises `followChanged`; a follow is
* framed at once, and its offset is the fake camera's eye in its frame, set
* by `lookAtTransform` in the frame it is in. Returns the port.
*/
function followingPort(viewer) {
const renderer = rendererOf(viewer);
const port = renderer.camera;
const listeners = new Set();
let followed = null;
renderer.camera = Object.create(port, {
followed: { get: () => followed },
followFramed: { get: () => followed !== null },
follow: {
value(target) {
followed = target;
for (const cb of [...listeners]) cb();
},
},
followOffset: {
value: Object.freeze({
get() {
const p = followed ? viewer.camera.position : null;
return p ? { x: p.x, y: p.y, z: p.z } : null;
},
set(offset) {
if (!followed) return;
viewer.camera.lookAtTransform(
viewer.camera.transform,
new Cesium.Cartesian3(offset.x, offset.y, offset.z),
);
},
}),
},
on: {
value(event, cb, options) {
if (event !== 'followChanged') return port.on(event, cb, options);
listeners.add(cb);
return () => listeners.delete(cb);
},
},
});
return renderer.camera;
}
/** Follow `entity` (an entity holder) through `viewer`'s camera port. */
const followIn = (viewer, entity) =>
rendererOf(viewer).camera.follow(
entity ? entityFollowTarget(entity, {}) : null,
);
/**
* A Cesium-shaped camera in `transform` with its eye at `position` there:
* a view set on it (a world pose) lands in that frame, and each eye it is
@@ -159,6 +212,13 @@ function cameraInFrame(transform, position, looks) {
looks.push(local);
this.position = local;
},
// As the follow moves the eye: an eye in the frame.
lookAtTransform(frame, offset) {
this.transform = frame;
const local = Cesium.Cartesian3.clone(offset);
looks.push(local);
this.position = local;
},
};
}
@@ -170,6 +230,7 @@ function openPage(t, href, { managerOptions = {}, tracking = null } = {}) {
const restores = [];
// Bound first with a frame port: the share follow runs on its preUpdate.
bindFakeCesiumCamera(viewer, { frame: preUpdateFrame(viewer) });
followingPort(viewer);
const manager = new ShareLinkManager(bindFakeCesiumCamera(viewer), {
onRestore: async (state) => restores.push(state),
isNavigationCurrent: () => true,
@@ -289,7 +350,7 @@ test('a thing view turns its layer on, follows by id and frames the shared offse
const page = openPage(t, `http://localhost/share/${aircraftCode()}`, {
tracking: (owner) => {
// What the coordinator does when the aircraft is in the feed.
owner.viewer.trackedEntity = entity;
followIn(owner.viewer, entity);
owner._handleShareTrackingRestoreStatus({
layerId: 'flights',
targetId: 'a1b2c3',
@@ -498,11 +559,13 @@ test('the followed entity is read in the share format, and its offset round-trip
const viewer = bindFakeCesiumCamera({
camera: { positionWC: cameraPosition },
clock: { currentTime: Cesium.JulianDate.now() },
trackedEntity: withEntityRecord(
{},
{ trackedId: 'military:ae1234', displayPosition: () => target },
),
});
followingPort(viewer);
const entity = withEntityRecord(
{},
{ trackedId: 'military:ae1234', displayPosition: () => target },
);
followIn(viewer, entity);
const subject = readShareFollowSubject({ viewer, nowMs: NOW_MS });
assert.equal(subject.kind, 'military_aircraft');
assert.equal(subject.id, 'ae1234');
@@ -510,10 +573,10 @@ test('the followed entity is read in the share format, and its offset round-trip
assert.ok(Math.abs(subject.lastKnown.lat - FIX.lat) < 1e-9);
assert.ok(Math.abs(subject.lastKnown.alt - FIX.alt) < 1e-3);
// A followed entity the share format has no kind for is not a thing view.
entityRecord(viewer.trackedEntity).trackedId = 'local-adsb:abc';
entityRecord(entity).trackedId = 'local-adsb:abc';
assert.equal(readShareFollowSubject({ viewer }), null);
// Nor is a camera that follows nothing (a selected vessel is not followed).
viewer.trackedEntity = undefined;
followIn(viewer, null);
assert.equal(readShareFollowSubject({ viewer }), null);
});
@@ -532,7 +595,6 @@ test('a shared offset on the far side swings round, never jumping through the en
},
},
},
trackedEntity: entity,
camera: cameraInFrame(
Cesium.Transforms.eastNorthUpToFixedFrame(target),
new Cesium.Cartesian3(0, -4800, 3300),
@@ -540,6 +602,8 @@ test('a shared offset on the far side swings round, never jumping through the en
),
};
bindFakeCesiumCamera(viewer, { frame: preUpdateFrame(viewer) });
followingPort(viewer);
followIn(viewer, entity);
// Looking south at the nose: the opposite side of the app's framing.
const offset = { range: 5800, heading: 180, pitch: -35 };
applyShareFollowOffset(viewer, followedOf(viewer), offset);
+8 -3
View File
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
import test from 'node:test';
import { LocationNavigation } from './locationNavigation.js';
import { LayerBindings } from './layerBindings.js';
import { bindRenderer } from '../render/rendererOf.js';
function replaceWindow(t, value) {
const previous = Object.getOwnPropertyDescriptor(globalThis, 'window');
@@ -128,12 +129,16 @@ test('camera-entry listeners are removed before any asynchronous layer cleanup',
const { owner } = bindings(t);
let changes = 0;
let trackedCallback;
owner.viewer = { trackedEntityChanged: {
addEventListener(callback) {
// What the camera follows changes through its port's followChanged.
owner.viewer = {};
bindRenderer(owner.viewer, { camera: {
followed: null,
on(event, callback) {
if (event !== 'followChanged') return () => {};
trackedCallback = callback;
return () => { changes += 1; };
},
} };
} });
owner._stampNavigation = () => { changes += 100; };
owner.observeCamera();
owner.stop();