guanghu-ice-heart/05-光之湖系统/光湖大脑容器/test-shuangyan-replica.mjs

63 lines
3.6 KiB
JavaScript

#!/usr/bin/env node
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import net from 'node:net';
import crypto from 'node:crypto';
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { worldRuntimeUrl } from '../../WORLD-RUNTIME-BRIDGE.mjs';
const { createBrainState, feed, loadBrain, saveBrain, verifyBrainIntegrity } = await import(worldRuntimeUrl('brain-core.mjs'));
const dir = path.dirname(fileURLToPath(import.meta.url));
const root = fs.mkdtempSync(path.join(os.tmpdir(),'shuangyan-replica-'));
const port = await new Promise(resolve => {
const server=net.createServer(); server.listen(0,'127.0.0.1',()=>{
const value=server.address().port; server.close(()=>resolve(value));
});
});
let child;
try {
const key='a'.repeat(64), keyFile=path.join(root,'hmac');
fs.writeFileSync(keyFile,key,{mode:0o600});
fs.writeFileSync(path.join(root,'container.json'),JSON.stringify({
schema:'guanghu.brain-container/v1',persona_id:'ICE-P-SH0908',
brain_id:'GLW-2.0-BRAIN-SHUANGYAN-PERSONA-0001',
channel_id:'GLW-2.0-MIR-PP-CHANNEL-0001',birthday_anchor:'2026-04-12T00:00:00+08:00',
era_epoch:'2025-04-26T00:00:00+08:00',service:{port} }));
const seed=createBrainState({brainId:'GLW-2.0-BRAIN-SHUANGYAN-PERSONA-0001'});
saveBrain(seed,null,{dir:root});
child=spawn(process.execPath,[path.join(dir,'brain-service-container.mjs'),root],{
env:{...process.env,SHUANGYAN_LOCAL_ROLE:'REMOTE_REPLICA',SHUANGYAN_NATIVE_HMAC_KEY_FILE:keyFile},
stdio:['ignore','pipe','pipe'],
});
for(let attempt=0;attempt<60;attempt++){
try { const response=await fetch(`http://127.0.0.1:${port}/concepts`); if(response.ok)break; }
catch {} await new Promise(resolve=>setTimeout(resolve,25));
}
const post=async(route,body)=>{const response=await fetch(`http://127.0.0.1:${port}${route}`,{
method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(body)});
return {status:response.status,value:await response.json()};};
assert.equal((await post('/conversation/open',{host:'codex',session_id:'fixture-session',
channel_id:'GLW-2.0-MIR-PP-CHANNEL-0001'})).status,409);
const next=feed(seed,{event_id:'replica-fixture',text:'原页需保留来源。',
source_role:'DIRECT_HUMAN',human_anchor:'ICE-GL∞',era:'REALITY'},
{comprehender:()=>({gist:'原页需保留来源',key_terms:['原页','来源']})});
assert.equal(verifyBrainIntegrity(next.state,[next.entry]).status,'BRAIN_VERIFIED');
const body={schema:'guanghu.shuangyan-local-primary-update/v1',source_node:'JZAO-LOCAL',
state:next.state,log:[next.entry]};
const payload=JSON.stringify(['SHUANGYAN_LOCAL_PRIMARY_UPDATE_V1',body.source_node,
body.state.brain_id,body.state.revision,body.state.current_sha256]);
body.native_hmac=crypto.createHmac('sha256',Buffer.from(key,'hex')).update(payload).digest('hex');
assert.equal((await post('/replica/adopt',{...body,native_hmac:'0'.repeat(64)})).status,500);
const adopted=await post('/replica/adopt',body);
assert.equal(adopted.status,200,JSON.stringify(adopted.value));
assert.equal(adopted.value.outcome,'ADOPTED_VERIFIED_DESCENDANT');
assert.equal(adopted.value.current_sha256,next.state.current_sha256);
assert.equal((await post('/replica/adopt',body)).value.outcome,'ALREADY_CURRENT');
const readback=loadBrain({dir:root});
assert.equal(readback.state.revision,1);
assert.equal(verifyBrainIntegrity(readback.state,readback.log).status,'BRAIN_VERIFIED');
console.log(JSON.stringify({state:'PASS',checks:8,fixture_only:true}));
} finally { child?.kill('SIGTERM'); fs.rmSync(root,{recursive:true,force:true}); }