test(pdf): step 6 phase 0 freeze — demo report, bare v3 PDF, 39 goldens (text, images, links, views) and route characterisation tests
This commit is contained in:
1 parent
694092610d
commit
8b86b926c8
46 files changed
+18502
-5
No files matched your search
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
* Step 6 Phase 0 characterisation of the import flow on the home route, with
|
||||
* `importReportFromPdf` mocked: the migration alert with its count, the three
|
||||
* ImportError kinds plus the generic error mapped to the FR messages, and
|
||||
* the navigation to /editor on success. Kept green unchanged by Step 6.
|
||||
*/
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
vi,
|
||||
beforeAll,
|
||||
beforeEach,
|
||||
afterEach,
|
||||
} from 'vitest'
|
||||
import {
|
||||
cleanup,
|
||||
fireEvent,
|
||||
render,
|
||||
screen,
|
||||
waitFor,
|
||||
} from '@testing-library/react'
|
||||
import { MemoryRouter, Route, Routes } from 'react-router-dom'
|
||||
import HomeRoute from '@/app/routes/HomeRoute'
|
||||
import { ImportError, importReportFromPdf } from '@/pdf/import'
|
||||
import { useReportStore } from '@/state/reportStore'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { fr } from '@/i18n/fr'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
||||
|
||||
vi.mock('@/pdf/import', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('@/pdf/import')>()),
|
||||
importReportFromPdf: vi.fn(),
|
||||
}))
|
||||
const importMock = vi.mocked(importReportFromPdf)
|
||||
|
||||
let sheet: SheetIndex
|
||||
|
||||
beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
})
|
||||
beforeEach(() => {
|
||||
useLocaleStore.setState({ locale: 'fr' })
|
||||
useReportStore.setState({ report: null, activeObservationIndex: 0 })
|
||||
vi.stubGlobal('alert', vi.fn())
|
||||
importMock.mockReset()
|
||||
})
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
/** The outcome shape importReportFromPdf resolves with. */
|
||||
function outcome(patch: {
|
||||
migrated: boolean
|
||||
unresolvedZones?: number
|
||||
unresolvedFindings?: number
|
||||
}) {
|
||||
return {
|
||||
report: minimalReport(sheet),
|
||||
unresolvedZones: 0,
|
||||
unresolvedFindings: 0,
|
||||
...patch,
|
||||
}
|
||||
}
|
||||
|
||||
async function mountAndPickPdf(): Promise<void> {
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<Routes>
|
||||
<Route path="/" element={<HomeRoute />} />
|
||||
<Route path="/editor" element={<p data-testid="editor-route" />} />
|
||||
</Routes>
|
||||
</MemoryRouter>,
|
||||
)
|
||||
const button = screen.getByRole('button', { name: fr['home.import.cta'] })
|
||||
await waitFor(() => expect(button).toBeEnabled())
|
||||
const input = document.querySelector('input[type="file"]')!
|
||||
const file = new File([new Uint8Array([37, 80, 68, 70])], 'bilan.pdf', {
|
||||
type: 'application/pdf',
|
||||
})
|
||||
fireEvent.change(input, { target: { files: [file] } })
|
||||
}
|
||||
|
||||
const alertMock = () => vi.mocked(window.alert)
|
||||
const errorText = () => screen.queryByText(/./, { selector: 'p.text-red-700' })
|
||||
|
||||
describe('HomeRoute import: success paths', () => {
|
||||
it('passes the file bytes and the sheet migration map, hydrates, navigates to /editor', async () => {
|
||||
importMock.mockResolvedValue(outcome({ migrated: false }))
|
||||
await mountAndPickPdf()
|
||||
await screen.findByTestId('editor-route')
|
||||
expect(importMock).toHaveBeenCalledTimes(1)
|
||||
const [bytes, map] = importMock.mock.calls[0]!
|
||||
expect(bytes).toBeInstanceOf(Uint8Array)
|
||||
expect(map).toBe(sheet.sheet.migrationMap)
|
||||
expect(useReportStore.getState().report?.id).toBe('r1')
|
||||
expect(alertMock()).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('alerts with the unresolved count (zones + findings) after a lossy migration', async () => {
|
||||
importMock.mockResolvedValue(
|
||||
outcome({ migrated: true, unresolvedZones: 1, unresolvedFindings: 1 }),
|
||||
)
|
||||
await mountAndPickPdf()
|
||||
await screen.findByTestId('editor-route')
|
||||
expect(alertMock()).toHaveBeenCalledTimes(1)
|
||||
const message = alertMock().mock.calls[0]![0] as string
|
||||
expect(message).toBe(fr['home.import.migrated']!.replace('{count}', '2'))
|
||||
expect(message).toContain('2')
|
||||
})
|
||||
|
||||
it('stays silent on a migration that lost nothing', async () => {
|
||||
importMock.mockResolvedValue(outcome({ migrated: true }))
|
||||
await mountAndPickPdf()
|
||||
await screen.findByTestId('editor-route')
|
||||
expect(alertMock()).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('HomeRoute import: error paths', () => {
|
||||
const cases: Array<[string, Error, string]> = [
|
||||
[
|
||||
'incompatible_version → message with the found version',
|
||||
new ImportError('incompatible_version', 7),
|
||||
fr['home.import.error_incompatible_version']!.replace('{version}', '7'),
|
||||
],
|
||||
[
|
||||
'invalid_pdf → unreadable PDF message',
|
||||
new ImportError('invalid_pdf'),
|
||||
fr['home.import.error_invalid_pdf']!,
|
||||
],
|
||||
[
|
||||
'no_attachment → not a Faunosteo PDF message',
|
||||
new ImportError('no_attachment'),
|
||||
fr['home.import.error_no_attachment']!,
|
||||
],
|
||||
[
|
||||
'generic Error → generic message',
|
||||
new Error('disk on fire'),
|
||||
fr['home.import.error_generic']!,
|
||||
],
|
||||
]
|
||||
|
||||
it.each(cases)('%s', async (_label, error, expected) => {
|
||||
importMock.mockRejectedValue(error)
|
||||
await mountAndPickPdf()
|
||||
await waitFor(() => expect(errorText()).not.toBeNull())
|
||||
expect(errorText()!.textContent).toBe(expected)
|
||||
expect(screen.queryByTestId('editor-route')).toBeNull()
|
||||
expect(useReportStore.getState().report).toBeNull()
|
||||
expect(alertMock()).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user