diff --git a/admin/src/tabs/ArchiveTab.test.tsx b/admin/src/tabs/ArchiveTab.test.tsx new file mode 100644 index 0000000..a5927b3 --- /dev/null +++ b/admin/src/tabs/ArchiveTab.test.tsx @@ -0,0 +1,171 @@ +import { screen, waitFor, within } from '@testing-library/react' +import { describe, expect, it } from 'vitest' +import type { Message } from '@/lib/api' +import { aList, installFakeApi, newState } from '@/test/fakeApi' +import { renderApp } from '@/test/render' + +const msg = (id: number, over: Partial = {}): Message => ({ + id, + thread_id: id, + message_id: `<${id}@x>`, + subject: `Subject ${id}`, + sender: `Sender ${id}`, + date: '2030-01-02T03:04:05Z', + size: 2048, + body: `Body of message ${id}`, + ...over, +}) + +async function setup(messages: Message[], list = aList({ id: 1, archive_policy: 'private' })) { + const state = installFakeApi(newState({ lists: [list], archive: { [list.id]: messages } })) + const ctx = renderApp(`/lists/${list.id}/archive`, { token: 'good' }) + await screen.findByRole('heading', { name: /^archive/i, level: 2 }) + return { ...ctx, state } +} + +describe('archive tab', () => { + it('lists messages with sender, date and size', async () => { + await setup([msg(1), msg(2)]) + const row = (await screen.findByRole('button', { name: 'Subject 1' })).closest('tr')! + expect(within(row).getByText('Sender 1')).toBeInTheDocument() + expect(within(row).getByText('2030-01-02 03:04 UTC')).toBeInTheDocument() + expect(within(row).getByText('2.0 KB')).toBeInTheDocument() + expect(screen.getByRole('heading', { name: 'Archive (2)', level: 2 })).toBeInTheDocument() + }) + + it('has an empty state', async () => { + await setup([]) + expect(await screen.findByText(/no messages archived yet/i)).toBeInTheDocument() + }) + + it('warns when archiving is off', async () => { + await setup([], aList({ id: 1, archive_policy: 'off' })) + expect(await screen.findByText(/archiving is off/i)).toBeInTheDocument() + }) + + it('does not warn when archiving is on', async () => { + await setup([msg(1)]) + await screen.findByRole('button', { name: 'Subject 1' }) + expect(screen.queryByText(/archiving is off/i)).not.toBeInTheDocument() + }) + + describe('searching', () => { + it('shows only matches and how many', async () => { + const { user } = await setup([msg(1, { subject: 'Deploy failed' }), msg(2, { subject: 'Lunch' })]) + await screen.findByRole('button', { name: 'Lunch' }) + await user.type(screen.getByRole('searchbox', { name: /search archive/i }), 'deploy') + await user.click(screen.getByRole('button', { name: /^search$/i })) + expect(await screen.findByText(/1 result for “deploy”/i)).toBeInTheDocument() + expect(screen.getByRole('button', { name: 'Deploy failed' })).toBeInTheDocument() + expect(screen.queryByRole('button', { name: 'Lunch' })).not.toBeInTheDocument() + }) + + it('submits with Enter and says when nothing matches', async () => { + const { user } = await setup([msg(1)]) + await screen.findByRole('button', { name: 'Subject 1' }) + await user.type(screen.getByRole('searchbox', { name: /search archive/i }), 'zzz{Enter}') + expect(await screen.findByText(/no messages found/i)).toBeInTheDocument() + }) + + it('clears the search to show everything again', async () => { + const { user } = await setup([msg(1, { subject: 'Alpha' }), msg(2, { subject: 'Beta' })]) + await screen.findByRole('button', { name: 'Beta' }) + await user.type(screen.getByRole('searchbox', { name: /search archive/i }), 'alpha{Enter}') + await waitFor(() => expect(screen.queryByRole('button', { name: 'Beta' })).not.toBeInTheDocument()) + await user.click(screen.getByRole('button', { name: /clear/i })) + expect(await screen.findByRole('button', { name: 'Beta' })).toBeInTheDocument() + expect(screen.getByRole('searchbox', { name: /search archive/i })).toHaveValue('') + }) + }) + + describe('paging', () => { + const many = Array.from({ length: 60 }, (_, i) => msg(i + 1)) + + it('shows 25 at a time and moves through the pages', async () => { + const { user, state } = await setup(many) + await screen.findByRole('button', { name: 'Subject 1' }) + expect(screen.getAllByRole('button', { name: /^Subject \d+$/ })).toHaveLength(25) + expect(screen.getByText('1–25 of 60')).toBeInTheDocument() + expect(screen.getByRole('button', { name: /previous/i })).toBeDisabled() + + await user.click(screen.getByRole('button', { name: /next/i })) + expect(await screen.findByText('26–50 of 60')).toBeInTheDocument() + expect(state.calls).toContain('GET /lists/1/archive') + await user.click(screen.getByRole('button', { name: /next/i })) + expect(await screen.findByText('51–60 of 60')).toBeInTheDocument() + expect(screen.getByRole('button', { name: /next/i })).toBeDisabled() + await user.click(screen.getByRole('button', { name: /previous/i })) + expect(await screen.findByText('26–50 of 60')).toBeInTheDocument() + }) + + it('goes back to the first page on a new search', async () => { + const { user } = await setup(many) + await screen.findByText('1–25 of 60') + await user.click(screen.getByRole('button', { name: /next/i })) + await screen.findByText('26–50 of 60') + await user.type(screen.getByRole('searchbox', { name: /search archive/i }), 'subject{Enter}') + expect(await screen.findByText('1–25 of 60')).toBeInTheDocument() + }) + + it('hides the pager when everything fits on one page', async () => { + await setup([msg(1)]) + await screen.findByRole('button', { name: 'Subject 1' }) + expect(screen.queryByRole('button', { name: /next/i })).not.toBeInTheDocument() + }) + }) + + describe('reading a message', () => { + async function open(messages = [msg(1, { body: 'Hello world' }), msg(2)]) { + const ctx = await setup(messages) + await ctx.user.click(await screen.findByRole('button', { name: 'Subject 1' })) + return { ...ctx, dialog: await screen.findByRole('dialog') } + } + + it('shows the full text as plain text', async () => { + const { dialog } = await open() + expect(await within(dialog).findByText(/Hello