feat(receiver): 持久化接收机发现端点并支持位置备注信息
This commit is contained in:
+28
-6
@@ -6,12 +6,15 @@ import {spawn} from 'node:child_process';
|
||||
import assert from 'node:assert/strict';
|
||||
import {DatabaseSync} from 'node:sqlite';
|
||||
import {inflateRawSync} from 'node:zlib';
|
||||
import {randomBytes} from 'node:crypto';
|
||||
const repo=path.resolve(import.meta.dirname,'..');
|
||||
const root=await mkdtemp(path.join(tmpdir(),'RID-中文验证-'));
|
||||
const base='http://127.0.0.1:8787';
|
||||
const png='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aocsAAAAASUVORK5CYII=';
|
||||
let child;let output='';
|
||||
let spawnError;
|
||||
let sessionCookie='';
|
||||
const account={username:'registry-verification',password:randomBytes(24).toString('base64url')};
|
||||
async function start(command,args){
|
||||
// Never attach to, or mutate, an unrelated service already listening on this port.
|
||||
let occupied=false;try{await fetch(base+'/api/records',{signal:AbortSignal.timeout(1000)});occupied=true;}catch{}
|
||||
@@ -19,10 +22,29 @@ async function start(command,args){
|
||||
output='';spawnError=null;child=spawn(command,args,{cwd:root,windowsHide:true,stdio:['ignore','pipe','pipe']});
|
||||
child.stdout.on('data',b=>output+=b);child.stderr.on('data',b=>output+=b);
|
||||
child.on('error',e=>{spawnError=e;output+=e.message;});
|
||||
for(let i=0;i<150;i++){if(spawnError||child.exitCode!==null)throw Error(output);try{const r=await fetch(base+'/api/records');if(r.ok)return;}catch{}await new Promise(r=>setTimeout(r,100));}throw Error('启动超时 '+output);
|
||||
sessionCookie='';
|
||||
for(let i=0;i<150;i++){
|
||||
if(spawnError||child.exitCode!==null)throw Error(output);
|
||||
const status=output.includes('FPV-RID Rust 登记站')
|
||||
?await fetch(base+'/api/auth/status',{signal:AbortSignal.timeout(1000)}).catch(()=>null)
|
||||
:null;
|
||||
if(status?.ok){
|
||||
const {configured}=await status.json();
|
||||
const session=await fetch(base+(configured?'/api/auth/login':'/api/auth/setup'),{
|
||||
method:'POST',headers:{origin:base,'content-type':'application/json'},body:JSON.stringify(account)
|
||||
});
|
||||
assert.ok(session.ok,`验收账号登录失败:HTTP ${session.status}`);
|
||||
sessionCookie=session.headers.get('set-cookie')?.split(';')[0]??'';
|
||||
assert.match(sessionCookie,/^rid_session=/);
|
||||
return;
|
||||
}
|
||||
await new Promise(r=>setTimeout(r,100));
|
||||
}
|
||||
throw Error('启动超时 '+output);
|
||||
}
|
||||
async function stop(){if(child&&!spawnError&&child.exitCode===null){const exited=new Promise(r=>child.once('exit',r));if(child.kill())await exited;}child=null;}
|
||||
async function api(route,method='GET',body,status=200){const r=await fetch(base+route,{method,headers:body?{'content-type':'application/json'}:{},body:body?JSON.stringify(body):undefined});const v=await r.json();assert.equal(r.status,status,JSON.stringify(v));return v;}
|
||||
function authenticated(route,options={}){return fetch(base+route,{...options,headers:{cookie:sessionCookie,...options.headers}});}
|
||||
async function api(route,method='GET',body,status=200){const r=await authenticated(route,{method,headers:body?{'content-type':'application/json'}:{},body:body?JSON.stringify(body):undefined});const v=await r.json();assert.equal(r.status,status,JSON.stringify(v));return v;}
|
||||
function unzip(bytes){
|
||||
const end=bytes.lastIndexOf(Buffer.from([0x50,0x4b,0x05,0x06]));assert.ok(end>=0);
|
||||
const entries=new Map();let offset=bytes.readUInt32LE(end+16);
|
||||
@@ -40,7 +62,7 @@ function unzip(bytes){
|
||||
}
|
||||
async function exports(id,label){
|
||||
for(const kind of ['archive','excel']){
|
||||
const r=await fetch(base+'/api/export/'+kind,{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({ids:[id]})});assert.equal(r.status,200);
|
||||
const r=await authenticated('/api/export/'+kind,{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({ids:[id]})});assert.equal(r.status,200);
|
||||
const bytes=Buffer.from(await r.arrayBuffer());const entries=unzip(bytes);
|
||||
if(kind==='archive'){
|
||||
const manifest=JSON.parse(entries.get('档案库/records.json').toString());assert.equal(manifest[0].id,id);assert.equal(manifest[0].fpvCode,'FPV00000001');
|
||||
@@ -52,7 +74,7 @@ async function exports(id,label){
|
||||
}
|
||||
await writeFile(path.join(root,`${label}-${kind}.zip`),bytes);
|
||||
}
|
||||
const r=await fetch(base+`/api/records/${id}/archive`);const html=await r.text();assert.match(html,/data:image\/png;base64,/);assert.ok(!html.includes('${'));assert.ok(html.includes('<测试>'));
|
||||
const r=await authenticated(`/api/records/${id}/archive`);const html=await r.text();assert.match(html,/data:image\/png;base64,/);assert.ok(!html.includes('${'));assert.ok(html.includes('<测试>'));
|
||||
}
|
||||
try{
|
||||
const exe=path.resolve(process.argv[2]||path.join(repo,'dist/FPV-RID-Rust/fpv-rid-registry.exe'));
|
||||
@@ -71,7 +93,7 @@ try{
|
||||
assert.equal((await api('/api/resolve?url='+encodeURIComponent(base+'/r/'+id))).id,id);
|
||||
await api('/api/resolve?url='+encodeURIComponent('http://example.com/r/'+id),'GET',undefined,400);
|
||||
await exports(id,'rust');db=new DatabaseSync(dbPath);assert.equal(db.prepare('SELECT hex(image) AS bytes FROM photos').get().bytes,before);db.close();
|
||||
const blocked=await fetch(base+'/api/records',{headers:{origin:'https://example.com'}});assert.equal(blocked.status,403);
|
||||
const blocked=await authenticated('/api/records',{headers:{origin:'https://example.com'}});assert.equal(blocked.status,403);
|
||||
const p=await api('/api/records?page=1&pageSize=1');assert.equal(p.total,1);assert.equal(p.records.length,1);
|
||||
await api('/api/records?page=0','GET',undefined,400);
|
||||
const edited=await api('/api/records/'+id,'PATCH',{modelName:'',ownerName:'新登记人'});assert.equal(edited.record.modelName,'');
|
||||
@@ -79,7 +101,7 @@ try{
|
||||
await api('/api/records/'+id,'PATCH',{ownerName:'must rollback',photo1:'invalid'},400);
|
||||
assert.equal((await api('/api/resolve?url='+encodeURIComponent(base+'/r/'+id))).ownerName,'新登记人');
|
||||
await api('/api/records/'+id,'PATCH',{photo1:null,photo2:png});
|
||||
assert.equal((await fetch(base+'/api/records/'+id+'/photos/2')).status,200);
|
||||
assert.equal((await authenticated('/api/records/'+id+'/photos/2')).status,200);
|
||||
await api('/api/records/'+id+'/photos/1','DELETE');await api('/api/records/'+id+'/photos/1','GET',undefined,404);
|
||||
await api('/api/records','POST',{serialNo:'TEST-SN-001',photo1:png});
|
||||
// Simulate an archive directory failure without modifying the active database.
|
||||
|
||||
Reference in New Issue
Block a user