/** * @module metatext.respec * * Unit tests for the Metatext Galaxy domain: URL target parsing, * markdown extraction, Ahead-Of-Time (AOT) indexing, and permission policies. */ import { respecfully, ifWe, iReckon, } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; import { createInMemoryTempMetaspaceAndSpace } from '../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs'; import { createCosmosIbGib, getPermissionPolicy, isAuthorizedToPost, setPermissionPolicy, getOwnerKeystoneTjp, } from '../cosmos/cosmos-helpers.mjs'; import { parseLineRangeAnchor, parseTargetUrl, extractTargetUrlsFromMarkdown, updateTargetsIndexData, getTargetAnchorsForFile, serializeTargetsIndex, deserializeTargetsIndex, } from './metatext-helpers.mjs'; import { METATEXT_ATOM, METATEXT_DEFAULT_GALAXY_NAME, TARGETS_INDEX_FILE_PATH, } from './metatext-constants.mjs'; const lc = `[core-gib/src/cosmology/metatext/metatext.respec.mts]`; await respecfully(lc, `Metatext Galaxy & AOT Index Specs`, async () => { await respecfully(lc, `Constants & Baseline`, async () => { await ifWe(lc, `exports expected metatext constants`, async () => { iReckon(lc, METATEXT_ATOM).willEqual('metatext'); iReckon(lc, METATEXT_DEFAULT_GALAXY_NAME).willEqual('metatext-1'); iReckon(lc, TARGETS_INDEX_FILE_PATH).willEqual('index/targets.json'); }); }); await respecfully(lc, `Line Range Parsing`, async () => { await ifWe(lc, `parses #L12-L24 as [12, 24]`, async () => { const range = parseLineRangeAnchor('#L12-L24'); iReckon(lc, range?.[0]).willEqual(12); iReckon(lc, range?.[1]).willEqual(24); }); await ifWe(lc, `parses #L15 as [15, 15]`, async () => { const range = parseLineRangeAnchor('#L15'); iReckon(lc, range?.[0]).willEqual(15); iReckon(lc, range?.[1]).willEqual(15); }); await ifWe(lc, `returns undefined for invalid anchor format`, async () => { iReckon(lc, parseLineRangeAnchor('')).isGonnaBeUndefined(); iReckon(lc, parseLineRangeAnchor('#')).isGonnaBeUndefined(); iReckon(lc, parseLineRangeAnchor('#invalid')).isGonnaBeUndefined(); }); }); await respecfully(lc, `Target URL Parsing`, async () => { await ifWe(lc, `parses full https://ibgib.space URL with userHandle and lineRange`, async () => { const url = 'https://ibgib.space/@bill/cosmos-1/code-1/main/src/index.mts#L12-L24'; const parsed = parseTargetUrl(url); iReckon(lc, Boolean(parsed)).isGonnaBeTrue(); iReckon(lc, parsed?.userHandle).willEqual('bill'); iReckon(lc, parsed?.cosmosName).willEqual('cosmos-1'); iReckon(lc, parsed?.galaxyName).willEqual('code-1'); iReckon(lc, parsed?.branchName).willEqual('main'); iReckon(lc, parsed?.filePath).willEqual('src/index.mts'); iReckon(lc, parsed?.lineRange?.[0]).willEqual(12); iReckon(lc, parsed?.lineRange?.[1]).willEqual(24); iReckon(lc, parsed?.targetKey).willEqual('code-1/src/index.mts'); }); await ifWe(lc, `parses ibgib:// protocol URI`, async () => { const uri = 'ibgib://code-1/main/src/utils.mts#L5'; const parsed = parseTargetUrl(uri); iReckon(lc, Boolean(parsed)).isGonnaBeTrue(); iReckon(lc, parsed?.galaxyName).willEqual('code-1'); iReckon(lc, parsed?.branchName).willEqual('main'); iReckon(lc, parsed?.filePath).willEqual('src/utils.mts'); iReckon(lc, parsed?.lineRange?.[0]).willEqual(5); iReckon(lc, parsed?.targetKey).willEqual('code-1/src/utils.mts'); }); await ifWe(lc, `returns undefined for non-target URLs`, async () => { iReckon(lc, parseTargetUrl('https://google.com')).isGonnaBeUndefined(); iReckon(lc, parseTargetUrl('')).isGonnaBeUndefined(); }); }); await respecfully(lc, `Markdown Extraction`, async () => { await ifWe(lc, `extracts multiple target links from Markdown body`, async () => { const markdown = ` # Code Review I found a bug in [index.mts](https://ibgib.space/@bill/cosmos-1/code-1/main/src/index.mts#L12-L24). Also check [helper](https://ibgib.space/@bill/cosmos-1/code-1/main/src/helpers.mts#L40-L50). And here is a bare url: https://ibgib.space/@bill/cosmos-1/code-1/main/src/index.mts#L12-L24 `; const targets = extractTargetUrlsFromMarkdown(markdown); // Should deduplicate identical targets iReckon(lc, targets.length).willEqual(2); iReckon(lc, targets[0].filePath).willEqual('src/index.mts'); iReckon(lc, targets[1].filePath).willEqual('src/helpers.mts'); }); }); await respecfully(lc, `AOT Index Management & O(1) Lookup`, async () => { await ifWe(lc, `updates TargetsIndexData and retrieves anchors for a file`, async () => { const targetUrls = extractTargetUrlsFromMarkdown( `Check [index.mts](https://ibgib.space/@bill/cosmos-1/code-1/main/src/index.mts#L12-L24)` ); const initialIndex = updateTargetsIndexData({ currentIndex: null, targetUrls, commentFilePath: 'topics/general/101_alice.md', author: '@alice', authorKeystoneTjp: 'keystone^alice_tjp', timestamp: '2026-10-01T18:05:00Z', }); // Add second comment targeting the same file but different lines const targetUrls2 = extractTargetUrlsFromMarkdown( `Also [index.mts](https://ibgib.space/@bill/cosmos-1/code-1/main/src/index.mts#L80-L90)` ); const updatedIndex = updateTargetsIndexData({ currentIndex: initialIndex, targetUrls: targetUrls2, commentFilePath: 'topics/general/102_bob.md', author: '@bob', authorKeystoneTjp: 'keystone^bob_tjp', timestamp: '2026-10-01T18:06:00Z', }); const anchors = getTargetAnchorsForFile({ indexData: updatedIndex, filePath: 'src/index.mts', galaxyName: 'code-1', }); iReckon(lc, anchors.length).willEqual(2); iReckon(lc, anchors[0].author).willEqual('@alice'); iReckon(lc, anchors[0].lineRange?.[0]).willEqual(12); iReckon(lc, anchors[1].author).willEqual('@bob'); iReckon(lc, anchors[1].lineRange?.[0]).willEqual(80); }); await ifWe(lc, `round-trips serialize and deserialize`, async () => { const data = { targets: { 'code-1/src/index.mts': [ { filePath: 'topics/general/101.md', timestamp: '2026-10-01T18:00:00Z', }, ], }, }; const json = serializeTargetsIndex(data); const parsed = deserializeTargetsIndex(json); iReckon(lc, parsed.targets['code-1/src/index.mts']?.[0]?.filePath).willEqual('topics/general/101.md'); }); }); await respecfully(lc, `Permission Policies & Admission Gates`, async () => { await ifWe(lc, `evaluates public-open, allowlist, and admin-only admission policies`, async () => { const { metaspace, space } = await createInMemoryTempMetaspaceAndSpace(); const ownerTjp = 'keystone^bill_primary_tjp'; let cosmos = await createCosmosIbGib({ cosmosName: 'my-project', ownerKeystoneTjp: ownerTjp, space, metaspace, }); iReckon(lc, getOwnerKeystoneTjp(cosmos)).willEqual(ownerTjp); // 1. Without policies, default access is public-open const defaultAuth = isAuthorizedToPost({ cosmosIbGib: cosmos, galaxyName: 'metatext-1', candidateKeystoneTjp: 'keystone^random_contributor', }); iReckon(lc, defaultAuth.isAuthorized).isGonnaBeTrue(); // 2. Set 'allowlist' policy on metatext-1 cosmos = await setPermissionPolicy({ cosmosIbGib: cosmos, policy: { galaxyName: 'metatext-1', access: 'allowlist', allowedKeystones: ['keystone^alice_tjp'], }, space, metaspace, }); const policy = getPermissionPolicy({ cosmosIbGib: cosmos, galaxyName: 'metatext-1' }); iReckon(lc, policy?.access).willEqual('allowlist'); // Alice is allowed const aliceAuth = isAuthorizedToPost({ cosmosIbGib: cosmos, galaxyName: 'metatext-1', candidateKeystoneTjp: 'keystone^alice_tjp', }); iReckon(lc, aliceAuth.isAuthorized).isGonnaBeTrue(); // Random user is rejected const strangerAuth = isAuthorizedToPost({ cosmosIbGib: cosmos, galaxyName: 'metatext-1', candidateKeystoneTjp: 'keystone^stranger_tjp', }); iReckon(lc, strangerAuth.isAuthorized).isGonnaBeFalse(); // Owner (Bill) is always authorized even if not explicitly on allowlist const ownerAuth = isAuthorizedToPost({ cosmosIbGib: cosmos, galaxyName: 'metatext-1', candidateKeystoneTjp: ownerTjp, }); iReckon(lc, ownerAuth.isAuthorized).isGonnaBeTrue(); }); }); });