openreader/tests/unit/compute-worker-client-contract.vitest.spec.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

58 lines
2 KiB
TypeScript

import { afterEach, describe, expect, test } from 'vitest';
import { createComputeWorkerApp, type ComputeWorkerApp } from '../../compute-worker/src/api/app';
import { FakeControlPlane } from '../../compute-worker/tests/fixtures/fake-control-plane';
import { ComputeWorkerClient } from '../../src/lib/server/compute-worker/client';
describe('ComputeWorkerClient contract', () => {
let runtime: ComputeWorkerApp | null = null;
afterEach(async () => {
await runtime?.close();
runtime = null;
});
test('exercises the real Fastify API through the app-owned HTTP client', async () => {
process.env.NATS_URL = 'nats://127.0.0.1:4222';
const fake = new FakeControlPlane();
runtime = await createComputeWorkerApp({
host: '127.0.0.1',
port: 0,
workerToken: 'contract-token',
disableWorkers: true,
routeDeps: fake.deps,
});
await runtime.start();
const address = runtime.app.server.address();
if (!address || typeof address === 'string') {
throw new Error('Compute worker did not bind a TCP port');
}
const client = new ComputeWorkerClient({
baseUrl: `http://127.0.0.1:${address.port}`,
token: 'contract-token',
});
const documentId = 'b'.repeat(64);
const request = {
documentId,
namespace: null,
documentObjectKey: `openreader/${documentId}.pdf`,
};
const created = await client.createPdfLayoutOperation(request);
expect(created).toMatchObject({
subject: { kind: 'pdf_layout', documentId, namespace: null },
status: 'queued',
});
expect(created).not.toHaveProperty('opKey');
expect(created).not.toHaveProperty('jobId');
await expect(client.getOperation(created.opId)).resolves.toMatchObject({
opId: created.opId,
subject: { kind: 'pdf_layout', documentId, namespace: null },
});
await expect(client.resolvePdfLayout(request)).resolves.toMatchObject({
artifact: null,
operation: { opId: created.opId },
});
});
});