Integrate infrastructure preparation and qualified map delivery with local scenes. Add semantic editing, object provenance, survey caching and operator scene tools.
74 lines
5.4 KiB
JavaScript
74 lines
5.4 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {build} from 'esbuild';
|
|
const built=await build({entryPoints:[new URL('../src/core/simulation/infrastructureAnalysis.ts',import.meta.url).pathname],bundle:true,write:false,platform:'node',format:'esm',plugins:[{name:'analysis-fixture',setup(b){
|
|
b.onResolve({filter:/^react$/},()=>({path:'react',namespace:'fixture'}));
|
|
b.onResolve({filter:/^\.\/urbanMap$/},()=>({path:'map',namespace:'fixture'}));
|
|
b.onLoad({filter:/.*/,namespace:'fixture'},args=>({contents:args.path==='react'?`
|
|
export const useRef=v=>({current:v});
|
|
export const useState=v=>{const index=globalThis.analysisFixture.index++;return [index===0?globalThis.analysisFixture.seed:v,value=>globalThis.analysisFixture.writes.push({index,value})];};
|
|
export const useEffect=f=>globalThis.analysisFixture.effects.push(f);
|
|
`:`export const loadUrbanMap=async()=>{throw new Error('Unexpected stale map load');};`}));
|
|
}}]});
|
|
const {useInfrastructureAnalysis,analysisProgressLabel}=await import('data:text/javascript;base64,'+Buffer.from(built.outputFiles[0].contents).toString('base64'));
|
|
const flush=()=>new Promise(resolve=>setImmediate(resolve));
|
|
const deferred=()=>{let resolve;const promise=new Promise(r=>resolve=r);return {promise,resolve};};
|
|
const response=value=>({ok:true,json:async()=>value});
|
|
const queued={key:'a'.repeat(64),status:'queued',phase:'queued',completed:0,total:0,error:null};
|
|
async function fixture(run,seed=null){
|
|
const original=Object.fromEntries(['analysisFixture','fetch','setTimeout','clearTimeout'].map(k=>[k,globalThis[k]]));
|
|
const state={index:0,seed,writes:[],effects:[]},calls=[],timers=[],get=deferred(),post=deferred();
|
|
globalThis.analysisFixture=state;
|
|
globalThis.setTimeout=(f,ms)=>{timers.push({f,ms});return timers.length;};globalThis.clearTimeout=()=>{};
|
|
globalThis.fetch=async(url,init={})=>{calls.push({url,init});return init.method==='POST'?post.promise:get.promise;};
|
|
const hook=useInfrastructureAnalysis('fixture','b'.repeat(64),()=>{throw new Error('Unexpected stale map callback');});
|
|
const cleanups=state.effects.map(f=>f());let cleaned=false;
|
|
const clean=()=>{if(!cleaned){cleaned=true;cleanups.forEach(f=>f?.());}};
|
|
try{await run({hook,calls,get,post,state,timers,clean});}
|
|
finally{clean();await flush();for(const [key,value]of Object.entries(original)){if(value===undefined)delete globalThis[key];else globalThis[key]=value;}}
|
|
}
|
|
test('twenty immediate calculation clicks make one request before React rerenders',async()=>fixture(async f=>{
|
|
const attempts=Array.from({length:20},()=>f.hook.start(true));
|
|
assert.equal(f.calls.filter(c=>c.init.method==='POST').length,1);
|
|
f.post.resolve(response(queued));await Promise.all(attempts);f.get.resolve(response(null));await flush();
|
|
assert.equal(f.state.writes.filter(w=>w.index===0).at(-1).value.key,queued.key);
|
|
}));
|
|
test('a poll predating calculation cannot replace the acknowledged queued job',async()=>fixture(async f=>{
|
|
const starting=f.hook.start(true);f.post.resolve(response(queued));await starting;
|
|
f.get.resolve(response({...queued,status:'ready'}));await flush();
|
|
assert.deepEqual(f.state.writes.filter(w=>w.index===0).at(-1).value,queued);
|
|
}));
|
|
test('polling pauses during cancellation and late replies cannot undo unmount',async()=>fixture(async f=>{
|
|
f.get.resolve(response({...queued,status:'running'}));await flush();
|
|
const cancelling=f.hook.cancel();const before=f.calls.length;
|
|
await f.timers.at(-1).f();assert.equal(f.calls.length,before);
|
|
assert.match(f.calls.at(-1).url,new RegExp('/'+queued.key+'/cancel$'));
|
|
f.clean();const writes=f.state.writes.length;
|
|
f.post.resolve(response({...queued,status:'cancelled'}));await cancelling;await flush();
|
|
assert.equal(f.state.writes.length,writes);
|
|
}, {...queued,status:'running'}));
|
|
test('retry after an unknown network outcome keeps the same request identity',async()=>fixture(async f=>{
|
|
f.post.resolve({ok:false,json:async()=>({detail:'Try again'})});
|
|
await f.hook.start(true);await f.hook.start(true);
|
|
const bodies=f.calls.filter(c=>c.init.method==='POST').map(c=>JSON.parse(c.init.body));
|
|
assert.equal(bodies.length,2);assert.equal(bodies[0].request_id,bodies[1].request_id);
|
|
f.get.resolve(response(null));await flush();
|
|
}));
|
|
|
|
test('an observed terminal generation acknowledges a lost response before the next retry',async()=>fixture(async f=>{
|
|
f.post.resolve({ok:false,json:async()=>({detail:'Response lost'})});
|
|
await f.hook.start(true);
|
|
const first=JSON.parse(f.calls.find(c=>c.init.method==='POST').init.body).request_id;
|
|
f.get.resolve(response({...queued,status:'failed',request_id:first}));await flush();
|
|
await f.timers.at(-1).f();await flush();
|
|
await f.hook.start(true);
|
|
const second=JSON.parse(f.calls.filter(c=>c.init.method==='POST').at(-1).init.body).request_id;
|
|
assert.notEqual(second,first,'a new attempt must not return the old terminal generation');
|
|
}));
|
|
|
|
test('stage counts are explicit and lost connection never retains an ETA',()=>{
|
|
const job={...queued,status:'running',progress:{label:'Построение видов сцены',stage:2,stages:90,eta_seconds:600,stale:false}};
|
|
assert.equal(analysisProgressLabel(job),'Построение видов сцены · этап 2 из 90 · ≈ 10 мин осталось');
|
|
assert.equal(analysisProgressLabel({...job,progress:{...job.progress,stale:true,eta_seconds:null}}),'Связь с расчётом потеряна · этап 2 из 90');
|
|
});
|