/** * @module sync-innerspace.withid.respec * * Verifies SyncSagaCoordinator using InnerSpace (in-memory/local) spaces with Identity. * This utilizes SyncPeerInnerspace_V1 for local-to-local synchronization simulations. */ import { respecfully, lastOfAll, ifWe, ifWeMight, iReckon } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; const maam = `[${import.meta.url}]`, sir = maam; import { clone, delay, extractErrorMsg, pretty } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { IbGib_V1 } from '@ibgib/ts-gib/dist/V1/types.mjs'; import { GLOBAL_LOG_A_LOT } from '../core-constants.mjs'; import { SyncSagaCoordinator } from './sync-saga-coordinator.mjs'; import { getFromSpace } from '../witness/space/space-helper.mjs'; import { Metaspace_Innerspace } from '../witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs'; import { getDependencyGraph } from '../common/other/graph-helper.mjs'; import { InnerSpace_V1 } from '../witness/space/inner-space/inner-space-v1.mjs'; import { createTimelineRootTestHelper } from '../test-helpers.mjs'; import { mut8Timeline } from '../timeline/timeline-api.mjs'; import { DEFAULT_INNER_SPACE_DATA_V1 } from '../witness/space/inner-space/inner-space-types.mjs'; import { toDto } from '../common/other/ibgib-helper.mjs'; import { fnObs } from '../common/pubsub/observer/observer-helper.mjs'; import { SyncPeerInnerspace_V1 } from './sync-peer/sync-peer-innerspace/sync-peer-innerspace-v1.mjs'; import { SYNC_PEER_INNERSPACE_DEFAULT_DATA_V1 } from './sync-peer/sync-peer-innerspace/sync-peer-innerspace-constants.mjs'; import { KeystoneService_V1 } from '../keystone/keystone-service-v1.mjs'; import { KEYSTONE_VERB_SYNC, POOL_ID_SYNC } from '../keystone/keystone-constants.mjs'; import { createStandardPoolConfig } from '../keystone/keystone-config-builder.mjs'; import { KeystoneReplenishStrategy } from '../keystone/keystone-types.mjs'; import { SyncConflictStrategy } from './sync-constants.mjs'; import { SYNC_SPACE_MODES, createSyncTestSpaces, verifySyncControlAndDomainSpaces } from './sync-respec-helper.mjs'; const logalot = GLOBAL_LOG_A_LOT; const lc = `[sync-innerspace.withid.respec]`; const SENDER_SECRET = 'test-sender-secret-innerspace-withid'; const SENDER_IDENTITY_SYNC_POOL_CONFIG = createStandardPoolConfig({ id: POOL_ID_SYNC, salt: 'senderidentitysyncsaltinnerspacewithid', verbs: [KEYSTONE_VERB_SYNC], size: 200, sequential: 1, random: 1, targetBinding: 2, replenishStrategy: KeystoneReplenishStrategy.topUp, }); await respecfully(sir, `Sync InnerSpaces with Identity`, async () => { for (const spaceMode of SYNC_SPACE_MODES) { // #region Round 1 - Push timeline await respecfully(sir, `Basic Push Sync with Identity (Source -> Dest) [${spaceMode}]`, async () => { // 1. Setup Spaces 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 }); if (!defaultLocalUserSpace) { throw new Error(`(UNEXPECTED) defaultLocalUserSpace falsy? (E: 488b7888915e9bcf73095f8c05eb9d6c)`); } await defaultLocalUserSpace.initialized; if (logalot) { console.log(`defaultLocalUserSpace: ${pretty(toDto({ ibGib: defaultLocalUserSpace }))}`); } const { sourceControlSpace, sourceDomainSpace, destControlSpace, destDomainSpace, } = await createSyncTestSpaces({ spaceMode, prefix: 'innerspace_withid' }); const keystoneSvc = new KeystoneService_V1(); const senderIdentity = await keystoneSvc.genesis({ masterSecret: SENDER_SECRET, configs: [SENDER_IDENTITY_SYNC_POOL_CONFIG], metaspace, space: sourceControlSpace, }); // post the senderIdentity to receiverControlSpace await metaspace.put({ ibGib: senderIdentity, space: destControlSpace }); await metaspace.registerNewIbGib({ ibGib: senderIdentity, space: destControlSpace }); // #region r1 setup // 2. Seed Source Data in sourceDomainSpace if (logalot) { console.log(`${lc} Creating data in Source...`); } const r1_alpha_v0_source = await createTimelineRootTestHelper({ ib: 'timeline_root', data: { type: 'root', }, space: sourceDomainSpace, }); const r1_alpha_v1_source = await mut8Timeline({ timeline: r1_alpha_v0_source, mut8Opts: { dataToAddOrPatch: { type: 'child', }, }, metaspace, space: sourceDomainSpace, }); const r1_alpha_v1_source_addr = getIbGibAddr({ ibGib: r1_alpha_v1_source }); // #endregion r1 setup // 3. Setup Coordinators & Peer if (logalot) { console.log(`${lc} Setting up Coordinators...`); } const senderCoordinator = new SyncSagaCoordinator(); const receiverCoordinator = new SyncSagaCoordinator(); const peer = new SyncPeerInnerspace_V1(clone(SYNC_PEER_INNERSPACE_DEFAULT_DATA_V1)); await peer.initialized; await peer.initializeOpts({ sagaId: '', localMetaspace: metaspace, controlSpace: sourceControlSpace, domainSpace: sourceDomainSpace, receiverControlSpace: destControlSpace, receiverDomainSpace: destDomainSpace, receiverCoordinator, receiverMetaspace: metaspace, }); // 4. Run Sync if (logalot) { console.log(`${lc} Running Sync...`); } const resSync = await senderCoordinator.sync({ peer: peer, controlSpace: sourceControlSpace, domainSpace: sourceDomainSpace, metaspace: metaspace, domainIbGibs: [r1_alpha_v1_source], senderIdentityInfo: { senderIdentity, senderDomainAddr: getIbGibAddr({ ibGib: senderIdentity }), }, fnSenderSecret: async () => SENDER_SECRET, conflictStrategy: SyncConflictStrategy.optimisticWithLCS, }); if (logalot) { console.log(`${lc} resSync: ${pretty(resSync)}`); } const sublc = `${lc}[updates$]`; const subscription = await resSync.updates$.subscribe(fnObs({ next: async (ctxIbGib) => { if (logalot) { console.log(`${sublc} next fired`); } }, error: async (e) => { if (logalot) { console.log(`${sublc} error fired`); } }, complete: async () => { if (logalot) { console.log(`${sublc} complete fired`); } }, })); if (logalot) { console.log(`${lc} awaiting resSync.done`) } await resSync.done; // #region r1 verify post // 5. Verify Dest if (logalot) { console.log(`${lc} Verifying Destination...`); } try { const getChildInDest = await getFromSpace({ space: destDomainSpace, addr: r1_alpha_v1_source_addr }); await ifWe(sir, `verify success getChildInDest`, async () => { iReckon(sir, getChildInDest.success).asTo('Child present in Dest').isGonnaBeTrue(); }); await ifWe(sir, `verify getChildInDest.ibGibs`, async () => { const firstChild = getChildInDest.ibGibs?.[0]; if (logalot) { console.log(`${lc} firstChild: ${pretty(firstChild)}`); } iReckon(sir, firstChild?.data?.n).asTo('Child content matches').isGonnaBe(2); }); await ifWe(sir, 'sessionIdentity evolved the expected number of times', async () => { const latestS = peer.currentSessionIdentity; iReckon(sir, latestS).asTo('latest session identity exists on peer').isGonnaBeTruthy(); if (latestS) { iReckon(sir, latestS.data?.n).asTo('session identity data.n').isGonnaBe(2); } }); await ifWe(sir, 'sender controlSpace has I (evolved frame)', async () => { const latestSenderIdentityAddr = await metaspace.getLatestAddr({ addr: getIbGibAddr({ ibGib: senderIdentity }), space: sourceControlSpace, }); iReckon(sir, latestSenderIdentityAddr).asTo('latestSenderIdentityAddr exists').isGonnaBeTruthy(); if (latestSenderIdentityAddr) { const resGetSourceI = await getFromSpace({ space: sourceControlSpace, addr: latestSenderIdentityAddr }); iReckon(sir, resGetSourceI.success && resGetSourceI.ibGibs?.length === 1).asTo('evolved I exists in sourceControlSpace').isGonnaBeTrue(); if (resGetSourceI.success && resGetSourceI.ibGibs?.[0]) { const evolvedI = resGetSourceI.ibGibs[0]; iReckon(sir, evolvedI.data?.n).asTo('evolved I data.n').isGonnaBe(1); } } }); await ifWe(sir, 'receiver controlSpace has I (evolved frame)', async () => { const latestSenderIdentityAddr = await metaspace.getLatestAddr({ addr: getIbGibAddr({ ibGib: senderIdentity }), space: sourceControlSpace, }); iReckon(sir, latestSenderIdentityAddr).asTo('latestSenderIdentityAddr exists').isGonnaBeTruthy(); if (latestSenderIdentityAddr) { const resGetDestI = await getFromSpace({ space: destControlSpace, addr: latestSenderIdentityAddr }); iReckon(sir, resGetDestI.success && resGetDestI.ibGibs?.length === 1).asTo('evolved I exists in destControlSpace').isGonnaBeTrue(); if (resGetDestI.success && resGetDestI.ibGibs?.[0]) { const evolvedI = resGetDestI.ibGibs[0]; iReckon(sir, evolvedI.data?.n).asTo('evolved I data.n').isGonnaBe(1); } } }); await ifWe(sir, 'sender controlSpace has full S dep graph', async () => { const latestS = peer.currentSessionIdentity; iReckon(sir, latestS).asTo('latestS exists').isGonnaBeTruthy(); if (latestS) { const depGraphS = await getDependencyGraph({ ibGib: latestS, space: sourceControlSpace }); const addrs = Object.keys(depGraphS); iReckon(sir, addrs.length > 2).asTo('number of nodes in S graph > 2').isGonnaBeTrue(); } }); await ifWe(sir, 'receiver controlSpace has full S dep graph', async () => { const latestS = peer.currentSessionIdentity; iReckon(sir, latestS).asTo('latestS exists').isGonnaBeTruthy(); if (latestS) { const depGraphS = await getDependencyGraph({ ibGib: latestS, space: destControlSpace }); const addrs = Object.keys(depGraphS); iReckon(sir, addrs.length > 2).asTo('number of nodes in S graph in destControlSpace > 2').isGonnaBeTrue(); } }); await verifySyncControlAndDomainSpaces({ sir, spaceMode, senderCoordinator, sourceControlSpace, sourceDomainSpace, destControlSpace, destDomainSpace, domainIbGibs: [r1_alpha_v1_source], }); } catch (error) { await ifWe(sir, `doh`, async () => { iReckon(sir, true).asTo(`error: ${extractErrorMsg(error)}`).isGonnaBeFalse(); }); } finally { if (logalot) { console.log(`${lc} Test complete.`); } } // #endregion r1 verify post }); // #endregion Round 1 - Push timeline } });