test: close gaps found by mutation checks on the archive import

- Cover the CRLF separator line, cycle roots entered from outside, linear rethreading of long chains
- Cover a read error mid-import, the "and N more" lines, and the mbox content type
- Cover clearing the previous result and the capped error list in the UI
This commit is contained in:
tgiachi 2026-11-09 19:50:40 +01:00
commit a170204ecb

View file

@ -1,5 +1,5 @@
import { screen, waitFor, within } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import type { Message } from '@/lib/api'
import { aList, installFakeApi, newState } from '@/test/fakeApi'
import { renderApp } from '@/test/render'
@ -244,5 +244,41 @@ describe('archive tab', () => {
await user.upload(screen.getByLabelText(/mbox file/i), file)
await waitFor(() => expect(state.importTypes).toHaveLength(2))
})
it('forgets the previous result when a new import starts', async () => {
const { user, state } = await setup([])
state.fail['POST /lists/1/archive/import'] = { status: 500, error: 'disk full' }
await user.upload(screen.getByLabelText(/mbox file/i), mboxFile(1))
expect(await screen.findByRole('alert')).toHaveTextContent('disk full')
await user.upload(screen.getByLabelText(/mbox file/i), mboxFile(1))
expect(await screen.findByRole('status')).toHaveTextContent('Imported 1 message')
expect(screen.queryByRole('alert')).not.toBeInTheDocument() // the old error is gone
state.fail['POST /lists/1/archive/import'] = { status: 500, error: 'again' }
await user.upload(screen.getByLabelText(/mbox file/i), mboxFile(2))
expect(await screen.findByRole('alert')).toHaveTextContent('again')
expect(screen.queryByRole('status')).not.toBeInTheDocument() // the old summary is gone
})
it('says how many more failures there are than the server listed', async () => {
const { user } = await setup([])
const real = fetch
vi.stubGlobal('fetch', vi.fn((url: string, init?: RequestInit) => {
if (url.endsWith('/archive/import')) {
const errors = Array.from({ length: 20 }, (_, i) => `message ${i + 1}: parse message: no headers`)
return Promise.resolve(new Response(JSON.stringify({ imported: 0, duplicates: 0, failed: 25, errors }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
}))
}
return real(url, init)
}))
await user.upload(screen.getByLabelText(/mbox file/i), mboxFile(1))
const status = await screen.findByRole('status')
expect(status).toHaveTextContent('25 failed')
expect(within(status).getByText(/and 5 more/)).toBeInTheDocument()
expect(within(status).getAllByRole('listitem')).toHaveLength(21)
})
})
})