openreader/compute-worker/tests/compute/control-plane/state-machine.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

76 lines
2 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import type { WorkerOperationState } from '../../../src/compute/api-contracts';
import {
explainReplacementReason,
isInflightStatus,
isTerminalStatus,
shouldReuseExistingOperation,
} from '../../../src/compute/control-plane/state-machine';
function runningState(overrides: Partial<WorkerOperationState> = {}): WorkerOperationState {
return {
opId: 'op-1',
opKey: 'key-1',
kind: 'pdf_layout',
jobId: 'job-1',
status: 'running',
queuedAt: 1_000,
updatedAt: 2_000,
...overrides,
};
}
describe('state-machine decisions', () => {
test('identifies terminal and inflight states', () => {
expect(isTerminalStatus('succeeded')).toBe(true);
expect(isTerminalStatus('failed')).toBe(true);
expect(isTerminalStatus('queued')).toBe(false);
expect(isInflightStatus('queued')).toBe(true);
expect(isInflightStatus('running')).toBe(true);
expect(isInflightStatus('failed')).toBe(false);
});
test('reuses fresh inflight operation and rejects stale inflight operation', () => {
const current = runningState();
expect(shouldReuseExistingOperation({
current,
requestKind: 'pdf_layout',
now: 2_900,
opStaleMs: 1_000,
})).toBe(true);
expect(shouldReuseExistingOperation({
current,
requestKind: 'pdf_layout',
now: 3_100,
opStaleMs: 1_000,
})).toBe(false);
expect(explainReplacementReason({
current,
requestKind: 'pdf_layout',
now: 3_100,
opStaleMs: 1_000,
})).toBe('stale_running');
});
test('never reuses kind-mismatched operation', () => {
const current = runningState({ kind: 'whisper_align' });
expect(shouldReuseExistingOperation({
current,
requestKind: 'pdf_layout',
now: 2_100,
opStaleMs: 10_000,
})).toBe(false);
expect(explainReplacementReason({
current,
requestKind: 'pdf_layout',
now: 2_100,
opStaleMs: 10_000,
})).toBe('kind_mismatch');
});
});