From 0a8899d4393a821a75cb723e4dedcf06df937e93 Mon Sep 17 00:00:00 2001 From: manjunath22466 Date: Mon, 16 Nov 2026 19:47:06 +0530 Subject: [PATCH] test(camera): compare recorded camera traces within platform roundoff The camera golden test compared the recorded traces byte for byte, so it failed on arm64, where trigonometry differs from the committed traces in the last bits (at most 2.1e-9 m on a height, 33 of 1,530 numbers). Compare structure, keys, array lengths, counters and the viewport exactly, and other numbers within 1e-8 m for positions and heights and 1e-14 otherwise; a self-test rejects larger errors, NaN, null, strings and missing or extra keys. The pose test compares pitch by its sine within 1e-12, as the conformance suite does, and checks the canonical range. The golden file and production camera code are unchanged. --- src/render/cesium/cameraGoldens.test.mjs | 73 +++++++++++++++++++++--- src/render/cesium/cameraPort.test.mjs | 11 +++- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/render/cesium/cameraGoldens.test.mjs b/src/render/cesium/cameraGoldens.test.mjs index 774b0a0114..91ca593516 100644 --- a/src/render/cesium/cameraGoldens.test.mjs +++ b/src/render/cesium/cameraGoldens.test.mjs @@ -2,15 +2,72 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { recordCameraGoldens } from './cameraGoldens.js'; +import { near } from '../conformance/expect.js'; + +// The recorder is deterministic, but platforms can differ in the last bits +// of trigonometric arithmetic. Keep structure and discrete values exact. +function compareTrace(actual, expected, path = []) { + const label = path.join('.'); + if (typeof expected === 'number') { + const key = path.at(-1); + if (['step', 'frames', 'count'].includes(key) || path[0] === 'viewport') { + assert.equal(actual, expected, label); + return; + } + const metres = + path.at(-2) === 'position' || + key === 'height' || + (path.at(-2) === 'view' && Number(key) >= 12 && Number(key) <= 14); + near(actual, expected, metres ? 1e-8 : 1e-14, label); + return; + } + if (expected !== null && typeof expected === 'object') { + assert.ok(actual !== null && typeof actual === 'object', label); + assert.equal(Array.isArray(actual), Array.isArray(expected), label); + assert.deepEqual(Object.keys(actual), Object.keys(expected), label); + if (Array.isArray(expected)) + assert.equal(actual.length, expected.length, label); + for (const key of Object.keys(expected)) + compareTrace(actual[key], expected[key], [...path, key]); + return; + } + assert.equal(actual, expected, label); +} test('the committed camera traces are what the installed Cesium records', async () => { - const committed = readFileSync( - new URL('../conformance/goldens/camera.json', import.meta.url), - 'utf8', - ); - const recorded = `${JSON.stringify(await recordCameraGoldens(), null, 2)}\n`; - assert.ok( - recorded === committed, - 'goldens/camera.json is out of date: run node scripts/record-camera-goldens.mjs and review the diff', + const committed = JSON.parse( + readFileSync( + new URL('../conformance/goldens/camera.json', import.meta.url), + 'utf8', + ), ); + compareTrace(await recordCameraGoldens(), committed); +}); + +test('trace comparison tolerates only numeric roundoff, preserving structure and counters', () => { + const expected = { + position: { x: 1000 }, + pitch: 0.3, + frames: 2, + values: [1], + label: 'pose', + }; + compareTrace( + { ...expected, position: { x: 1000 + 2e-9 }, pitch: 0.3 + 1e-16 }, + expected, + ); + for (const actual of [ + { ...expected, extra: true }, + { ...expected, values: [] }, + { ...expected, position: { x: 1000 + 2e-8 } }, + { ...expected, pitch: 0.3 + 2e-14 }, + { ...expected, frames: 2 + 1e-15 }, + { ...expected, pitch: NaN }, + { ...expected, pitch: null }, + { ...expected, pitch: '0.3' }, + { ...expected, label: 'other' }, + ]) + assert.throws(() => compareTrace(actual, expected)); + const { label, ...missing } = expected; + assert.throws(() => compareTrace(missing, expected)); }); diff --git a/src/render/cesium/cameraPort.test.mjs b/src/render/cesium/cameraPort.test.mjs index 967ff1f761..9d81e928fa 100644 --- a/src/render/cesium/cameraPort.test.mjs +++ b/src/render/cesium/cameraPort.test.mjs @@ -416,7 +416,16 @@ test('a pose measures heading, pitch and roll as Cesium does, and leaves the cam const pitch = camera.pitch; const roll = camera.roll; assert.equal(pose.heading, heading, `heading ${k}`); - assert.ok(Math.abs(pose.pitch - pitch) < 1e-12, `pitch ${k}`); + assert.ok( + pose.pitch >= -Math.PI / 2 && pose.pitch <= Math.PI / 2, + `canonical pitch ${k}`, + ); + // Near vertical, acos amplifies last-bit direction differences. Match + // the conformance suite's well-conditioned pitch comparison. + assert.ok( + Math.abs(Math.sin(pose.pitch) - Math.sin(pitch)) < 1e-12, + `pitch ${k}`, + ); const turn = Math.abs(pose.roll - roll); assert.ok(Math.min(turn, 2 * Math.PI - turn) < 1e-12, `roll ${k}`); }