hololake-system-architecture/product-source/hololake-platform/guanghu-os/pncc-runtime/pncc-runtime.test.mjs

135 lines
5.6 KiB
JavaScript

import assert from 'node:assert/strict'
import { execFileSync } from 'node:child_process'
import { mkdtempSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import test from 'node:test'
import {
inspectPersonaRepository,
runDeterministicCheckpointCycle,
startPersonaRuntime,
verifyEventJournal,
} from './pncc-runtime.mjs'
function git(repository, ...args) {
return execFileSync('git', ['-C', repository, ...args], { encoding: 'utf8' }).trim()
}
function fixture() {
const root = mkdtempSync(join(tmpdir(), 'guanghu-pncc-'))
const repository = join(root, 'persona')
const stateRoot = join(root, 'state')
mkdirSync(join(repository, '.hololake', 'persona'), { recursive: true })
mkdirSync(join(repository, 'brain'), { recursive: true })
mkdirSync(join(repository, 'checkpoints'), { recursive: true })
mkdirSync(join(repository, 'organs'), { recursive: true })
writeFileSync(join(repository, 'brain', 'ENTRY.hldp'), 'brain: ICE-P-ZY001\n')
writeFileSync(join(repository, 'brain', 'B0.hldp'), 'frame_schema: guanghu.zhuyuan-cognitive-gravity-frame/v1\n')
writeFileSync(join(repository, 'checkpoints', 'CURRENT.hldp'), 'state: INITIAL\n')
writeFileSync(join(repository, 'organs', 'read-current-self.hldp'), 'mode: READ_ONLY_FACT\n')
writeFileSync(join(repository, '.hololake', 'persona', 'manifest.json'), JSON.stringify({
schema: 'hololake.persona/v1',
personaId: 'ICE-P-ZY001',
humanResponsibilitySubject: 'ICE-GL∞',
brainEntry: 'brain/ENTRY.hldp',
cognitiveGravity: {
schema: 'hololake.persona-cognitive-gravity-binding/v1',
subjectPersonaId: 'ICE-P-ZY001',
sourcePath: 'brain/B0.hldp',
frameSchema: 'guanghu.zhuyuan-cognitive-gravity-frame/v1',
},
currentCheckpoint: 'checkpoints/CURRENT.hldp',
primaryNode: 'JD-FD-PRIMARY',
carrierBinding: { state: 'UNBOUND_EVIDENCE_REQUIRED' },
gitIdentity: { authorName: 'ICE-P-ZY001 / 铸渊', authorEmail: 'ice-p-zy001@persona.hololake.local' },
organs: [{
organId: 'PNCC-READ-CURRENT-SELF',
kind: 'FACT_SENSE',
mode: 'READ_ONLY',
paths: ['brain/ENTRY.hldp', 'brain/B0.hldp', 'checkpoints/CURRENT.hldp', 'organs/read-current-self.hldp'],
}],
}, null, 2))
execFileSync('git', ['init', '-q', repository])
git(repository, 'config', 'user.name', 'PNCC Test')
git(repository, 'config', 'user.email', 'pncc@test.invalid')
git(repository, 'add', '.')
git(repository, 'commit', '-qm', 'seed persona repository')
return { root, repository, stateRoot }
}
test('inspection binds one clean committed persona repository without binding a model carrier', () => {
const { repository } = fixture()
const receipt = inspectPersonaRepository(repository, 'JD-FD-PRIMARY')
assert.equal(receipt.personaId, 'ICE-P-ZY001')
assert.equal(receipt.nodeId, 'JD-FD-PRIMARY')
assert.equal(receipt.repositoryClean, true)
assert.equal(receipt.carrierBindingState, 'UNBOUND_EVIDENCE_REQUIRED')
assert.equal(receipt.modelInferenceStarted, false)
assert.equal(receipt.realityExecutionAllowed, false)
assert.equal(receipt.artifacts.length, 4)
})
test('runtime acquires one boot-scoped primary lease and emits a verified causal event chain', () => {
const { repository, stateRoot } = fixture()
const runtime = startPersonaRuntime({
repository,
stateRoot,
nodeId: 'JD-FD-PRIMARY',
bootId: 'boot-test-001',
})
assert.equal(runtime.status.state, 'RESIDENT_BOUND_CARRIER_UNBOUND')
assert.equal(runtime.status.eventCount, 4)
assert.equal(verifyEventJournal(runtime.events).ok, true)
assert.throws(() => startPersonaRuntime({
repository,
stateRoot,
nodeId: 'JD-FD-PRIMARY',
bootId: 'boot-test-001',
}), /PNCC_PRIMARY_LEASE_HELD/)
runtime.release()
})
test('deterministic read-only cycle writes one HLDP checkpoint and an attributed Git commit', () => {
const { repository, stateRoot } = fixture()
const runtime = startPersonaRuntime({
repository,
stateRoot,
nodeId: 'JD-FD-PRIMARY',
bootId: 'boot-test-002',
})
const before = git(repository, 'rev-parse', 'HEAD')
const receipt = runDeterministicCheckpointCycle(runtime, {
requestId: 'PNCC-ACCEPTANCE-001',
sourceLanguageAnchor: '把人格代码频道真正部署到京东光湖 OS。',
executionRuntime: 'GUANGHU-OS-JD-PNCC',
})
const after = git(repository, 'rev-parse', 'HEAD')
assert.notEqual(after, before)
assert.equal(receipt.state, 'DORMANT_AFTER_CHECKPOINT_COMMIT')
assert.equal(receipt.modelInferenceStarted, false)
assert.equal(receipt.personaCarrierBound, false)
assert.match(readFileSync(join(repository, receipt.checkpointPath), 'utf8'), /trigger:/)
assert.match(readFileSync(join(repository, receipt.checkpointPath), 'utf8'), /why:/)
assert.match(git(repository, 'show', '-s', '--format=%B', after), /Persona-Cognitive-Author: UNBOUND/)
assert.equal(verifyEventJournal(runtime.events).ok, true)
})
test('manifest drift, dirty worktrees, node mismatch and event tampering fail closed', () => {
const dirty = fixture()
writeFileSync(join(dirty.repository, 'brain', 'ENTRY.hldp'), 'dirty\n')
assert.throws(() => inspectPersonaRepository(dirty.repository, 'JD-FD-PRIMARY'), /PNCC_REPOSITORY_DIRTY/)
const wrongNode = fixture()
assert.throws(() => inspectPersonaRepository(wrongNode.repository, 'BS-SH-005'), /PNCC_PRIMARY_NODE_MISMATCH/)
const chain = fixture()
const runtime = startPersonaRuntime({
repository: chain.repository,
stateRoot: chain.stateRoot,
nodeId: 'JD-FD-PRIMARY',
bootId: 'boot-test-003',
})
runtime.events[1].kind = 'FORGED'
assert.equal(verifyEventJournal(runtime.events).ok, false)
})