142 lines
5.2 KiB
TypeScript
142 lines
5.2 KiB
TypeScript
import { existsSync, readFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { describe, expect, it } from 'bun:test';
|
|
import { Hono } from 'hono';
|
|
import type { IServiceContainer } from '@stock-bot/handlers';
|
|
import { ExchangesHandler } from '../../apps/stock/data-pipeline/src/handlers/exchanges/exchanges.handler';
|
|
import { createPipelineRoutes } from '../../apps/stock/web-api/src/routes/pipeline.routes';
|
|
import { PipelineService } from '../../apps/stock/web-api/src/services/pipeline.service';
|
|
|
|
const repository = fileURLToPath(new URL('../../', import.meta.url));
|
|
|
|
function scanSourceFiles(relativeDir: string): string[] {
|
|
const dir = join(repository, relativeDir);
|
|
return [...new Bun.Glob('**/*.{ts,tsx}').scanSync({ cwd: dir })].map(file => join(dir, file));
|
|
}
|
|
|
|
describe('legacy sync-ib-exchanges removal', () => {
|
|
it('legacy MongoDB-writing sync-ib-exchanges operations file does not exist', () => {
|
|
const legacyFile = join(
|
|
repository,
|
|
'apps/stock/data-pipeline/src/handlers/exchanges/operations/sync-ib-exchanges.operations.ts'
|
|
);
|
|
expect(existsSync(legacyFile)).toBe(false);
|
|
});
|
|
|
|
it('no source file in data-pipeline, web-api, or web-app references sync-ib-exchanges or syncIBExchanges', () => {
|
|
const trees = [
|
|
'apps/stock/data-pipeline/src',
|
|
'apps/stock/web-api/src',
|
|
'apps/stock/web-app/src',
|
|
];
|
|
const forbiddenSymbols = ['sync-ib-exchanges', 'syncIBExchanges'];
|
|
const hits: string[] = [];
|
|
|
|
for (const tree of trees) {
|
|
for (const file of scanSourceFiles(tree)) {
|
|
const content = readFileSync(file, 'utf8');
|
|
for (const token of forbiddenSymbols) {
|
|
if (content.includes(token)) {
|
|
hits.push(`${file}: ${token}`);
|
|
}
|
|
}
|
|
if (tree !== 'apps/stock/data-pipeline/src' && content.includes('/provider-mappings/ib')) {
|
|
hits.push(`${file}: /provider-mappings/ib`);
|
|
}
|
|
}
|
|
}
|
|
|
|
expect(hits).toEqual([]);
|
|
});
|
|
|
|
it('POST /api/pipeline/provider-mappings/ib returns 404 without enqueueing jobs while QM mapping route stays active', async () => {
|
|
const jobs: unknown[] = [];
|
|
const services = {
|
|
queueManager: {
|
|
getQueue() {
|
|
return {
|
|
async add(_name: string, data: unknown) {
|
|
jobs.push(data);
|
|
return { id: 'fixture' };
|
|
},
|
|
};
|
|
},
|
|
},
|
|
} as unknown as IServiceContainer;
|
|
|
|
const app = new Hono();
|
|
app.route('/api/pipeline', createPipelineRoutes(services));
|
|
|
|
const removed = await app.request('/api/pipeline/provider-mappings/ib', { method: 'POST' });
|
|
expect(removed.status).toBe(404);
|
|
expect(jobs).toHaveLength(0);
|
|
|
|
const active = await app.request('/api/pipeline/provider-mappings/qm', { method: 'POST' });
|
|
expect(active.status).toBe(200);
|
|
expect(jobs).toEqual([
|
|
{ handler: 'exchanges', operation: 'sync-qm-provider-mappings', payload: {} },
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('exchanges handler after sync-ib-exchanges removal', () => {
|
|
it('registers exactly the remaining exchange operations and no IB operation', () => {
|
|
const metadata = ExchangesHandler.extractMetadata();
|
|
|
|
expect(metadata?.name).toBe('exchanges');
|
|
// @Operation + @ScheduledOperation on one method registers the name twice, so compare the set.
|
|
expect([...new Set(metadata?.operations ?? [])].sort()).toEqual([
|
|
'clear-postgresql-data',
|
|
'get-exchange-stats',
|
|
'get-provider-mapping-stats',
|
|
'sync-all-exchanges',
|
|
'sync-qm-exchanges',
|
|
'sync-qm-provider-mappings',
|
|
]);
|
|
});
|
|
|
|
it('keeps the other nightly schedules but drops the 03:00 IB exchange schedule', () => {
|
|
const metadata = ExchangesHandler.extractMetadata();
|
|
|
|
const schedules = (metadata?.scheduledJobs ?? [])
|
|
.map(job => ({ operation: job.operation, cronPattern: job.cronPattern }))
|
|
.sort((a, b) => a.operation.localeCompare(b.operation));
|
|
|
|
expect(schedules).toEqual([
|
|
{ operation: 'sync-all-exchanges', cronPattern: '0 0 * * 0' },
|
|
{ operation: 'sync-qm-exchanges', cronPattern: '0 1 * * *' },
|
|
{ operation: 'sync-qm-provider-mappings', cronPattern: '0 3 * * *' },
|
|
]);
|
|
});
|
|
|
|
it('rejects a leftover queued sync-ib-exchanges job without touching MongoDB', async () => {
|
|
const mongoCalls: string[] = [];
|
|
const mongodb = new Proxy(
|
|
{},
|
|
{
|
|
get(_target, property) {
|
|
mongoCalls.push(String(property));
|
|
return () => {
|
|
throw new Error('MongoDB must not be used');
|
|
};
|
|
},
|
|
}
|
|
);
|
|
const handler = new ExchangesHandler({ mongodb } as unknown as IServiceContainer);
|
|
|
|
await expect(
|
|
handler.execute('sync-ib-exchanges', {}, { type: 'queue', metadata: {} } as never)
|
|
).rejects.toThrow('Unknown operation: sync-ib-exchanges');
|
|
expect(mongoCalls).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('web-api pipeline service after sync-ib-exchanges removal', () => {
|
|
it('no longer exposes a method that enqueues sync-ib-exchanges', () => {
|
|
const service = new PipelineService({} as IServiceContainer);
|
|
|
|
expect('syncIBExchanges' in service).toBe(false);
|
|
expect(typeof service.syncQMProviderMappings).toBe('function');
|
|
});
|
|
});
|