/** * @module branch-tree.respec * * Unit tests for Composite Branching: hierarchical branch tree creation, * soft-link parent registration ($@branch), depth-pruned traversal, * and scoped sub-branch metadata. */ 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 { createInMemoryTempMetaspaceAndSpace } from '../../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs'; import { executeVcsInit } from '../commands/core-init-cmd.mjs'; import { executeVcsBranch } from '../commands/core-branch-cmd.mjs'; import { executeVcsMerge } from '../commands/core-merge-cmd.mjs'; import { getCompositeBranchTreeFromGalaxy, getCompositeBranchTreeFromBranch, getAllBranchInfosFromGalaxy, findNodeInTree, pruneTreeDepth, flattenCompositeBranchTree, } from './branch-helpers.mjs'; import { B2tFSGalaxyIbGib_V1 } from '../b2tfs-galaxy/b2tfs-galaxy-types.mjs'; const lc = `[core-gib/src/cosmology/branching/branch/branch-tree.respec.mts]`; await respecfully(lc, `Composite Branching Engine Specs`, async () => { await respecfully(lc, `Composite Branch Creation and Tree Structure`, async () => { await ifWe(lc, `builds a multi-tier branch tree: main -> (branch1 -> branch1-1, branch2)`, async () => { const { metaspace, space: localSpace } = await createInMemoryTempMetaspaceAndSpace(); // 1. Initialize polycosmos (main) const initRes = await executeVcsInit({ metaspace, localSpace, galaxyName: 'code-composite', branchName: 'main', }); let galaxyIbGib = initRes.galaxyIbGib; iReckon(lc, galaxyIbGib).isGonnaBeTruthy(); iReckon(lc, galaxyIbGib.data.trunkBranchSqAddr).isGonnaBeTruthy(); iReckon(lc, galaxyIbGib.data['$@branch']?.length).willEqual(1); // 2. Branch 'branch1' from 'main' const branch1Res = await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'branch1', sourceBranchName: 'main', }); galaxyIbGib = branch1Res.updatedGalaxy!; iReckon(lc, galaxyIbGib).isGonnaBeTruthy(); // Galaxy still points only to trunk iReckon(lc, galaxyIbGib.data['$@branch']?.length).willEqual(1); // 3. Branch 'branch1-1' from 'branch1' const branch1_1Res = await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'branch1-1', sourceBranchName: 'branch1', }); // Galaxy is NOT mutated when branching from a non-trunk branch iReckon(lc, branch1_1Res.updatedGalaxy).isGonnaBeTruthy(); // 4. Branch 'branch2' from 'main' const branch2Res = await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'branch2', sourceBranchName: 'main', }); galaxyIbGib = branch2Res.updatedGalaxy!; // 5. Query full composite tree from galaxy const fullTree = await getCompositeBranchTreeFromGalaxy({ galaxyIbGib, metaspace, }); iReckon(lc, fullTree).isGonnaBeTruthy(); iReckon(lc, fullTree.branchInfo.branchName).willEqual('main'); iReckon(lc, fullTree.children.length).willEqual(2); const branch1Node = fullTree.children.find(c => c.branchInfo.branchName === 'branch1'); const branch2Node = fullTree.children.find(c => c.branchInfo.branchName === 'branch2'); iReckon(lc, branch1Node).isGonnaBeTruthy(); iReckon(lc, branch2Node).isGonnaBeTruthy(); // branch1 has child branch1-1 iReckon(lc, branch1Node!.children.length).willEqual(1); iReckon(lc, branch1Node!.children[0].branchInfo.branchName).willEqual('branch1-1'); iReckon(lc, branch1Node!.children[0].parentBranchName).willEqual('branch1'); iReckon(lc, branch1Node!.children[0].parentBranchSqAddr).isGonnaBeTruthy(); // branch2 has no children iReckon(lc, branch2Node!.children.length).willEqual(0); // 6. Test getAllBranchInfosFromGalaxy (flat listing) const allInfos = await getAllBranchInfosFromGalaxy({ galaxyIbGib, metaspace }); iReckon(lc, allInfos.length).willEqual(4); const branchNames = allInfos.map(b => b.branchName); iReckon(lc, branchNames.includes('main')).isGonnaBeTrue(); iReckon(lc, branchNames.includes('branch1')).isGonnaBeTrue(); iReckon(lc, branchNames.includes('branch1-1')).isGonnaBeTrue(); iReckon(lc, branchNames.includes('branch2')).isGonnaBeTrue(); }); await ifWe(lc, `respects depth limits when traversing composite tree`, async () => { const { metaspace, space: localSpace } = await createInMemoryTempMetaspaceAndSpace(); const initRes = await executeVcsInit({ metaspace, localSpace, galaxyName: 'code-depth', branchName: 'main', }); let galaxyIbGib = initRes.galaxyIbGib; const b1Res = await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'branch1', sourceBranchName: 'main', }); galaxyIbGib = b1Res.updatedGalaxy!; await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'branch1-1', sourceBranchName: 'branch1', }); // Depth 0: trunk only, no children const depth0Tree = await getCompositeBranchTreeFromGalaxy({ galaxyIbGib, metaspace, depth: 0, }); iReckon(lc, depth0Tree.branchInfo.branchName).willEqual('main'); iReckon(lc, depth0Tree.children.length).willEqual(0); // Depth 1: trunk + immediate children, but branch1's children are not traversed const depth1Tree = await getCompositeBranchTreeFromGalaxy({ galaxyIbGib, metaspace, depth: 1, }); iReckon(lc, depth1Tree.branchInfo.branchName).willEqual('main'); iReckon(lc, depth1Tree.children.length).willEqual(1); iReckon(lc, depth1Tree.children[0].branchInfo.branchName).willEqual('branch1'); iReckon(lc, depth1Tree.children[0].children.length).willEqual(0); }); await ifWe(lc, `traverses composite tree starting from a sub-branch (startBranchName)`, async () => { const { metaspace, space: localSpace } = await createInMemoryTempMetaspaceAndSpace(); const initRes = await executeVcsInit({ metaspace, localSpace, galaxyName: 'code-start-branch', branchName: 'main', }); let galaxyIbGib = initRes.galaxyIbGib; const b1Res = await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'branch1', sourceBranchName: 'main', }); galaxyIbGib = b1Res.updatedGalaxy!; await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'branch1-1', sourceBranchName: 'branch1', }); // Start traversal from 'branch1' const subTree = await getCompositeBranchTreeFromGalaxy({ galaxyIbGib, metaspace, startBranchName: 'branch1', }); iReckon(lc, subTree.branchInfo.branchName).willEqual('branch1'); iReckon(lc, subTree.children.length).willEqual(1); iReckon(lc, subTree.children[0].branchInfo.branchName).willEqual('branch1-1'); }); await ifWe(lc, `enforces branch name uniqueness across composite hierarchy`, async () => { const { metaspace, space: localSpace } = await createInMemoryTempMetaspaceAndSpace(); const initRes = await executeVcsInit({ metaspace, localSpace, galaxyName: 'code-unique', branchName: 'main', }); let galaxyIbGib = initRes.galaxyIbGib; const b1Res = await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'feature-x', sourceBranchName: 'main', }); galaxyIbGib = b1Res.updatedGalaxy!; // Attempting to create duplicate branch name should throw let threw = false; try { await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'feature-x', sourceBranchName: 'main', }); } catch (err: any) { threw = true; iReckon(lc, err.message.includes('already exists')).isGonnaBeTrue(); } iReckon(lc, threw).isGonnaBeTrue(); }); await ifWe(lc, `isolates galaxy when merging into sub-branches, and updates trunk when merging into main`, async () => { const { metaspace, space: localSpace } = await createInMemoryTempMetaspaceAndSpace(); const initRes = await executeVcsInit({ metaspace, localSpace, galaxyName: 'code-merge', branchName: 'main', }); let galaxyIbGib = initRes.galaxyIbGib; const b1Res = await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'branch1', sourceBranchName: 'main', }); galaxyIbGib = b1Res.updatedGalaxy!; await executeVcsBranch({ metaspace, localSpace, galaxyIbGib, branchName: 'branch1-1', sourceBranchName: 'branch1', }); // Merge branch1-1 into branch1 const mergeSubRes = await executeVcsMerge({ metaspace, localSpace, galaxyIbGib, baseBranchName: 'branch1', incomingBranchName: 'branch1-1', }); // Galaxy was NOT mutated because base branch is NOT trunk iReckon(lc, mergeSubRes.updatedGalaxy).willEqual(galaxyIbGib); iReckon(lc, mergeSubRes.updatedGalaxy.data['$@branch']?.length).willEqual(1); // Merge branch1 into main const mergeMainRes = await executeVcsMerge({ metaspace, localSpace, galaxyIbGib, baseBranchName: 'main', incomingBranchName: 'branch1', }); galaxyIbGib = mergeMainRes.updatedGalaxy; iReckon(lc, galaxyIbGib.data['$@branch']?.length).willEqual(1); iReckon(lc, galaxyIbGib.data.trunkBranchSqAddr).isGonnaBeTruthy(); // All branches still present in composite tree const allInfos = await getAllBranchInfosFromGalaxy({ galaxyIbGib, metaspace }); iReckon(lc, allInfos.length).willEqual(3); }); }); });