openreader/compute-worker/tests/compute/algorithms/whisper-alignment-mapping.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

35 lines
1.6 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import {
mapWordsToSentenceOffsets,
} from '../../../src/inference/whisper/timestamps';
describe('whisper alignment mapping', () => {
test('maps words to sentence offsets with punctuation and repeated spaces', () => {
const aligned = mapWordsToSentenceOffsets('Hello, world again.', [
{ word: 'Hello', start: 0, end: 0.25 },
{ word: 'world', start: 0.25, end: 0.5 },
{ word: 'again', start: 0.5, end: 1.0 },
]);
expect(aligned.words).toHaveLength(3);
expect(aligned.words[0].charStart).toBe(0);
expect(aligned.words[0].charEnd).toBe(5);
expect(aligned.words[1].charStart).toBeGreaterThan(aligned.words[0].charEnd);
expect(aligned.words[2].charStart).toBeGreaterThan(aligned.words[1].charEnd);
expect(aligned.words[2].charEnd).toBeLessThanOrEqual('Hello, world again.'.length);
});
test('joins line-break hyphenation across unicode letters', () => {
// "Über-\n mensch" must normalize to "Übermensch" so the offsets match the
// client char map. This only works with the unicode-aware hyphen regex that
// is kept in lock-step across nlp.ts / alignment-map.ts / highlight-char-map.ts.
const aligned = mapWordsToSentenceOffsets('Über-\n mensch walks', [
{ word: 'Übermensch', start: 0, end: 0.5 },
{ word: 'walks', start: 0.5, end: 1.0 },
]);
expect(aligned.words[0].charStart).toBe(0);
expect(aligned.words[0].charEnd).toBe('Übermensch'.length);
expect(aligned.words[1].charStart).toBeGreaterThanOrEqual(aligned.words[0].charEnd);
});
});