import { respecfully, ifWe, iReckon } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
import { InnerSpace_V1 } from './inner-space/inner-space-v1.mjs';
import { DEFAULT_INNER_SPACE_DATA_V1 } from './inner-space/inner-space-types.mjs';
import { lockSpace, unlockSpace } from './space-helper.mjs';
const maam = `[${import.meta.url}]`, sir = maam;
await respecfully(sir, 'space-helper', async () => {
    await respecfully(sir, 'lockSpace', async () => {
        await ifWe(sir, 'should fail if two locks are acquired simultaneously on the same scope (race condition check)', async () => {
            const space = new InnerSpace_V1(DEFAULT_INNER_SPACE_DATA_V1, undefined);
            // initialize the space first (often required for internal structures)
            await space.initialized;
            const scope = 'test-lock-scope';
            const secondsValid = 5;
            // Try to acquire lock simultaneously from two "instances"
            const p1 = lockSpace({ space, scope, secondsValid, instanceId: 'A' });
            const p2 = lockSpace({ space, scope, secondsValid, instanceId: 'B' });
            const [res1, res2] = await Promise.all([p1, p2]);
            // Expectation: Only one should succeed
            const success1 = res1.data?.success;
            const success2 = res2.data?.success;
            // If both verify, we have a problem (race condition)
            // We reckon that NOT both are true.
            iReckon(sir, success1 && success2).asTo('both locks acquired (mutual exclusion failed)').isGonnaBeFalse();
            // Clean up
            await unlockSpace({ space, scope, instanceId: 'A' });
            await unlockSpace({ space, scope, instanceId: 'B' });
        });
    });
});
//# sourceMappingURL=space-helper.respec.mjs.map