/** * @module cosmos.respec * * Unit tests for CosmosIbGib constants, types, genesis, validation, and soft-link management. */ 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 { getSpaceQualifiedIbGibAddr } from '../../common/other/ibgib-helper.mjs'; import { createInMemoryTempMetaspaceAndSpace } from '../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs'; import { COSMOS_ATOM, COSMOS_GALAXY_SOFT_LINK_REL8N_NAME, } from './cosmos-constants.mjs'; import { validateCosmosName, getCosmosIb, parseCosmosIb, validateCosmosIb, isCosmos, createCosmosIbGib, getGalaxyAddrsFromCosmos, addGalaxyToCosmos, getGalaxyInfoFromCosmos, getGalaxiesFromCosmos, } from './cosmos-helpers.mjs'; import { executeCreateGalaxyInCosmos } from './cosmos-commands.mjs'; import { SpaceQualifiedIbGibAddr } from '../../common/other/ibgib-types.mjs'; import { createGalaxyIbGib } from '../galaxy/galaxy-helpers.mjs'; import { GalaxyPresetType } from '../galaxy/galaxy-configs.mjs'; const lc = `[core-gib/src/cosmology/cosmos/cosmos.respec.mts]`; await respecfully(lc, `Cosmos Domain Specs`, async () => { await respecfully(lc, `Cosmos Naming & Ib Parsing`, async () => { await ifWe(lc, `validates URL-safe cosmos names without whitespace`, async () => { iReckon(lc, validateCosmosName('welcome-cosmos')).isGonnaBeTrue(); iReckon(lc, validateCosmosName('my_cosmos_1')).isGonnaBeTrue(); iReckon(lc, validateCosmosName('cosmos.v1')).isGonnaBeTrue(); // Invalid names iReckon(lc, validateCosmosName('my cosmos')).isGonnaBeFalse(); iReckon(lc, validateCosmosName('cosmos/sub')).isGonnaBeFalse(); iReckon(lc, validateCosmosName('')).isGonnaBeFalse(); iReckon(lc, validateCosmosName(' ')).isGonnaBeFalse(); }); await ifWe(lc, `round-trips getCosmosIb and parseCosmosIb`, async () => { const cosmosName = 'welcome-cosmos'; const ib = getCosmosIb({ cosmosName }); iReckon(lc, ib).willEqual(`${COSMOS_ATOM} ${cosmosName}`); const parsed = await parseCosmosIb({ ib }); iReckon(lc, parsed.atom).willEqual(COSMOS_ATOM); iReckon(lc, parsed.cosmosName).willEqual(cosmosName); const isValid = await validateCosmosIb({ ib }); iReckon(lc, isValid).isGonnaBeTrue(); const isInvalid = await validateCosmosIb({ ib: 'notacosmos invalid' }); iReckon(lc, isInvalid).isGonnaBeFalse(); }); }); await respecfully(lc, `Cosmos Genesis and Management in Metaspace`, async () => { const { metaspace, space } = await createInMemoryTempMetaspaceAndSpace(); await ifWe(lc, `creates a valid CosmosIbGib_V1 in space`, async () => { const cosmosName = 'test-cosmos'; const cosmos = await createCosmosIbGib({ cosmosName, description: 'Test Cosmos for Unit Tests', space, metaspace, }); iReckon(lc, cosmos).isGonnaBeTruthy(); iReckon(lc, isCosmos(cosmos)).isGonnaBeTrue(); iReckon(lc, cosmos.data.cosmosName).willEqual(cosmosName); iReckon(lc, cosmos.data.description).willEqual('Test Cosmos for Unit Tests'); iReckon(lc, cosmos.data['@galaxy']).isGonnaBeTruthy(); iReckon(lc, cosmos.data['@galaxy']?.length).willEqual(0); // Verify soft-link helper returns empty list const galaxyAddrs = getGalaxyAddrsFromCosmos(cosmos); iReckon(lc, galaxyAddrs.length).willEqual(0); }); await ifWe(lc, `adds a member galaxy soft-link to the cosmos timeline`, async () => { const cosmosName = 'multi-galaxy-cosmos'; const cosmos = await createCosmosIbGib({ cosmosName, space, metaspace, }); const fakeGalaxySqAddr: SpaceQualifiedIbGibAddr = getSpaceQualifiedIbGibAddr({ spaceId: 'test_space_123', ibGibAddr: 'b2tfs_galaxy repo1^0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', }); const updatedCosmos = await addGalaxyToCosmos({ cosmosIbGib: cosmos, sqGalaxyAddr: fakeGalaxySqAddr, space, metaspace, }); iReckon(lc, updatedCosmos).isGonnaBeTruthy(); iReckon(lc, updatedCosmos.data.n).willEqual((cosmos.data.n ?? 0) + 1); const galaxyAddrs = getGalaxyAddrsFromCosmos(updatedCosmos); iReckon(lc, galaxyAddrs.length).willEqual(1); iReckon(lc, galaxyAddrs[0]).willEqual(fakeGalaxySqAddr); // Adding the exact same address is idempotent const duplicateCosmos = await addGalaxyToCosmos({ cosmosIbGib: updatedCosmos, sqGalaxyAddr: fakeGalaxySqAddr, space, metaspace, }); iReckon(lc, duplicateCosmos.data.n).willEqual(updatedCosmos.data.n); iReckon(lc, duplicateCosmos.data['@galaxy']?.length).willEqual(1); }); await ifWe(lc, `registers member galaxies in the manifest and allows O(1) policy queries`, async () => { const cosmos = await createCosmosIbGib({ cosmosName: 'manifest-test-cosmos', space, metaspace, }); // Create a Code Galaxy const codeGalaxy = await createGalaxyIbGib({ galaxyName: 'code-1', discriminator: GalaxyPresetType.code, space, metaspace, }); const sqCodeAddr = getSpaceQualifiedIbGibAddr({ spaceId: 'test_space_123', ibGibAddr: getIbGibAddr({ ibGib: codeGalaxy }), }); // Create a Metatext Galaxy const metatextGalaxy = await createGalaxyIbGib({ galaxyName: 'metatext-1', discriminator: GalaxyPresetType.metatext, space, metaspace, }); const sqMetatextAddr = getSpaceQualifiedIbGibAddr({ spaceId: 'test_space_123', ibGibAddr: getIbGibAddr({ ibGib: metatextGalaxy }), }); // Register Code Galaxy let currentCosmos = await addGalaxyToCosmos({ cosmosIbGib: cosmos, sqGalaxyAddr: sqCodeAddr, galaxyIbGib: codeGalaxy, space, metaspace, }); // Register Metatext Galaxy currentCosmos = await addGalaxyToCosmos({ cosmosIbGib: currentCosmos, sqGalaxyAddr: sqMetatextAddr, galaxyIbGib: metatextGalaxy, space, metaspace, }); // Query manifest entries const codeEntry = getGalaxyInfoFromCosmos(currentCosmos, 'code-1'); iReckon(lc, codeEntry).isGonnaBeTruthy(); iReckon(lc, codeEntry?.galaxyName).willEqual('code-1'); iReckon(lc, codeEntry?.discriminator).willEqual(GalaxyPresetType.code); iReckon(lc, codeEntry?.policy.topology).willEqual('branching'); iReckon(lc, codeEntry?.policy.role).willEqual('intrinsic'); iReckon(lc, codeEntry?.label).willEqual('Code Space'); iReckon(lc, codeEntry?.icon).willEqual(''); const metatextEntry = getGalaxyInfoFromCosmos(currentCosmos, 'metatext-1'); iReckon(lc, metatextEntry).isGonnaBeTruthy(); iReckon(lc, metatextEntry?.galaxyName).willEqual('metatext-1'); iReckon(lc, metatextEntry?.discriminator).willEqual(GalaxyPresetType.metatext); iReckon(lc, metatextEntry?.policy.topology).willEqual('linear'); iReckon(lc, metatextEntry?.policy.role).willEqual('extrinsic'); iReckon(lc, metatextEntry?.label).willEqual('Metatext Space'); iReckon(lc, metatextEntry?.icon).willEqual('📝'); // Unknown galaxy query returns undefined const unknownEntry = getGalaxyInfoFromCosmos(currentCosmos, 'unknown-galaxy'); iReckon(lc, unknownEntry).isGonnaBeFalsy(); // All galaxies query returns both const allGalaxies = getGalaxiesFromCosmos(currentCosmos); iReckon(lc, allGalaxies.length).willEqual(2); }); }); await respecfully(lc, `Cosmos Galaxy Orchestration Commands`, async () => { await ifWe(lc, `executes executeCreateGalaxyInCosmos and updates cosmos manifest`, async () => { const { metaspace, space } = await createInMemoryTempMetaspaceAndSpace(); const cosmos = await createCosmosIbGib({ cosmosName: 'orchestrated-cosmos', space, metaspace, }); iReckon(lc, cosmos.data['@galaxy']?.length ?? 0).willEqual(0); // Create code-1 galaxy via unified command const res = await executeCreateGalaxyInCosmos({ metaspace, space, cosmosIbGib: cosmos, galaxyName: 'code-1', discriminator: GalaxyPresetType.code, branchName: 'main', }); iReckon(lc, res.cosmosIbGib.data['@galaxy']?.length).willEqual(1); iReckon(lc, res.cosmosIbGib.data.galaxies?.['code-1']?.galaxyName).willEqual('code-1'); iReckon(lc, res.cosmosIbGib.data.galaxies?.['code-1']?.discriminator).willEqual(GalaxyPresetType.code); iReckon(lc, res.initRes.branchName).willEqual('main'); iReckon(lc, res.initRes.rootItem).isGonnaBeTruthy(); // Create metatext-1 galaxy in the same cosmos const resMeta = await executeCreateGalaxyInCosmos({ metaspace, space, cosmosIbGib: res.cosmosIbGib, galaxyName: 'metatext-1', discriminator: GalaxyPresetType.metatext, branchName: 'main', }); iReckon(lc, resMeta.cosmosIbGib.data['@galaxy']?.length).willEqual(2); iReckon(lc, resMeta.cosmosIbGib.data.galaxies?.['metatext-1']?.galaxyName).willEqual('metatext-1'); iReckon(lc, resMeta.cosmosIbGib.data.galaxies?.['metatext-1']?.discriminator).willEqual(GalaxyPresetType.metatext); }); }); });