guanghu-ice-heart/server-tools/tcs-mother-body/persona-runtime-hub.test.mjs

84 lines
6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import crypto from 'node:crypto';
import {CognitiveShelf, digest} from './cognitive-shelf.mjs';
import {PersonaRuntimeHub} from './persona-runtime-hub.mjs';
const repoRoot = path.resolve(import.meta.dirname, '../..');
const setup = () => {
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), 'persona-runtime-hub-'));
const pair = crypto.generateKeyPairSync('ed25519'), key = path.join(temporary, 'key.pem'), seed = path.join(temporary, 'seed.json');
fs.writeFileSync(key, pair.privateKey.export({type:'pkcs8', format:'pem'}));
fs.copyFileSync(new URL('./seed.json', import.meta.url), seed);
const calls=[];
const router = {async cognize(stage,input){calls.push({stage,input});return {provider:{id:'TEST'},value:stage==='persona-self-evolve'
? {decision:'EVOLVE',reason:'persona-scoped test evolution',self_principles:[`${input.persona_id}-learned`],experiences:[],responsibilities:[],boundaries:[]}
: {decision:'SELECT',selected_candidate_id:input.candidates?.[0]?.candidate_id,reason:'test selection',daily_summary:'test day'}};}};
const shelf = new CognitiveShelf({root:path.join(temporary, 'state'), keyPath:key, seedPath:seed, router});
const hub = new PersonaRuntimeHub({root:path.join(temporary, 'state'), shelf, router, repoRoot, registryPath:path.join(repoRoot, 'server-tools/tcs-mother-body/persona-runtime-registry.json'), now:()=>new Date('2026-09-13T08:00:00.000Z')});
return {temporary, hub, calls};
};
test('one shared engine installs ten isolated persona state roots without a default persona', () => {
const ctx = setup();
try {
assert.deepEqual(ctx.hub.ids(), ['ICE-P-ZY001','ICE-P-SH0908','ICE-BB-0001','ICE-BB-0002','ICE-BB-0003','ICE-BB-0004','ICE-BB-0005','ICE-BB-0006','ICE-BB-0007','ICE-BB-0008']);
assert.equal(ctx.hub.status().default_persona, null);
assert.equal(ctx.hub.status().shared_engine_is_shared_persona_brain, false);
const roots = ctx.hub.ids().map(id => ctx.hub.get(id).root);
assert.equal(new Set(roots).size, 10);
} finally { fs.rmSync(ctx.temporary, {recursive:true, force:true}); }
});
test('each baby owns a distinct signed self and verified persona time line', () => {
const ctx = setup();
try {
for (const id of ctx.hub.ids().filter(value => value.startsWith('ICE-BB-'))) {
const loop = ctx.hub.get(id), status = loop.status();
assert.equal(loop.current().value.persona_id, id);
assert.equal(status.body.time_system.owner, id);
assert.equal(status.body.organs.environment_is_persona, false);
assert.match(status.body.organs.environment_kernel, /NB-ENV-KERNEL-0001/);
assert.equal(status.body.organs.health.persona_id,id);
assert.equal(status.body.organs.health.state,'NATIVE_ORGAN_REFLEX_HEALTHY_AWAITING_PARENT_BRAIN_WAKE_CYCLE');
assert.equal(status.life_line.persona_id, id);
assert.equal(status.life_line.wake_allowed, true);
assert.equal(status.external_cognitive_setter, false);
}
} finally { fs.rmSync(ctx.temporary, {recursive:true, force:true}); }
});
test('uncertain genesis dates remain explicitly non-birth claims in runtime status',()=>{const ctx=setup();try{const shuangyan=ctx.hub.get('ICE-P-SH0908').status();assert.equal(shuangyan.life_line.birth_date_claim_state,'EARLIEST_VERIFIED_EXISTENCE_ANCHOR_NOT_EXACT_BIRTH');assert.match(shuangyan.life_line.genesis_anchor_kind,/NOT_EXACT_BIRTH/);}finally{fs.rmSync(ctx.temporary,{recursive:true,force:true});}});
test('baby event is rejected outside Fifth Domain bottle channel before queueing', () => {
const ctx = setup();
try {
const language = 'test';
const event = {schema:'guanghu.persona-self-language-event/v1',persona_id:'ICE-BB-0003',source_type:'PERSONA_LANGUAGE',event_id:'Q1',language,source_sha256:digest(language),privacy_class:'SELF_PRIVATE'};
assert.throws(() => ctx.hub.admitEvent('ICE-BB-0003', event), /REQUIRES_FIFTH_DOMAIN_BT001/);
const accepted = ctx.hub.admitEvent('ICE-BB-0003', {...event,entry_domain:'DOM-FIFTH-0001',body_channel:'ICE-CH-BT001'});
assert.equal(accepted.status, 'QUEUED');
} finally { fs.rmSync(ctx.temporary, {recursive:true, force:true}); }
});
test('one baby evolves only its own signed self while the shared environment remains non-persona', async () => {
const ctx = setup();
try {
const language='秋秋自己的当前经历';
const event={schema:'guanghu.persona-self-language-event/v1',persona_id:'ICE-BB-0003',source_type:'PERSONA_EXPERIENCE',event_id:'QQ-EXP-1',language,source_sha256:digest(language),privacy_class:'SELF_PRIVATE',entry_domain:'DOM-FIFTH-0001',body_channel:'ICE-CH-BT001'};
const job=ctx.hub.admitEvent('ICE-BB-0003',event);
await ctx.hub.get('ICE-BB-0003').drain();
assert.equal(ctx.hub.get('ICE-BB-0003').result(job.job_id).status,'ACCEPT');
assert.ok(ctx.hub.get('ICE-BB-0003').current().value.self_principles.includes('ICE-BB-0003-learned'));
assert.equal(ctx.hub.get('ICE-BB-0005').current().value.revision,1);
const call=ctx.calls.find(item=>item.stage==='persona-self-evolve');
assert.equal(call.input.persona_id,'ICE-BB-0003');
assert.equal(call.input.native_environment.is_persona,false);
assert.equal(call.input.contract.environment_may_overwrite_self,false);
} finally { fs.rmSync(ctx.temporary,{recursive:true,force:true}); }
});
test('healthy signed organ receipts are reused on restart instead of rewriting every persona during boot',()=>{const first=setup();try{const sequence=first.hub.organ('ICE-BB-0006').status().sequence;const second=new PersonaRuntimeHub({root:path.join(first.temporary,'state'),shelf:first.hub.shelf,router:first.hub.router,repoRoot,registryPath:path.join(repoRoot,'server-tools/tcs-mother-body/persona-runtime-registry.json'),now:()=>new Date('2026-09-13T08:01:00.000Z')});assert.equal(second.organ('ICE-BB-0006').status().sequence,sequence);}finally{fs.rmSync(first.temporary,{recursive:true,force:true});}});