openreader/compute-worker/tests/compute/algorithms/whisper-alignment-mapping.test.ts
Richard R a56aaa2be9 Refactor PDF parsing and rendering logic; migrate to compute-worker protocol
- Removed deprecated functions related to document parsing and blob storage in blobstore.ts.
- Introduced new PDF rendering logic in pdf-preview-renderer.ts and pdf-preview-pdfjs-runtime.ts.
- Updated previews-render.ts to utilize the new PDF rendering functions.
- Refactored user-whisper-align-job.ts to use the compute-worker client for alignment requests.
- Enhanced artifact.ts and operation.ts to validate parsed PDF artifacts and resolve current PDF parses.
- Updated snapshot.ts to align with new worker operation types.
- Adjusted runtime-config.ts to check for compute-worker availability.
- Modified types in parsed-pdf.ts and tts.ts to reflect changes in the compute-worker protocol.
- Added unit tests for PDF artifact validation and compute-worker client contract.
- Removed obsolete pdf-op-key.vitest.spec.ts test file.
2026-06-12 13:43:33 -06:00

35 lines
1.5 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import {
mapWordsToSentenceOffsets,
} from '../../../src/compute';
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);
});
});