import {UI_URL, assertUiPortAvailable} from './ui-test-port.mjs'; const base=UI_URL; await assertUiPortAvailable(); // Real SQLite and server monitor, without a browser or physical receiver. import assert from 'node:assert/strict'; import {mkdtemp,writeFile} from 'node:fs/promises'; import {tmpdir} from 'node:os'; import path from 'node:path'; import {spawn} from 'node:child_process'; const exe=process.argv[2]||'D:/Rust_Contact/util/target/Rid_Drone_Receivev/release/fpv-rid-registry-fullstack.exe'; const root=await mkdtemp(path.join(tmpdir(),'RID-monitoring-')); let child,output='',seq=0;const delay=ms=>new Promise(r=>setTimeout(r,ms)); function start(){child=spawn(exe,['--data-root',root,'--simulation-api'],{env:{...process.env},windowsHide:true,stdio:['ignore','pipe','pipe']});child.stdout.on('data',b=>output+=b);child.stderr.on('data',b=>output+=b);child.on('error',e=>output+=e);} async function stop(){if(child?.exitCode===null){const done=new Promise(r=>child.once('exit',r));child.kill();await done;}} async function ready(){for(let i=0;i<150;i++){try{if((await fetch(base+'/api/geofences')).ok)return}catch{}await delay(100)}throw Error(output);} async function json(url,method='GET',body){const r=await fetch(base+url,{method,headers:body?{'content-type':'application/json'}:{},body:body?JSON.stringify(body):undefined});assert.equal(r.status,200,await r.clone().text());return r.json();} async function list(kind){const s=await json('/api/target-lists');return json('/api/target-lists/DRONE-A','PUT',{list_kind:kind,note:'测试',expected_revision:s.revision});} async function ingest(lon=120.0){return json('/api/realtime/ingest','POST',[{target_id:'DRONE-A',receiver_id:'TEST',source_epoch:1,sequence:++seq,longitude:lon,latitude:30.0,altitude_m:100,heading_deg:30}]);} async function events(){return (await json('/api/monitoring/events?limit=200')).entries;} async function count(expected){for(let i=0;i<80;i++){const es=await events();if(es.length===expected)return es;await delay(50)}throw Error('event count '+JSON.stringify(await events()));} let report={root,exe}; try{ start();await ready();assert.equal((await json('/api/maps/preferences')).zoom,15); let fences=await json('/api/geofences');assert.equal(fences.entries.length,0); const fence={id:'TEST-FENCE',name:'隔离验收区域',enabled:true,geometry:{type:'circle',center:[120,30],radius_m:500}}; fences=await json('/api/geofences/TEST-FENCE','PUT',{fence,expected_revision:0});assert.equal(fences.revision,1); assert.equal((await fetch(base+'/api/geofences/TEST-FENCE',{method:'PUT',headers:{'content-type':'application/json'},body:JSON.stringify({fence,expected_revision:0})})).status,400); const bad={...fence,geometry:{type:'polygon',vertices:[[0,0],[1,1],[0,1],[1,0]]}}; assert.equal((await fetch(base+'/api/geofences/TEST-FENCE',{method:'PUT',headers:{'content-type':'application/json'},body:JSON.stringify({fence:bad,expected_revision:1})})).status,400); await list('white');await ingest();await delay(400);await count(0); await list('black');let es=await count(2);assert.deepEqual(new Set(es.map(e=>e.event_type)),new Set(['blacklist','geofence_enter'])); for(let i=0;i<6;i++)await ingest();await delay(400);await count(2); // Multiple accepted crossings faster than the monitor interval must not be coalesced away. await ingest(121);await ingest(120);await ingest(121);await ingest(120);es=await count(4); assert.equal((await json('/api/monitoring/events?type=blacklist&target_id=DRONE-A')).entries.length,1); assert.equal((await json('/api/monitoring/events?fence_id=TEST-FENCE&limit=2')).entries.length,2); const page1=(await json('/api/monitoring/events?limit=2')).entries;const page2=(await json('/api/monitoring/events?limit=2&before='+page1.at(-1).id)).entries;assert.equal(new Set([...page1,...page2].map(e=>e.id)).size,4); assert.equal((await fetch(base+'/api/monitoring/events',{headers:{origin:'https://foreign.invalid'}})).status,403); await stop();start();await ready();await ingest();await delay(400);assert.equal((await events()).length,4);assert.equal((await json('/api/geofences')).entries[0].name,fence.name); const h=await json('/api/monitoring/status');assert.deepEqual(h,{droppedObservations:0,error:null}); await list('white');await delay(400);await list('black');await count(6); await json('/api/geofences/TEST-FENCE?revision=1','DELETE');await ingest(121);await ingest(120);await delay(400);await count(6); report={...report,status:'PASS',events:(await events()).length,health:h,checks:['default zoom15','versioned CRUD','invalid geometry rejected','white exemption','blacklist + fence enter','dedup frames','rapid crossings','filter and cursor pagination','foreign origin rejected','restart durable history and dedup','list transitions','deleted fence silent','no browser required']};console.log(JSON.stringify(report)); }finally{await stop();await writeFile(path.join(root,'report.json'),JSON.stringify(report,null,2));await writeFile(path.join(root,'server.log'),output);}