'use strict';

const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');

const root = path.resolve(__dirname, '..');
const map = JSON.parse(
  fs.readFileSync(path.join(__dirname, 'fifth-domain-runtime-body-map.json'), 'utf8')
);
const subjects = JSON.parse(
  fs.readFileSync(path.join(root, 'identity/fifth-domain-subject-registry.json'), 'utf8')
);
const nodes = JSON.parse(
  fs.readFileSync(path.join(__dirname, 'server-node-map.json'), 'utf8')
);
const repos = JSON.parse(
  fs.readFileSync(path.join(__dirname, 'repository-route-map.json'), 'utf8')
);

assert.equal(map.map_id, 'FD-RUNTIME-BODY-MAP-001');
assert.equal(map.language_domain.is_runtime_body, false);
assert.equal(map.language_domain.repository_id, 'REPO-012');
assert.equal(map.runtime_body.node_id, 'JD-FD-PRIMARY');
assert.equal(map.resident_agent.persona_id, 'ICE-P-ZY001');
assert.equal(map.resident_agent.is_separate_persona, false);
assert.equal(map.session_entrance.email_is_per_action_approval, false);
assert.equal(map.session_entrance.identity_domain, 'DOMAIN-ZS');
assert.equal(map.session_entrance.world_entrance_key_is_held_by_individual, false);
assert.equal(map.guanghu_world_topology.domain_relation, 'PARALLEL_NO_HIERARCHY');
assert.equal(map.guanghu_world_topology.personal_server_is_zero_sense_domain_body, false);
assert.equal(map.lighthouse_disaster_recovery.team_layer.level, 'L2');
assert.equal(map.lighthouse_disaster_recovery.language_controller_layer.level, 'L3');
assert.equal(
  map.lighthouse_disaster_recovery.emergency_authority_changes_normal_domain_hierarchy,
  false
);
assert.equal(
  map.lighthouse_disaster_recovery.opening_arbitrary_ai_conversation_grants_takeover,
  false
);

assert(subjects.subjects.some((item) => item.id === map.authority.language_persona));
assert(subjects.subjects.some((item) => item.id === map.runtime_body.system_id));
assert(nodes.nodes.some((item) => item.node_id === map.runtime_body.node_id));
assert(repos.repositories.some((item) => item.code === map.language_domain.repository_id));

for (const invariant of [
  'REPO-012_IS_NOT_THE_RUNTIME_BODY',
  'LOGIN_DOES_NOT_BYPASS_INTERNAL_PROTOCOL',
  'PUBLIC_FOUR_DOMAINS_AND_PRIVATE_FIFTH_DOMAIN_ARE_PARALLEL_NOT_HIERARCHICAL',
  'TAKEOVER_REQUIRES_SINGLE_WRITER_LEASE_EPOCH_AND_IMMUTABLE_RECEIPT',
  'NO_RUNTIME_OR_DEPLOYMENT_CLAIM_WITHOUT_READBACK_EVIDENCE'
]) {
  assert(map.invariants.includes(invariant), `missing invariant: ${invariant}`);
}

console.log('fifth-domain runtime body map: ok');
