Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(mail-ingest): forget the mail import of an account on sign-out

Maurice310e5fba8fda
diff --git a/client/src/store/authStore.ts b/client/src/store/authStore.ts
index 297b14562..587d9477c 100644
--- a/client/src/store/authStore.ts
+++ b/client/src/store/authStore.ts
@@ -11,6 +11,7 @@ import { isEffectivelyOffline, setForcedOffline } from '../sync/networkMode'
import { mutationQueue } from '../sync/mutationQueue'
import { registerSyncTriggers, unregisterSyncTriggers } from '../sync/syncTriggers'
import { useSystemNoticeStore } from './systemNoticeStore.js'
+import { useMailIngestStore } from './mailIngestStore'
import { clearAppearanceSnapshot } from '../theme/applyAppearance'
import { clearAllPluginSessions } from './pluginStore'
import { forgetStartDestination } from '../utils/startDestination'
@@ -273,6 +274,7 @@ export const useAuthStore = create<AuthState>()(
// 3. Tear down the live connection.
disconnect()
useSystemNoticeStore.getState().reset()
+ useMailIngestStore.getState().reset()
// Drop the per-device appearance snapshot so the next user on a shared
// browser doesn't get a pre-paint flash of this user's theme.
clearAppearanceSnapshot()
diff --git a/client/src/store/mailIngestStore.test.ts b/client/src/store/mailIngestStore.test.ts
index 2da9c890a..0100fdac0 100644
--- a/client/src/store/mailIngestStore.test.ts
+++ b/client/src/store/mailIngestStore.test.ts
@@ -1,4 +1,4 @@
-// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-005
+// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-006
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('../api/mailIngest', () => ({
@@ -89,6 +89,21 @@ describe('mailIngestStore', () => {
expect(useMailIngestStore.getState().pending).toBe(1);
});
+ it('FE-STORE-MAILINGEST-006: a reset forgets the account, and its answers still on the way land nowhere', async () => {
+ let answer: (value: unknown) => void = () => undefined;
+ vi.mocked(mailIngestApi.inbox).mockImplementationOnce(() => new Promise((resolve) => (answer = resolve)) as never);
+ useMailIngestStore.setState({ settings: { address: '[email protected]' } as never, pending: 2 });
+ const reading = useMailIngestStore.getState().loadInbox();
+ useMailIngestStore.getState().reset();
+ answer({ entries: [entry(1)], pending: 1, next_before: null });
+ await reading;
+ const state = useMailIngestStore.getState();
+ expect(state.settings).toBeNull();
+ expect(state.entries).toEqual([]);
+ expect(state.pending).toBe(0);
+ expect(state.loadingInbox).toBe(false);
+ });
+
it('FE-STORE-MAILINGEST-005: the server inbox events move the count, read the list again and reload the settings', async () => {
vi.mocked(mailIngestApi.inbox).mockResolvedValue({ entries: [entry(3)], pending: 1, next_before: null });
vi.mocked(mailIngestApi.settings).mockResolvedValue({ nope: true });
diff --git a/client/src/store/mailIngestStore.ts b/client/src/store/mailIngestStore.ts
index 23f7525d8..de97a8996 100644
--- a/client/src/store/mailIngestStore.ts
+++ b/client/src/store/mailIngestStore.ts
@@ -24,65 +24,85 @@ interface MailIngestState {
loadInbox: (more?: boolean) => Promise<void>;
/** Runs an action on an entry; the entry leaves the open list once it is decided. */
act: (candidateId: number, action: MailIngestAction, body?: Record<string, unknown>) => Promise<MailIngestCandidate>;
+ /** Forgets the person on sign-out: the next account on this tab must not see their address or inbox. */
+ reset: () => void;
}
const OPEN = new Set(['pending']);
-export const useMailIngestStore = create<MailIngestState>((set, get) => ({
- settings: null,
- entries: [],
- nextBefore: null,
- pending: 0,
- loadingInbox: false,
+const EMPTY = { settings: null, entries: [], nextBefore: null, pending: 0, loadingInbox: false };
- loadSettings: async () => {
- if (isEffectivelyOffline()) return;
- try {
- const settings = await mailIngestRepo.settings();
- set({ settings, pending: settings.pending });
- } catch {
- // Without an answer the menus keep what they showed; the next sign-in or event reads it again.
- }
- },
+export const useMailIngestStore = create<MailIngestState>((set, get) => {
+ // Bumped on sign-out, so an answer still on its way for the account before lands nowhere.
+ let account = 0;
+ return {
+ ...EMPTY,
- setSettings: (settings) => set({ settings, pending: settings.pending }),
+ loadSettings: async () => {
+ if (isEffectivelyOffline()) return;
+ const asked = account;
+ try {
+ const settings = await mailIngestRepo.settings();
+ if (asked === account) set({ settings, pending: settings.pending });
+ } catch {
+ // Without an answer the menus keep what they showed; the next sign-in or event reads it again.
+ }
+ },
- loadCount: async () => {
- if (isEffectivelyOffline()) return;
- try {
- set({ pending: await mailIngestRepo.inboxCount() });
- } catch {
- // The badge keeps its last count until the next event brings a new one.
- }
- },
+ setSettings: (settings) => set({ settings, pending: settings.pending }),
- loadInbox: async (more = false) => {
- if (isEffectivelyOffline() || get().loadingInbox) return;
- set({ loadingInbox: true });
- try {
- const before = more ? (get().nextBefore ?? undefined) : undefined;
- const { entries, pending, next_before } = await mailIngestRepo.inbox(before);
- set((state) => ({ entries: more ? [...state.entries, ...entries] : entries, pending, nextBefore: next_before }));
- } catch {
- // The inbox stays as it was; the next open reads it again.
- } finally {
- set({ loadingInbox: false });
- }
- },
+ loadCount: async () => {
+ if (isEffectivelyOffline()) return;
+ const asked = account;
+ try {
+ const pending = await mailIngestRepo.inboxCount();
+ if (asked === account) set({ pending });
+ } catch {
+ // The badge keeps its last count until the next event brings a new one.
+ }
+ },
- act: async (candidateId, action, body = {}) => {
- const candidate = await mailIngestRepo.act(candidateId, action, body);
- set((state) => {
- const stillOpen = OPEN.has(candidate.resolution);
- const entries = state.entries.flatMap((entry) =>
- entry.id !== candidate.id ? [entry] : stillOpen ? [{ ...entry, ...candidate }] : []
- );
- const removed = state.entries.length - entries.length;
- return { entries, pending: Math.max(0, state.pending - removed) };
- });
- return candidate;
- },
-}));
+ loadInbox: async (more = false) => {
+ if (isEffectivelyOffline() || get().loadingInbox) return;
+ const asked = account;
+ set({ loadingInbox: true });
+ try {
+ const before = more ? (get().nextBefore ?? undefined) : undefined;
+ const { entries, pending, next_before } = await mailIngestRepo.inbox(before);
+ if (asked !== account) return;
+ set((state) => ({
+ entries: more ? [...state.entries, ...entries] : entries,
+ pending,
+ nextBefore: next_before,
+ }));
+ } catch {
+ // The inbox stays as it was; the next open reads it again.
+ } finally {
+ if (asked === account) set({ loadingInbox: false });
+ }
+ },
+
+ act: async (candidateId, action, body = {}) => {
+ const asked = account;
+ const candidate = await mailIngestRepo.act(candidateId, action, body);
+ if (asked !== account) return candidate;
+ set((state) => {
+ const stillOpen = OPEN.has(candidate.resolution);
+ const entries = state.entries.flatMap((entry) =>
+ entry.id !== candidate.id ? [entry] : stillOpen ? [{ ...entry, ...candidate }] : []
+ );
+ const removed = state.entries.length - entries.length;
+ return { entries, pending: Math.max(0, state.pending - removed) };
+ });
+ return candidate;
+ },
+
+ reset: () => {
+ account += 1;
+ set(EMPTY);
+ },
+ };
+});
/**
* Keeps the inbox current while the app is open: the server says when the
diff --git a/client/tests/unit/stores/authStore.test.ts b/client/tests/unit/stores/authStore.test.ts
index 1189fce87..080c2521b 100644
--- a/client/tests/unit/stores/authStore.test.ts
+++ b/client/tests/unit/stores/authStore.test.ts
@@ -3,6 +3,7 @@ import { http, HttpResponse } from 'msw';
import { server } from '../../helpers/msw/server';
import { useAuthStore } from '../../../src/store/authStore';
import { useSystemNoticeStore } from '../../../src/store/systemNoticeStore';
+import { useMailIngestStore } from '../../../src/store/mailIngestStore';
import { authApi } from '../../../src/api/client';
import { resetAllStores } from '../../helpers/store';
import { buildUser } from '../../helpers/factories';
@@ -578,6 +579,20 @@ describe('authStore', () => {
});
});
+ describe('FE-STORE-AUTH-MAIL-001: logout forgets the mail import', () => {
+ it('drops the forwarding address, the inbox and the badge count of the account that left', async () => {
+ useAuthStore.setState({ user: buildUser(), isAuthenticated: true });
+ useMailIngestStore.setState({ settings: { address: '[email protected]' } as never, entries: [{ id: 1 }] as never, pending: 3 });
+
+ await useAuthStore.getState().logout();
+
+ const mail = useMailIngestStore.getState();
+ expect(mail.settings).toBeNull();
+ expect(mail.entries).toEqual([]);
+ expect(mail.pending).toBe(0);
+ });
+ });
+
describe('FE-STORE-AUTH-PERSIST-002: 401 resets persisted snapshot', () => {
it('snapshot has isAuthenticated:false after 401 (expired session clears offline access)', async () => {
useAuthStore.setState({ user: buildUser(), isAuthenticated: true });