Files
gods-eye-view/src/globe/cesium/picker.test.mjs
T
German Khokhlov a3e929870d feat(globe): camera.commanded, picker.pickGlobe, pose.aspectRatio and isDestroyed
Shell navigation needs to know when a new owner takes the camera (the
arrival ground guard), where the globe surface is under a window position
(orientation pivot, view-centred place search), the frustum aspect ratio
(landmark framing) and whether the engine is gone. Each member has a
contract suite check and is implemented by the Cesium engine, the Navara
engine and the fake. On Cesium, commanded hooks the viewer camera's
command methods and trackedEntityChanged only while it has listeners, so
direct Cesium calls from layers still announce a new owner.
2026-10-15 19:19:41 +04:00

116 lines
4.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import * as geo from '../../geo/index.js';
import { createCesiumPicker } from './picker.js';
import { createFakeViewer } from './testing/fakeViewer.mjs';
function setup() {
const viewer = createFakeViewer();
const { picker } = createCesiumPicker(viewer);
return { viewer, picker, scene: viewer.scene };
}
test('pick passes a Cesium window position and keeps id and primitive', () => {
const { viewer, picker, scene } = setup();
assert.equal(picker.pick(geo.cartesian2.create(1, 2)), undefined);
const entity = { name: 'entity' };
const primitive = { name: 'billboard' };
const raw = { id: entity, primitive };
scene.pick = (...args) => {
viewer.calls.push({ name: 'scene.pick', args });
return raw;
};
const picked = picker.pick(geo.cartesian2.create(3, 4), 5, 6);
assert.equal(picked.id, entity);
assert.equal(picked.primitive, primitive);
assert.equal(picked.object, raw, 'the Cesium pick result rides along');
const [screen, width, height] = viewer.callsOf('scene.pick')[1].args;
assert.ok(screen instanceof Cesium.Cartesian2);
assert.deepEqual([screen.x, screen.y, width, height], [3, 4, 5, 6]);
});
test('drillPick passes the limit and normalises every result', () => {
const { viewer, picker, scene } = setup();
scene.drillPick = (...args) => {
viewer.calls.push({ name: 'scene.drillPick', args });
return [{ id: 'a', primitive: 1 }, { primitive: 2 }];
};
const drilled = picker.drillPick(geo.cartesian2.create(1, 1), 3, 5, 7);
assert.deepEqual(
drilled.map(({ id, primitive }) => [id, primitive]),
[
['a', 1],
[undefined, 2],
],
);
assert.ok('id' in drilled[1]);
const [screen, limit, width, height] =
viewer.callsOf('scene.drillPick')[0].args;
assert.ok(screen instanceof Cesium.Cartesian2);
assert.deepEqual([limit, width, height], [3, 5, 7]);
});
test('pickPosition passes a new Cesium.Cartesian2 per call and returns geo', () => {
const { viewer, picker, scene } = setup();
const position = picker.pickPosition(geo.cartesian2.create(7, 8));
assert.ok(!(position instanceof Cesium.Cartesian3));
assert.deepEqual([position.x, position.y, position.z], [6378137, 1, 2]);
picker.pickPosition(geo.cartesian2.create(9, 10));
const [first, second] = viewer
.callsOf('scene.pickPosition')
.map((call) => call.args[0]);
// Cesium caches pickPosition by windowPosition.toString().
assert.ok(first instanceof Cesium.Cartesian2);
assert.notEqual(first, second);
assert.equal(second.toString(), '(9, 10)');
scene.pickPosition = () => undefined;
assert.equal(picker.pickPosition(geo.cartesian2.create(1, 1)), undefined);
scene.pickPosition = () => {
throw new Error('depth texture missing');
};
assert.equal(picker.pickPosition(geo.cartesian2.create(1, 1)), undefined);
});
test('pickPositionSupported follows the scene', () => {
const { picker, scene } = setup();
assert.equal(picker.pickPositionSupported, true);
scene.pickPositionSupported = false;
assert.equal(picker.pickPositionSupported, false);
assert.equal(picker.pickPosition(geo.cartesian2.create(1, 1)), undefined);
});
test('pick results carry 3D tile content for surface checks', () => {
const { picker, scene } = setup();
const content = { name: 'tile content' };
const tileset = { isCesium3DTileset: true };
scene.pick = () => ({ content, primitive: tileset });
const picked = picker.pick(geo.cartesian2.create(1, 1));
assert.equal(picked.content, content);
assert.equal(picked.id, undefined);
assert.equal(picked.primitive, tileset);
scene.pick = () => ({ id: 'entity', primitive: {} });
assert.equal(picker.pick(geo.cartesian2.create(1, 1)).content, undefined);
});
test('pickGlobe asks the globe along the camera pick ray and returns geo', () => {
const { viewer, picker, scene } = setup();
const hit = picker.pickGlobe(geo.cartesian2.create(3, 4));
assert.ok(!(hit instanceof Cesium.Cartesian3));
assert.deepEqual([hit.x, hit.y, hit.z], [6378137, 3, 4]);
const [screen] = viewer.callsOf('camera.getPickRay')[0].args;
assert.ok(screen instanceof Cesium.Cartesian2);
assert.equal(screen.toString(), '(3, 4)');
const [ray, pickedScene] = viewer.callsOf('globe.pick')[0].args;
assert.ok(ray instanceof Cesium.Ray);
assert.equal(pickedScene, scene);
scene.globe.pick = () => undefined;
assert.equal(picker.pickGlobe(geo.cartesian2.create(1, 1)), undefined);
scene.globe.pick = () => {
throw new Error('no ray');
};
assert.equal(picker.pickGlobe(geo.cartesian2.create(1, 1)), undefined);
scene.globe = undefined;
assert.equal(picker.pickGlobe(geo.cartesian2.create(1, 1)), undefined);
});