openreader/compute/core/tests/control-plane/state-machine.test.ts
Richard R e1483ab80a refactor(worker): modularize worker entrypoint and add vitest infra
move worker startup logic from server.ts to runtime.ts for better modularity
add worker-loop-policy.ts for loop control abstraction
delete legacy unit tests under tests/unit/ related to compute worker and control-plane
add vitest config and test directories for compute-core and compute-worker
add vitest workflow for CI
update package.json scripts for new test commands and add vitest as dev dependency

BREAKING CHANGE: worker entrypoint is now compute/worker/src/runtime.ts instead of server.ts; legacy Playwright-based unit tests for compute worker are removed in favor of Vitest
2026-05-30 11:00:01 -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/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');
});
});