Files
gods-eye-view/src/globe/cesium/testing/fakeViewer.mjs
T
German Khokhlov 7f478e3815 Remove always-true layer availability and leftover viewer paths
Every layer draws on both engines, so the per-engine availability
machinery always answered yes. The renderer manifest is now the id to
display map plus the wind flow backend; the catalog filter, the voice and
QA-probe refusals and the engine-kind checks go with it. The Navara
notice lists the same layers in the same words.

Paths that read the Cesium viewer behind an engine that is always there
are gone: the Recent Imagery viewer fallbacks, the shell's imagery-host
service (one shared host module remains), the engineless map controller,
viewer gates in voice actions, world packets and camera verbs, the
orientation controls' tracked-entity fallback, and the Bhote Koshi and
FIRMS Cesium-shaped paths. setCameraInputs, which nothing called, leaves
the contract; suspensions remain.

Smaller duplicates go too: one tracker-release helper for navigation and
location, the no-op frame latch, missing-engine guards, one name for the
injected display, flat bikeshare and installations displays, one overlay
host and binder, one name per world-overlay method, and stub-viewer
guards in the Cesium engine. Layers that still read the viewer keep the
shared init(viewer, context) entry.

Behaviour is unchanged on both engines. Viewer lines fall from 849 to
750 and engine-name literals from 32 to 27.
2026-11-08 17:06:01 +04:00

267 lines
8.6 KiB
JavaScript

