2026-09-13 01:13:06 +08:00
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 } ;
} ;
2026-09-13 01:47:41 +08:00
test ( 'one shared engine installs seven isolated persona state roots without a default persona' , ( ) => {
2026-09-13 01:13:06 +08:00
const ctx = setup ( ) ;
try {
2026-09-13 01:47:41 +08:00
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' ] ) ;
2026-09-13 01:13:06 +08:00
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 ) ;
2026-09-13 01:47:41 +08:00
assert . equal ( new Set ( roots ) . size , 7 ) ;
2026-09-13 01:13:06 +08:00
} 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/ ) ;
2026-09-13 01:34:21 +08:00
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' ) ;
2026-09-13 01:13:06 +08:00
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 ( '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 } ) ; }
} ) ;