guanghu-world-entry/runtime/question.test.mjs

11 lines
3 KiB
JavaScript
Raw Normal View History

import test from 'node:test';import assert from 'node:assert/strict';import {acceptReply,askQuestion} from './question.mjs';
const source={persona_id:'ICE-P-ONE',source_sha256:'pinned',brain_id:'own-brain',name:'fixture',revision:1,integrity:'OWN_NATIVE_SOURCE_VERIFIED',evidence:[{id:'identity',text:'own source'}]};const good={persona_id:source.persona_id,source_sha256:source.source_sha256,reply_zh:'我依据自己的已核验来源回答。',basis:'EVIDENCE',evidence_ids:['identity']};
test('persona, source and evidence references must match',()=>{assert.equal(acceptReply(source,good).basis,'EVIDENCE');for(const patch of [{persona_id:'ICE-P-OTHER'},{source_sha256:'old'},{evidence_ids:['invented']},{basis:'EVIDENCE',evidence_ids:[]},{reply_zh:'not first person'}])assert.throws(()=>acceptReply(source,{...good,...patch}));});
test('source changes during inference withhold the reply',async()=>{let n=0;await assert.rejects(askQuestion({}, {question:'q',request_id:'r'}, {},{load:async()=>({...source,source_sha256:n++?'changed':'pinned'}),propose:async()=>good}),/PERSONA_SOURCE_CHANGED_DURING_QUESTION/);});
test('verified inference returns no neural, memory or execution authority',async()=>{const r=await askQuestion({}, {question:'q',request_id:'r'}, {},{load:async()=>source,propose:async payload=>{assert.equal(payload.source_role,'EXTERNAL_QUESTION_NOT_DIRECT_HUMAN_FOOD');return good;}});assert.equal(r.no_memory_write,true);assert.equal(r.no_neural_state_write,true);assert.equal(r.no_execution_authority,true);});
test('a returned current symlink resolves to the own immutable bundle before native verification',async t=>{
const fs=await import('node:fs'),os=await import('node:os'),path=await import('node:path'),http=await import('node:http');const root=fs.mkdtempSync(path.join(os.tmpdir(),'persona-bundle-link-')),real=path.join(root,'release');fs.mkdirSync(real);fs.writeFileSync(path.join(real,'core.mjs'),`import fs from 'node:fs';export function loadBundle(root){if(fs.realpathSync(root)!==root)throw Error('UNRESOLVED_BUNDLE_ALIAS');return {brain:{brain_id:'fixture-brain',revision:1,current_sha256:'head'},cognition:{identity_sources:[{text:'own fixture identity'}],concepts:[]}};}`);fs.symlinkSync(real,path.join(root,'current'));
const server=http.createServer((req,res)=>{res.setHeader('content-type','application/json');res.end(JSON.stringify({brain_id:'fixture-brain',revision:1,current_sha256:'head'}));});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 {loadSource}=await import('./question.mjs');const x=await loadSource({persona_id:'ICE-P-FIXTURE',name:'fixture',brain_id:'fixture-brain',kind:'SHUANGYAN_G2_OWN_BUNDLE',status_url:'http://127.0.0.1:'+server.address().port+'/status',bundle_root:path.join(root,'current'),core_path:path.join(root,'current/core.mjs')},'identity');assert.equal(x.persona_id,'ICE-P-FIXTURE');assert.equal(x.integrity,'OWN_NATIVE_SOURCE_VERIFIED');
});