/** * @module sync-clone-readonly.respec * * Test Suite 2: Verification of SyncSagaCoordinator.cloneReadOnly engine. * Tests clean, one-way read-only pull of target DAGs using pure innerspaces and ReadOnlyMountedSpace. * * Invariant Assertions: * 1. Target DAG (roots, children, CAS items) is 100% materialized in Bob's local domain space. * 2. SyncSaga audit trail stones are written strictly to Bob's control space (both client and server). * 3. Alice's domain space receives ZERO write calls and has 0 byte/stone bloat. */ import { respecfully, ifWe, iReckon } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs'; import { clone, delay, extractErrorMsg, pretty } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.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 { 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 { 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 { createReadOnlyMountedSpace } from '../witness/space/readonly-space-wrapper.mjs'; import { fnObs } from '../common/pubsub/observer/observer-helper.mjs'; const logalot = GLOBAL_LOG_A_LOT; const lc = `[sync-clone-readonly.respec.mts]`; interface TestRepoData { type?: string; title?: string; commitMsg?: string; } await respecfully(lc, `SyncSagaCoordinator.cloneReadOnly`, async () => { await ifWe(lc, `cleanly clones target DAG into Bob's domain space with zero writes to Alice's space`, async () => { // 1. Setup Metaspace const 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) => { return null; }, }); while (!metaspace.initialized) { await delay(10); } // 2. Setup Alice's Spaces (Publisher) const aliceDomainSpace = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: `alice_domain_${Date.now()}`, uuid: `alice_domain_uuid_${Date.now()}`, }); await aliceDomainSpace.initialized; // Seed Alice's published repo: root + child commit const root = await createTimelineRootTestHelper({ ib: 'published_repo_root', data: { type: 'repo_root', title: 'Alice Published Repo' }, space: aliceDomainSpace, }); const rootAddr = getIbGibAddr({ ibGib: root }); const child = await mut8Timeline({ timeline: root, mut8Opts: { dataToAddOrPatch: { type: 'repo_child', commitMsg: 'Alice First Commit' }, }, metaspace, space: aliceDomainSpace, }); const childAddr = getIbGibAddr({ ibGib: child }); // Record Alice's exact initial stone count before Bob's clone const aliceInitialAddrs = Object.keys(aliceDomainSpace.ibGibs || {}); iReckon(lc, aliceInitialAddrs.length >= 2).isGonnaBeTrue(); // 3. Mount Alice's domain space as ReadOnlyMountedSpace const aliceReadOnlySpace = createReadOnlyMountedSpace(aliceDomainSpace); await aliceReadOnlySpace.initialized; // 4. Setup Bob's Spaces (Client & Server tenant control space) const bobClientControlSpace = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: `bob_client_control_${Date.now()}`, uuid: `bob_client_control_uuid_${Date.now()}`, }); await bobClientControlSpace.initialized; const bobClientDomainSpace = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: `bob_client_domain_${Date.now()}`, uuid: `bob_client_domain_uuid_${Date.now()}`, }); await bobClientDomainSpace.initialized; const bobServerControlSpace = new InnerSpace_V1({ ...DEFAULT_INNER_SPACE_DATA_V1, name: `bob_server_control_${Date.now()}`, uuid: `bob_server_control_uuid_${Date.now()}`, }); await bobServerControlSpace.initialized; // 5. Setup Coordinators & Innerspace Peer const bobCoordinator = new SyncSagaCoordinator(); const serverCoordinator = new SyncSagaCoordinator(); const peer = new SyncPeerInnerspace_V1(clone(SYNC_PEER_INNERSPACE_DEFAULT_DATA_V1)); await peer.initialized; await peer.initializeOpts({ localMetaspace: metaspace, controlSpace: bobClientControlSpace, domainSpace: bobClientDomainSpace, receiverControlSpace: bobServerControlSpace, // Server stores saga frames in Bob's space receiverDomainSpace: aliceReadOnlySpace, // Server serves Alice's space via read-only mount receiverCoordinator: serverCoordinator, receiverMetaspace: metaspace, }); // 6. Execute cloneReadOnly targeting Alice's repo const cloneRes = await bobCoordinator.cloneReadOnly({ peer, targetAddrs: [childAddr], controlSpace: bobClientControlSpace, domainSpace: bobClientDomainSpace, metaspace, }); const subscription = await cloneRes.updates$.subscribe(fnObs({ next: async () => {}, error: async (err) => { console.error(`${lc} updates$ error: ${extractErrorMsg(err)}`); }, complete: async () => {}, })); await cloneRes.done; await subscription.unsubscribe(); // 7. Invariant Verification 1: Bob's domain space received the full DAG const childInBob = await getFromSpace({ space: bobClientDomainSpace, addr: childAddr }); iReckon(lc, childInBob.success).isGonnaBeTrue(); iReckon(lc, (childInBob.ibGibs?.[0]?.data as any)?.commitMsg).willEqual('Alice First Commit'); const rootInBob = await getFromSpace({ space: bobClientDomainSpace, addr: rootAddr }); iReckon(lc, rootInBob.success).isGonnaBeTrue(); iReckon(lc, (rootInBob.ibGibs?.[0]?.data as any)?.title).willEqual('Alice Published Repo'); // 8. Invariant Verification 2: Bob's control spaces contain the SyncSaga audit logs const bobClientControlAddrs = Object.keys(bobClientControlSpace.ibGibs || {}); iReckon(lc, bobClientControlAddrs.length > 0).isGonnaBeTrue(); const bobServerControlAddrs = Object.keys(bobServerControlSpace.ibGibs || {}); iReckon(lc, bobServerControlAddrs.length > 0).isGonnaBeTrue(); // 9. Invariant Verification 3: Alice's source space is 100% unmodified (Zero writes, zero bloat) const aliceFinalAddrs = Object.keys(aliceDomainSpace.ibGibs || {}); iReckon(lc, aliceFinalAddrs.length).willEqual(aliceInitialAddrs.length); for (const addr of aliceFinalAddrs) { iReckon(lc, aliceInitialAddrs.includes(addr)).isGonnaBeTrue(); } }); });