diff --git a/integrations/iphone-lidar/web/codec.test.mjs b/integrations/iphone-lidar/web/codec.test.mjs new file mode 100644 index 00000000..424d3364 --- /dev/null +++ b/integrations/iphone-lidar/web/codec.test.mjs @@ -0,0 +1,44 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { decodeLiDARPacket, depthToPointCloud } from './codec.mjs'; + +function b64(bytes) { + return Buffer.from(bytes).toString('base64'); +} + +test('decodes u16 millimeter depth and confidence', () => { + const packet = { + type: 'ruview.lidar.depth.v1', + intrinsics: { fx: 100, fy: 100, cx: 1, cy: 1, imageWidth: 2, imageHeight: 2 }, + pose: { matrix: Array(16).fill(0) }, + depth: { + width: 2, + height: 2, + encoding: 'u16le-mm+u8-confidence', + millimetersBase64: b64([0xe8,0x03,0xd0,0x07,0xb8,0x0b,0xa0,0x0f]), + confidenceBase64: b64([2,2,1,0]), + }, + provenance: { sensor: 'test', source: 'live', privacyClass: 'geometry-only', sequence: 1, timestampNs: 1, schema: 'ruview.lidar.depth.v1' }, + }; + + const frame = decodeLiDARPacket(packet); + assert.deepEqual(Array.from(frame.depth.meters), [1,2,3,4]); + assert.deepEqual(Array.from(frame.depth.confidence), [2,2,1,0]); +}); + +test('rejects malformed payload length', () => { + assert.throws(() => decodeLiDARPacket({ + type: 'ruview.lidar.depth.v1', + depth: { width: 2, height: 2, encoding: 'u16le-mm+u8-confidence', millimetersBase64: b64([1,2]), confidenceBase64: b64([1,1,1,1]) }, + }), /length mismatch/); +}); + +test('projects depth into a point cloud and honors confidence', () => { + const frame = { + intrinsics: { fx: 100, fy: 100, cx: 0, cy: 0, imageWidth: 2, imageHeight: 2 }, + depth: { width: 2, height: 2, meters: Float32Array.from([1,1,1,1]), confidence: Uint8Array.from([2,0,2,0]) }, + }; + const points = depthToPointCloud(frame, 1); + assert.equal(points.length, 2); + assert.deepEqual(points[0], [0, 0, -1]); +});