From 2caa82b10af726edeb6f85ce8cf97dfd72cd82e4 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 2 Aug 2026 06:30:40 +0000 Subject: [PATCH] fix(ops): logout always clears persisted outbox and mirror Cold reload left IndexedDB/OPFS intact when executor/store singletons were null. Wipe adapters directly, clear both mirror backends, and always revoke the session even if storage wipe fails. --- .../campaign/opsSync/opsDomainOutbox.ts | 21 +++++---- .../campaign/opsSync/opsEstimateOutbox.ts | 21 +++++---- .../campaign/opsSync/opsMirrorClient.ts | 8 ++-- .../campaign/opsSync/opsMirrorPersistence.ts | 8 ++++ .../campaign/opsSync/opsMunicipalityOutbox.ts | 21 +++++---- .../campaign/shell/CampaignSidebar.tsx | 11 +++-- tests/unit/opsLogoutWipe.unit.spec.ts | 44 +++++++++++++++++++ 7 files changed, 102 insertions(+), 32 deletions(-) create mode 100644 tests/unit/opsLogoutWipe.unit.spec.ts diff --git a/src/components/campaign/opsSync/opsDomainOutbox.ts b/src/components/campaign/opsSync/opsDomainOutbox.ts index 3fd16252..1c3a5ec6 100644 --- a/src/components/campaign/opsSync/opsDomainOutbox.ts +++ b/src/components/campaign/opsSync/opsDomainOutbox.ts @@ -130,6 +130,9 @@ const handleUpdatedAtConflict = ( throw new NonRetriableError(message) } +const DOMAIN_OUTBOX_DB = 'teqo-ops-domain' +const DOMAIN_OUTBOX_STORE = 'outbox' + const createDomainOfflineExecutor = (): OfflineExecutor => startOfflineExecutor({ collections: { @@ -142,7 +145,7 @@ const createDomainOfflineExecutor = (): OfflineExecutor => demands: demandsCollection, activities: activitiesCollection, }, - storage: new IndexedDBAdapter('teqo-ops-domain', 'outbox'), + storage: new IndexedDBAdapter(DOMAIN_OUTBOX_DB, DOMAIN_OUTBOX_STORE), beforeRetry: collapseAllDomainOutbox, mutationFns: { [LEADERSHIP_UPDATE_FN]: async ({ transaction }) => { @@ -636,17 +639,19 @@ const wipeDomainOutboxCollections = (): void => { } } -/** Logout wipe — outbox storage + in-memory rows (OH11/OH13). */ +/** Logout wipe — persisted IndexedDB even when the executor singleton was never booted (cold reload). */ export const clearOpsDomainOutboxForLogout = async (): Promise => { - if (executorSingleton) { - try { + try { + if (executorSingleton) { await executorSingleton.clearOutbox() executorSingleton.dispose() - } catch { - // Best effort — private mode / torn-down storage. + } else { + await new IndexedDBAdapter(DOMAIN_OUTBOX_DB, DOMAIN_OUTBOX_STORE).clear() } - executorSingleton = null - initPromise = null + } catch { + // Best effort — private mode / torn-down storage. } + executorSingleton = null + initPromise = null wipeDomainOutboxCollections() } diff --git a/src/components/campaign/opsSync/opsEstimateOutbox.ts b/src/components/campaign/opsSync/opsEstimateOutbox.ts index e656d8e9..b44d2cff 100644 --- a/src/components/campaign/opsSync/opsEstimateOutbox.ts +++ b/src/components/campaign/opsSync/opsEstimateOutbox.ts @@ -47,6 +47,9 @@ const opsEstimateOutboxCollection = createCollection( let executorSingleton: OfflineExecutor | null = null let initPromise: Promise | null = null +const ESTIMATE_OUTBOX_DB = 'teqo-ops-estimate' +const ESTIMATE_OUTBOX_STORE = 'outbox' + const createEstimateOfflineExecutor = (): OfflineExecutor => startOfflineExecutor({ collections: { @@ -54,7 +57,7 @@ const createEstimateOfflineExecutor = (): OfflineExecutor => // Mirror rows touched in onMutate must be registered for persistence. votePledges: votePledgesCollection, }, - storage: new IndexedDBAdapter('teqo-ops-estimate', 'outbox'), + storage: new IndexedDBAdapter(ESTIMATE_OUTBOX_DB, ESTIMATE_OUTBOX_STORE), beforeRetry: collapseEstimateOutboxByPledge, mutationFns: { [ESTIMATE_MUTATION_FN]: async ({ transaction }) => { @@ -188,18 +191,20 @@ export const collectOpsEstimateOutboxKeys = (): Set => { return keys } -/** Logout wipe — outbox storage + in-memory rows (OH11). */ +/** Logout wipe — persisted IndexedDB even when the executor singleton was never booted (cold reload). */ export const clearOpsEstimateOutboxForLogout = async (): Promise => { - if (executorSingleton) { - try { + try { + if (executorSingleton) { await executorSingleton.clearOutbox() executorSingleton.dispose() - } catch { - // Best effort — private mode / torn-down storage. + } else { + await new IndexedDBAdapter(ESTIMATE_OUTBOX_DB, ESTIMATE_OUTBOX_STORE).clear() } - executorSingleton = null - initPromise = null + } catch { + // Best effort — private mode / torn-down storage. } + executorSingleton = null + initPromise = null for (const row of opsEstimateOutboxCollection.toArray) { if (opsEstimateOutboxCollection.has(row.pledgeId)) { opsEstimateOutboxCollection.delete(row.pledgeId) diff --git a/src/components/campaign/opsSync/opsMirrorClient.ts b/src/components/campaign/opsSync/opsMirrorClient.ts index e5f3988d..c5846e82 100644 --- a/src/components/campaign/opsSync/opsMirrorClient.ts +++ b/src/components/campaign/opsSync/opsMirrorClient.ts @@ -12,6 +12,7 @@ import { stateDeputiesCollection, } from '@/components/campaign/opsSync/opsMirrorCollections' import { + clearAllOpsMirrorPersistence, openOpsMirrorStore, type OpsMirrorPersistenceMode, type OpsMirrorStore, @@ -117,12 +118,11 @@ export const getOpsMirrorLastSyncedAt = (): string | null => lastSyncedAtMirror export const clearOpsMirrorPersistenceForLogout = async (): Promise => { wipeOpsMirrorCollections() bootPromise = null - const store = storeSingleton storeSingleton = null persistenceMode = null - if (store) { - await store.clear() - } + // Clear both durable backends — prior sessions may have used either, and the + // singleton is null after a cold reload even when IndexedDB/OPFS still hold data. + await clearAllOpsMirrorPersistence() } /** Test/DI seam — reset module singletons between unit cases. */ diff --git a/src/components/campaign/opsSync/opsMirrorPersistence.ts b/src/components/campaign/opsSync/opsMirrorPersistence.ts index 24a5db7a..99d34aa4 100644 --- a/src/components/campaign/opsSync/opsMirrorPersistence.ts +++ b/src/components/campaign/opsSync/opsMirrorPersistence.ts @@ -156,6 +156,14 @@ const createOpfsOpsMirrorStore = (): OpsMirrorStore => { * Boot-time feature detect: try OPFS, fall back to IndexedDB on any failure * (private mode, missing API, write probe fail). Never throws. */ +/** + * Logout wipe for both durable backends. Clears OPFS and IndexedDB even when + * the in-memory store singleton was never booted (cold reload after enqueue). + */ +export const clearAllOpsMirrorPersistence = async (): Promise => { + await Promise.allSettled([createOpfsOpsMirrorStore().clear(), createIdbOpsMirrorStore().clear()]) +} + export const openOpsMirrorStore = async (options?: { forceMode?: OpsMirrorPersistenceMode }): Promise => { diff --git a/src/components/campaign/opsSync/opsMunicipalityOutbox.ts b/src/components/campaign/opsSync/opsMunicipalityOutbox.ts index aa3757dd..1b3899cf 100644 --- a/src/components/campaign/opsSync/opsMunicipalityOutbox.ts +++ b/src/components/campaign/opsSync/opsMunicipalityOutbox.ts @@ -148,6 +148,9 @@ const handleUpdatedAtConflict = ( throw new NonRetriableError(message) } +const MUNICIPALITY_OUTBOX_DB = 'teqo-ops-municipality' +const MUNICIPALITY_OUTBOX_STORE = 'outbox' + const createMunicipalityOfflineExecutor = (): OfflineExecutor => startOfflineExecutor({ collections: { @@ -159,7 +162,7 @@ const createMunicipalityOfflineExecutor = (): OfflineExecutor => // Mirror rows touched in onMutate must be registered for persistence. votePledges: votePledgesCollection, }, - storage: new IndexedDBAdapter('teqo-ops-municipality', 'outbox'), + storage: new IndexedDBAdapter(MUNICIPALITY_OUTBOX_DB, MUNICIPALITY_OUTBOX_STORE), beforeRetry: collapseAllMunicipalityOutbox, mutationFns: { [DECLARE_MUTATION_FN]: async ({ transaction }) => { @@ -695,17 +698,19 @@ const wipeMunicipalityOutboxCollections = (): void => { } } -/** Logout wipe — outbox storage + in-memory rows (OH11). */ +/** Logout wipe — persisted IndexedDB even when the executor singleton was never booted (cold reload). */ export const clearOpsMunicipalityOutboxForLogout = async (): Promise => { - if (executorSingleton) { - try { + try { + if (executorSingleton) { await executorSingleton.clearOutbox() executorSingleton.dispose() - } catch { - // Best effort — private mode / torn-down storage. + } else { + await new IndexedDBAdapter(MUNICIPALITY_OUTBOX_DB, MUNICIPALITY_OUTBOX_STORE).clear() } - executorSingleton = null - initPromise = null + } catch { + // Best effort — private mode / torn-down storage. } + executorSingleton = null + initPromise = null wipeMunicipalityOutboxCollections() } diff --git a/src/components/campaign/shell/CampaignSidebar.tsx b/src/components/campaign/shell/CampaignSidebar.tsx index 872ddfb5..944b5fda 100644 --- a/src/components/campaign/shell/CampaignSidebar.tsx +++ b/src/components/campaign/shell/CampaignSidebar.tsx @@ -80,10 +80,13 @@ export const CampaignSidebar = ({ user }: { user: CampaignSidebarUser }) => { clearRecentVisits() clearLastActedMunicipality() clearMunicipalitySavedFilters() - await clearCampaignOpsStorage() - await clearCampaignPwaCaches() - // logoutCampaign redirects — no need to reset the pending flag on success. - await logoutCampaign() + try { + await clearCampaignOpsStorage() + await clearCampaignPwaCaches() + } finally { + // logoutCampaign redirects — always revoke the session even if storage wipe fails. + await logoutCampaign() + } } return ( diff --git a/tests/unit/opsLogoutWipe.unit.spec.ts b/tests/unit/opsLogoutWipe.unit.spec.ts new file mode 100644 index 00000000..93094d88 --- /dev/null +++ b/tests/unit/opsLogoutWipe.unit.spec.ts @@ -0,0 +1,44 @@ +// @vitest-environment jsdom +import { afterEach, describe, expect, it, vi } from 'vitest' + +const adapterClear = vi.fn(async () => undefined) + +vi.mock('@tanstack/offline-transactions', async () => { + const actual = await vi.importActual( + '@tanstack/offline-transactions', + ) + return { + ...actual, + IndexedDBAdapter: class { + clear = adapterClear + }, + } +}) + +describe('campaignOps logout wipe (Pass 5 P0)', () => { + afterEach(() => { + vi.resetModules() + adapterClear.mockClear() + }) + + it('clears estimate/municipality/domain IndexedDB when executor singletons are null', async () => { + const { clearOpsEstimateOutboxForLogout } = + await import('@/components/campaign/opsSync/opsEstimateOutbox') + const { clearOpsMunicipalityOutboxForLogout } = + await import('@/components/campaign/opsSync/opsMunicipalityOutbox') + const { clearOpsDomainOutboxForLogout } = + await import('@/components/campaign/opsSync/opsDomainOutbox') + + await clearOpsEstimateOutboxForLogout() + await clearOpsMunicipalityOutboxForLogout() + await clearOpsDomainOutboxForLogout() + + expect(adapterClear).toHaveBeenCalledTimes(3) + }) + + it('clearAllOpsMirrorPersistence settles without throwing when backends are empty', async () => { + const { clearAllOpsMirrorPersistence } = + await import('@/components/campaign/opsSync/opsMirrorPersistence') + await expect(clearAllOpsMirrorPersistence()).resolves.toBeUndefined() + }) +})