/**
 * @module metaspace-innerspace.respec
 *
 * specs for the in-memory metaspace implementation.
 */
import { firstOfEach, ifWe, iReckon, respecfully } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs';
import { Metaspace_Innerspace } from './metaspace-innerspace.mjs';
import { createTimeline, mut8Timeline } from '../../../../timeline/timeline-api.mjs';
import { resetInnerSpaces } from './metaspace-innerspace-helper.mjs';
const maam = `[${import.meta.url}]`, sir = maam;
respecfully(sir, `[metaspace-innerspace]`, async () => {
    let metaspace;
    firstOfEach(sir, async () => {
        resetInnerSpaces();
        metaspace = new Metaspace_Innerspace(undefined); // no cache for now
        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; },
        });
    });
    await ifWe(sir, `can instantiate`, async () => {
        iReckon(sir, metaspace).asTo('metaspace').isGonnaBeTruthy();
        iReckon(sir, metaspace.initialized).asTo('initialized').isGonnaBeTrue();
    });
    await ifWe(sir, `has zeroSpace`, async () => {
        const zs = metaspace.zeroSpace;
        iReckon(sir, zs).asTo('zeroSpace').isGonnaBeTruthy();
    });
    await ifWe(sir, `can createTimeline and track latest`, async () => {
        // This implicitly tests getLatestAddr/s via timeline-api usage if we were using it,
        // but here we call it directly or via minimal timeline op.
        // 1. Create a timeline via timeline-api using this metaspace
        const resCreate = await createTimeline({
            space: metaspace.zeroSpace,
            metaspace: metaspace,
            data: { n: 0 },
            ib: 'timeline'
        });
        const t0 = resCreate.newIbGib;
        const t0Addr = getIbGibAddr({ ibGib: t0 });
        // 2. Verify we can get latest addr
        // Ideally metaspace should track this automatically if registerNewIbGib is called inside createTimeline,
        // which calls metaspace.registerNewIbGib.
        // Note: timeline-api calls metaspace.registerNewIbGib.
        // So we check if metaspace has it.
        const latestAddr = await metaspace.getLatestAddr({
            ibGib: t0,
            space: metaspace.zeroSpace,
        });
        iReckon(sir, latestAddr).asTo('latestAddr matches initial').isGonnaBe(t0Addr);
        // 3. Mutate
        const t1 = await mut8Timeline({
            timeline: t0,
            metaspace,
            space: metaspace.zeroSpace,
            mut8Opts: { dataToAddOrPatch: { n: 1 } }
        });
        const t1Addr = getIbGibAddr({ ibGib: t1 });
        // 4. Verify latest addr updated
        const latestAddr2 = await metaspace.getLatestAddr({
            ibGib: t0, // asking about t0's latest
            space: metaspace.zeroSpace,
        });
        iReckon(sir, latestAddr2).asTo('latestAddr2 matches mutated').isGonnaBe(t1Addr);
    });
});
//# sourceMappingURL=metaspace-innerspace.respec.mjs.map