import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { Factory_V1 as factory } from '@ibgib/ts-gib/dist/V1/factory.mjs'; import { ROOT } from '@ibgib/ts-gib/dist/V1/constants.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { KeystoneIbGib_V1 } from '@ibgib/core-gib/dist/keystone/keystone-types.mjs'; import { getGlobalMetaspace_waitIfNeeded } from "@ibgib/web-gib/dist/helpers.mjs"; import { mut8Timeline } from '@ibgib/core-gib/dist/timeline/timeline-api.mjs'; import { graphsAreEquivalent } from '@ibgib/core-gib/dist/common/other/graph-helper.mjs'; import { SpaceGibApiBridge } from '../api/space-gib-api-bridge.mjs'; import { debugState, devLog, lc, setupDomainAndDelegateIdentities, runSyncPass, verifyIdentitiesOnServer } from './common.mjs'; export function init4_4bSetupButton(): void { const btn = document.getElementById('btn-4-4b-setup') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', async () => { try { btn.disabled = true; devLog('4.4B Setup: Generating Domain Keystone (I) and Target timeline V0 -> V1 -> V2...'); const metaspace = await getGlobalMetaspace_waitIfNeeded(); const space = await metaspace.getLocalUserSpace({}); if (!space) { throw new Error("No default space."); } const apiBridge = new SpaceGibApiBridge(); // Create domain + delegate identities (DRY helper) await setupDomainAndDelegateIdentities({ masterSecret: 'test-sender-secret-phase4-4', syncSalt: 'senderidentitysyncsaltphase4-4', manageSalt: 'senderidentitymanagesaltphase4-4', metaspace, space, apiBridge, phaseText: '4.4B' }); // Create V0 const resV0 = await factory.firstGen({ parentIbGib: ROOT, ib: 'timeline_root_deep_4_4b', data: { type: 'root', label: 'Root4_4b', random: Math.random() }, dna: true, nCounter: true, tjp: { uuid: true, timestamp: true } }); const r1_v0 = resV0.newIbGib; await metaspace.persistTransformResult({ resTransform: resV0, space }); await metaspace.registerNewIbGib({ ibGib: r1_v0 }); debugState.targetC1 = r1_v0; // Create V1 const r1_v1 = await mut8Timeline({ timeline: r1_v0, mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V1_4_4b' } }, metaspace, space, }); debugState.targetC2 = r1_v1; // Create V2 const r1_v2 = await mut8Timeline({ timeline: r1_v1, mut8Opts: { dataToAddOrPatch: { type: 'comment', label: 'V2_4_4b' } }, metaspace, space, }); debugState.targetX = r1_v2; devLog(`4.4B Setup: ✓ Timeline V0 -> V1 -> V2 created locally. V2 Tip: ${getIbGibAddr({ ibGib: r1_v2 })}`); devLog('✓ 4.4B Setup Complete! Ready for 4.4B Sync.'); btn.textContent = '✓ 4.4B Setup Complete'; const syncBtn = document.getElementById('btn-4-4b-sync') as HTMLButtonElement | null; if (syncBtn) { syncBtn.disabled = false; } } catch (error) { devLog(`✗ 4.4B Setup FAILED: ${extractErrorMsg(error)}`); console.error(error); btn.disabled = false; } }); } async function performPhaseSync4_4b(): Promise { const lc_fn = `${lc}[performPhaseSync4_4b]`; const btn = document.getElementById('btn-4-4b-sync') as HTMLButtonElement | null; if (!btn) { return; } try { btn.disabled = true; devLog(`4.4B Sync: Initiating WebSocket Sync of V2 Tip...`); const targetV2 = debugState.targetX; if (!debugState.domainI || !targetV2) { devLog(`⚠ 4.4B Sync: Missing setup state. Please run Setup first.`); btn.disabled = false; return; } const metaspace = await getGlobalMetaspace_waitIfNeeded(); const space = await metaspace.getLocalUserSpace({}); if (!space) { throw new Error("No default space."); } await runSyncPass({ domainIbGibs: [targetV2], passLabel: 'Pass 1 (Sync V2 tip)', metaspace, space }); devLog(`4.4B Sync: ✓ WebSocket Sync completed.`); devLog(`✓ 4.4B Sync Sequence Complete! Ready for State Checks.`); btn.textContent = `✓ 4.4B Sync Run`; const checkBtn = document.getElementById('btn-4-4b-check') as HTMLButtonElement | null; if (checkBtn) { checkBtn.disabled = false; } } catch (error) { devLog(`✗ 4.4B Sync FAILED: ${extractErrorMsg(error)}`); console.error(error); btn.disabled = false; } } export function init4_4bSyncButton(): void { const btn = document.getElementById('btn-4-4b-sync') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', () => { performPhaseSync4_4b(); }); } export function init4_4bCheckButton(): void { const lc_fn = `${lc}[init4_4bCheckButton]`; const btn = document.getElementById('btn-4-4b-check') as HTMLButtonElement | null; if (!btn) { return; } btn.addEventListener('click', async () => { try { devLog('4.4B Check: Asserting cryptographic and WebSocket state expectations...'); const metaspace = await getGlobalMetaspace_waitIfNeeded(); const space = await metaspace.getLocalUserSpace({}); if (!space) { throw new Error("No default space."); } const domainI = debugState.domainI!; const targetV0 = debugState.targetC1!; const targetV1 = debugState.targetC2!; const targetV2 = debugState.targetX!; const v0Addr = getIbGibAddr({ ibGib: targetV0 }); const v1Addr = getIbGibAddr({ ibGib: targetV1 }); const v2Addr = getIbGibAddr({ ibGib: targetV2 }); const domainAddr = getIbGibAddr({ ibGib: domainI }); const apiBridge = new SpaceGibApiBridge(); // 1. Verify V0, V1, V2 exist locally const localV0Res = await metaspace.get({ addrs: [v0Addr], space }); const localV1Res = await metaspace.get({ addrs: [v1Addr], space }); const localV2Res = await metaspace.get({ addrs: [v2Addr], space }); if (!localV0Res.success || !localV0Res.ibGibs?.[0]) { devLog('✗ Check: Timeline frame V0 NOT found locally.'); return; } if (!localV1Res.success || !localV1Res.ibGibs?.[0]) { devLog('✗ Check: Timeline frame V1 NOT found locally.'); return; } if (!localV2Res.success || !localV2Res.ibGibs?.[0]) { devLog('✗ Check: Timeline frame V2 NOT found locally.'); return; } devLog(`✓ Check: Timeline frames V0, V1, and V2 exist locally.`); // 2. Verify V2, V1 exist on server with correct past refs, and V2 graph matches const serverV2Res = await apiBridge.getIbGib(domainAddr, v2Addr); if (!serverV2Res.success || !serverV2Res.ibGib) { devLog(`✗ Check: Timeline frame V2 NOT found on server: ${serverV2Res.message}`); return; } devLog(`✓ Check: Timeline frame V2 exists on server.`); const pastAddrV2 = serverV2Res.ibGib.rel8ns?.past?.at(-1); if (pastAddrV2 === v1Addr) { devLog('✓ Check: Server V2 correctly references V1 in its past relation.'); } else { devLog(`✗ Check: Server V2 does not reference V1 in past relation! got: ${pastAddrV2}`); return; } const serverV1Res = await apiBridge.getIbGib(domainAddr, v1Addr); if (!serverV1Res.success || !serverV1Res.ibGib) { devLog(`✗ Check: Timeline frame V1 NOT found on server: ${serverV1Res.message}`); return; } devLog(`✓ Check: Timeline frame V1 exists on server.`); const pastAddrV1 = serverV1Res.ibGib.rel8ns?.past?.at(-1); if (pastAddrV1 === v0Addr) { devLog('✓ Check: Server V1 correctly references V0 in its past relation.'); } else { devLog(`✗ Check: Server V1 does not reference V0 in past relation! got: ${pastAddrV1}`); return; } const localV2Graph = await metaspace.getDependencyGraph({ ibGibAddr: v2Addr, space }); const serverV2GetGraphRes = await apiBridge.getIbGibGraph(domainAddr, v2Addr, true); if (!serverV2GetGraphRes.success || !serverV2GetGraphRes.graph) { devLog(`✗ Check: Failed to fetch V2 graph from server: ${serverV2GetGraphRes.message}`); return; } if (!graphsAreEquivalent({ graphA: localV2Graph, graphB: serverV2GetGraphRes.graph })) { devLog('✗ Check: V2 timeline dependency graphs mismatch between client and server!'); return; } devLog('✓ Check: V2 timeline dependency graphs are equivalent on client and server.'); // 3. Verify domain keystone n=1, then verify domain/delegate/session on server const latestLocalIAddr = await metaspace.getLatestAddr({ addr: domainAddr, space }); if (!latestLocalIAddr) { devLog('✗ Check: Domain keystone tip not found locally!'); return; } const localIRes = await metaspace.get({ addrs: [latestLocalIAddr], space }); const localI = localIRes.ibGibs?.[0] as KeystoneIbGib_V1 | undefined; if (!localI) { devLog('✗ Check: Failed to load domain keystone!'); return; } devLog(`✓ Check: Evolved Domain Keystone exists locally: ${latestLocalIAddr}`); const nI = localI.data?.n; devLog(`✓ Check: Evolved I tip is at n = ${nI}.`); if (nI !== 1) { devLog(`✗ Check: Expected I to evolve to n = 1, got n = ${nI}.`); return; } const { sessionIdentityTjpAddr } = await verifyIdentitiesOnServer({ localI, metaspace, space, apiBridge, domainAddr }); // 4. Verify session keystone S n=3 and graph matches const latestLocalSAddr = await metaspace.getLatestAddr({ addr: sessionIdentityTjpAddr, space }); if (!latestLocalSAddr) { devLog('✗ Check: Latest Session Keystone (S) NOT found in local space.'); return; } devLog(`✓ Check: Found latest session keystone (S) locally: ${latestLocalSAddr}`); const localSRes = await metaspace.get({ addrs: [latestLocalSAddr], space }); const sessionS = localSRes.ibGibs?.[0] as KeystoneIbGib_V1 | undefined; if (!sessionS) { devLog('✗ Check: Failed to load Session Keystone (S) locally.'); return; } const nS = sessionS.data?.n; devLog(`✓ Check: Latest session S tip evolved to n = ${nS}.`); if (nS !== 3) { devLog(`✗ Check: Expected latest session S to evolve to n = 3, got n = ${nS}.`); return; } const serverSGetRes = await apiBridge.getIbGib(domainAddr, latestLocalSAddr); if (!serverSGetRes.success || !serverSGetRes.ibGib) { devLog(`✗ Check: Latest Session Keystone (S) NOT found on server!`); return; } devLog(`✓ Check: Latest Session Keystone (S) exists on server.`); const localSGraph = await metaspace.getDependencyGraph({ ibGibAddr: latestLocalSAddr, space }); const serverSGetGraphRes = await apiBridge.getIbGibGraph(domainAddr, sessionIdentityTjpAddr, true); if (!serverSGetGraphRes.success || !serverSGetGraphRes.graph) { devLog(`✗ Check: Failed to fetch session S graph from server: ${serverSGetGraphRes.message}`); return; } if (!graphsAreEquivalent({ graphA: localSGraph, graphB: serverSGetGraphRes.graph })) { devLog('✗ Check: Session Keystone (S) dependency graphs mismatch between client and server!'); return; } devLog('✓ Check: Session Keystone (S) dependency graphs are equivalent on client and server.'); devLog('🎉 ALL PHASE 4.4B TRANSACTION SYNC CHECKS PASSED FLAWLESSLY! ✓'); btn.textContent = '✓ 4.4B All Passed'; } catch (error) { devLog(`✗ 4.4B Check FAILED: ${extractErrorMsg(error)}`); console.error(`${lc_fn} 4.4B Check error:`, error); } }); }