Files
TREK/client/src/store/mailIngestStore.test.ts
T
Maurice a7adef5353 feat(mail-ingest): admin card, review mark, inbox events and links, translations
- Admin settings (desktop and phone, self-hosted only): the instance switch,
  how mail arrives (IMAP or a signed webhook) with its fields, the address
  template, the AI allowance per person and day, a connection test and the
  status in counts. Changed fields only are saved; secrets stay masked.
- The review mark of an imported booking takes its mark off: the pill in the
  desktop dialog and a row in the phone's transport sheet ('Looks right'),
  through the ordinary booking update. Its hint no longer speaks of airports.
- The inbox follows mailInbox:changed and mailInbox:settings, and
  ?inbox=mail (the 'bookings waiting' notification) opens it.
- mailIngestRepo: the online-only path the views take, answers parsed
  against the shared contracts.
- Dropped 'read with AI' and the unused mail-review contract: the server
  reads a passing AI failure again on its own.
- mailInbox, mailAdmin and the new reservation, settings and notification
  strings in all 28 locales; the pending notice is a plural group.
- The live flight line on the booking cards is one of the card's fields,
  full width on the phone; the phone's live card keeps its map to the middle.
- Admin flight and mail cards sit in the left column.
2026-11-13 18:31:07 +01:00

112 lines
4.7 KiB
TypeScript

// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-005
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('../api/mailIngest', () => ({
mailIngestApi: { settings: vi.fn(), inbox: vi.fn(), inboxCount: vi.fn(), act: vi.fn() },
}));
vi.mock('../sync/networkMode', () => ({ isEffectivelyOffline: vi.fn(() => false) }));
const listeners = vi.hoisted(() => new Set<(event: Record<string, unknown>) => void>());
vi.mock('../api/websocket', () => ({
addListener: (fn: (event: Record<string, unknown>) => void) => listeners.add(fn),
removeListener: (fn: (event: Record<string, unknown>) => void) => listeners.delete(fn),
}));
import { mailIngestApi } from '../api/mailIngest';
import { useMailIngestStore, watchMailInbox } from './mailIngestStore';
const candidate = (id: number, resolution = 'pending') => ({
id,
message_id: 10 + id,
item: null,
source: null,
uncertain_fields: [],
checks: {},
suggested_trip: null,
suggested_new_trip: null,
resolution,
reason: 'not_a_booking',
resolved_trip_id: null,
reservation_ids: [],
file_ids: [],
created_at: '2026-06-01T10:00:00.000Z',
resolved_at: null,
undo_until: null,
});
const entry = (id: number) => ({
...candidate(id),
message: {
id: 10 + id,
transport: 'upload',
subject: null,
received_at: '2026-06-01T09:59:00.000Z',
status: 'processed',
reason: null,
hints: [],
ai_used: false,
trip_id: null,
attachments: [],
forwarding_code: null,
},
});
beforeEach(() => {
useMailIngestStore.setState({ settings: null, entries: [], nextBefore: null, pending: 0, loadingInbox: false });
vi.mocked(mailIngestApi.inbox).mockReset();
vi.mocked(mailIngestApi.act).mockReset();
});
describe('mailIngestStore', () => {
it('FE-STORE-MAILINGEST-001: reads the open entries and the count', async () => {
vi.mocked(mailIngestApi.inbox).mockResolvedValue({ entries: [entry(1), entry(2)], pending: 2, next_before: null });
await useMailIngestStore.getState().loadInbox();
expect(useMailIngestStore.getState().entries.map((e) => e.id)).toEqual([1, 2]);
expect(useMailIngestStore.getState().pending).toBe(2);
});
it('FE-STORE-MAILINGEST-002: a further page is appended, a malformed answer is ignored', async () => {
vi.mocked(mailIngestApi.inbox).mockResolvedValueOnce({ entries: [entry(1)], pending: 2, next_before: 1 });
await useMailIngestStore.getState().loadInbox();
vi.mocked(mailIngestApi.inbox).mockResolvedValueOnce({ entries: [entry(2)], pending: 2, next_before: null });
await useMailIngestStore.getState().loadInbox(true);
vi.mocked(mailIngestApi.inbox).mockResolvedValueOnce({ nope: true });
await useMailIngestStore.getState().loadInbox(true);
expect(useMailIngestStore.getState().entries.map((e) => e.id)).toEqual([1, 2]);
});
it('FE-STORE-MAILINGEST-003: a decided entry leaves the list and the count', async () => {
useMailIngestStore.setState({ entries: [entry(1), entry(2)] as never, pending: 2 });
vi.mocked(mailIngestApi.act).mockResolvedValue({ candidate: candidate(1, 'discarded') });
await useMailIngestStore.getState().act(1, 'discard');
expect(useMailIngestStore.getState().entries.map((e) => e.id)).toEqual([2]);
expect(useMailIngestStore.getState().pending).toBe(1);
});
it('FE-STORE-MAILINGEST-004: an entry still open after an action stays, updated', async () => {
useMailIngestStore.setState({ entries: [entry(1)] as never, pending: 1 });
vi.mocked(mailIngestApi.act).mockResolvedValue({ candidate: { ...candidate(1), reason: 'ai_provider_error' } });
await useMailIngestStore.getState().act(1, 'apply');
expect(useMailIngestStore.getState().entries[0].reason).toBe('ai_provider_error');
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 });
const stop = watchMailInbox();
const send = (event: Record<string, unknown>) => listeners.forEach((fn) => fn(event));
send({ type: 'mailInbox:changed', pending: 1 });
expect(useMailIngestStore.getState().pending).toBe(1);
await vi.waitFor(() => expect(useMailIngestStore.getState().entries.map((e) => e.id)).toEqual([3]));
send({ type: 'mailInbox:settings' });
expect(mailIngestApi.settings).toHaveBeenCalled();
// A malformed event changes nothing, and after stopping nothing is heard.
send({ type: 'mailInbox:changed', pending: 'many' });
expect(useMailIngestStore.getState().pending).toBe(1);
stop();
expect(listeners.size).toBe(0);
});
});