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{ const c=optics.liveCalibration(fixture()),width=384,height=192,data=new Uint8ClampedArray(width*height*4); for(let y=0;y=.75&&gain<=4/3); });