feat(ocr): extract translateOcrProgress with ANALYZING_PAGE and DONE:skipped support
Move translateOcrProgress from page.svelte to a testable module. Return structured result with currentPage/totalPages/skippedPages for the progress bar. Add ANALYZING_PAGE and DONE with skipped pages parsing. Add i18n keys for de/en/es. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
78
frontend/src/lib/ocr/translateOcrProgress.spec.ts
Normal file
78
frontend/src/lib/ocr/translateOcrProgress.spec.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
|
||||
vi.mock('$lib/paraglide/messages.js', () => ({
|
||||
m: {
|
||||
ocr_progress_heading: () => 'OCR-Analyse',
|
||||
ocr_status_preparing: () => 'Dokument wird vorbereitet…',
|
||||
ocr_status_loading: () => 'Lade Modell und Dokument…',
|
||||
ocr_status_analyzing: () => 'OCR-Analyse läuft…',
|
||||
ocr_status_creating_blocks: ({ count }: { count: string }) => `${count} Textblöcke erkannt`,
|
||||
ocr_status_done_blocks: ({ count }: { count: string }) => `${count} Blöcke erstellt`,
|
||||
ocr_status_done_skipped: ({ count, skipped }: { count: string; skipped: string }) =>
|
||||
`${count} Blöcke erstellt, ${skipped} Seite(n) übersprungen`,
|
||||
ocr_status_analyzing_page: ({ current, total }: { current: string; total: string }) =>
|
||||
`Seite ${current} von ${total} wird analysiert…`,
|
||||
ocr_status_error: () => 'OCR fehlgeschlagen'
|
||||
}
|
||||
}));
|
||||
|
||||
import { translateOcrProgress } from './translateOcrProgress';
|
||||
|
||||
describe('translateOcrProgress', () => {
|
||||
it('returns heading for empty code', () => {
|
||||
const result = translateOcrProgress('');
|
||||
expect(result.message).toBe('OCR-Analyse');
|
||||
});
|
||||
|
||||
it('translates PREPARING', () => {
|
||||
const result = translateOcrProgress('PREPARING');
|
||||
expect(result.message).toBe('Dokument wird vorbereitet…');
|
||||
expect(result.currentPage).toBeUndefined();
|
||||
expect(result.totalPages).toBeUndefined();
|
||||
});
|
||||
|
||||
it('translates LOADING', () => {
|
||||
expect(translateOcrProgress('LOADING').message).toBe('Lade Modell und Dokument…');
|
||||
});
|
||||
|
||||
it('translates ANALYZING', () => {
|
||||
expect(translateOcrProgress('ANALYZING').message).toBe('OCR-Analyse läuft…');
|
||||
});
|
||||
|
||||
it('translates CREATING_BLOCKS with count', () => {
|
||||
expect(translateOcrProgress('CREATING_BLOCKS:42').message).toBe('42 Textblöcke erkannt');
|
||||
});
|
||||
|
||||
it('translates DONE without skipped pages', () => {
|
||||
const result = translateOcrProgress('DONE:15');
|
||||
expect(result.message).toBe('15 Blöcke erstellt');
|
||||
expect(result.skippedPages).toBeUndefined();
|
||||
});
|
||||
|
||||
it('translates DONE with zero skipped', () => {
|
||||
const result = translateOcrProgress('DONE:15:0');
|
||||
expect(result.message).toBe('15 Blöcke erstellt');
|
||||
expect(result.skippedPages).toBeUndefined();
|
||||
});
|
||||
|
||||
it('translates DONE with skipped pages', () => {
|
||||
const result = translateOcrProgress('DONE:12:2');
|
||||
expect(result.message).toBe('12 Blöcke erstellt, 2 Seite(n) übersprungen');
|
||||
expect(result.skippedPages).toBe(2);
|
||||
});
|
||||
|
||||
it('translates ANALYZING_PAGE with current, total, and blocks', () => {
|
||||
const result = translateOcrProgress('ANALYZING_PAGE:2:5:10');
|
||||
expect(result.message).toBe('Seite 2 von 5 wird analysiert…');
|
||||
expect(result.currentPage).toBe(2);
|
||||
expect(result.totalPages).toBe(5);
|
||||
});
|
||||
|
||||
it('translates ERROR', () => {
|
||||
expect(translateOcrProgress('ERROR').message).toBe('OCR fehlgeschlagen');
|
||||
});
|
||||
|
||||
it('returns raw code for unknown codes', () => {
|
||||
expect(translateOcrProgress('UNKNOWN_CODE').message).toBe('UNKNOWN_CODE');
|
||||
});
|
||||
});
|
||||
56
frontend/src/lib/ocr/translateOcrProgress.ts
Normal file
56
frontend/src/lib/ocr/translateOcrProgress.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { m } from '$lib/paraglide/messages.js';
|
||||
|
||||
export interface OcrProgressResult {
|
||||
message: string;
|
||||
currentPage?: number;
|
||||
totalPages?: number;
|
||||
skippedPages?: number;
|
||||
}
|
||||
|
||||
export function translateOcrProgress(code: string): OcrProgressResult {
|
||||
if (!code) return { message: m.ocr_progress_heading() };
|
||||
|
||||
const parts = code.split(':');
|
||||
const key = parts[0];
|
||||
|
||||
switch (key) {
|
||||
case 'PREPARING':
|
||||
return { message: m.ocr_status_preparing() };
|
||||
case 'LOADING':
|
||||
return { message: m.ocr_status_loading() };
|
||||
case 'ANALYZING':
|
||||
return { message: m.ocr_status_analyzing() };
|
||||
case 'CREATING_BLOCKS':
|
||||
return { message: m.ocr_status_creating_blocks({ count: parts[1] ?? '0' }) };
|
||||
case 'DONE': {
|
||||
const count = parts[1] ?? '0';
|
||||
const skipped = parts[2] ? parseInt(parts[2], 10) : 0;
|
||||
if (skipped > 0) {
|
||||
return {
|
||||
message: m.ocr_status_done_skipped({
|
||||
count,
|
||||
skipped: String(skipped)
|
||||
}),
|
||||
skippedPages: skipped
|
||||
};
|
||||
}
|
||||
return { message: m.ocr_status_done_blocks({ count }) };
|
||||
}
|
||||
case 'ANALYZING_PAGE': {
|
||||
const current = parseInt(parts[1] ?? '0', 10);
|
||||
const total = parseInt(parts[2] ?? '0', 10);
|
||||
return {
|
||||
message: m.ocr_status_analyzing_page({
|
||||
current: String(current),
|
||||
total: String(total)
|
||||
}),
|
||||
currentPage: current,
|
||||
totalPages: total
|
||||
};
|
||||
}
|
||||
case 'ERROR':
|
||||
return { message: m.ocr_status_error() };
|
||||
default:
|
||||
return { message: code };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user