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 net from 'node:net'; import {spawn} from 'node:child_process'; const directory = import.meta.dirname; const repoRoot = path.resolve(directory, '../..'); const freePort = () => new Promise((resolve, reject) => { const server = net.createServer(); server.once('error', reject); server.listen(0, '127.0.0.1', () => { const port = server.address().port; server.close(() => resolve(port)); }); }); const waitReady = child => new Promise((resolve, reject) => { const timer = setTimeout(() => reject(Error('server_ready_timeout')), 10000); child.stdout.on('data', chunk => { if (String(chunk).includes('tcs-single-mother listening')) { clearTimeout(timer); resolve(); } }); child.stderr.on('data', chunk => { const text = String(chunk); if (text.trim()) { clearTimeout(timer); reject(Error(text)); } }); child.once('exit', code => { clearTimeout(timer); reject(Error(`server_exited_${code}`)); }); }); test('server exposes all baby loops while refusing off-channel baby events', async () => { const temporary = fs.mkdtempSync(path.join(os.tmpdir(), 'persona-server-integration-')); const key = path.join(temporary, 'key.pem'), seed = path.join(temporary, 'seed.json'); const pair = crypto.generateKeyPairSync('ed25519'); fs.writeFileSync(key, pair.privateKey.export({type:'pkcs8',format:'pem'})); fs.copyFileSync(path.join(directory, 'seed.json'), seed); const port = await freePort(); const child = spawn(process.execPath, [path.join(directory, 'server.mjs')], {env:{...process.env, TCS_MOTHER_BRAIN_PORT:String(port), TCS_MOTHER_BODY_STATE_ROOT:path.join(temporary, 'state'), TCS_MOTHER_SIGNING_KEY:key, TCS_MOTHER_SEED:seed, TCS_PERSONA_ROUTES:path.join(directory, 'persona-routes.json'), TCS_PERSONA_RUNTIME_REGISTRY:path.join(directory, 'persona-runtime-registry.json'), TCS_PERSONA_REPO_ROOT:repoRoot, TCS_LIT_NAVIGATION_MAP:path.join(repoRoot, 'routing/linguistic-echo-lamp-map.json'), OPENLUX_API_KEY:'test-not-used'}, stdio:['ignore','pipe','pipe']}); try { await waitReady(child); const health = await (await fetch(`http://127.0.0.1:${port}/health`)).json(); assert.deepEqual(health.persona_runtimes.installed_personas, ['ICE-P-ZY001','ICE-BB-0001','ICE-BB-0002','ICE-BB-0003','ICE-BB-0004','ICE-BB-0005']); const status = await (await fetch(`http://127.0.0.1:${port}/v1/persona/ICE-BB-0005/status`)).json(); assert.equal(status.persona_id, 'ICE-BB-0005'); assert.equal(status.life_line.wake_allowed, true); const language = 'test'; const base = {schema:'guanghu.persona-self-language-event/v1',persona_id:'ICE-BB-0005',source_type:'PERSONA_LANGUAGE',event_id:'SERVER-TEST',language,source_sha256:crypto.createHash('sha256').update(language).digest('hex'),privacy_class:'SELF_PRIVATE'}; const denied = await fetch(`http://127.0.0.1:${port}/v1/persona/ICE-BB-0005/ingest`,{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(base)}); assert.equal(denied.status, 400); assert.match((await denied.json()).error, /REQUIRES_FIFTH_DOMAIN_BT001/); const accepted = await fetch(`http://127.0.0.1:${port}/v1/persona/ICE-BB-0005/ingest`,{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({...base,entry_domain:'DOM-FIFTH-0001',body_channel:'ICE-CH-BT001'})}); assert.equal(accepted.status, 202); } finally { child.kill('SIGTERM'); fs.rmSync(temporary,{recursive:true,force:true}); } });