openreader/compute-worker/tests/unit/key-and-progress.test.ts
Richard R 3d5dfd2a88 refactor(compute-worker): migrate compute logic to modular architecture
- Remove legacy compute-worker monolith under `src/compute/`, including PDF and Whisper inference, control-plane, and runtime orchestration
- Move PDF and Whisper inference logic to new `src/inference/` module
- Move control-plane, orchestrator, and state machine logic to `src/operations/`
- Move NATS/JetStream adapters to `src/infrastructure/`
- Move job orchestration, progress, and artifact persistence to `src/jobs/`
- Update imports throughout tests and main app to reference new module structure
- Update Dockerfile and scripts to use new asset paths
- Add new entrypoints: `src/api/app.ts` and `src/api/contracts.ts`
- Remove obsolete files and update `.gitignore` for new dev artifacts

This refactor modularizes the compute-worker, improving maintainability and separation of concerns. No functional changes to inference or orchestration logic.

BREAKING CHANGE: compute-worker internal APIs, directory structure, and imports have changed; downstream code must update imports and integration points.
2026-06-12 14:07:15 -06:00

52 lines
1.9 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import { hashOpKey, opIndexKvKey, opStateKvKey } from '../../src/infrastructure/nats-adapters';
import {
buildInferProgressForPageParsed,
buildInferProgressForPageStart,
} from '../../src/jobs/pdf-progress';
import { buildPdfOperationKey } from '../../src/api/operation-keys';
import { parsedPdfArtifactKey } from '../../src/storage/artifact-addressing';
describe('compute worker helpers', () => {
test('hash and kv key helpers are stable and deterministic', () => {
const hashA = hashOpKey('doc-123|layout|v1');
const hashB = hashOpKey('doc-123|layout|v1');
const hashC = hashOpKey('doc-123|layout|v2');
expect(hashA).toBe(hashB);
expect(hashA).toHaveLength(64);
expect(hashC).not.toBe(hashA);
expect(opIndexKvKey('doc-123|layout|v1')).toBe(`op_index.${hashA}`);
expect(opStateKvKey('op-1')).toBe('op_state.op-1');
});
test('progress helpers infer page counters correctly', () => {
expect(buildInferProgressForPageStart({ pageNumber: 1, totalPages: 12 })).toEqual({
totalPages: 12,
pagesParsed: 0,
currentPage: 1,
phase: 'infer',
});
expect(buildInferProgressForPageParsed({ pageNumber: 5, totalPages: 12 })).toEqual({
totalPages: 12,
pagesParsed: 5,
currentPage: 5,
phase: 'infer',
});
});
test('parser-version changes rotate worker-owned operation and artifact identities', () => {
const request = {
documentId: 'a'.repeat(64),
namespace: null,
documentObjectKey: `openreader/${'a'.repeat(64)}.pdf`,
};
expect(buildPdfOperationKey(request, 'parser-v1'))
.not.toBe(buildPdfOperationKey(request, 'parser-v2'));
expect(parsedPdfArtifactKey({ ...request, prefix: 'openreader', parserVersion: 'parser-v1' }))
.not.toBe(parsedPdfArtifactKey({ ...request, prefix: 'openreader', parserVersion: 'parser-v2' }));
});
});