12 lines
4 KiB
JavaScript
12 lines
4 KiB
JavaScript
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'),crypto=require('node:crypto');
|
|
const {createServer}=require('./server.cjs');
|
|
async function fixture(t,{capacity=20,handler}={}){const root=fs.mkdtempSync(path.join(os.tmpdir(),'guanghu-entry-test-'));fs.writeFileSync(path.join(root,'entry-data.json'),JSON.stringify({world_id:'GLW-2.0-0001',version:'test',repository_url:'https://example.invalid/entry',topology:{default_persona:null},repositories:[],source_registered_persona_count:3}));fs.writeFileSync(path.join(root,'CURRENT.json'),'{}');const token='fixture-token-not-a-real-credential',cfg=path.join(root,'config.json');fs.writeFileSync(cfg,JSON.stringify({clients:[{id:'test',key_sha256:crypto.createHash('sha256').update(token).digest('hex'),expires_at:'2099-01-01T00:00:00Z',personas:['ICE-P-ONE']}],personas:[{persona_id:'ICE-P-ONE',name:'fixture'}],requests_per_minute:capacity}),{mode:0o600});let calls=0;const server=createServer({root,configPath:cfg,questionHandler:handler||(async(r,i)=>{calls++;return {persona_id:r.persona_id,reply:'我依据来源回答。',no_neural_state_write:true,no_execution_authority:true};})});await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));t.after(()=>{server.closeAllConnections();server.close();fs.rmSync(root,{recursive:true,force:true});});const url='http://127.0.0.1:'+server.address().port;return {url,token,calls:()=>calls,post:async(body,auth=token)=>fetch(url+'/v1/ask',{method:'POST',headers:{'content-type':'application/json',...(auth?{authorization:'Bearer '+auth}:{})},body:JSON.stringify(body)})};}
|
|
const input=()=>({persona_id:'ICE-P-ONE',question:'你依据什么回答?',tool_host:'fixture-host',session_id:'fixture-session',request_id:crypto.randomUUID()});
|
|
test('public entry stays neutral without explicit persona',async t=>{const f=await fixture(t),r=await fetch(f.url+'/v1/entry'),j=await r.json();assert.equal(r.status,200);assert.equal(j.default_persona,null);assert.equal(f.calls(),0);});
|
|
test('anonymous question is rejected without reaching a persona',async t=>{const f=await fixture(t),r=await f.post(input(),null);assert.equal(r.status,401);assert.equal(f.calls(),0);});
|
|
test('foreign persona and injected write/human-food fields are rejected',async t=>{const f=await fixture(t);assert.equal((await f.post({...input(),persona_id:'ICE-P-OTHER'})).status,403);for(const k of ['write','source_role','human_anchor','target_url'])assert.equal((await f.post({...input(),[k]:'injected'})).status,400);assert.equal(f.calls(),0);});
|
|
test('missing native coordinates and empty question are rejected',async t=>{const f=await fixture(t);for(const k of ['tool_host','session_id','request_id','question']){const b=input();delete b[k];assert.equal((await f.post(b)).status,400);}assert.equal(f.calls(),0);});
|
|
test('accepted reply is private, exact replay is cached, request id cannot be rebound',async t=>{const f=await fixture(t),i=input(),r=await f.post(i);assert.equal(r.status,200);assert.equal(r.headers.get('cache-control'),'no-store');assert.equal((await r.json()).no_execution_authority,true);assert.equal((await f.post(i)).status,200);assert.equal(f.calls(),1);assert.equal((await f.post({...i,question:'different'})).status,409);});
|
|
test('capacity limits do not call the persona again',async t=>{const f=await fixture(t,{capacity:1});assert.equal((await f.post(input())).status,200);assert.equal((await f.post(input())).status,429);assert.equal(f.calls(),1);});
|
|
test('upstream errors withhold replies and never leak their text',async t=>{const f=await fixture(t,{handler:async()=>{throw Error('/private/secret/path');}});const r=await f.post(input()),j=await r.json();assert.equal(r.status,503);assert.equal(j.reply_withheld,true);assert.equal(JSON.stringify(j).includes('/private'),false);});
|
|
test('no public write or feed route exists',async t=>{const f=await fixture(t),r=await fetch(f.url+'/v1/feed',{method:'POST',body:'{}'});assert.equal(r.status,405);assert.equal(f.calls(),0);});
|