/**
 * @module sync-innerspace-partial-update.respec
 *
 * Verifies Sync Scenarios where the Destination has partial history.
 */
import { respecfully, ifWe, iReckon } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
const maam = `[${import.meta.url}]`, sir = maam;
import { clone, delay } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
import { SyncSagaCoordinator } from './sync-saga-coordinator.mjs';
import { putInSpace, 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';
const logalot = true;
const lc = `[sync-innerspace-partial-update.respec]`;
await respecfully(sir, `Sync InnerSpaces (Partial Update)`, async () => {
    let metaspace;
    let sourceSpace;
    let destSpace;
    await respecfully(sir, `Sender Newer (Partial History)`, async () => {
        // 1. Setup Spaces
        metaspace = new Metaspace_Innerspace(undefined);
        await metaspace.initialize({
            getFnAlert: () => async ({ title, msg }) => { },
            getFnPrompt: () => async ({ title, msg }) => { return ''; },
            getFnPromptPassword: () => async (title, msg) => { return null; },
        });
        while (!metaspace.initialized) {
            await delay(10);
        }
        const defaultLocalUserSpace = await metaspace.getLocalUserSpace({ lock: false });
        await defaultLocalUserSpace.initialized;
        sourceSpace = new InnerSpace_V1({
            ...DEFAULT_INNER_SPACE_DATA_V1,
            name: 'source',
            uuid: 'source_uuid',
            description: 'source test space',
        });
        await sourceSpace.initialized;
        destSpace = new InnerSpace_V1({
            ...DEFAULT_INNER_SPACE_DATA_V1,
            name: 'dest',
            uuid: 'dest_uuid',
            description: 'dest test space',
        });
        await destSpace.initialized;
        // 2. See Data
        // Root -> V1 (Shared) -> V2 (Source Only)
        const root = await createTimelineRootTestHelper({
            ib: 'timeline_root_partial',
            data: { type: 'root', label: 'Root' },
            space: sourceSpace,
        });
        // V1
        const v1 = await mut8Timeline({
            timeline: root,
            mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V1' } },
            metaspace,
            space: sourceSpace,
        });
        // V2 (Source Only)
        const v2 = await mut8Timeline({
            timeline: v1,
            mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V2' } },
            metaspace,
            space: sourceSpace,
        });
        const addrV2 = getIbGibAddr({ ibGib: v2 });
        // Transfer Root & V1 to Dest (Simulate previous sync)
        await putInSpace({ space: destSpace, ibGibs: [root, v1] });
        await ifWe(sir, 'verify setup', async () => {
            // Verify Dest has V1
            const checkV1 = await getFromSpace({ space: destSpace, addr: getIbGibAddr({ ibGib: v1 }) });
            iReckon(sir, checkV1.success).asTo('Dest has V1').isGonnaBeTrue();
            // Verify Dest does NOT have V2
            const checkV2 = await getFromSpace({ space: destSpace, addr: addrV2 });
            iReckon(sir, checkV2.success && !!checkV2.ibGibs?.length).asTo('Dest has V2').isGonnaBeFalse();
        });
        // 3. Setup Sync
        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({
            localMetaspace: metaspace,
            localSpace: sourceSpace, // "Client"
            receiverSpace: destSpace, // "Server"
            receiverCoordinator,
            receiverMetaspace: metaspace,
        });
        // 4. Run Sync (Source Pushes V2)
        console.log(`${lc} Running Sync...`);
        const resSync = await senderCoordinator.sync({
            peer: peer,
            localSpace: sourceSpace,
            metaspace: metaspace,
            domainIbGibs: [v2],
        });
        await resSync.done;
        // 5. Verify Dest (Should now have V2)
        console.log(`${lc} Verifying Destination...`);
        await ifWe(sir, `verify dest updated`, async () => {
            // Verify Tip (V2)
            const getV2 = await getFromSpace({ space: destSpace, addr: addrV2 });
            iReckon(sir, getV2.success).asTo('V2 present in Dest').isGonnaBeTrue();
            // Verify V2 points to V1
            const v2IbGib = getV2.ibGibs[0];
            const pastAddr = v2IbGib.rel8ns?.past?.at(-1);
            iReckon(sir, pastAddr === getIbGibAddr({ ibGib: v1 })).asTo('V2 points to V1').isGonnaBeTrue();
        });
    });
});
//# sourceMappingURL=sync-innerspace-partial-update.respec.mjs.map