diff --git a/frontend/src/routes/documents/[id]/page.svelte.test.ts b/frontend/src/routes/documents/[id]/page.svelte.test.ts index 6113c74d..f0251c02 100644 --- a/frontend/src/routes/documents/[id]/page.svelte.test.ts +++ b/frontend/src/routes/documents/[id]/page.svelte.test.ts @@ -299,4 +299,84 @@ describe('documents/[id] page', () => { const main = document.body.firstElementChild; expect(main).not.toBeNull(); }); + + it('handles transcription-block fetch failure gracefully', async () => { + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockRejectedValue(new Error('network')); + try { + mockPage.url = new URL('http://localhost/documents/d-fail?task=transcribe'); + expect(() => + render(DocumentDetailPage, { + props: { + data: baseData({ document: { ...baseDoc, id: 'd-fail' } }) + } + }) + ).not.toThrow(); + await new Promise((r) => setTimeout(r, 100)); + } finally { + fetchSpy.mockRestore(); + } + }); + + it('renders blocks fetched in transcribe mode', async () => { + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue( + new Response( + JSON.stringify([ + { + id: 'b1', + annotationId: 'ann-1', + text: 'Erster', + sortOrder: 1, + reviewed: false, + mentionedPersons: [], + label: null + } + ]), + { status: 200, headers: { 'Content-Type': 'application/json' } } + ) + ); + try { + mockPage.url = new URL('http://localhost/documents/d-blocks?task=transcribe'); + render(DocumentDetailPage, { + props: { data: baseData({ document: { ...baseDoc, id: 'd-blocks' } }) } + }); + await new Promise((r) => setTimeout(r, 100)); + // No throw; the page rendered with fetched blocks. + expect(document.body.firstElementChild).not.toBeNull(); + } finally { + fetchSpy.mockRestore(); + } + }); + + it('reads localStorage on mount for last-visited (existing data overwritten)', async () => { + localStorage.setItem( + 'familienarchiv.lastVisited', + JSON.stringify({ id: 'old-doc', title: 'Old' }) + ); + mockPage.url = new URL('http://localhost/documents/d-new'); + render(DocumentDetailPage, { + props: { + data: baseData({ document: { ...baseDoc, id: 'd-new', title: 'New Doc' } }) + } + }); + await new Promise((r) => setTimeout(r, 50)); + const stored = JSON.parse(localStorage.getItem('familienarchiv.lastVisited') ?? '{}'); + expect(stored.id).toBe('d-new'); + }); + + it('renders the OCR error path when ocr-status fetch returns 500', async () => { + const fetchSpy = vi + .spyOn(globalThis, 'fetch') + .mockResolvedValue(new Response('error', { status: 500 })); + try { + mockPage.url = new URL('http://localhost/documents/d-ocr-fail?task=transcribe'); + expect(() => + render(DocumentDetailPage, { + props: { data: baseData({ document: { ...baseDoc, id: 'd-ocr-fail' } }) } + }) + ).not.toThrow(); + await new Promise((r) => setTimeout(r, 100)); + } finally { + fetchSpy.mockRestore(); + } + }); });