import { respecfully, iReckon, ifWe, ifWeMight } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; import { KeystoneService_V1 } from './keystone-service-v1.mjs'; import { createStandardPoolConfig } from './keystone-config-builder.mjs'; import { POOL_ID_DEFAULT, KEYSTONE_CHECKPOINT_FREQUENCY } from './keystone-constants.mjs'; import { KeystoneData_V1, KeystoneIbGib_V1, KeystoneClaim } from './keystone-types.mjs'; const sir = `[${import.meta.url}]`; class MockMetaspace { public storage = new Map(); async get(args: { addr: string }) { const ibGib = this.storage.get(args.addr); return { success: !!ibGib, ibGibs: ibGib ? [ibGib] : [] }; } async put(args: { ibGib: any }) { this.storage.set(`${args.ibGib.ib}^${args.ibGib.gib}`, args.ibGib); } async registerNewIbGib(args: { ibGib: any }) { await this.put(args); } async getLocalUserSpace() { return new MockSpace(); } } class MockSpace { async put() { } async witness() { return { data: { success: true } }; } } await respecfully(sir, 'Keystone State Aggregation', async () => { const service = new KeystoneService_V1(); const metaspace = new MockMetaspace() as any; const space = new MockSpace() as any; const masterSecret = "AliceSecret"; const config = createStandardPoolConfig({ id: POOL_ID_DEFAULT, salt: POOL_ID_DEFAULT, size: 1000 }); await ifWe(sir, 'aggregates frameDetails correctly across multiple evolutions', async () => { // 1. Genesis with some details const genesis = await service.genesis({ masterSecret, configs: [config], metaspace, space, frameDetails: { profile: { name: 'Alice' } } }); // 2. Evolve with more details const claim: Partial = { target: "comment 1^gib", verb: "post" }; const frame2 = await service.sign({ latestKeystone: genesis, masterSecret, claim, metaspace, space, frameDetails: { profile: { email: 'alice@example.com' }, lastLogin: '2026-05-01' } }); // 3. Evolve again (no new details) const frame3 = await service.sign({ latestKeystone: frame2, masterSecret, claim: { ...claim, target: "comment 2^gib" }, metaspace, space }); // 4. Verify aggregation const aggregated = await service.getAggregateDetails({ latestKeystone: frame3, metaspace, space }); iReckon(sir, aggregated.profile.name).willEqual('Alice'); iReckon(sir, aggregated.profile.email).willEqual('alice@example.com'); iReckon(sir, aggregated.lastLogin).willEqual('2026-05-01'); }); await ifWe(sir, 'generates a checkpoint and uses it for aggregation', async () => { // We will force a checkpoint by evolving KEYSTONE_CHECKPOINT_FREQUENCY times. // For testing, we might want to lower the frequency, but let's stick to the constant // and just do a loop if it's not too large. 50 is fine for a test. let current = await service.genesis({ masterSecret, configs: [config], metaspace, space, frameDetails: { initial: true } }); const totalEvolutions = KEYSTONE_CHECKPOINT_FREQUENCY; console.log(`[test] Performing ${totalEvolutions} evolutions to trigger checkpoint...`); for (let i = 1; i <= totalEvolutions; i++) { current = await service.sign({ latestKeystone: current, masterSecret, claim: { target: `item_${i}^gib`, verb: "update" }, metaspace, space, frameDetails: { [`step_${i}`]: true } }); } // The latest frame should now have a checkpointDetails iReckon(sir, current.data.checkpointDetails).asTo('checkpoint exists').isGonnaBeTruthy(); iReckon(sir, current.data.n).willEqual(totalEvolutions); // Aggregation should work and be fast (it stops at the checkpoint) // We can verify it has all the steps const aggregated = await service.getAggregateDetails({ latestKeystone: current, metaspace, space }); iReckon(sir, aggregated.initial).isGonnaBeTrue(); iReckon(sir, aggregated[`step_${totalEvolutions}`]).isGonnaBeTrue(); iReckon(sir, aggregated[`step_1`]).isGonnaBeTrue(); // Verify that it actually used the checkpoint (white-box-ish) // If we clear the previous frames from storage, getAggregateDetails should still work // if it only goes back to the checkpoint. // Wait, current IS the checkpoint. So it should stop immediately. metaspace.storage.clear(); // Re-put ONLY the latest (which has the checkpoint) await metaspace.put({ ibGib: current }); const aggregatedFromCheckpoint = await service.getAggregateDetails({ latestKeystone: current, metaspace, space }); iReckon(sir, aggregatedFromCheckpoint.initial).isGonnaBeTrue(); }); await ifWe(sir, 'aggregates dictionary entries and respects null tombstone deletions across evolutions', async () => { // Downstream use case: passkey and credential tracking where metadata is registered monotonically // in frameDetails and tombstoned with null when revoked. // 1. Genesis with initial map entry (e.g. entry1) let current = await service.genesis({ masterSecret, configs: [config], metaspace, space, frameDetails: { passkeys: { c1: { credentialId: 'c1', poolId: 'pool_c1', name: 'Key 1', salt: 'salt1', createdUTC: '2026-08-20T00:00:00.000Z' } } } }); // 2. Evolve adding second entry (e.g. entry2) const claim: Partial = { target: "comment 1^gib", verb: "post" }; current = await service.sign({ latestKeystone: current, masterSecret, claim, metaspace, space, frameDetails: { passkeys: { c2: { credentialId: 'c2', poolId: 'pool_c2', name: 'Key 2', salt: 'salt2', createdUTC: '2026-08-20T00:00:00.000Z' } } } }); // Aggregated details should contain both c1 and c2 let aggregated = await service.getAggregateDetails({ latestKeystone: current, metaspace, space }); iReckon(sir, aggregated.passkeys).asTo('passkeys map exists').isGonnaBeTruthy(); iReckon(sir, aggregated.passkeys.c1.name).willEqual('Key 1'); iReckon(sir, aggregated.passkeys.c2.name).willEqual('Key 2'); iReckon(sir, Object.keys(aggregated.passkeys).length).willEqual(2); // 3. Evolve tombstoning c1 by setting it to null current = await service.sign({ latestKeystone: current, masterSecret, claim: { ...claim, target: "comment 2^gib" }, metaspace, space, frameDetails: { passkeys: { c1: null } } }); // Aggregated details should have c1 pruned and only c2 present aggregated = await service.getAggregateDetails({ latestKeystone: current, metaspace, space }); iReckon(sir, aggregated.passkeys.c1).asTo('c1 is pruned').isGonnaBeFalsy(); iReckon(sir, aggregated.passkeys.c2.name).willEqual('Key 2'); iReckon(sir, Object.keys(aggregated.passkeys).length).willEqual(1); // 4. Evolve adding c3 while c1 remains tombstoned current = await service.sign({ latestKeystone: current, masterSecret, claim: { ...claim, target: "comment 3^gib" }, metaspace, space, frameDetails: { passkeys: { c3: { credentialId: 'c3', poolId: 'pool_c3', name: 'Key 3', salt: 'salt3', createdUTC: '2026-08-20T00:00:00.000Z' } } } }); aggregated = await service.getAggregateDetails({ latestKeystone: current, metaspace, space }); iReckon(sir, aggregated.passkeys.c1).isGonnaBeFalsy(); iReckon(sir, aggregated.passkeys.c2.name).willEqual('Key 2'); iReckon(sir, aggregated.passkeys.c3.name).willEqual('Key 3'); iReckon(sir, Object.keys(aggregated.passkeys).length).willEqual(2); }); await ifWe(sir, 'preserves null tombstone deletions across checkpoint boundaries', async () => { // 1. Genesis with initial key let current = await service.genesis({ masterSecret, configs: [config], metaspace, space, frameDetails: { passkeys: { c1: { credentialId: 'c1', poolId: 'pool_c1', name: 'Key 1', salt: 'salt1', createdUTC: '2026-08-20T00:00:00.000Z' } } } }); // 2. Evolve frame 1 tombstoning c1 current = await service.sign({ latestKeystone: current, masterSecret, claim: { target: "item 1^gib", verb: "post" }, metaspace, space, frameDetails: { passkeys: { c1: null, c2: { credentialId: 'c2', poolId: 'pool_c2', name: 'Key 2', salt: 'salt2', createdUTC: '2026-08-20T00:00:00.000Z' } } } }); // 3. Evolve until checkpoint frequency is reached const totalEvolutions = KEYSTONE_CHECKPOINT_FREQUENCY; for (let i = 2; i <= totalEvolutions; i++) { current = await service.sign({ latestKeystone: current, masterSecret, claim: { target: `item_${i}^gib`, verb: "post" }, metaspace, space, frameDetails: { [`step_${i}`]: true } }); } iReckon(sir, current.data.checkpointDetails).asTo('checkpoint created').isGonnaBeTruthy(); // 4. Retrieve aggregated details directly from checkpoint const aggregated = await service.getAggregateDetails({ latestKeystone: current, metaspace, space }); iReckon(sir, aggregated.passkeys.c1).asTo('c1 remains pruned in checkpoint').isGonnaBeFalsy(); iReckon(sir, aggregated.passkeys.c2.name).willEqual('Key 2'); iReckon(sir, Object.keys(aggregated.passkeys).length).willEqual(1); }); });