/** * @module graph-helper.respec * * Comprehensive unit tests for `reifyDependencyGraph` in `libs/core-gib`. * Tests bottom-up DAG reification across shallow, deep (2-, 3-, 4-, 5-level), * branching, and relationship-filtered graphs. */ import { respecfully, ifWeMight, ifWe, 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 { rel8 } from '@ibgib/ts-gib/dist/V1/transforms/rel8.mjs'; import { mut8 } from '@ibgib/ts-gib/dist/V1/transforms/mut8.mjs'; import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.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 { persistTransformResult, registerNewIbGib } from '../../witness/space/space-helper.mjs'; import { reifyDependencyGraph } from './graph-helper.mjs'; const lc = `[graph-helper.respec]`; await respecfully(sir, `reifyDependencyGraph`, async () => { let metaspace: Metaspace_Innerspace; let space: InnerSpace_V1; async function initTestSpaces() { metaspace = new Metaspace_Innerspace(undefined); await metaspace.initialize({ getFnAlert: () => async ({ title, msg }) => { console.log(`[Alert] ${title}: ${msg}`); }, getFnPrompt: () => async ({ title, msg }) => { return ''; }, getFnPromptPassword: () => async (title, msg) => { return null; }, }); while (!metaspace.initialized) { await delay(10); } space = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: `test_space_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`, uuid: `space_uuid_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`, description: 'in-memory test space for reifyDependencyGraph', }); await space.initialized; } async function rel8Nodes({ parent, childrenByRel8n, }: { parent: IbGib_V1; childrenByRel8n: { [rel8nName: string]: IbGib_V1[] }; }): Promise { const rel8nsToAddByAddr: { [rel8nName: string]: string[] } = {}; for (const [rel8n, children] of Object.entries(childrenByRel8n)) { rel8nsToAddByAddr[rel8n] = children.map(c => getIbGibAddr({ ibGib: c })); } const res = await rel8({ src: parent, rel8nsToAddByAddr, dna: true, nCounter: true, }); await persistTransformResult({ resTransform: res, space }); await registerNewIbGib({ ibGib: res.newIbGib, space }); return res.newIbGib; } async function mutateNode({ node, dataToAddOrPatch, }: { node: IbGib_V1; dataToAddOrPatch: any; }): Promise { const res = await mut8({ src: node, dataToAddOrPatch, dna: true, nCounter: true, }); await persistTransformResult({ resTransform: res, space }); await registerNewIbGib({ ibGib: res.newIbGib, space }); return res.newIbGib; } // ───────────────────────────────────────────────────────────────────────── // 2.1 Validation & Guard Tests // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.1 throws if both ibGib and ibGibAddr are passed`, async () => { await initTestSpaces(); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); let threw = false; try { await reifyDependencyGraph({ ibGib: root, ibGibAddr: getIbGibAddr({ ibGib: root }), metaspace, space, }); } catch { threw = true; } iReckon(sir, threw).isGonnaBeTrue(); }); await ifWe(sir, `2.1 throws if neither ibGib nor ibGibAddr is passed`, async () => { await initTestSpaces(); let threw = false; try { await reifyDependencyGraph({ metaspace, space, }); } catch { threw = true; } iReckon(sir, threw).isGonnaBeTrue(); }); await ifWe(sir, `2.1 throws if onlyRel8nNames is passed as an empty array`, async () => { await initTestSpaces(); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); let threw = false; try { await reifyDependencyGraph({ ibGib: root, onlyRel8nNames: [], metaspace, space, }); } catch { threw = true; } iReckon(sir, threw).isGonnaBeTrue(); }); await ifWe(sir, `2.1 throws if onlyRel8nNames contains forbidden intrinsic rel8ns ('past', 'dna', 'tjp', 'ancestor')`, async () => { await initTestSpaces(); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); let threwPast = false; try { await reifyDependencyGraph({ ibGib: root, onlyRel8nNames: ['past'], metaspace, space, }); } catch { threwPast = true; } iReckon(sir, threwPast).isGonnaBeTrue(); let threwDna = false; try { await reifyDependencyGraph({ ibGib: root, onlyRel8nNames: ['child', 'dna'], metaspace, space, }); } catch { threwDna = true; } iReckon(sir, threwDna).isGonnaBeTrue(); }); // ───────────────────────────────────────────────────────────────────────── // 2.2 No-Op Baseline (Unchanged Graphs) // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.2 no-op baseline: single leaf node with no updates returns hasChanges false`, async () => { await initTestSpaces(); const leaf = await createTimelineRootTestHelper({ ib: 'node leaf', data: { text: 'v1' }, space }); const res = await reifyDependencyGraph({ ibGib: leaf, metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeFalse(); iReckon(sir, getIbGibAddr({ ibGib: res.reifiedIbGib })).willEqual(getIbGibAddr({ ibGib: leaf })); iReckon(sir, res.evolvedIbGibs.length).willEqual(0); }); await ifWe(sir, `2.2 no-op baseline: 3-level graph with no updates returns hasChanges false`, async () => { await initTestSpaces(); const leaf = await createTimelineRootTestHelper({ ib: 'node leaf', data: { text: 'v1' }, space }); const mid = await createTimelineRootTestHelper({ ib: 'node mid', space }); const midWithLeaf = await rel8Nodes({ parent: mid, childrenByRel8n: { child: [leaf] } }); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); const rootWithMid = await rel8Nodes({ parent: root, childrenByRel8n: { child: [midWithLeaf] } }); const res = await reifyDependencyGraph({ ibGib: rootWithMid, metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeFalse(); iReckon(sir, getIbGibAddr({ ibGib: res.reifiedIbGib })).willEqual(getIbGibAddr({ ibGib: rootWithMid })); }); // ───────────────────────────────────────────────────────────────────────── // 2.3 1-Level Graph (Direct Child Edit) // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.3 1-level graph: direct child edit evolves root referencing updated child`, async () => { await initTestSpaces(); const childA = await createTimelineRootTestHelper({ ib: 'node childA', data: { text: 'A_v1' }, space }); const childB = await createTimelineRootTestHelper({ ib: 'node childB', data: { text: 'B_v1' }, space }); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); const rootWithChildren = await rel8Nodes({ parent: root, childrenByRel8n: { child: [childA, childB] } }); // Mutate Child A const childAV2 = await mutateNode({ node: childA, dataToAddOrPatch: { text: 'A_v2' }, }); const res = await reifyDependencyGraph({ ibGib: rootWithChildren, metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeTrue(); const reifiedRoot = res.reifiedIbGib; const reifiedChildren = reifiedRoot.rel8ns?.child ?? []; iReckon(sir, reifiedChildren).willEqual([getIbGibAddr({ ibGib: childAV2 }), getIbGibAddr({ ibGib: childB })]); iReckon(sir, reifiedChildren.includes(getIbGibAddr({ ibGib: childA }))).isGonnaBeFalse(); }); // ───────────────────────────────────────────────────────────────────────── // 2.4 2-Level Deep Graph (Grandchild Edit) // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.4 2-level deep graph: grandchild edit evolves parent and root`, async () => { await initTestSpaces(); const grandchild = await createTimelineRootTestHelper({ ib: 'node grandchild', data: { text: 'GC_v1' }, space }); const parent = await createTimelineRootTestHelper({ ib: 'node parent', space }); const parentWithGC = await rel8Nodes({ parent, childrenByRel8n: { child: [grandchild] } }); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); const rootWithParent = await rel8Nodes({ parent: root, childrenByRel8n: { child: [parentWithGC] } }); // Mutate grandchild -> GC_v2 const grandchildV2 = await mutateNode({ node: grandchild, dataToAddOrPatch: { text: 'GC_v2' }, }); const res = await reifyDependencyGraph({ ibGib: rootWithParent, metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeTrue(); const reifiedRoot = res.reifiedIbGib; const reifiedParentAddr = reifiedRoot.rel8ns?.child?.at(0); iReckon(sir, reifiedParentAddr).isGonnaBeTruthy(); iReckon(sir, reifiedParentAddr === getIbGibAddr({ ibGib: parentWithGC })).isGonnaBeFalse(); // Load the reified parent and verify its child is grandchildV2 const reifiedParent = res.evolvedIbGibs.find(node => getIbGibAddr({ ibGib: node }) === reifiedParentAddr); iReckon(sir, reifiedParent).isGonnaBeTruthy(); iReckon(sir, reifiedParent!.rel8ns?.child).includes(getIbGibAddr({ ibGib: grandchildV2 })); }); // ───────────────────────────────────────────────────────────────────────── // 2.5 3-Level Deep Graph (Great-Grandchild Edit with Branching) // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.5 3-level deep graph: great-grandchild edit bubbles up strictly along modified branch`, async () => { await initTestSpaces(); // Branch 1: Parent1 -> Grandchild1 -> Leaf1 const leaf1 = await createTimelineRootTestHelper({ ib: 'node leaf1', data: { text: 'L1_v1' }, space }); const gc1 = await createTimelineRootTestHelper({ ib: 'node gc1', space }); const gc1WithLeaf = await rel8Nodes({ parent: gc1, childrenByRel8n: { child: [leaf1] } }); const p1 = await createTimelineRootTestHelper({ ib: 'node p1', space }); const p1WithGC = await rel8Nodes({ parent: p1, childrenByRel8n: { child: [gc1WithLeaf] } }); // Branch 2: Parent2 -> Grandchild2 -> Leaf2 (untouched) const leaf2 = await createTimelineRootTestHelper({ ib: 'node leaf2', data: { text: 'L2_v1' }, space }); const gc2 = await createTimelineRootTestHelper({ ib: 'node gc2', space }); const gc2WithLeaf = await rel8Nodes({ parent: gc2, childrenByRel8n: { child: [leaf2] } }); const p2 = await createTimelineRootTestHelper({ ib: 'node p2', space }); const p2WithGC = await rel8Nodes({ parent: p2, childrenByRel8n: { child: [gc2WithLeaf] } }); // Root -> [Parent1, Parent2] const root = await createTimelineRootTestHelper({ ib: 'node root', space }); const rootWithBranches = await rel8Nodes({ parent: root, childrenByRel8n: { child: [p1WithGC, p2WithGC] } }); // Mutate Leaf 1 ONLY await mutateNode({ node: leaf1, dataToAddOrPatch: { text: 'L1_v2' }, }); const res = await reifyDependencyGraph({ ibGib: rootWithBranches, metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeTrue(); const reifiedRoot = res.reifiedIbGib; const rootChildren = reifiedRoot.rel8ns?.child ?? []; // Branch 2 (p2) must remain untouched with its original addr iReckon(sir, rootChildren).includes(getIbGibAddr({ ibGib: p2WithGC })); // Branch 1 (p1) must be evolved iReckon(sir, rootChildren.includes(getIbGibAddr({ ibGib: p1WithGC }))).isGonnaBeFalse(); }); // ───────────────────────────────────────────────────────────────────────── // 2.6 4-Level Deep Graph (Buried Multi-Leaf Edits) // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.6 4-level deep graph: buried multi-leaf edits update both converging paths`, async () => { await initTestSpaces(); // Path A: Level 1 -> Level 2 -> Level 3 -> LeafA (Level 4) const leafA = await createTimelineRootTestHelper({ ib: 'node leafA', data: { text: 'A_v1' }, space }); const l3A = await createTimelineRootTestHelper({ ib: 'node l3A', space }); const l3AWithLeaf = await rel8Nodes({ parent: l3A, childrenByRel8n: { child: [leafA] } }); const l2A = await createTimelineRootTestHelper({ ib: 'node l2A', space }); const l2AWithL3 = await rel8Nodes({ parent: l2A, childrenByRel8n: { child: [l3AWithLeaf] } }); const l1A = await createTimelineRootTestHelper({ ib: 'node l1A', space }); const l1AWithL2 = await rel8Nodes({ parent: l1A, childrenByRel8n: { child: [l2AWithL3] } }); // Path B: Level 1 -> Level 2 -> LeafB (Level 3 edit) const leafB = await createTimelineRootTestHelper({ ib: 'node leafB', data: { text: 'B_v1' }, space }); const l2B = await createTimelineRootTestHelper({ ib: 'node l2B', space }); const l2BWithLeaf = await rel8Nodes({ parent: l2B, childrenByRel8n: { child: [leafB] } }); const l1B = await createTimelineRootTestHelper({ ib: 'node l1B', space }); const l1BWithL2 = await rel8Nodes({ parent: l1B, childrenByRel8n: { child: [l2BWithLeaf] } }); // Root -> [L1A, L1B] const root = await createTimelineRootTestHelper({ ib: 'node root', space }); const rootNode = await rel8Nodes({ parent: root, childrenByRel8n: { child: [l1AWithL2, l1BWithL2] } }); // Mutate LeafA (level 4) and LeafB (level 3) await mutateNode({ node: leafA, dataToAddOrPatch: { text: 'A_v2' } }); await mutateNode({ node: leafB, dataToAddOrPatch: { text: 'B_v2' } }); const res = await reifyDependencyGraph({ ibGib: rootNode, metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeTrue(); iReckon(sir, res.evolvedIbGibs.length).willEqual(6); // Root + L1A + L2A + L3A + L1B + L2B }); // ───────────────────────────────────────────────────────────────────────── // 2.7 5-Level Deep Stress Graph // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.7 5-level deep stress graph: deep linear chain reifies deterministically`, async () => { await initTestSpaces(); // L5 (Leaf) -> L4 -> L3 -> L2 -> L1 -> Root const l5 = await createTimelineRootTestHelper({ ib: 'node l5', data: { text: 'v1' }, space }); const l4 = await createTimelineRootTestHelper({ ib: 'node l4', space }); const l4Node = await rel8Nodes({ parent: l4, childrenByRel8n: { child: [l5] } }); const l3 = await createTimelineRootTestHelper({ ib: 'node l3', space }); const l3Node = await rel8Nodes({ parent: l3, childrenByRel8n: { child: [l4Node] } }); const l2 = await createTimelineRootTestHelper({ ib: 'node l2', space }); const l2Node = await rel8Nodes({ parent: l2, childrenByRel8n: { child: [l3Node] } }); const l1 = await createTimelineRootTestHelper({ ib: 'node l1', space }); const l1Node = await rel8Nodes({ parent: l1, childrenByRel8n: { child: [l2Node] } }); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); const rootNode = await rel8Nodes({ parent: root, childrenByRel8n: { child: [l1Node] } }); // Mutate bottom leaf L5 -> v2 await mutateNode({ node: l5, dataToAddOrPatch: { text: 'v2' } }); const res = await reifyDependencyGraph({ ibGib: rootNode, metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeTrue(); iReckon(sir, res.evolvedIbGibs.length).willEqual(5); // Root + L1 + L2 + L3 + L4 iReckon(sir, getIbGibAddr({ ibGib: res.reifiedIbGib }) === getIbGibAddr({ ibGib: rootNode })).isGonnaBeFalse(); }); // ───────────────────────────────────────────────────────────────────────── // 2.8 Multiple Sibling Relationships // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.8 multiple sibling rel8ns: updates across distinct rel8n keys reify in single pass`, async () => { await initTestSpaces(); const primary = await createTimelineRootTestHelper({ ib: 'node primary', data: { text: 'P_v1' }, space }); const tag = await createTimelineRootTestHelper({ ib: 'node tag', data: { text: 'T_v1' }, space }); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); const rootNode = await rel8Nodes({ parent: root, childrenByRel8n: { primary_child: [primary], metadata_tag: [tag], }, }); // Mutate both primary and tag const resP = await mutateNode({ node: primary, dataToAddOrPatch: { text: 'P_v2' } }); const resT = await mutateNode({ node: tag, dataToAddOrPatch: { text: 'T_v2' } }); const res = await reifyDependencyGraph({ ibGib: rootNode, metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeTrue(); const reifiedRoot = res.reifiedIbGib; iReckon(sir, reifiedRoot.rel8ns?.primary_child).includes(getIbGibAddr({ ibGib: resP })); iReckon(sir, reifiedRoot.rel8ns?.metadata_tag).includes(getIbGibAddr({ ibGib: resT })); }); // ───────────────────────────────────────────────────────────────────────── // 2.9 onlyRel8nNames Filtering Permutation // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.9 onlyRel8nNames filter: reifies only specified relationship keys`, async () => { await initTestSpaces(); const child1 = await createTimelineRootTestHelper({ ib: 'node child1', data: { text: 'C1_v1' }, space }); const ignored = await createTimelineRootTestHelper({ ib: 'node ignored', data: { text: 'IGN_v1' }, space }); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); const rootNode = await rel8Nodes({ parent: root, childrenByRel8n: { target_rel8n: [child1], ignored_rel8n: [ignored], }, }); // Mutate both const resC1 = await mutateNode({ node: child1, dataToAddOrPatch: { text: 'C1_v2' } }); const resIgn = await mutateNode({ node: ignored, dataToAddOrPatch: { text: 'IGN_v2' } }); const res = await reifyDependencyGraph({ ibGib: rootNode, onlyRel8nNames: ['target_rel8n'], metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeTrue(); const reifiedRoot = res.reifiedIbGib; // target_rel8n is updated iReckon(sir, reifiedRoot.rel8ns?.target_rel8n).includes(getIbGibAddr({ ibGib: resC1 })); // ignored_rel8n remains pointing to the old un-reified version iReckon(sir, reifiedRoot.rel8ns?.ignored_rel8n).includes(getIbGibAddr({ ibGib: ignored })); iReckon(sir, reifiedRoot.rel8ns?.ignored_rel8n?.includes(getIbGibAddr({ ibGib: resIgn }))).isGonnaBeFalse(); }); // ───────────────────────────────────────────────────────────────────────── // 2.10 skipRel8nNames Filtering Permutation // ───────────────────────────────────────────────────────────────────────── await ifWe(sir, `2.10 skipRel8nNames filter: skips specified relationship keys during reification`, async () => { await initTestSpaces(); const included = await createTimelineRootTestHelper({ ib: 'node included', data: { text: 'INC_v1' }, space }); const skipped = await createTimelineRootTestHelper({ ib: 'node skipped', data: { text: 'SKP_v1' }, space }); const root = await createTimelineRootTestHelper({ ib: 'node root', space }); const rootNode = await rel8Nodes({ parent: root, childrenByRel8n: { included_rel8n: [included], skipped_rel8n: [skipped], }, }); // Mutate both const resInc = await mutateNode({ node: included, dataToAddOrPatch: { text: 'INC_v2' } }); await mutateNode({ node: skipped, dataToAddOrPatch: { text: 'SKP_v2' } }); const res = await reifyDependencyGraph({ ibGib: rootNode, skipRel8nNames: ['skipped_rel8n'], metaspace, space, }); iReckon(sir, res.hasChanges).isGonnaBeTrue(); const reifiedRoot = res.reifiedIbGib; // included_rel8n is updated iReckon(sir, reifiedRoot.rel8ns?.included_rel8n).includes(getIbGibAddr({ ibGib: resInc })); // skipped_rel8n remains pointing to the old version iReckon(sir, reifiedRoot.rel8ns?.skipped_rel8n).includes(getIbGibAddr({ ibGib: skipped })); }); });