mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-22 03:02:20 +00:00
refactor(voice): W1 actions on the engine
The voice runner drives the globe engine (an `engine` option, by default the one registered for the viewer) instead of the Cesium viewer: track_entity, stop_tracking (camera.following().stop()), frame_overhead (camera.flyToSphere with the pose heading), fly_to_location, zoom_to_globe (follow released through camera.following()), adjust_camera_zoom, next_iss_pass / next_satellite_pass, select_nearest_aircraft and the analyst view context, get_current_view_state, get_entity_context (projector.worldToWindow), the view target (picker.pickPosition, then camera.pickEllipsoid, then picker.pickGlobe) and its moveEnd prewarm, and the viewport samples. getBasemapLabelContext takes the engine or a viewer with a registered engine (the HUD). Without a viewer the tools that still need one (VIEWER_ONLY_ACTIONS: control_scene, annotate_map, clear_annotations, fly_route, move_camera, control_cockpit, set_context_mode, control_radio, control_cctv) answer a structured refusal (code UNAVAILABLE_ON_ENGINE) instead of throwing. Point-and-ask picks with engine.picker and keys the camera pose from engine.camera.pose(); the viewport capture reads engine.host.canvas and waits for the engine's postRender (both engines keep their drawing buffer: Cesium's contextOptions, Navara's own WebGL2 canvas, S4). Tests: gevActionsEngine.test.mjs runs each moved action on the fake engine. gevActions.test.mjs keeps its expectations; its runner helper now registers an engine for the test viewer (the Cesium engine over it, or a fake engine at its positionCartographic) and the two harness cameras gain roll and frustum for camera.pose().
This commit is contained in:
1 parent
ec7dcc944e
commit
bc281ab246
9 files changed
+701
-190
No files matched your search
@@ -0,0 +1,422 @@
|
||||
// The W1 voice actions on the globe engine (phase 3, Task 9): the same
|
||||
// results as on the Cesium viewer, through engine.camera / picker /
|
||||
// projector only, and a structured refusal for the actions that still need
|
||||
// a viewer when there is none (the Navara engine).
|
||||
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../geo/index.js';
|
||||
import { createFakeEngine } from '../globe/testing/fakeEngine.js';
|
||||
import { GEV_ACTION_SCHEMAS } from './actionSchemas.js';
|
||||
import {
|
||||
UNAVAILABLE_ON_ENGINE,
|
||||
VIEWER_ONLY_ACTIONS,
|
||||
createGevActionRunner,
|
||||
} from './gevActions.js';
|
||||
import { cameraPoseKey } from './pointerContext.js';
|
||||
import { createPointerPicker } from './pointerPick.js';
|
||||
import { renderFreshFrame } from './realtimeViewport.js';
|
||||
|
||||
globalThis.window ||= {
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
requestIdleCallback: null,
|
||||
};
|
||||
|
||||
const toRad = geo.math.toRadians;
|
||||
|
||||
/** A fake engine over Austin at `altitude`, looking straight down. */
|
||||
function engineOverAustin(options = {}) {
|
||||
return createFakeEngine({
|
||||
longitude: toRad(-97.74),
|
||||
latitude: toRad(30.27),
|
||||
altitude: 20_000,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
/** The UI navigation seam, without its policy: runs the move. */
|
||||
const navigatingStyleManager = (extra = {}) => ({
|
||||
runImmediateNavigation: (noun, navigate) => navigate(),
|
||||
...extra,
|
||||
});
|
||||
|
||||
function dataManagerWith(modules, { enabled = Object.keys(modules) } = {}) {
|
||||
const on = new Set(enabled);
|
||||
const params = [];
|
||||
return {
|
||||
params,
|
||||
layers: new Map(
|
||||
Object.entries(modules).map(([id, module]) => [id, { id, module }]),
|
||||
),
|
||||
isEnabled: (id) => on.has(id),
|
||||
getAll: () =>
|
||||
Object.keys(modules).map((id) => ({
|
||||
id,
|
||||
name: id,
|
||||
enabled: on.has(id),
|
||||
stats: modules[id]?.getStats?.() || {},
|
||||
})),
|
||||
setLayerParams(id, values) {
|
||||
params.push({ id, values });
|
||||
return true;
|
||||
},
|
||||
async setEnabled(id, value) {
|
||||
if (value) on.add(id);
|
||||
else on.delete(id);
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const runnerOn = (engine, options = {}) =>
|
||||
createGevActionRunner({
|
||||
viewer: null,
|
||||
engine,
|
||||
styleManager: navigatingStyleManager(),
|
||||
dataManager: dataManagerWith({}),
|
||||
...options,
|
||||
});
|
||||
|
||||
const follow = (engine, position) =>
|
||||
engine.camera.follow({
|
||||
position: () => position,
|
||||
viewFrom: { x: 0, y: -2000, z: 800 },
|
||||
});
|
||||
|
||||
test('the viewer-only actions are one exported list of real tools', () => {
|
||||
assert.deepEqual(VIEWER_ONLY_ACTIONS, [
|
||||
'control_scene',
|
||||
'annotate_map',
|
||||
'clear_annotations',
|
||||
'fly_route',
|
||||
'move_camera',
|
||||
'control_cockpit',
|
||||
'set_context_mode',
|
||||
'control_radio',
|
||||
'control_cctv',
|
||||
]);
|
||||
assert.ok(Object.isFrozen(VIEWER_ONLY_ACTIONS));
|
||||
const tools = new Set(GEV_ACTION_SCHEMAS.map((schema) => schema.name));
|
||||
for (const name of VIEWER_ONLY_ACTIONS)
|
||||
assert.ok(tools.has(name), `${name} is a voice tool`);
|
||||
});
|
||||
|
||||
test('without a viewer the viewer-only actions refuse with a structured result', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
const touched = [];
|
||||
const styleManager = new Proxy(navigatingStyleManager(), {
|
||||
get(target, key) {
|
||||
if (typeof key === 'string') touched.push(key);
|
||||
return target[key];
|
||||
},
|
||||
});
|
||||
const runner = runnerOn(engine, { styleManager });
|
||||
engine.fake.resetCalls();
|
||||
for (const name of VIEWER_ONLY_ACTIONS) {
|
||||
const result = await runner(name, { action: 'status' });
|
||||
assert.equal(result.ok, false, name);
|
||||
assert.equal(result.action, name);
|
||||
assert.equal(result.unavailable, true, name);
|
||||
assert.equal(result.code, UNAVAILABLE_ON_ENGINE, name);
|
||||
assert.equal(result.engine, 'navara', name);
|
||||
assert.equal(result.error, 'Unavailable on this engine', name);
|
||||
}
|
||||
assert.deepEqual(touched, [], 'a refusal reaches no controller');
|
||||
assert.deepEqual(engine.fake.calls, [], 'a refusal moves no camera');
|
||||
});
|
||||
|
||||
test('with a viewer the viewer-only actions still run', async () => {
|
||||
const runner = createGevActionRunner({
|
||||
viewer: {
|
||||
clock: { onTick: { addEventListener: () => () => {} } },
|
||||
scene: { canvas: { addEventListener() {}, removeEventListener() {} } },
|
||||
camera: { moveEnd: { addEventListener() {} } },
|
||||
},
|
||||
engine: engineOverAustin(),
|
||||
styleManager: {},
|
||||
dataManager: dataManagerWith({}),
|
||||
});
|
||||
const radio = await runner('control_radio', { action: 'status' });
|
||||
assert.equal(radio.error, 'Radio layer unavailable');
|
||||
assert.equal(radio.unavailable, undefined);
|
||||
});
|
||||
|
||||
test('the runner warms the view target on the engine camera, once per engine', () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
runnerOn(engine);
|
||||
runnerOn(engine);
|
||||
assert.equal(engine.camera.moveEnd.numberOfListeners, 1);
|
||||
});
|
||||
|
||||
test('stop_tracking stops the engine follow', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
const target = geo.cartesian3.fromDegrees(-97.7, 30.3, 9000);
|
||||
follow(engine, target);
|
||||
assert.ok(engine.camera.following());
|
||||
const released = [];
|
||||
const runner = runnerOn(engine, {
|
||||
dataManager: dataManagerWith({
|
||||
flights: {
|
||||
getTrackedInfo: () => ({ id: 'abc' }),
|
||||
stopTracking: () => released.push('flights'),
|
||||
},
|
||||
}),
|
||||
});
|
||||
const result = await runner('stop_tracking');
|
||||
assert.equal(result.ok, true);
|
||||
assert.deepEqual(result.released, ['flights']);
|
||||
assert.equal(engine.camera.following(), null);
|
||||
});
|
||||
|
||||
test('zoom_to_globe leaves the follow and flies the engine camera', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
follow(engine, geo.cartesian3.fromDegrees(-97.7, 30.3, 9000));
|
||||
const runner = runnerOn(engine);
|
||||
const result = await runner('zoom_to_globe');
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.heightKm > 1000, true);
|
||||
assert.equal(engine.camera.following(), null);
|
||||
assert.equal(engine.fake.callsOf('camera.flyTo').length, 1);
|
||||
});
|
||||
|
||||
test('fly_to_location to coordinates leaves the follow and flies the engine', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
follow(engine, geo.cartesian3.fromDegrees(-97.7, 30.3, 9000));
|
||||
const runner = runnerOn(engine);
|
||||
const result = await runner('fly_to_location', {
|
||||
latitude: 51.5,
|
||||
longitude: -0.12,
|
||||
});
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.navigationMode, 'close-coordinate');
|
||||
assert.equal(engine.camera.following(), null);
|
||||
assert.equal(engine.fake.callsOf('camera.flyToSphere').length, 1);
|
||||
engine.fake.frames(400, 16);
|
||||
const place = engine.camera.pose().cartographic;
|
||||
assert.ok(Math.abs(geo.math.toDegrees(place.latitude) - 51.5) < 0.05);
|
||||
});
|
||||
|
||||
test('track_entity flies to a vessel through the engine', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
let selected = null;
|
||||
const runner = runnerOn(engine, {
|
||||
dataManager: dataManagerWith({
|
||||
'ais-live-vessels': {
|
||||
findByQuery: () => ({
|
||||
mmsi: 366999999,
|
||||
name: 'EVER GIVEN',
|
||||
latitude: 29.7,
|
||||
longitude: -95.0,
|
||||
}),
|
||||
selectById: (mmsi) => (selected = mmsi),
|
||||
},
|
||||
}),
|
||||
});
|
||||
const result = await runner('track_entity', {
|
||||
query: 'ever given',
|
||||
layerId: 'ais-live-vessels',
|
||||
});
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.label, 'EVER GIVEN');
|
||||
assert.equal(selected, 366999999);
|
||||
assert.equal(engine.fake.callsOf('camera.flyToSphere').length, 1);
|
||||
});
|
||||
|
||||
test('frame_overhead frames the nearby contacts with flyToSphere as on Cesium', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
const center = engine.camera.pickEllipsoid({ x: 512, y: 384 });
|
||||
const positions = [
|
||||
geo.cartesian3.fromDegrees(-97.7, 30.3, 9000),
|
||||
geo.cartesian3.fromDegrees(-97.9, 30.1, 11000),
|
||||
geo.cartesian3.fromDegrees(-97.5, 30.5, 10000),
|
||||
];
|
||||
let asked = null;
|
||||
const runner = runnerOn(engine, {
|
||||
dataManager: dataManagerWith({
|
||||
flights: {
|
||||
getNearby(at, radiusM, limit) {
|
||||
asked = { at, radiusM, limit };
|
||||
return positions.map((position, index) => ({
|
||||
id: `F${index}`,
|
||||
icao24: `a${index}`,
|
||||
label: `F${index}`,
|
||||
position,
|
||||
}));
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
const heading = engine.camera.pose().heading;
|
||||
const result = await runner('frame_overhead', { target: 'flights' });
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.count, 3);
|
||||
assert.equal(asked.radiusM, 150_000);
|
||||
assert.equal(asked.limit, 80);
|
||||
assert.ok(geo.cartesian3.distance(asked.at, center) < 1e-6);
|
||||
// The same sphere and offset the Cesium path flew.
|
||||
const sphere = Cesium.BoundingSphere.fromPoints(positions);
|
||||
const radius = Math.max(sphere.radius * 1.25, 8000);
|
||||
const [call] = engine.fake.callsOf('camera.flyToSphere');
|
||||
const [flown, options] = call.args;
|
||||
assert.ok(geo.cartesian3.distance(flown.center, sphere.center) < 1e-6);
|
||||
assert.equal(flown.radius, radius);
|
||||
assert.equal(options.duration, 2);
|
||||
assert.equal(options.heading, heading);
|
||||
assert.equal(options.pitch, toRad(-62));
|
||||
assert.equal(options.range, radius * 2.4);
|
||||
});
|
||||
|
||||
test('next_iss_pass observes from the engine camera', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
let observer = null;
|
||||
const runner = runnerOn(engine, {
|
||||
dataManager: dataManagerWith({
|
||||
satellites: {
|
||||
getNextIssPass(options) {
|
||||
observer = options;
|
||||
return { status: 'none' };
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
const result = await runner('next_iss_pass');
|
||||
assert.equal(result.ok, false);
|
||||
assert.ok(Math.abs(observer.latDeg - 30.27) < 1e-6);
|
||||
assert.ok(Math.abs(observer.lonDeg + 97.74) < 1e-6);
|
||||
});
|
||||
|
||||
test('get_current_view_state reads the engine camera', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
const runner = runnerOn(engine, {
|
||||
styleManager: navigatingStyleManager({ activeStyle: 'noir' }),
|
||||
});
|
||||
const result = await runner('get_current_view_state');
|
||||
assert.equal(result.ok, true);
|
||||
assert.ok(Math.abs(result.camera.latitude - 30.27) < 1e-6);
|
||||
assert.ok(Math.abs(result.camera.longitude + 97.74) < 1e-6);
|
||||
assert.ok(Math.abs(result.camera.heightM - 20_000) < 1e-3);
|
||||
});
|
||||
|
||||
test('adjust_camera_zoom moves the engine camera toward the view target', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
const runner = runnerOn(engine);
|
||||
const result = await runner('adjust_camera_zoom', {
|
||||
direction: 'in',
|
||||
amount: 'medium',
|
||||
});
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.movementRequestedM, 11_000);
|
||||
assert.equal(result.movementActualM, 11_000);
|
||||
assert.equal(result.afterHeightM, 9000);
|
||||
assert.equal(engine.fake.callsOf('camera.cancelFlight').length, 1);
|
||||
});
|
||||
|
||||
test('get_entity_context samples the view through the engine', async () => {
|
||||
// Continental: no reverse geocoding, every viewport sample on the globe.
|
||||
const engine = engineOverAustin({ kind: 'navara', altitude: 4_000_000 });
|
||||
const runner = runnerOn(engine);
|
||||
const result = await runner('get_entity_context', { scope: 'in_view' });
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.scene.camera.heightM, 4_000_000);
|
||||
assert.equal(result.scene.basemap.viewScale, 'continental');
|
||||
assert.equal(result.scene.basemap.viewportSamples.length, 7);
|
||||
// The view centre, on the ellipsoid under the camera.
|
||||
const [centre] = result.scene.basemap.viewportSamples;
|
||||
assert.ok(Math.abs(centre.latitude - 30.27) < 0.1);
|
||||
assert.equal(centre.longitude, -97.74);
|
||||
});
|
||||
|
||||
test('select_nearest_aircraft runs on the engine end to end', async () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
let tracked = null;
|
||||
const flights = {
|
||||
getStats: () => ({ count: 2, source: 'fixture' }),
|
||||
getAnalystRecords: () => [
|
||||
{
|
||||
id: 'FAR1',
|
||||
icao24: 'f1',
|
||||
callsign: 'FAR1',
|
||||
lat: 30.4,
|
||||
lon: -97.7,
|
||||
onGround: false,
|
||||
},
|
||||
{
|
||||
id: 'NEAR1',
|
||||
icao24: 'n1',
|
||||
callsign: 'NEAR1',
|
||||
lat: 30.3,
|
||||
lon: -97.7,
|
||||
onGround: false,
|
||||
},
|
||||
],
|
||||
findByQuery: (query) =>
|
||||
query === 'n1'
|
||||
? { icao24: 'n1', callsign: 'NEAR1', latitude: 30.3, longitude: -97.7 }
|
||||
: null,
|
||||
trackById: (id) => (tracked = id),
|
||||
};
|
||||
const dataManager = dataManagerWith({ flights }, { enabled: [] });
|
||||
dataManager.refreshLayer = async () => true;
|
||||
const runner = runnerOn(engine, { dataManager });
|
||||
const pending = runner('select_nearest_aircraft', {
|
||||
latitude: 30.27,
|
||||
longitude: -97.74,
|
||||
});
|
||||
for (let index = 0; index < 50 && !tracked; index++) {
|
||||
engine.fake.frames(20, 16);
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
}
|
||||
const result = await pending;
|
||||
assert.equal(result.ok, true, result.error);
|
||||
assert.equal(result.aircraft.id, 'n1');
|
||||
assert.equal(tracked, 'n1');
|
||||
});
|
||||
|
||||
test('the camera pose key reads the engine as it reads the viewer', () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
const pose = engine.camera.pose();
|
||||
const viewer = {
|
||||
camera: {
|
||||
positionWC: pose.position,
|
||||
heading: pose.heading,
|
||||
pitch: pose.pitch,
|
||||
roll: pose.roll,
|
||||
},
|
||||
};
|
||||
assert.equal(typeof cameraPoseKey(engine), 'string');
|
||||
assert.equal(cameraPoseKey(engine), cameraPoseKey(viewer));
|
||||
});
|
||||
|
||||
test('the pointer picker picks through the engine', () => {
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
engine.fake.script('pick', { id: { __gevContextId: 'none' } });
|
||||
const pick = createPointerPicker({ engine, dataManager: null });
|
||||
const result = pick(512, 384);
|
||||
assert.equal(result.entity, null);
|
||||
assert.ok(Math.abs(result.ground.lat - 30.27) < 1e-4);
|
||||
assert.ok(Math.abs(result.ground.lon + 97.74) < 1e-4);
|
||||
assert.equal(result.radiusKm, 15);
|
||||
assert.equal(result.cameraKey, cameraPoseKey(engine));
|
||||
const [screen] = engine.fake.callsOf('picker.pick')[0].args;
|
||||
assert.deepEqual([screen.x, screen.y], [512, 384]);
|
||||
});
|
||||
|
||||
test('a fresh frame for capture comes from the engine render loop', async () => {
|
||||
const previous = globalThis.document;
|
||||
try {
|
||||
globalThis.document = { hidden: false };
|
||||
const engine = engineOverAustin({ kind: 'navara' });
|
||||
engine.fake.resetCalls();
|
||||
const fresh = renderFreshFrame(engine);
|
||||
assert.equal(engine.fake.renderRequests, 1);
|
||||
engine.fake.frame();
|
||||
assert.equal(await fresh, true);
|
||||
globalThis.document = { hidden: true };
|
||||
assert.equal(await renderFreshFrame(engine), false);
|
||||
assert.equal(engine.fake.renderRequests, 1, 'no render while hidden');
|
||||
} finally {
|
||||
globalThis.document = previous;
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user