import test from 'node:test'; import assert from 'node:assert/strict'; import { assertRegisteredDomainRuntimeHandoffEndpoint, parseDomainRuntimeHandoffEndpointRegistry, resolveDomainRuntimeHandoffEndpoint, } from './domain-runtime-handoff-registry.js'; const COMMIT = 'a'.repeat(40); const SOURCE = { repositoryId: 'REPO-012' as const, sourceCommit: COMMIT, sourceUrl: `https://guanghulab.com/code/bingshuo/guanghu-ice-heart/raw/commit/${COMMIT}/routing/domain-runtime-handoff-endpoints.json`, }; function endpoint(overrides: Record = {}) { return { domainIds: ['DOM-FIFTH-0001'], endpointId: 'GH-DOMAIN-HANDOFF-001', nodeTypes: ['cloud-resident'], status: 'ACTIVE', url: 'https://guanghulab.com/api/ai/v1/domain-runtime/handoff', ...overrides, }; } function registry(endpoints: unknown[] = [endpoint()]) { return { endpoints, registryId: 'GH-AIOS-DOMAIN-RUNTIME-HANDOFF-ENDPOINTS-001', schema: 'gh-aios.domain-runtime-handoff-endpoints/v1', state: 'CURRENT', version: '1.0.0', }; } test('parses and resolves one anchored endpoint for the exact scope', () => { const parsed = parseDomainRuntimeHandoffEndpointRegistry(registry(), SOURCE); const resolved = resolveDomainRuntimeHandoffEndpoint(parsed, { domainId: 'DOM-FIFTH-0001', nodeType: 'cloud-resident', }); assert.ok(resolved); assert.equal(resolved.url, 'https://guanghulab.com/api/ai/v1/domain-runtime/handoff'); assert.equal(resolveDomainRuntimeHandoffEndpoint(parsed, { domainId: 'DOM-FIFTH-0001', nodeType: 'local-terminal', }), null); }); test('rejects unsafe URLs, unknown fields and duplicate active scopes', () => { for (const unsafe of [ 'http://guanghulab.com/api/ai/v1/handoff', 'https://example.com/api/ai/v1/handoff', 'https://user@guanghulab.com/api/ai/v1/handoff', 'https://guanghulab.com:8443/api/ai/v1/handoff', 'https://guanghulab.com/api/ai/v1/handoff?target=other', ]) { assert.throws(() => parseDomainRuntimeHandoffEndpointRegistry(registry([endpoint({ url: unsafe })]), SOURCE)); } assert.throws(() => parseDomainRuntimeHandoffEndpointRegistry(registry([endpoint({ extra: true })]), SOURCE)); assert.throws(() => parseDomainRuntimeHandoffEndpointRegistry(registry([ endpoint(), endpoint({ endpointId: 'GH-DOMAIN-HANDOFF-002' }), ]), SOURCE)); }); test('rejects caller-created and copied endpoint objects', () => { const parsed = parseDomainRuntimeHandoffEndpointRegistry(registry(), SOURCE); const resolved = resolveDomainRuntimeHandoffEndpoint(parsed, { domainId: 'DOM-FIFTH-0001', nodeType: 'cloud-resident', }); assert.ok(resolved); assert.throws(() => assertRegisteredDomainRuntimeHandoffEndpoint({ ...resolved }), /endpoint_unregistered/); });