Files
familienarchiv/frontend/src/lib/ocr/OcrTrainingCard.svelte.spec.ts
Marcel 7ee038faaf
Some checks failed
CI / Unit & Component Tests (pull_request) Failing after 1m50s
CI / OCR Service Tests (pull_request) Successful in 16s
CI / Backend Unit Tests (pull_request) Successful in 4m11s
CI / fail2ban Regex (pull_request) Successful in 39s
CI / Compose Bucket Idempotency (pull_request) Failing after 11s
CI / Unit & Component Tests (push) Failing after 1m50s
CI / OCR Service Tests (push) Successful in 16s
CI / Backend Unit Tests (push) Successful in 4m7s
CI / fail2ban Regex (push) Successful in 37s
CI / Compose Bucket Idempotency (push) Failing after 10s
test(ocr): fix track_reactivity_loss in OcrTrainingCard spec
Two root causes:

1. In-flight test: resolveFetch() was the last line, leaving the async
   finally-block writing `training = false` after cleanup destroyed the
   component. Awaiting the button becoming re-enabled ensures the finally
   block settles before cleanup runs.

2. Success-dismiss test: startTraining() schedules setTimeout(5000) which
   fired after cleanup destroyed the component. vi.useFakeTimers() +
   vi.runAllTimers() scoped to the describe block drains the timer while
   the component is still alive.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-12 10:22:03 +02:00

123 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cleanup, render } from 'vitest-browser-svelte';
import { page } from 'vitest/browser';
import OcrTrainingCard from './OcrTrainingCard.svelte';
afterEach(cleanup);
afterEach(() => vi.restoreAllMocks());
const baseInfo = {
availableBlocks: 10,
totalOcrBlocks: 20,
availableDocuments: 3,
ocrServiceAvailable: true,
lastRun: null,
runs: []
};
describe('OcrTrainingCard — disabled states', () => {
it('disables button and shows hint when availableBlocks is 0', async () => {
render(OcrTrainingCard, { trainingInfo: { ...baseInfo, availableBlocks: 0 } });
const btn = page.getByRole('button', { name: /Training starten/i });
await expect.element(btn).toBeDisabled();
await expect
.element(page.getByText(/Mindestens 5 geprüfte Blöcke erforderlich/i))
.toBeInTheDocument();
});
it('disables button and shows hint when availableBlocks is less than 5', async () => {
render(OcrTrainingCard, { trainingInfo: { ...baseInfo, availableBlocks: 3 } });
const btn = page.getByRole('button', { name: /Training starten/i });
await expect.element(btn).toBeDisabled();
await expect.element(page.getByText(/Mindestens 5/i)).toBeInTheDocument();
});
it('disables button and shows service-down warning when ocrServiceAvailable is false', async () => {
render(OcrTrainingCard, { trainingInfo: { ...baseInfo, ocrServiceAvailable: false } });
const btn = page.getByRole('button', { name: /Training starten/i });
await expect.element(btn).toBeDisabled();
await expect.element(page.getByText(/OCR-Dienst ist nicht erreichbar/i)).toBeInTheDocument();
});
it('does not show service-down warning when blocks are insufficient', async () => {
// tooFewBlocks hint takes priority over serviceDown hint
render(OcrTrainingCard, {
trainingInfo: { ...baseInfo, availableBlocks: 2, ocrServiceAvailable: false }
});
await expect.element(page.getByText(/Mindestens 5/i)).toBeInTheDocument();
// serviceDown text should NOT appear because tooFewBlocks branch hides it
const serviceMsg = document.querySelector('.text-orange-600');
expect(serviceMsg).toBeNull();
});
});
describe('OcrTrainingCard — enabled state', () => {
it('enables button when availableBlocks >= 5 and service is up', async () => {
render(OcrTrainingCard, { trainingInfo: baseInfo });
const btn = page.getByRole('button', { name: /Training starten/i });
await expect.element(btn).not.toBeDisabled();
});
it('shows block count info text', async () => {
render(OcrTrainingCard, {
trainingInfo: { ...baseInfo, availableBlocks: 7, totalOcrBlocks: 15 }
});
await expect.element(page.getByText(/7/)).toBeInTheDocument();
await expect.element(page.getByText(/von 15 OCR-Blöcken/i)).toBeInTheDocument();
});
});
describe('OcrTrainingCard — success dismiss button', () => {
beforeEach(() => vi.useFakeTimers());
afterEach(() => {
vi.runAllTimers();
vi.useRealTimers();
});
it('dismiss button has 44×44px touch target (h-11 w-11)', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true }));
render(OcrTrainingCard, { trainingInfo: baseInfo });
const btn = page.getByRole('button', { name: /Training starten/i });
await btn.click();
const dismissBtn = page.getByRole('button', { name: /Schließen/i });
await expect.element(dismissBtn).toBeInTheDocument();
const el = await dismissBtn.element();
expect(el.classList.contains('h-11')).toBe(true);
expect(el.classList.contains('w-11')).toBe(true);
});
});
describe('OcrTrainingCard — in-flight state', () => {
it('shows "…" while POST is in-flight', async () => {
let resolveFetch!: (v: unknown) => void;
const pendingFetch = new Promise((resolve) => {
resolveFetch = resolve;
});
vi.stubGlobal('fetch', vi.fn().mockReturnValue(pendingFetch));
render(OcrTrainingCard, { trainingInfo: baseInfo });
const btn = page.getByRole('button', { name: /Training starten/i });
await btn.click();
// While fetch is still pending the button label becomes "…"
await expect.element(page.getByRole('button', { name: '…' })).toBeInTheDocument();
resolveFetch({ ok: false });
await expect
.element(page.getByRole('button', { name: /Training starten/i }))
.not.toBeDisabled();
});
});