import test from 'node:test'; import assert from 'node:assert/strict'; import { assertLoadedDomainRuntimeHandoffSnapshot, DomainRuntimeHandoffSnapshotLoader, } from './domain-runtime-handoff-snapshot.js'; const COMMIT = 'a'.repeat(40); const ANCHOR_URL = 'https://guanghulab.com/api/ai/v1/anchor'; const REGISTRY_URL = `https://guanghulab.com/code/bingshuo/guanghu-ice-heart/raw/commit/${COMMIT}/routing/domain-runtime-handoff-endpoints.json`; function anchor(includeMap = true) { return { schema: 'guanghu.public-navigation-anchor/v1', anchor_id: 'GLW-PUBLIC-NAV-ANCHOR-001', state: 'CURRENT_CANONICAL', repository_id: 'REPO-012', branch: 'main', public_entry: ANCHOR_URL, code_entry: 'https://guanghulab.com/code/bingshuo/guanghu-ice-heart', maps: includeMap ? { domain_runtime_handoff_endpoints: { path: 'routing/domain-runtime-handoff-endpoints.json', id: 'GH-AIOS-DOMAIN-RUNTIME-HANDOFF-ENDPOINTS-001', version: '1.0.0', } } : {}, navigation_source: { anchor_id: 'GLW-PUBLIC-NAV-ANCHOR-001', source_commit: COMMIT, source_mode: 'REPO-012_MAIN_GIT_SNAPSHOT', source_degraded: false }, }; } function registry(version = '1.0.0') { return { endpoints: [{ domainIds: ['DOM-FIFTH-0001'], endpointId: 'GH-DOMAIN-HANDOFF-001', nodeTypes: ['cloud-resident'], status: 'ACTIVE', url: 'https://guanghulab.com/api/ai/v1/domain-runtime/handoff' }], registryId: 'GH-AIOS-DOMAIN-RUNTIME-HANDOFF-ENDPOINTS-001', schema: 'gh-aios.domain-runtime-handoff-endpoints/v1', state: 'CURRENT', version, }; } function loader(readAnchor: () => unknown, readRegistry: () => unknown) { return new DomainRuntimeHandoffSnapshotLoader({ async fetchJson(url) { if (url === ANCHOR_URL) return readAnchor(); if (url === REGISTRY_URL) return readRegistry(); throw new Error('unexpected_url'); }, }); } test('loads only the registry at the exact anchor source commit', async () => { const snapshot = await loader(() => anchor(), () => registry()).refresh(); assert.equal(snapshot.receipt.status, 'CURRENT'); assert.equal(snapshot.receipt.sourceCommit, COMMIT); assert.equal(snapshot.receipt.endpointCount, 1); assert.equal(assertLoadedDomainRuntimeHandoffSnapshot(snapshot), snapshot); }); test('current anchor without the handoff map is explicitly unavailable', async () => { const snapshot = await loader(() => anchor(false), () => registry()).refresh(); assert.deepEqual(snapshot.receipt, { endpointCount: 0, reason: 'REGISTRY_UNREGISTERED', registryVersion: null, sourceCommit: null, status: 'UNAVAILABLE' }); assert.equal(snapshot.registry, null); }); test('invalid and degraded sources remain unusable as current authority', async () => { const invalid = await loader(() => anchor(), () => registry('2.0.0')).refresh(); assert.equal(invalid.receipt.reason, 'REGISTRY_INVALID'); let currentAnchor: unknown = anchor(); const snapshots = loader(() => currentAnchor, () => registry()); await snapshots.refresh(); currentAnchor = anchor(false); const degraded = await snapshots.refresh(); assert.equal(degraded.receipt.status, 'DEGRADED_LAST_KNOWN_GOOD'); assert.equal(degraded.receipt.reason, 'REGISTRY_UNREGISTERED'); }); test('caller-created snapshots are rejected', () => { assert.throws(() => assertLoadedDomainRuntimeHandoffSnapshot({ receipt: { endpointCount: 0, registryVersion: null, sourceCommit: null, status: 'UNAVAILABLE' }, registry: null, }), /snapshot_unregistered/); });