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

76 lines
2 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import type { WorkerOperationState } from '../../../src/api/contracts';
import {
explainReplacementReason,
isInflightStatus,
isTerminalStatus,
shouldReuseExistingOperation,
} from '../../../src/operations/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');
});
});