/** * @module galaxy.respec * * Unit tests for Universal GalaxyIbGib constants, types, preset configs, * genesis, validation, and policy resolution. */ import { respecfully, ifWe, ifWeMight, iReckon, } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { mut8 } from '@ibgib/ts-gib/dist/V1/transforms/mut8.mjs'; import { createInMemoryTempMetaspaceAndSpace } from '../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs'; import { GALAXY_ATOM, GALAXY_NAME_REGEXP, } from './galaxy-constants.mjs'; import { GalaxyPresetType, GALAXY_PRESET_CONFIGS, isGalaxyPresetType, getGalaxyPresetConfig, } from './galaxy-configs.mjs'; import { validateGalaxyName, getGalaxyIb, parseGalaxyIb, validateGalaxyIb, isGalaxy, isBranchingGalaxy, isLinearGalaxy, createGalaxyIbGib, getGalaxyIbGib, } from './galaxy-helpers.mjs'; const lc = `[core-gib/src/cosmology/galaxy/galaxy.respec.mts]`; await respecfully(lc, `Universal Galaxy Domain Specs`, async () => { await respecfully(lc, `Galaxy Naming & Ib Parsing`, async () => { await ifWe(lc, `validates URL-safe galaxy names without whitespace or tildes`, async () => { iReckon(lc, validateGalaxyName('code-1')).isGonnaBeTrue(); iReckon(lc, validateGalaxyName('notes-1')).isGonnaBeTrue(); iReckon(lc, validateGalaxyName('my_galaxy.v1')).isGonnaBeTrue(); // Invalid names: tildes (~), whitespace, slashes iReckon(lc, validateGalaxyName('b2s~code~main')).isGonnaBeFalse(); iReckon(lc, validateGalaxyName('my galaxy')).isGonnaBeFalse(); iReckon(lc, validateGalaxyName('galaxy/sub')).isGonnaBeFalse(); iReckon(lc, validateGalaxyName('')).isGonnaBeFalse(); iReckon(lc, validateGalaxyName(' ')).isGonnaBeFalse(); }); await ifWe(lc, `round-trips getGalaxyIb and parseGalaxyIb`, async () => { const galaxyName = 'code-1'; const ib = getGalaxyIb({ galaxyName, topology: 'branching', storageMode: 'text', concreteClassname: 'B2tFSGalaxyIbGib_V1', }); iReckon(lc, ib).willEqual(`${GALAXY_ATOM} branching text B2tFSGalaxyIbGib_V1 ${galaxyName}`); const parsed = await parseGalaxyIb({ ib }); iReckon(lc, parsed.atom).willEqual(GALAXY_ATOM); iReckon(lc, parsed.topology).willEqual('branching'); iReckon(lc, parsed.storageMode).willEqual('text'); iReckon(lc, parsed.concreteClassname).willEqual('B2tFSGalaxyIbGib_V1'); iReckon(lc, parsed.galaxyName).willEqual(galaxyName); const isValid = await validateGalaxyIb({ ib }); iReckon(lc, isValid).isGonnaBeTrue(); const isInvalid = await validateGalaxyIb({ ib: 'notagalaxy invalid' }); iReckon(lc, isInvalid).isGonnaBeFalse(); }); }); await respecfully(lc, `Preset Configurations & ibgib-enum-pattern`, async () => { await ifWe(lc, `validates standard preset discriminators`, async () => { iReckon(lc, isGalaxyPresetType(GalaxyPresetType.code)).isGonnaBeTrue(); iReckon(lc, isGalaxyPresetType(GalaxyPresetType.metatext)).isGonnaBeTrue(); iReckon(lc, isGalaxyPresetType(GalaxyPresetType.notes)).isGonnaBeTrue(); iReckon(lc, isGalaxyPresetType(GalaxyPresetType.curriculum)).isGonnaBeTrue(); iReckon(lc, isGalaxyPresetType(GalaxyPresetType.telemetry)).isGonnaBeTrue(); iReckon(lc, isGalaxyPresetType('custom-downstream')).isGonnaBeFalse(); iReckon(lc, isGalaxyPresetType('')).isGonnaBeFalse(); }); await ifWe(lc, `retrieves correct archetype policies for presets`, async () => { const codeConfig = getGalaxyPresetConfig(GalaxyPresetType.code); iReckon(lc, codeConfig).isGonnaBeTruthy(); iReckon(lc, codeConfig?.policy.topology).willEqual('branching'); iReckon(lc, codeConfig?.policy.role).willEqual('intrinsic'); iReckon(lc, codeConfig?.policy.defaultBranch).willEqual('main'); const metatextConfig = getGalaxyPresetConfig(GalaxyPresetType.metatext); iReckon(lc, metatextConfig).isGonnaBeTruthy(); iReckon(lc, metatextConfig?.policy.topology).willEqual('linear'); iReckon(lc, metatextConfig?.policy.role).willEqual('extrinsic'); iReckon(lc, metatextConfig?.defaultName).willEqual('metatext-1'); const notesConfig = getGalaxyPresetConfig(GalaxyPresetType.notes); iReckon(lc, notesConfig).isGonnaBeTruthy(); iReckon(lc, notesConfig?.defaultName).willEqual('metatext-1'); const curriculumConfig = getGalaxyPresetConfig(GalaxyPresetType.curriculum); iReckon(lc, curriculumConfig).isGonnaBeTruthy(); iReckon(lc, curriculumConfig?.policy.topology).willEqual('linear'); iReckon(lc, curriculumConfig?.policy.role).willEqual('intrinsic'); const telemetryConfig = getGalaxyPresetConfig(GalaxyPresetType.telemetry); iReckon(lc, telemetryConfig).isGonnaBeTruthy(); iReckon(lc, telemetryConfig?.policy.topology).willEqual('linear'); iReckon(lc, telemetryConfig?.policy.role).willEqual('extrinsic'); }); }); await respecfully(lc, `Galaxy Genesis and Management in Metaspace`, async () => { const { metaspace, space } = await createInMemoryTempMetaspaceAndSpace(); await ifWe(lc, `creates a Branching Code Galaxy from preset discriminator`, async () => { const galaxy = await createGalaxyIbGib({ galaxyName: 'code-1', discriminator: GalaxyPresetType.code, space, metaspace, }); iReckon(lc, galaxy).isGonnaBeTruthy(); iReckon(lc, isGalaxy(galaxy)).isGonnaBeTrue(); iReckon(lc, isBranchingGalaxy(galaxy)).isGonnaBeTrue(); iReckon(lc, isLinearGalaxy(galaxy)).isGonnaBeFalse(); iReckon(lc, galaxy.data.galaxyName).willEqual('code-1'); iReckon(lc, galaxy.data.discriminator).willEqual(GalaxyPresetType.code); iReckon(lc, galaxy.data.policy.topology).willEqual('branching'); iReckon(lc, galaxy.data.policy.role).willEqual('intrinsic'); iReckon(lc, galaxy.data.label).willEqual('Code Space'); iReckon(lc, galaxy.data.icon).willEqual(''); iReckon(lc, galaxy.data['$@branch']).isGonnaBeTruthy(); iReckon(lc, galaxy.data['$@branch']?.length).willEqual(0); }); await ifWe(lc, `creates a Linear Metatext Galaxy from preset discriminator`, async () => { const galaxy = await createGalaxyIbGib({ galaxyName: 'metatext-1', discriminator: GalaxyPresetType.metatext, space, metaspace, }); iReckon(lc, galaxy).isGonnaBeTruthy(); iReckon(lc, isGalaxy(galaxy)).isGonnaBeTrue(); iReckon(lc, isBranchingGalaxy(galaxy)).isGonnaBeFalse(); iReckon(lc, isLinearGalaxy(galaxy)).isGonnaBeTrue(); iReckon(lc, galaxy.data.galaxyName).willEqual('metatext-1'); iReckon(lc, galaxy.data.discriminator).willEqual(GalaxyPresetType.metatext); iReckon(lc, galaxy.data.policy.topology).willEqual('linear'); iReckon(lc, galaxy.data.policy.role).willEqual('extrinsic'); iReckon(lc, galaxy.data.label).willEqual('Metatext Space'); iReckon(lc, galaxy.data.icon).willEqual('📝'); iReckon(lc, galaxy.data['$@topic']).isGonnaBeTruthy(); iReckon(lc, galaxy.data['$@topic']?.length).willEqual(0); }); await ifWe(lc, `creates an extensible custom downstream galaxy with explicit policy`, async () => { const galaxy = await createGalaxyIbGib({ galaxyName: 'pulmoniq-clinical', discriminator: 'clinical-registry', policy: { topology: 'linear', role: 'intrinsic', storageMode: 'text', }, label: 'Clinical Trials', icon: '🩺', customData: { protocolVersion: '1.0.4', }, space, metaspace, }); iReckon(lc, galaxy).isGonnaBeTruthy(); iReckon(lc, isGalaxy(galaxy)).isGonnaBeTrue(); iReckon(lc, isLinearGalaxy(galaxy)).isGonnaBeTrue(); iReckon(lc, galaxy.data.galaxyName).willEqual('pulmoniq-clinical'); iReckon(lc, galaxy.data.discriminator).willEqual('clinical-registry'); iReckon(lc, galaxy.data.policy.topology).willEqual('linear'); iReckon(lc, galaxy.data.label).willEqual('Clinical Trials'); iReckon(lc, galaxy.data.icon).willEqual('🩺'); iReckon(lc, galaxy.data.protocolVersion).willEqual('1.0.4'); }); await ifWe(lc, `retrieves galaxy via getGalaxyIbGib and resolves latest frame in space`, async () => { const genesisGalaxy = await createGalaxyIbGib({ galaxyName: 'notes-demo', discriminator: GalaxyPresetType.notes, space, metaspace, }); const genesisAddr = getIbGibAddr({ ibGib: genesisGalaxy }); // 1. Retrieve via address const byAddr = await getGalaxyIbGib({ galaxyAddr: genesisAddr, space, metaspace, }); iReckon(lc, byAddr).isGonnaBeTruthy(); iReckon(lc, getIbGibAddr({ ibGib: byAddr })).willEqual(genesisAddr); // 2. Retrieve via metaspace special anchor const bySpecial = await getGalaxyIbGib({ space, metaspace, }); iReckon(lc, bySpecial).isGonnaBeTruthy(); // 3. Evolve galaxy frame and verify getGalaxyIbGib automatically resolves to latest frame const resMut8 = await mut8({ src: genesisGalaxy, dataToAddOrPatch: { label: 'Updated Notes Display Label', }, dna: true, nCounter: true, }); await metaspace.persistTransformResult({ resTransform: resMut8, space }); await metaspace.registerNewIbGib({ ibGib: resMut8.newIbGib, space }); const evolvedAddr = getIbGibAddr({ ibGib: resMut8.newIbGib }); // Resolving from genesis address should yield latest evolved frame const resolvedFromGenesis = await getGalaxyIbGib({ galaxyAddr: genesisAddr, space, metaspace, }); iReckon(lc, resolvedFromGenesis).isGonnaBeTruthy(); iReckon(lc, getIbGibAddr({ ibGib: resolvedFromGenesis })).willEqual(evolvedAddr); iReckon(lc, resolvedFromGenesis?.data.label).willEqual('Updated Notes Display Label'); iReckon(lc, resolvedFromGenesis?.data.n).willEqual(2); }); }); });