62 lines
2.6 KiB
TypeScript
62 lines
2.6 KiB
TypeScript
import { describe, expect, it, mock } from 'bun:test';
|
|
import { getLogger } from '@stock-bot/logger';
|
|
import type { IServiceContainer, QueueManager } from '@stock-bot/services';
|
|
import { createQueueRoutes } from '../../apps/stock/data-ingestion/src/routes/queue.routes';
|
|
import { PipelineService } from '../../apps/stock/web-api/src/services/pipeline.service';
|
|
|
|
function fixture() {
|
|
const job = { id: 'job-1' };
|
|
const queue = {
|
|
add: mock(async () => job),
|
|
waitForJob: mock(async () => ({ exchanges: 12 })),
|
|
};
|
|
const manager = {
|
|
getQueue: mock(() => queue),
|
|
getGlobalStats: mock(async () => ({ totalJobs: 3 })),
|
|
};
|
|
const container: IServiceContainer = {
|
|
logger: getLogger('service-consumer-test'),
|
|
queueManager: manager as unknown as QueueManager,
|
|
};
|
|
return { container, manager, queue, job };
|
|
}
|
|
|
|
describe('service consumers', () => {
|
|
it('the web API submits work to the pipeline queue through the real adapter API', async () => {
|
|
const { container, manager, queue } = fixture();
|
|
const result = await new PipelineService(container).syncQMSymbols();
|
|
expect(result.success).toBe(true);
|
|
expect(result.jobId).toBe('job-1');
|
|
expect(manager.getQueue).toHaveBeenCalledWith('{data-pipeline_symbols}');
|
|
expect(queue.add).toHaveBeenCalledWith('sync-qm-symbols', {
|
|
handler: 'symbols',
|
|
operation: 'sync-qm-symbols',
|
|
payload: {},
|
|
});
|
|
});
|
|
|
|
it('statistics wait for completion through the queue-owned events connection', async () => {
|
|
const { container, manager, queue, job } = fixture();
|
|
const result = await new PipelineService(container).getExchangeStats();
|
|
expect(result).toEqual({ success: true, data: { exchanges: 12 } });
|
|
expect(manager.getQueue).toHaveBeenCalledWith('{data-pipeline_exchanges}');
|
|
expect(queue.waitForJob).toHaveBeenCalledWith(job);
|
|
});
|
|
|
|
it('queue monitoring uses queueManager without requiring an individual queue', async () => {
|
|
const { container, manager } = fixture();
|
|
const response = await createQueueRoutes(container).request('http://localhost/status');
|
|
expect(response.status).toBe(200);
|
|
expect(manager.getGlobalStats).toHaveBeenCalledTimes(1);
|
|
expect(await response.json()).toMatchObject({ status: 'success', data: { totalJobs: 3 } });
|
|
});
|
|
|
|
it('a missing queue manager produces an explicit unavailable result', async () => {
|
|
const { container } = fixture();
|
|
container.queueManager = null;
|
|
expect(await new PipelineService(container).syncQMSymbols()).toEqual({
|
|
success: false,
|
|
error: 'Queue manager not available',
|
|
});
|
|
});
|
|
});
|