// A recording stand-in for a Cesium Viewer, for the Cesium engine's unit tests.
// It carries real Cesium value types and events (no WebGL) and records every
// call into `viewer.calls` so tests can check what reached which Cesium member.
import * as Cesium from 'cesium';
/** A fake viewer whose members record their calls. */
export function createFakeViewer({ width = 1024, height = 768 } = {}) {
const calls = [];
const record =
(name, value) =>
(...args) => {
calls.push({ name, args });
return typeof value === 'function' ? value(...args) : value;
};
const primitiveList = [];
const entityList = [];
let trackedEntity;
const imageryList = [];
const staticCredits = [];
const creditDisplay = {
addStaticCredit: record('creditDisplay.addStaticCredit', (credit) =>
staticCredits.push(credit),
),
removeStaticCredit: record('creditDisplay.removeStaticCredit', (credit) => {
const index = staticCredits.indexOf(credit);
if (index >= 0) staticCredits.splice(index, 1);
}),
};
const fovy = 2 * Math.atan(Math.tan(Math.PI / 6) * (height / width));
const camera = {
positionWC: new Cesium.Cartesian3(7e6, 1e5, 2e5),
// Position and basis in the camera's transform (identity: world).
position: new Cesium.Cartesian3(7e6, 1e5, 2e5),
direction: new Cesium.Cartesian3(-1, 0, 0),
up: new Cesium.Cartesian3(0, 0, 1),
right: new Cesium.Cartesian3(0, 1, 0),
directionWC: new Cesium.Cartesian3(-1, 0, 0),
upWC: new Cesium.Cartesian3(0, 0, 1),
rightWC: new Cesium.Cartesian3(0, 1, 0),
heading: Cesium.Math.TWO_PI,
pitch: -Cesium.Math.PI_OVER_TWO,
roll: Cesium.Math.TWO_PI,
frustum: {
fovy,
aspectRatio: width / height,
projectionMatrix: Cesium.Matrix4.fromArray([
2, 0, 0, 0, 0, 3, 0, 0, 0, 0, -1, -1, 0, 0, -2, 0,
]),
},
viewMatrix: Cesium.Matrix4.fromTranslation(new Cesium.Cartesian3(1, 2, 3)),
transform: Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY),
get inverseTransform() {
return Cesium.Matrix4.inverseTransformation(
camera.transform,
new Cesium.Matrix4(),
);
},
defaultZoomAmount: 100000,
percentageChanged: 0.5,
changed: new Cesium.Event(),
moveStart: new Cesium.Event(),
moveEnd: new Cesium.Event(),
flights: [],
setView: record('camera.setView'),
flyTo: record('camera.flyTo', (options) => {
// Cesium cancels a running flight before starting the next one.
const previous = camera.flights.at(-1);
if (previous && !previous.done) {
previous.done = true;
previous.options.cancel?.();
}
const flight = { options, done: false };
camera.flights.push(flight);
if (options.duration !== undefined && options.duration <= 0) {
flight.done = true;
options.complete?.();
}
}),
flyToBoundingSphere: record(
'camera.flyToBoundingSphere',
(sphere, options) => camera.flyTo({ ...options, sphere }),
),
completeFlight() {
const flight = camera.flights.at(-1);
if (!flight || flight.done) return;
flight.done = true;
flight.options.complete?.();
},
cancelFlight: record('camera.cancelFlight', () => {
const flight = camera.flights.at(-1);
if (!flight || flight.done) return;
flight.done = true;
flight.options.cancel?.();
}),
lookAt: record('camera.lookAt'),
// With an offset (a Cartesian3), the camera sits there in the new frame
// looking at its origin.
lookAtTransform: record('camera.lookAtTransform', (transform, offset) => {
Cesium.Matrix4.clone(transform, camera.transform);
if (offset instanceof Cesium.Cartesian3) {
Cesium.Cartesian3.clone(offset, camera.position);
Cesium.Cartesian3.normalize(
Cesium.Cartesian3.negate(offset, camera.direction),
camera.direction,
);
}
}),
zoomIn: record('camera.zoomIn'),
zoomOut: record('camera.zoomOut'),
computeViewRectangle: record(
'camera.computeViewRectangle',
() => new Cesium.Rectangle(0.1, 0.2, 0.3, 0.4),
),
pickEllipsoid: record(
'camera.pickEllipsoid',
() => new Cesium.Cartesian3(6378137, 0, 0),
),
getPickRay: record(
'camera.getPickRay',
() =>
new Cesium.Ray(
new Cesium.Cartesian3(7e6, 0, 0),
new Cesium.Cartesian3(-1, 0, 0),
),
),
};
const globe = {
show: true,
tilesLoaded: true,
getHeight: record('globe.getHeight', () => 12.5),
pick: record('globe.pick', () => new Cesium.Cartesian3(6378137, 3, 4)),
};
const scene = {
camera,
canvas: { clientWidth: width, clientHeight: height },
globe,
preUpdate: new Cesium.Event(),
postUpdate: new Cesium.Event(),
preRender: new Cesium.Event(),
postRender: new Cesium.Event(),
requestRenderMode: true,
maximumRenderTimeChange: Infinity,
// Cesium's running camera flights (a flight on viewer.camera is one).
tweens: [],
requestRender: record('scene.requestRender'),
pick: record('scene.pick', () => undefined),
drillPick: record('scene.drillPick', () => []),
pickPositionSupported: true,
pickPosition: record(
'scene.pickPosition',
() => new Cesium.Cartesian3(6378137, 1, 2),
),
sampleHeightSupported: true,
sampleHeight: record('scene.sampleHeight', () => 42),
clampToHeightSupported: true,
clampToHeight: record(
'scene.clampToHeight',
() => new Cesium.Cartesian3(6378200, 0, 0),
),
screenSpaceCameraController: { enableInputs: true },
frameState: { creditDisplay },
primitives: {
get length() {
return primitiveList.length;
},
get: (index) => primitiveList[index],
add: record('primitives.add', (value) => {
primitiveList.push(value);
return value;
}),
remove: record('primitives.remove', (value) => {
const index = primitiveList.indexOf(value);
if (index < 0) return false;
primitiveList.splice(index, 1);
return true;
}),
contains: (value) => primitiveList.includes(value),
},
setTerrain: record('scene.setTerrain'),
};
const viewer = {
calls,
callsOf: (name) => calls.filter((call) => call.name === name),
scene,
camera,
container: { kind: 'container' },
canvas: scene.canvas,
creditDisplay,
staticCredits,
primitiveList,
trackedEntityChanged: new Cesium.Event(),
// Assigning raises trackedEntityChanged, as Cesium's Viewer does.
get trackedEntity() {
return trackedEntity;
},
set trackedEntity(value) {
if (value === trackedEntity) return;
trackedEntity = value;
calls.push({ name: 'trackedEntity', args: [value] });
viewer.trackedEntityChanged.raiseEvent(value);
},
entities: {
values: entityList,
add: record('entities.add', (options) => {
const entity = new Cesium.Entity(options);
entityList.push(entity);
return entity;
}),
remove: record('entities.remove', (entity) => {
const index = entityList.indexOf(entity);
if (index >= 0) entityList.splice(index, 1);
return index >= 0;
}),
contains: (entity) => entityList.includes(entity),
},
isDestroyed: () => false,
// The viewer clock: its tick event (CesiumWidget ticks it right before
// scene.render on every animation frame) and current time.
clock: {
onTick: new Cesium.Event(),
currentTime: Cesium.JulianDate.fromIso8601('2026-10-16T12:00:00Z'),
},
imageryList,
terrainProvider: undefined,
imageryLayers: {
add: record('imageryLayers.add', (layer, index) =>
imageryList.splice(index ?? imageryList.length, 0, layer),
),
remove: record('imageryLayers.remove', (layer) => {
const index = imageryList.indexOf(layer);
if (index >= 0) imageryList.splice(index, 1);
return index >= 0;
}),
},
};
return viewer;
}
/** A fake ScreenSpaceEventHandler factory that records its actions. */
export function createFakeEventHandlers() {
const handlers = [];
const create = (canvas) => {
const actions = new Map();
const handler = {
canvas,
destroyed: false,
actions,
setInputAction(action, type, modifier) {
actions.set(`${type}:${modifier}`, action);
},
removeInputAction(type, modifier) {
actions.delete(`${type}:${modifier}`);
},
fire(type, event, modifier) {
actions.get(`${type}:${modifier}`)?.(event);
},
destroy() {
handler.destroyed = true;
},
};
handlers.push(handler);
return handler;
};
return { handlers, create };
}