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.

7.3 KB
Raw Blame
// FE-STORE-MAILINGEST-001 to FE-STORE-MAILINGEST-008
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-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-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 });
    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);
  });
});