/** * @module snapshot-helpers.respec * * Respec test suite for B2tFS Snapshot Manifest Stones: * deterministic tree mapping, zero hard links in rel8ns, fail-fast author keystone signing, * hash integrity, and branch linear history retrieval. */ import { respecfully, ifWe, ifWeMight, iReckon, } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; import { getUUID } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { createInMemoryTempMetaspaceAndSpace } from '../../../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs'; import { getTjpAddr } from '../../../common/other/ibgib-helper.mjs'; import { KeystoneService_V1 } from '../../../keystone/keystone-service-v1.mjs'; import { createStandardPoolConfig } from '../../../keystone/keystone-config-builder.mjs'; import { KEYSTONE_VERB_AUTHOR, POOL_ID_DEFAULT, POOL_ID_MANAGE, } from '../../../keystone/keystone-constants.mjs'; import { createB2tFSItemIbGib } from '../item/item-helpers.mjs'; import { B2tFSItemIbGib_V1, B2TFS_CHILD_ITEM_REL8N_NAME, BINARY_REL8N_NAME } from '../item/item-types.mjs'; import { createBinIbGib } from '../../../common/bin/bin-helpers.mjs'; import { createB2tFSBranchIbGib } from '../branch/branch-helpers.mjs'; import { B2tFSBranchIbGib_V1 } from '../branch/branch-types.mjs'; import { createBranchSnapshotIbGib, validateSnapshotIbGib, getLatestBranchSnapshot, getBranchSnapshotHistory, diffSnapshotTrees, } from './snapshot-helpers.mjs'; const lc = `[snapshot-helpers.respec.mts]`; await respecfully(lc, `B2tFS Snapshot Manifest Stones`, async () => { const { metaspace, space } = await createInMemoryTempMetaspaceAndSpace(); const keystoneService = new KeystoneService_V1(); const parentSecret = "ParentSecret_Alice_SnapshotTest_12345"; const salt = (await getUUID()).substring(0, 16); const parentDefaultConfig = createStandardPoolConfig({ id: POOL_ID_DEFAULT, salt }); const parentManageConfig = createStandardPoolConfig({ id: POOL_ID_MANAGE, salt: salt + "_manage" }); // 1. Create Parent Identity (Alice) const parentKeystone = await keystoneService.genesis({ masterSecret: parentSecret, configs: [parentDefaultConfig, parentManageConfig], metaspace, space, }); const parentTjp = getTjpAddr({ ibGib: parentKeystone })!; // 2. Create Author Delegate Keystone (Device) const delegateSecret = "DelegateSecret_Device_Author_67890"; const delegateConfig = createStandardPoolConfig({ id: POOL_ID_DEFAULT, salt: salt + "_delegate" }); const delegateKeystone = await keystoneService.genesis({ masterSecret: delegateSecret, configs: [delegateConfig], metaspace, space, }); // 3. Register Delegate under Parent with KEYSTONE_VERB_AUTHOR await keystoneService.registerDelegate({ parentKeystone, delegateKeystone, masterSecret: parentSecret, signingPoolId: POOL_ID_MANAGE, metaspace, space, allowedVerbs: [KEYSTONE_VERB_AUTHOR], }); const deviceDelegate = { delegateKeystone, delegateSecret, }; // Helper to assemble a small test filesystem tree: // root/ // README.md (file) // src/ (folder) // index.ts (file) async function createTestTree(): Promise<{ rootFolder: B2tFSItemIbGib_V1; file1Addr: string; file2Addr: string }> { // Create binary stones const bin1 = await createBinIbGib({ data: '# Readme content', space, metaspace }); const bin2 = await createBinIbGib({ data: 'console.log("hello");', space, metaspace }); // File 1: README.md const file1Res = await createB2tFSItemIbGib({ fsType: 'file', name: 'README.md', data: { fileHash: bin1.binHash }, rel8ns: { [BINARY_REL8N_NAME]: [bin1.binAddr] }, }); const file1 = file1Res.newIbGib as B2tFSItemIbGib_V1; await metaspace.put({ ibGibs: [file1], space }); const file1Addr = getIbGibAddr({ ibGib: file1 }); // File 2: index.ts const file2Res = await createB2tFSItemIbGib({ fsType: 'file', name: 'index.ts', data: { fileHash: bin2.binHash }, rel8ns: { [BINARY_REL8N_NAME]: [bin2.binAddr] }, }); const file2 = file2Res.newIbGib as B2tFSItemIbGib_V1; await metaspace.put({ ibGibs: [file2], space }); const file2Addr = getIbGibAddr({ ibGib: file2 }); // Folder: src const srcFolderRes = await createB2tFSItemIbGib({ fsType: 'folder', name: 'src', data: { nameHash: 'srcHash' }, rel8ns: { [B2TFS_CHILD_ITEM_REL8N_NAME]: [file2Addr] }, }); const srcFolder = srcFolderRes.newIbGib as B2tFSItemIbGib_V1; await metaspace.put({ ibGibs: [srcFolder], space }); const srcFolderAddr = getIbGibAddr({ ibGib: srcFolder }); // Root Folder const rootFolderRes = await createB2tFSItemIbGib({ fsType: 'folder', name: 'root', data: { nameHash: 'rootHash' }, rel8ns: { [B2TFS_CHILD_ITEM_REL8N_NAME]: [file1Addr, srcFolderAddr] }, }); const rootFolder = rootFolderRes.newIbGib as B2tFSItemIbGib_V1; await metaspace.put({ ibGibs: [rootFolder], space }); return { rootFolder, file1Addr, file2Addr }; } await ifWe(lc, `creates a valid snapshot stone with zero hard links and full pre-computed path mapping`, async () => { const { rootFolder, file1Addr, file2Addr } = await createTestTree(); const branchIbGib = await createB2tFSBranchIbGib({ branchName: 'main', rootItem: rootFolder, space, metaspace, }); const res = await createBranchSnapshotIbGib({ branchIbGib, rootItem: rootFolder, cosmosInfo: { name: 'cosmos-alpha', addr: 'cosmos^alpha', tjpAddr: 'cosmos^tjp' }, galaxyInfo: { name: 'code-1', addr: 'galaxy^code1', tjpAddr: 'galaxy^tjp' }, primaryIdentityTjpAddr: parentTjp, deviceDelegate, space, metaspace, }); const { snapshotIbGib, snapshotAddr, evolvedAuthorDelegateKeystone } = res; // 1. Zero hard links in rel8ns iReckon(lc, snapshotIbGib.rel8ns).asTo('rel8ns defined').isGonnaBeTruthy(); iReckon(lc, Object.keys(snapshotIbGib.rel8ns).length).asTo('rel8ns zero keys').willEqual(0); // 2. Pre-computed items map const items = snapshotIbGib.data.items; iReckon(lc, items).asTo('items defined').isGonnaBeTruthy(); iReckon(lc, items['README.md']).asTo('README item').isGonnaBeTruthy(); iReckon(lc, items['README.md'][0]).asTo('README itemAddr').willEqual(file1Addr); iReckon(lc, items['src']).asTo('src folder').isGonnaBeTruthy(); iReckon(lc, items['src/index.ts']).asTo('src index item').isGonnaBeTruthy(); iReckon(lc, items['src/index.ts'][0]).asTo('src index itemAddr').willEqual(file2Addr); // 3. File & folder metrics iReckon(lc, snapshotIbGib.data.fileCount).asTo('fileCount').willEqual(2); iReckon(lc, snapshotIbGib.data.folderCount).asTo('folderCount').willEqual(2); // root (1) + src (1) // 4. Cosmology coordinates iReckon(lc, snapshotIbGib.data.branchName).asTo('branchName').willEqual('main'); iReckon(lc, snapshotIbGib.data.cosmosName).asTo('cosmosName').willEqual('cosmos-alpha'); iReckon(lc, snapshotIbGib.data.galaxyName).asTo('galaxyName').willEqual('code-1'); iReckon(lc, snapshotIbGib.data.primaryIdentityTjpAddr).asTo('primaryIdentityTjpAddr').willEqual(parentTjp); // 5. Author delegate keystone signed iReckon(lc, evolvedAuthorDelegateKeystone).asTo('evolved delegate').isGonnaBeTruthy(); const latestProof = evolvedAuthorDelegateKeystone.data?.proofs?.[0]; iReckon(lc, latestProof?.claim?.verb).asTo('proof verb').willEqual(KEYSTONE_VERB_AUTHOR); iReckon(lc, latestProof?.claim?.target).asTo('proof target').willEqual(snapshotAddr); // 6. Validation helper const validation = await validateSnapshotIbGib({ snapshotIbGib }); iReckon(lc, validation.isValid).asTo('snapshot valid').isGonnaBeTrue(); iReckon(lc, validation.errors.length).asTo('validation errors count').willEqual(0); }); await ifWe(lc, `fails fast if author delegate keystone or primaryIdentityTjpAddr is missing`, async () => { const { rootFolder } = await createTestTree(); const branchIbGib = await createB2tFSBranchIbGib({ branchName: 'main', rootItem: rootFolder, space, metaspace, }); let threwMissingIdentity = false; try { await createBranchSnapshotIbGib({ branchIbGib, rootItem: rootFolder, primaryIdentityTjpAddr: '' as any, deviceDelegate, space, metaspace, }); } catch (err: any) { threwMissingIdentity = true; iReckon(lc, err.message.includes('primaryIdentityTjpAddr is required')).asTo('error contains primaryIdentityTjpAddr').isGonnaBeTrue(); } iReckon(lc, threwMissingIdentity).asTo('threw on missing identity').isGonnaBeTrue(); let threwMissingDelegate = false; try { await createBranchSnapshotIbGib({ branchIbGib, rootItem: rootFolder, primaryIdentityTjpAddr: parentTjp, deviceDelegate: undefined as any, space, metaspace, }); } catch (err: any) { threwMissingDelegate = true; iReckon(lc, err.message.includes('deviceDelegate with delegateKeystone and delegateSecret is required')).asTo('error contains delegate requirement').isGonnaBeTrue(); } iReckon(lc, threwMissingDelegate).asTo('threw on missing delegate').isGonnaBeTrue(); }); await ifWe(lc, `tracks linear snapshot history on branch stone`, async () => { const { rootFolder } = await createTestTree(); let branchIbGib = await createB2tFSBranchIbGib({ branchName: 'dev', rootItem: rootFolder, space, metaspace, }); // Snapshot 1 const snap1 = await createBranchSnapshotIbGib({ branchIbGib, rootItem: rootFolder, primaryIdentityTjpAddr: parentTjp, deviceDelegate, space, metaspace, }); // Mutate branch stone to record Snapshot 1 branchIbGib.data['@snapshot'] = [snap1.snapshotAddr]; // Snapshot 2 const snap2 = await createBranchSnapshotIbGib({ branchIbGib, rootItem: rootFolder, primaryIdentityTjpAddr: parentTjp, deviceDelegate: { delegateKeystone: snap1.evolvedAuthorDelegateKeystone, delegateSecret, }, space, metaspace, }); branchIbGib.data['@snapshot'].push(snap2.snapshotAddr); // Verify retrieval helpers const latest = await getLatestBranchSnapshot({ branchIbGib, space }); iReckon(lc, latest).asTo('latest snapshot exists').isGonnaBeTruthy(); iReckon(lc, getIbGibAddr({ ibGib: latest! })).asTo('latest snapshot addr').willEqual(snap2.snapshotAddr); const history = await getBranchSnapshotHistory({ branchIbGib, space }); iReckon(lc, history.length).asTo('history count').willEqual(2); iReckon(lc, getIbGibAddr({ ibGib: history[0] })).asTo('history 0 addr').willEqual(snap1.snapshotAddr); iReckon(lc, getIbGibAddr({ ibGib: history[1] })).asTo('history 1 addr').willEqual(snap2.snapshotAddr); }); await ifWe(lc, `diffSnapshotTrees correctly computes genesis and incremental diffs`, async () => { const { rootFolder } = await createTestTree(); const branchIbGib = await createB2tFSBranchIbGib({ branchName: 'diff-test', rootItem: rootFolder, space, metaspace, }); // 1. Snapshot 1 (genesis) const snap1 = await createBranchSnapshotIbGib({ branchIbGib, rootItem: rootFolder, primaryIdentityTjpAddr: parentTjp, deviceDelegate, space, metaspace, }); const initialDiff = await diffSnapshotTrees({ latestSnapshot: snap1.snapshotIbGib, branchIbGib, space, }); iReckon(lc, initialDiff.manifest.latestSnapshotAddr).willEqual(snap1.snapshotAddr); iReckon(lc, initialDiff.manifest.previousSnapshotAddr).isGonnaBeFalsy(); iReckon(lc, initialDiff.changedFiles.length).willEqual(2); // README.md and src/index.ts iReckon(lc, initialDiff.changedFolders.length).willEqual(2); // root ('') and src iReckon(lc, initialDiff.changedFolders.some(f => f.path === '')).isGonnaBeTrue(); iReckon(lc, initialDiff.changedFolders.some(f => f.path === 'src')).isGonnaBeTrue(); // 2. Incremental diff with identical tree (no changes) const noChangeDiff = await diffSnapshotTrees({ latestSnapshot: snap1.snapshotIbGib, previousSnapshot: snap1.snapshotIbGib, branchIbGib, space, }); iReckon(lc, noChangeDiff.changedFiles.length).willEqual(0); iReckon(lc, noChangeDiff.changedFolders.length).willEqual(0); }); });