Merge pull request #233 from fsolla/cursor/pass5-p0-logout-wipe-171a

P5-P0 — logout always clears persisted ops outbox/mirror
This commit is contained in:
Francisco Solla 2026-08-02 03:45:37 -03:00 committed by GitHub
commit 113f886640
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 102 additions and 32 deletions

View file

@ -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<void> => {
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()
}

View file

@ -47,6 +47,9 @@ const opsEstimateOutboxCollection = createCollection(
let executorSingleton: OfflineExecutor | null = null
let initPromise: Promise<OfflineExecutor> | 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<OpsOutboxKey> => {
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<void> => {
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)

View file

@ -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<void> => {
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. */

View file

@ -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<void> => {
await Promise.allSettled([createOpfsOpsMirrorStore().clear(), createIdbOpsMirrorStore().clear()])
}
export const openOpsMirrorStore = async (options?: {
forceMode?: OpsMirrorPersistenceMode
}): Promise<OpsMirrorStore> => {

View file

@ -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<void> => {
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()
}

View file

@ -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 (

View file

@ -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<typeof import('@tanstack/offline-transactions')>(
'@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()
})
})