Files
NODEDC_MISSION_CORE/apps/control-station/test/insta360Panorama.test.mjs
T
DCCONSTRUCTIONS 70ef3f9f1c chore(storage): bound build retention, caches and test resources
Track build ownership, qualified pins and bounded cache/log lifecycles. Move Vite fixtures to a job-owned server helper and preserve private outputs outside Git.
2026-10-29 09:09:58 +03:00

41 lines
3.4 KiB
JavaScript

import assert from 'node:assert/strict';
import {after,before,test} from 'node:test';
import {createServer} from './support/vite-server.mjs';
let server,optics,exposure;
before(async()=>{server=await createServer({appType:'custom',logLevel:'silent',server:{middlewareMode:true}});optics=await server.ssrLoadModule('../../plugins/insta360-x4/frontend/src/panoramaCalibration.ts');exposure=await server.ssrLoadModule('../../plugins/insta360-x4/frontend/src/panoramaExposure.ts');});
after(async()=>{await server?.close();});
const block=(cx)=>[1,240,240,cx,300,0,0,90,0,0,0,0,0,0,0,0,1600,600,71];
const fixture=()=>({offsets:[[2,...block(400),...block(1200),3*65536].join('_')],crop:{source_width:800,source_height:600,width:576,height:576,x:0,y:0}});
const close=(a,b)=>assert.ok(Math.abs(a-b)<1e-9,`${a} != ${b}`);
test('rectangular sensor calibration maps each centred preview crop independently',()=>{
const c=optics.liveCalibration(fixture());assert.equal(c.version,3);assert.equal(c.width,2);
assert.deepEqual(c.lenses.map(l=>l.center),[[.5,.5],[1.5,.5]]);
assert.deepEqual(optics.projectLens(c,0,[-1,0,0],90),[.25,.5]);
assert.deepEqual(optics.projectLens(c,1,[1,0,0],270),[.75,.5]);
const f=fixture();f.crop.x=12;f.crop.y=-6;const shifted=optics.liveCalibration(f);
close(shifted.lenses[0].center[0],.5-12/576);close(shifted.lenses[1].center[1],.5+6/576);
});
test('unrecognised, malformed and incompatible optics cannot appear calibrated',()=>{
for(const v of [null,{},'x',NaN,{offsets:['2_NaN']}, {...fixture(),offsets:['2__1']}, {...fixture(),offsets:[fixture().offsets[0].replace('_196608','_393216')]}, {...fixture(),crop:{...fixture().crop,width:900}}, {...fixture(),crop:{...fixture().crop,x:999}}, {...fixture(),crop:{...fixture().crop,width:575}}])assert.equal(optics.liveCalibration(v),null);
});
test('rays in both seam bands remain within the correct lens windows at all mounting rotations',()=>{
const c=optics.liveCalibration(fixture());
for(const turn of [0,90,180,270])for(const sign of [-1,1])for(let lat=-70;lat<=70;lat+=10){const a=lat*Math.PI/180,ray=[0,sign*Math.cos(a),Math.sin(a)];
for(const i of [0,1]){const uv=optics.projectLens(c,i,ray,turn);assert.ok(uv);assert.ok(uv[0]>=i*.5&&uv[0]<=(i+1)*.5);assert.ok(uv[1]>=0&&uv[1]<=1);}
}
assert.equal(optics.projectLens(c,0,[1,0,0],0),null);
});
test('overlap exposure matching brings equal scenes together without chasing black or saturated pixels',()=>{
const c=optics.liveCalibration(fixture()),width=384,height=192,data=new Uint8ClampedArray(width*height*4);
for(let y=0;y<height;y++)for(let x=0;x<width;x++){const k=(y*width+x)*4;data.fill(x<width/2?100:150,k,k+3);data[k+3]=255;}
const gains=exposure.overlapGains(data,width,height,c,[90,270]);assert.ok(gains);
for(let k=0;k<3;k++)close(100*gains[0][k],150*gains[1][k]);
data.fill(0);assert.equal(exposure.overlapGains(data,width,height,c,[90,270]),null);
data.fill(255);assert.equal(exposure.overlapGains(data,width,height,c,[90,270]),null);
});
test('extreme brightness mismatch is bounded rather than producing unlimited gain',()=>{
const c=optics.liveCalibration(fixture()),width=384,height=192,data=new Uint8ClampedArray(width*height*4);
for(let y=0;y<height;y++)for(let x=0;x<width;x++){const k=(y*width+x)*4;data.fill(x<width/2?25:225,k,k+3);data[k+3]=255;}
const gains=exposure.overlapGains(data,width,height,c,[90,270]);for(const lens of gains)for(const gain of lens)assert.ok(gain>=.75&&gain<=4/3);
});