/** * @module graph-helper-multispace.respec * * Unit tests for graph-helper multi-space and single-space getDependencyGraph functionality. */ import { respecfully, ifWe, ifWeMight, iReckon } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; const maam = `[${import.meta.url}]`, sir = maam; import { delay } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { IbGibData_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs'; import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs'; import { getDependencyGraph, getDeltaDependencyGraph, } from './graph-helper.mjs'; import { Metaspace_Innerspace } from '../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs'; import { InnerSpace_V1 } from '../../witness/space/inner-space/inner-space-v1.mjs'; import { DEFAULT_INNER_SPACE_DATA_V1 } from '../../witness/space/inner-space/inner-space-types.mjs'; import { createTimelineRootTestHelper } from '../../test-helpers.mjs'; import { mut8Timeline } from '../../timeline/timeline-api.mjs'; import { putInSpace } from '../../witness/space/space-helper.mjs'; const logalot = GLOBAL_LOG_A_LOT; const lc = sir; interface TestData extends IbGibData_V1 { label?: string; type?: string; } await respecfully(sir, `graph-helper multispace tests`, async () => { await respecfully(sir, `single space basic getDependencyGraph`, async () => { const metaspace = new Metaspace_Innerspace(undefined); await metaspace.initialize({ getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); }, getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; }, getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; }, }); while (!metaspace.initialized) { await delay(10); } const defaultLocalUserSpace = await metaspace.getLocalUserSpace({ lock: false }); await defaultLocalUserSpace!.initialized; const space = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: 'single_space_test', uuid: 'single_space_uuid', description: 'single space graph helper test space', }); await space.initialized; const root = await createTimelineRootTestHelper({ ib: 'graph_test_root', data: { type: 'root', label: 'Root' }, space: space, }); const v1 = await mut8Timeline({ timeline: root, mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V1' } }, metaspace, space: space, }); const v1Addr = getIbGibAddr({ ibGib: v1 }); await ifWe(sir, `get dependency graph in single space`, async () => { const graph = await getDependencyGraph({ ibGib: v1, space: space, }); iReckon(sir, !!graph).asTo('graph returned').isGonnaBeTrue(); const keys = Object.keys(graph); iReckon(sir, keys.length > 0).asTo('graph non-empty').isGonnaBeTrue(); iReckon(sir, keys.includes(v1Addr)).asTo('graph includes target ibGib').isGonnaBeTrue(); }); }); await respecfully(sir, `getDependencyGraph skipAddrs and getDeltaDependencyGraph tests`, async () => { const metaspace = new Metaspace_Innerspace(undefined); await metaspace.initialize({ getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); }, getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; }, getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; }, }); while (!metaspace.initialized) { await delay(10); } const space = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: 'delta_test_space', uuid: 'delta_test_uuid', description: 'delta test space', }); await space.initialized; const root = await createTimelineRootTestHelper({ ib: 'delta_test_root', data: { type: 'root', label: 'Root' }, space, }); const rootAddr = getIbGibAddr({ ibGib: root }); const v1 = await mut8Timeline({ timeline: root, mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V1' } }, metaspace, space, }); const v1Addr = getIbGibAddr({ ibGib: v1 }); const v2 = await mut8Timeline({ timeline: v1, mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V2' } }, metaspace, space, }); const v2Addr = getIbGibAddr({ ibGib: v2 }); await ifWe(sir, `getDependencyGraph respects skipAddrs and excludes skipped nodes`, async () => { const graphFull = await getDependencyGraph({ ibGib: v2, space, }); iReckon(sir, Object.keys(graphFull).includes(rootAddr)).asTo('full graph contains root').isGonnaBeTrue(); iReckon(sir, Object.keys(graphFull).includes(v1Addr)).asTo('full graph contains v1').isGonnaBeTrue(); iReckon(sir, Object.keys(graphFull).includes(v2Addr)).asTo('full graph contains v2').isGonnaBeTrue(); const graphSkipped = await getDependencyGraph({ ibGib: v2, skipAddrs: [v1Addr, rootAddr], space, }); iReckon(sir, Object.keys(graphSkipped).includes(v2Addr)).asTo('skipped graph still contains v2').isGonnaBeTrue(); iReckon(sir, Object.keys(graphSkipped).includes(v1Addr)).asTo('skipped graph excludes v1').isGonnaBeFalse(); iReckon(sir, Object.keys(graphSkipped).includes(rootAddr)).asTo('skipped graph excludes root').isGonnaBeFalse(); }); await ifWe(sir, `getDeltaDependencyGraph returns only delta nodes since latestCommonFrameAddr`, async () => { const deltaGraph = await getDeltaDependencyGraph({ ibGibAddr: v2Addr, latestCommonFrameAddr: v1Addr, live: false, space, }); iReckon(sir, !!deltaGraph).asTo('delta graph returned').isGonnaBeTrue(); const deltaKeys = Object.keys(deltaGraph); iReckon(sir, deltaKeys.includes(v2Addr)).asTo('delta graph contains v2').isGonnaBeTrue(); iReckon(sir, deltaKeys.includes(v1Addr)).asTo('delta graph excludes v1').isGonnaBeFalse(); iReckon(sir, deltaKeys.includes(rootAddr)).asTo('delta graph excludes root').isGonnaBeFalse(); }); await ifWe(sir, `getDeltaDependencyGraph returns empty graph when target equals common frame`, async () => { const emptyDelta = await getDeltaDependencyGraph({ ibGibAddr: v2Addr, latestCommonFrameAddr: v2Addr, live: false, space, }); iReckon(sir, Object.keys(emptyDelta).length === 0).asTo('empty delta graph has 0 keys').isGonnaBeTrue(); }); }); await respecfully(sir, `multi-space getDependencyGraph tests`, async () => { const metaspace = new Metaspace_Innerspace(undefined); await metaspace.initialize({ getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); }, getFnPrompt: () => async ({ title, msg }) => { console.log(`[Prompt] ${title}: ${msg}`); return ''; }, getFnPromptPassword: () => async (title, msg) => { console.log(`[PromptPwd] ${title}: ${msg}`); return null; }, }); while (!metaspace.initialized) { await delay(10); } const spaceA = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: 'multi_space_a', uuid: 'multi_space_a_uuid', description: 'multi space test space A', }); await spaceA.initialized; const spaceB = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: 'multi_space_b', uuid: 'multi_space_b_uuid', description: 'multi space test space B', }); await spaceB.initialized; await ifWe(sir, `mutual exclusivity guard when space and spaces both provided`, async () => { let errorThrown = false; try { await getDependencyGraph({ ibGibAddr: 'test^gib', space: spaceA, spaces: [spaceB], }); } catch (error) { errorThrown = true; iReckon(sir, error.message.includes('space and spaces parameters are mutually exclusive')).asTo('error message mentions mutual exclusivity').isGonnaBeTrue(); } iReckon(sir, errorThrown).asTo('error was thrown for mutual exclusivity').isGonnaBeTrue(); }); await ifWe(sir, `multi-space fallback resolution when ibgib in space B but missing in space A`, async () => { const rootB = await createTimelineRootTestHelper({ ib: 'graph_multispace_root_b', data: { type: 'root', label: 'RootB' }, space: spaceB, }); const v1B = await mut8Timeline({ timeline: rootB, mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V1B' } }, metaspace, space: spaceB, }); const v1BAddr = getIbGibAddr({ ibGib: v1B }); const graph = await getDependencyGraph({ ibGibAddr: v1BAddr, spaces: [spaceA, spaceB], }); iReckon(sir, !!graph).asTo('graph returned from multi-space').isGonnaBeTrue(); const keys = Object.keys(graph); iReckon(sir, keys.includes(v1BAddr)).asTo('graph includes target ibGib resolved from space B').isGonnaBeTrue(); }); await ifWe(sir, `multi-space live graph projection resolves latest address across spaces`, async () => { const rootLive = await createTimelineRootTestHelper({ ib: 'graph_multispace_root_live', data: { type: 'root', label: 'RootLive' }, space: spaceA, }); // put initial root in spaceB as well so spaceB knows about the timeline await putInSpace({ ibGib: rootLive, space: spaceB }); const v1Live = await mut8Timeline({ timeline: rootLive, mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V1Live' } }, metaspace, space: spaceA, }); await putInSpace({ ibGib: v1Live, space: spaceB }); // Now mutate to V2Live ONLY in spaceB const v2Live = await mut8Timeline({ timeline: v1Live, mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V2Live' } }, metaspace, space: spaceB, }); const v2LiveAddr = getIbGibAddr({ ibGib: v2Live }); const graph = await getDependencyGraph({ ibGib: rootLive, live: true, spaces: [spaceA, spaceB], }); iReckon(sir, !!graph).asTo('live graph returned').isGonnaBeTrue(); const keys = Object.keys(graph); iReckon(sir, keys.includes(v2LiveAddr)).asTo('live graph found latest address v2Live in space B').isGonnaBeTrue(); }); }); });