Files
NODEDC_MISSION_CORE/apps/control-station/test/locationImportQueue.test.mjs
T

102 lines
7.2 KiB
JavaScript

import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {test} from 'node:test';
import ts from 'typescript';
async function load(name){
const code=ts.transpileModule(readFileSync(new URL(`../src/core/simulation/${name}.ts`,import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext}}).outputText;
return import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
}
const {createLocationImportQueue}=await load('locationImportQueue');
const {classifySimulationSource,candidatesFromDrop}=await load('sourceFiles');
const tick=()=>new Promise(resolve=>setImmediate(resolve));
const deferred=()=>{let resolve;const promise=new Promise(r=>{resolve=r;});return {promise,resolve};};
const archive={kind:'lcc',candidates:[{logicalPath:'scene.zip',file:new File(['source'],'scene.zip')}]};
test('row precedes validation; unsupported input never creates a server project',async()=>{
let created=0;const queue=createLocationImportQueue({create:()=>{created++;}});
const input=deferred(),id=queue.enqueue('bad.las',input.promise.then(classifySimulationSource));
assert.equal(queue.getSnapshot()[0].phase,'checking');
input.resolve([{logicalPath:'bad.las',file:new File(['x'],'bad.las')}]);await tick();
assert.equal(created,0);assert.match(queue.getSnapshot()[0].error,/Формат не поддерживается/);
queue.forget(id);assert.equal(queue.getSnapshot().length,0);
});
test('background upload survives loss of all UI subscribers; queued files run sequentially',async()=>{
const release=deferred(),order=[];let active=0,maxActive=0;
const queue=createLocationImportQueue({create:async name=>({projectId:name}),upload:async project=>{
active++;maxActive=Math.max(maxActive,active);order.push(project.projectId);
if(project.projectId==='first')await release.promise;active--;return {...project,status:'queued'};
}});
const unsubscribe=queue.subscribe(()=>{});
queue.enqueue('first',Promise.resolve(archive));await tick();unsubscribe();
queue.enqueue('second',Promise.resolve(archive));queue.enqueue('third',Promise.resolve(archive));await tick();
assert.equal(queue.getSnapshot().find(row=>row.name==='second').phase,'waiting');
release.resolve();await tick();await tick();
assert.deepEqual(order,['first','second','third']);assert.equal(maxActive,1);
assert.ok(queue.getSnapshot().every(row=>row.phase==='submitted'));
});
test('retry after transfer failure reuses the created project instead of making a duplicate',async()=>{
let creates=0,attempts=0;
const queue=createLocationImportQueue({create:async()=>{creates++;return {projectId:'same'};},upload:async project=>{
if(!attempts++)throw new Error('Связь прервана');return {...project,status:'queued'};
}});
const id=queue.enqueue('scene',Promise.resolve(archive));await tick();
assert.equal(queue.getSnapshot()[0].phase,'failed');queue.retry(id);await tick();
assert.equal(creates,1);assert.equal(queue.getSnapshot()[0].phase,'submitted');
});
test('drop captures file references before asynchronous directory enumeration revokes DataTransfer',async()=>{
let live=true;const file=new File(['x'],'scene.lcc');
const entry={isDirectory:true,createReader:()=>{let done=false;return {readEntries:callback=>{
setImmediate(()=>{callback(done?[]:[{isFile:true,fullPath:'/scan/scene.lcc',file:callback=>callback(file)}]);done=true;});
}};}};
const second=new File(['mesh'],'mesh.ply');
const transfer={files:[],items:[{getAsFile:()=>null,webkitGetAsEntry:()=>entry},{getAsFile:()=>live?second:null}]};
const result=candidatesFromDrop(transfer);live=false;
assert.deepEqual((await result).map(row=>row.logicalPath),['scan/scene.lcc','mesh.ply']);
});
test('format dispatch checks emptiness, PLY size limit, archive multiplicity and LCC descriptor count',()=>{
const candidate=(name,size=1)=>({logicalPath:name,file:{name,size}});
assert.equal(classifySimulationSource([candidate('SCAN.PLY')]).kind,'ply');
assert.equal(classifySimulationSource([candidate('scene.7z')]).kind,'lcc');
assert.throws(()=>classifySimulationSource([candidate('scene.ply',0)]),/пуст/);
assert.throws(()=>classifySimulationSource([candidate('scene.ply',9*1024**3)]),/8 ГБ/);
assert.throws(()=>classifySimulationSource([candidate('a.zip'),candidate('b.zip')]),/одним файлом/);
assert.throws(()=>classifySimulationSource([candidate('folder/a.lcc'),candidate('folder/b.lcc2')]),/ровно одна/);
});
test('PLY and archive forms share durable project upload and preserve source options',async()=>{
const calls=[];
const queue=createLocationImportQueue({create:async(name,source)=>{calls.push(source);return {projectId:name};},upload:async project=>({...project,status:'queued'})});
const source={...classifySimulationSource([{logicalPath:'world.ply',file:new File(['ply'],'world.ply')}]),sceneType:'interior',attribution:{author:'Owner',license:'Private',source_url:null}};
queue.enqueue('ply',Promise.resolve(source));await tick();
assert.equal(calls.length,1);assert.equal(calls[0].kind,'ply');assert.equal(calls[0].sceneType,'interior');
assert.equal(calls[0].attribution.author,'Owner');assert.equal(queue.getSnapshot()[0].phase,'submitted');
});
test('delete during creation waits for server identity and never starts its upload',async()=>{
const created=deferred(),events=[];
const queue=createLocationImportQueue({create:()=>created.promise,upload:async()=>events.push('upload'),delete:async p=>events.push('delete:'+p.projectId)});
const id=queue.enqueue('late',Promise.resolve(archive));await tick();
const removal=queue.remove(id);await tick();assert.equal(queue.getSnapshot()[0].phase,'deleting');
created.resolve({projectId:'late'});await removal;
assert.deepEqual(events,['delete:late']);assert.equal(queue.getSnapshot()[0].phase,'deleting');
queue.reconcile([],0);assert.equal(queue.getSnapshot().length,1);
queue.reconcile(['late'],Infinity);assert.equal(queue.getSnapshot().length,1);
queue.reconcile([],Infinity);assert.equal(queue.getSnapshot().length,0);
});
test('delete aborts active upload; failed delete remains retryable without restarting upload',async()=>{
let attempts=0,aborted=false,uploads=0;
const queue=createLocationImportQueue({create:async()=>({projectId:'active'}),upload:async(p,s,progress,signal)=>{
uploads++;await new Promise((resolve,reject)=>signal.addEventListener('abort',()=>{aborted=true;reject(new Error('abort'));}));
},delete:async()=>{if(!attempts++)throw new Error('offline');}});
const id=queue.enqueue('active',Promise.resolve(archive));await tick();
await assert.rejects(queue.remove(id),/offline/);assert.equal(aborted,true);
assert.equal(queue.getSnapshot()[0].deleteRequested,true);queue.retry(id);await tick();
assert.equal(uploads,1);await queue.remove(id);queue.reconcile([],Infinity);assert.equal(queue.getSnapshot().length,0);
});
test('deleting a checking row prevents a late validation result from admitting a project',async()=>{
const checked=deferred();let creates=0;
const queue=createLocationImportQueue({create:async()=>{creates++;}});
const id=queue.enqueue('checking',checked.promise);await queue.remove(id);checked.resolve(archive);await tick();
assert.equal(creates,0);assert.equal(queue.getSnapshot().length,0);
});