openreader/compute-worker/tests/compute/algorithms/pdf-merge-text-with-regions.test.ts
Richard R 6e69b48103 chore(worker): restructure inference modules and centralize config
- Remove legacy inference and platform files from `src/inference/` and migrate responsibilities to new modular locations under `src/infrastructure/`, `src/inference/pdf/`, and `src/inference/whisper/`
- Consolidate environment/config logic into `src/infrastructure/config.ts`
- Move docstore and ffmpeg platform utilities to `src/infrastructure/platform.ts`
- Refactor PDF and Whisper inference code to use new document layout, layout model, and timestamp utilities
- Update API contracts to define and export types and constants previously scattered in inference/types
- Adjust all imports in jobs, storage, and tests to reference new module structure and type locations
- Inline parser version and encoding logic into API contracts
- Remove obsolete files and update test fixtures for new type locations

This update improves codebase modularity, maintainability, and separation of concerns. No changes to inference or orchestration functionality.

BREAKING CHANGE: inference module structure, type imports, and config utilities have changed; downstream code must update imports and integration points.
2026-06-12 14:16:13 -06:00

52 lines
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 { describe, expect, test } from 'vitest';
import { mergeTextWithRegions } from '../../../src/inference/pdf/document-layout';
describe('mergeTextWithRegions', () => {
test('assigns text items to containing regions by centroid and joins in reading order', () => {
const regions = [
{ bbox: [0, 0, 100, 50] as [number, number, number, number], label: 'text' as const },
{ bbox: [0, 50, 100, 100] as [number, number, number, number], label: 'figure_title' as const },
];
const textItems = [
{ text: 'world', x: 40, y: 20, width: 20, height: 8 },
{ text: 'hello', x: 10, y: 20, width: 20, height: 8 },
{ text: 'Figure', x: 10, y: 70, width: 24, height: 8 },
{ text: '1.2', x: 40, y: 70, width: 10, height: 8 },
];
const merged = mergeTextWithRegions(regions, textItems);
expect(merged).toHaveLength(2);
expect(merged[0].text).toBe('hello world');
expect(merged[1].text).toBe('Figure 1.2');
});
test('drops text whose centroid is outside every region', () => {
const regions = [
{ bbox: [0, 0, 50, 50] as [number, number, number, number], label: 'text' as const },
];
const textItems = [
{ text: 'inside', x: 10, y: 10, width: 10, height: 8 },
{ text: 'outside', x: 80, y: 80, width: 12, height: 8 },
];
const merged = mergeTextWithRegions(regions, textItems);
expect(merged).toHaveLength(1);
expect(merged[0].text).toBe('inside');
});
test('keeps decorative drop caps attached to the same line when boxes overlap vertically', () => {
const regions = [
{ bbox: [0, 0, 200, 120] as [number, number, number, number], label: 'text' as const },
];
const textItems = [
{ text: 'I', x: 0, y: 10, width: 12, height: 60 },
{ text: 'ts funny,', x: 12, y: 40, width: 50, height: 12 },
];
const merged = mergeTextWithRegions(regions, textItems);
expect(merged).toHaveLength(1);
expect(merged[0].text).toBe('Its funny,');
});
});