fix(mail-ingest): read the inbox again when it changes while a read is on its way

A reload asked for during a read was dropped and the older answer overwrote the newer count, and an entry decided meanwhile came back with it.
This commit is contained in:
Maurice
2026-11-23 15:09:02 +01:00
parent 310e5fba8f
commit 855ec37935
2 changed files with 58 additions and 10 deletions
+33 -1
View File
@@ -1,4 +1,4 @@
// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-006
// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-008
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('../api/mailIngest', () => ({
@@ -104,6 +104,38 @@ describe('mailIngestStore', () => {
expect(state.loadingInbox).toBe(false);
});
it('FE-STORE-MAILINGEST-007: a reload asked for while a read is on its way runs once that read is in', async () => {
let answer: (value: unknown) => void = () => undefined;
vi.mocked(mailIngestApi.inbox)
.mockImplementationOnce(() => new Promise((resolve) => (answer = resolve)) as never)
.mockResolvedValueOnce({ entries: [entry(1), entry(2), entry(3)], pending: 3, next_before: null });
const stop = watchMailInbox();
const reading = useMailIngestStore.getState().loadInbox();
listeners.forEach((fn) => fn({ type: 'mailInbox:changed', pending: 3 }));
answer({ entries: [entry(1), entry(2)], pending: 2, next_before: null });
await reading;
const state = useMailIngestStore.getState();
expect(state.entries.map((e) => e.id)).toEqual([1, 2, 3]);
expect(state.pending).toBe(3);
expect(mailIngestApi.inbox).toHaveBeenCalledTimes(2);
stop();
});
it('FE-STORE-MAILINGEST-008: an entry decided while a read is on its way does not come back with it', async () => {
useMailIngestStore.setState({ entries: [entry(1), entry(2)] as never, pending: 2 });
let answer: (value: unknown) => void = () => undefined;
vi.mocked(mailIngestApi.inbox)
.mockImplementationOnce(() => new Promise((resolve) => (answer = resolve)) as never)
.mockResolvedValueOnce({ entries: [entry(2)], pending: 1, next_before: null });
vi.mocked(mailIngestApi.act).mockResolvedValue({ candidate: candidate(1, 'discarded') });
const reading = useMailIngestStore.getState().loadInbox();
await useMailIngestStore.getState().act(1, 'discard');
answer({ entries: [entry(1), entry(2)], pending: 2, next_before: null });
await reading;
expect(useMailIngestStore.getState().entries.map((e) => e.id)).toEqual([2]);
expect(useMailIngestStore.getState().pending).toBe(1);
});
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 });
+25 -9
View File
@@ -35,6 +35,8 @@ const EMPTY = { settings: null, entries: [], nextBefore: null, pending: 0, loadi
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;
// Set when the inbox changed while a read was on its way: that answer is older, so the read runs again.
let stale = false;
return {
...EMPTY,
@@ -63,18 +65,30 @@ export const useMailIngestStore = create<MailIngestState>((set, get) => {
},
loadInbox: async (more = false) => {
if (isEffectivelyOffline() || get().loadingInbox) return;
if (isEffectivelyOffline()) return;
if (get().loadingInbox) {
if (!more) stale = true;
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,
}));
let page = more;
do {
stale = false;
const before = page ? (get().nextBefore ?? undefined) : undefined;
const { entries, pending, next_before } = await mailIngestRepo.inbox(before);
if (asked !== account) return;
if (stale) {
page = false;
continue;
}
set((state) => ({
entries: page ? [...state.entries, ...entries] : entries,
pending,
nextBefore: next_before,
}));
} while (stale);
} catch {
// The inbox stays as it was; the next open reads it again.
} finally {
@@ -86,6 +100,7 @@ export const useMailIngestStore = create<MailIngestState>((set, get) => {
const asked = account;
const candidate = await mailIngestRepo.act(candidateId, action, body);
if (asked !== account) return candidate;
if (get().loadingInbox) stale = true;
set((state) => {
const stillOpen = OPEN.has(candidate.resolution);
const entries = state.entries.flatMap((entry) =>
@@ -99,6 +114,7 @@ export const useMailIngestStore = create<MailIngestState>((set, get) => {
reset: () => {
account += 1;
stale = false;
set(EMPTY);
},
};