Files
gods-eye-view/src/layers/traffic/deadline.test.mjs
T
Sameh Khamis bbd6049e21 Draw the traffic dots through the draw port and read the road surface through renderer ports
The traffic layer's vehicle dots are now a points set on the draw port,
moved, added to and taken from in place each frame, instead of a Cesium
point collection. Road surface heights and readiness read the query,
camera, frame and globe ports and the heights service instead of the
scene; the surface identity and "any tileset drawn" checks, which the
contract cannot answer, move to rendering.js with the heat lines.

animation.js, style.js and surface.js leave the Cesium allow-list.

The Cesium draw port compares a moved point's look without allocating:
colours by index, and a frozen colour or curve of plain data by identity,
so a thousand dots moved every frame cost about what direct writes did.
The traffic QA scripts read the dots' draw items.
2026-11-07 23:16:11 -08:00

371 lines
15 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import * as C from 'cesium';
import { createIngestion } from './ingestion.js';
import { createFlow } from './flow.js';
import { createRetention } from './retention.js';
import { createControls } from './controls.js';
import { createState } from './state.js';
import { bindRenderer } from '../../render/rendererOf.js';
import { surfaceViewer } from '../../testSupport/fakeSceneSurface.mjs';
const bounds = { west: 0, east: 0.01, south: 0, north: 0.01 };
const flush = async () => { for (let i = 0; i < 30; i++) await Promise.resolve(); };
function setup(source) {
const state = { _enabled: true, _roadMode: 'osm', _loadGeneration: 0,
_tileCache: new Map(), _parseRoads: (data) => data.roads };
const paints = [];
const ingestion = createIngestion({ state, source, services: {}, parts: {
viewport: { clampBounds: (b) => b, getBoundsCenter: () => ({ lat: 0, lon: 0 }), onCameraChanged() {} },
flow: { warmFlow: async () => [], applyFlowThenRender: async (roads) => { paints.push(roads); return true; } },
} });
return { state, ingestion, paints };
}
for (const stage of ['request', 'body']) test(`road ${stage} deadline releases an uncooperative transport and ignores late callbacks`, async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
let options, resolveLate;
const stalled = new Promise((resolve) => { resolveLate = resolve; });
const h = setup({ requestRoads: (_bounds, opts) => {
options = opts;
return stage === 'request' ? stalled : Promise.resolve({ ok: true, json: () => stalled });
} });
const request = h.ingestion.loadRoadsForBounds(bounds, 450);
await flush();
t.mock.timers.tick(12001);
await request;
assert.equal(options.signal.aborted, true);
assert.equal(h.state._fetching, false);
assert.equal(h.state._retryAttempts, 1);
assert.ok(h.state._retryTimer);
const source = h.state._roadSource;
h.state._loadGeneration++;
h.state._fetching = true;
options.onTile({ roads: [{ coords: [] }], roadSource: 'late' });
resolveLate(stage === 'request' ? { ok: true, json: async () => ({ roads: [], roadSource: 'late' }) } : { roads: [], roadSource: 'late' });
await flush();
assert.equal(h.state._roadSource, source);
assert.equal(h.state._fetching, true, 'old completion cannot clear a new generation');
assert.equal(h.paints.length, 0);
h.ingestion.cancelActiveFetch();
});
test('detail deadline retains painted coarse roads and schedules bounded retry', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const coarse = [{ coords: [[0, 0], [0.01, 0.01]] }];
const h = setup({ requestRoads: async (_b, options) => options.majorOnly
? { ok: true, json: async () => ({ roads: coarse }) } : new Promise(() => {}) });
const request = h.ingestion.loadRoadsForBounds(bounds, 450);
await flush();
t.mock.timers.tick(20001);
await request;
assert.deepEqual(h.paints.at(-1), coarse);
assert.equal(h.state._fetching, false);
assert.equal(h.state._roadPartial, true);
assert.equal(h.state._retryAttempts, 1);
h.ingestion.cancelActiveFetch();
});
test('superseding a stalled pass aborts promptly without clearing newer loading', async () => {
const h = setup({ requestRoads: () => new Promise(() => {}) });
const old = h.ingestion.loadRoadsForBounds(bounds, 450);
const next = h.ingestion.loadRoadsForBounds(bounds, 450);
await old;
assert.equal(h.state._fetching, true);
h.state._enabled = false;
h.ingestion.cancelActiveFetch();
await next;
clearTimeout(h.state._retryTimer);
});
// The camera-plane scenes below, as the renderer's ports see them: the eye
// and the near plane, and window positions from the scene itself.
const ANGLE = 35 * Math.PI / 180;
function portView(scene) {
const viewer = { scene, canvas: scene.canvas };
const position = { x: 0, y: 0, z: 450 };
const direction = { x: 0, y: Math.cos(ANGLE), z: -Math.sin(ANGLE) };
bindRenderer(viewer, {
camera: { pose: () => ({ position: { ...position }, direction: { ...direction }, near: scene.camera.frustum.near }) },
query: { project(p) {
const s = C.SceneTransforms.worldToWindowCoordinates(scene, new C.Cartesian3(p.x, p.y, p.z));
return s ? { x: s.x, y: s.y } : null;
} },
});
return viewer;
}
for (const y of [-175, -200, -300]) test(`off-screen camera-plane height correction settles without holding public loading (${y})`, () => {
const angle = 35 * Math.PI / 180;
const scene = {
canvas: { clientWidth: 1280, clientHeight: 800 },
frameState: { mode: C.SceneMode.SCENE3D },
camera: { viewMatrix: C.Matrix4.computeView(new C.Cartesian3(0, 0, 450),
new C.Cartesian3(0, Math.cos(angle), -Math.sin(angle)),
new C.Cartesian3(0, Math.sin(angle), Math.cos(angle)), C.Cartesian3.UNIT_X, new C.Matrix4()),
frustum: new C.PerspectiveFrustum({ fov: Math.PI / 3, aspectRatio: 1.6, near: 1, far: 1e7 }) },
};
const point = new C.Cartesian3(0, y, 100), target = new C.Cartesian3(0, y, 197);
const state = createState({ services: {} });
Object.assign(state, { _viewer: portView(scene), _roads: [{ heightMoving: true, waypoints: [point], targetWaypoints: [target], segmentDist: [] }] });
const retention = createRetention({ state, parts: {} });
retention.easeHeights(1 / 60);
const controls = createControls({ state, services: {}, source: { getFlowSessionStats: () => ({}) }, parts: { model: { trafficFeedPresentation: () => ({}) } } });
assert.equal(controls.methods.getStats().loading, false);
for (let frame = 0; frame < 180; frame++) retention.easeHeights(1 / 60);
assert.equal(state._heightPending, false);
assert.equal(state._roads[0].heightMoving, false);
assert.ok(C.Cartesian3.distance(point, target) < 0.01);
assert.equal(controls.methods.getLoadingDiagnostics().movingWaypoints, 0);
});
test('visible corrections use the pixel limit without the off-screen world-speed cap', () => {
const angle = 35 * Math.PI / 180;
const scene = {
canvas: { clientWidth: 1280, clientHeight: 800 },
frameState: { mode: C.SceneMode.SCENE3D },
camera: { viewMatrix: C.Matrix4.computeView(new C.Cartesian3(0, 0, 450),
new C.Cartesian3(0, Math.cos(angle), -Math.sin(angle)),
new C.Cartesian3(0, Math.sin(angle), Math.cos(angle)), C.Cartesian3.UNIT_X, new C.Matrix4()),
frustum: new C.PerspectiveFrustum({ fov: Math.PI / 3, aspectRatio: 1.6, near: 1, far: 1e7 }) },
};
const point = new C.Cartesian3(0, 1000, 0), target = new C.Cartesian3(0, 1000, 300);
const state = { _viewer: portView(scene), _roads: [{ heightMoving: true, waypoints: [point], targetWaypoints: [target], segmentDist: [] }] };
const retention = createRetention({ state, parts: {} });
let loadingFrames = 0, maxPixels = 0;
for (let frame = 0; frame < 180; frame++) {
const before = C.SceneTransforms.worldToWindowCoordinates(scene, point);
retention.easeHeights(1 / 60);
const after = C.SceneTransforms.worldToWindowCoordinates(scene, point);
if (before.y >= 0 && after.y >= 0) maxPixels = Math.max(maxPixels, C.Cartesian2.distance(before, after));
if (state._heightPending) loadingFrames++;
}
assert.ok(maxPixels <= 12.01, `visible correction: ${maxPixels} px`);
assert.ok(loadingFrames <= 66, `visible loading: ${loadingFrames / 60} s`);
assert.equal(state._roads[0].heightMoving, false);
});
for (const targetHeight of [200, 2000]) test(`visible height steps stay pixel-limited outside the margin or across the near plane (${targetHeight})`, () => {
const angle = 35 * Math.PI / 180;
const scene = {
canvas: { clientWidth: 1280, clientHeight: 800 },
frameState: { mode: C.SceneMode.SCENE3D },
camera: { viewMatrix: C.Matrix4.computeView(new C.Cartesian3(0, 0, 450),
new C.Cartesian3(0, Math.cos(angle), -Math.sin(angle)),
new C.Cartesian3(0, Math.sin(angle), Math.cos(angle)), C.Cartesian3.UNIT_X, new C.Matrix4()),
frustum: new C.PerspectiveFrustum({ fov: Math.PI / 3, aspectRatio: 1.6, near: 1, far: 1e7 }) },
};
const point = new C.Cartesian3(0, 50, 400), target = new C.Cartesian3(0, 50, targetHeight);
const state = { _viewer: portView(scene), _roads: [{ heightMoving: true, waypoints: [point], targetWaypoints: [target], segmentDist: [] }] };
const before = C.SceneTransforms.worldToWindowCoordinates(scene, point);
assert.ok(before.y > 0 && before.y < 800);
const trial = C.Cartesian3.lerp(point, target, 0.2, new C.Cartesian3());
const trialEye = C.Matrix4.multiplyByPoint(scene.camera.viewMatrix, trial, new C.Cartesian3());
if (targetHeight === 200) {
assert.ok(trialEye.z < -1);
assert.ok(C.SceneTransforms.worldToWindowCoordinates(scene, trial).y > 900);
} else assert.ok(trialEye.z >= -1);
createRetention({ state, parts: {} }).easeHeights(1 / 60);
const after = C.SceneTransforms.worldToWindowCoordinates(scene, point);
const pixels = C.Cartesian2.distance(before, after);
assert.ok(pixels > 0 && pixels <= 12, `visible correction: ${pixels} px`);
assert.ok(C.Matrix4.multiplyByPoint(scene.camera.viewMatrix, point, new C.Cartesian3()).z < -1);
assert.equal(state._heightPending, true);
});
function holdFlowTasks(t) {
const yields = [];
const scheduler = Object.getOwnPropertyDescriptor(globalThis, 'scheduler');
Object.defineProperty(globalThis, 'scheduler', {
configurable: true,
value: { yield: () => new Promise((resolve) => yields.push(resolve)) },
});
t.after(() => {
for (const resolve of yields) resolve();
if (scheduler) Object.defineProperty(globalThis, 'scheduler', scheduler);
else delete globalThis.scheduler;
});
return async () => {
assert.ok(yields.length, 'pass reached a flow task boundary');
yields.shift()();
await flush();
};
}
function flowFixture(source) {
const state = createState({ services: {} });
const calls = { samples: 0, selections: 0 };
const road = {
type: 'primary',
oneway: 1,
coords: [
[0, 0],
[0.001, 0],
],
waypoints: [
C.Cartesian3.fromDegrees(0, 0),
C.Cartesian3.fromDegrees(0.001, 0),
],
segmentDist: [111],
};
const paints = [];
Object.assign(state, {
_enabled: true,
_roadMode: 'osm',
_parseRoads: (data) => data.roads,
_viewer: surfaceViewer({
globe: {
show: true,
getHeight: () => {
calls.samples++;
return 100;
},
},
}),
});
const parts = {
viewport: {
clampBounds: (b) => b,
getBoundsCenter: () => ({ lat: 0, lon: 0 }),
onCameraChanged() {},
},
rendering: {
visibleRoadsForAltitude: (roads) => {
calls.selections++;
return roads;
},
renderRoadsForAltitude: (...args) => paints.push(args),
},
model: {
allocateRoadDotBudgets: (roads) => roads.map(() => 1),
recolorDotsInPlace() {},
},
};
const ctx = { state, source, services: { credits: {} }, parts };
parts.flow = createFlow(ctx);
return { state, road, paints, parts, ctx, calls };
}
for (const outcome of ['timeout', 'abort'])
test(`a road pass ${outcome} invalidates queued tiles and terrain publication without a newer generation`, async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const advanceFlowTask = holdFlowTasks(t);
const held = [];
const setTimer = globalThis.setTimeout;
t.mock.method(globalThis, 'setTimeout', (fn, delay, ...args) =>
delay === 0 ? (held.push(fn), held.length) : setTimer(fn, delay, ...args),
);
let clock = 0;
t.mock.method(performance, 'now', () => (clock += 13));
let options;
const source = {
getStatus: async () => ({ hasKey: false }),
requestRoads: (_b, opts) => {
options = opts;
opts.onTile({ roads: [road] });
return new Promise(() => {});
},
};
const { state, road, paints, ctx, calls } = flowFixture(source);
const ingestion = createIngestion(ctx);
const request = ingestion.loadRoadsForBounds(bounds, 450);
await flush();
await advanceFlowTask();
const samplesBeforeSurface = calls.samples;
await advanceFlowTask();
assert.equal(held.length, 1);
held.shift()();
await flush();
assert.equal(
held.length,
1,
'held at the final station before the real terrain onReady callback',
);
assert.equal(
calls.samples - samplesBeforeSurface,
road.coords.length,
'both terrain stations were sampled before the final hold',
);
options.onTile({ roads: [road] });
const generation = state._loadGeneration;
const revision = state._surfaceRevision;
const samplesBefore = calls.samples;
if (outcome === 'timeout') t.mock.timers.tick(12001);
else ingestion.cancelActiveFetch();
await request;
assert.equal(options.signal.aborted, true);
assert.equal(state._loadGeneration, generation);
assert.equal(state._fetching, false);
if (outcome === 'timeout') {
assert.equal(state._activeFetchAbort.signal.aborted, false);
assert.match(state._roadError, /timed out/);
assert.equal(state._retryAttempts, 1);
}
held.shift()();
await flush();
assert.equal(paints.length, 0, 'expired terrain cannot publish');
assert.equal(
calls.samples,
samplesBefore,
'queued tiles cannot start surface preparation',
);
assert.equal(
state._surfaceRevision,
revision,
'queued tiles cannot invalidate another surface pass',
);
ingestion.cancelActiveFetch();
});
for (const checkpoint of ['none', 'prepare', 'surface'])
test(
checkpoint === 'none'
? 'a current surface pass crosses both task boundaries'
: `a newer surface revision stops stale ${checkpoint} work without aborting the pass`,
async (t) => {
// These cases exercise ownership boundaries, not the surface slice budget.
t.mock.method(performance, 'now', () => 0);
const advanceFlowTask = holdFlowTasks(t);
const { state, road, paints, parts, calls } = flowFixture({});
const controller = new AbortController();
t.after(() => controller.abort());
const generation = state._loadGeneration;
const pass = parts.flow.applyFlowThenRender(
[road],
bounds,
generation,
450,
'Loaded tile',
null,
false,
controller.signal,
);
await flush();
if (checkpoint === 'prepare') state._surfaceRevision++;
await advanceFlowTask();
if (checkpoint === 'prepare') {
assert.equal(
calls.selections,
0,
'stale whole-graph preparation cannot start',
);
} else {
assert.ok(calls.selections > 0, 'the healthy preparation prefix ran');
if (checkpoint === 'surface') state._surfaceRevision++;
await advanceFlowTask();
}
const rendered = await pass;
assert.equal(state._loadGeneration, generation);
assert.equal(controller.signal.aborted, false);
if (checkpoint === 'none') {
assert.equal(rendered, true);
assert.ok(calls.samples > 0);
assert.ok(paints.length > 0);
} else {
assert.equal(rendered, false);
assert.equal(calls.samples, 0, 'superseded work cannot sample terrain');
assert.equal(paints.length, 0, 'superseded work cannot publish');
}
},
);