91 lines
3.3 KiB
TypeScript
91 lines
3.3 KiB
TypeScript
/** Read-only aggregation regression; no collections, source records or credentials are written. */
|
|
import { describe, expect, test } from 'bun:test';
|
|
import { Decimal128, Double, Int32, Long, MongoClient } from 'mongodb';
|
|
import type { MongoDBClient } from '@stock-bot/services';
|
|
import { storedSourceCoverage } from '../../apps/stock/web-api/src/services/stored-source-inspection';
|
|
|
|
const url = process.env.SOURCE_TEST_MONGODB_URL;
|
|
describe.skipIf(!url)('stored source BSON timestamp coverage on MongoDB', () => {
|
|
test('coverage agrees with the strict JS reader for real BSON types and time bounds', async () => {
|
|
if (!url || !new URL(url).pathname.startsWith('/stock_bot_safety')) {
|
|
throw new Error('Explicit stock_bot_safety MongoDB test URL required');
|
|
}
|
|
const client = new MongoClient(url, { serverSelectionTimeoutMS: 5000 });
|
|
try {
|
|
await client.connect();
|
|
const good = [new Int32(1727789400), new Double(1727789700), Long.fromNumber(1727790000)];
|
|
const bad = [
|
|
Decimal128.fromString('1727789400'),
|
|
null,
|
|
'1727789400',
|
|
new Double(1727789400.5),
|
|
new Double(-1),
|
|
Long.fromString('8640000000001'),
|
|
new Double(NaN),
|
|
new Double(Infinity),
|
|
];
|
|
// A collection-free $documents source runs the exact production aggregation on
|
|
// actual BSON values. The adapter seam substitutes only where records originate.
|
|
const store: Pick<MongoDBClient, 'aggregate' | 'find'> = {
|
|
aggregate: async <T extends Record<string, unknown>>(
|
|
collection: string,
|
|
pipeline: Record<string, unknown>[]
|
|
) => {
|
|
const docs =
|
|
collection === 'eodIntraday5M'
|
|
? [
|
|
...good.map(timestamp => ({ eodSearchCode: 'VALID.US', timestamp })),
|
|
...bad.map(timestamp => ({ eodSearchCode: 'INVALID.US', timestamp })),
|
|
]
|
|
: [];
|
|
return client
|
|
.db()
|
|
.aggregate<T>([{ $documents: docs }, ...pipeline])
|
|
.toArray();
|
|
},
|
|
find: async () => {
|
|
throw new Error('Coverage must not query source rows');
|
|
},
|
|
};
|
|
const signal = new AbortController().signal;
|
|
const invalid = await storedSourceCoverage(
|
|
store,
|
|
{ instrumentId: 'INVALID.US', interval: '5m' },
|
|
signal
|
|
);
|
|
expect(invalid.series).toEqual([
|
|
{
|
|
instrumentId: 'INVALID.US',
|
|
interval: '5m',
|
|
rowCount: bad.length,
|
|
firstTimestamp: null,
|
|
lastTimestamp: null,
|
|
invalidTimestampRows: bad.length,
|
|
},
|
|
]);
|
|
const valid = await storedSourceCoverage(
|
|
store,
|
|
{ instrumentId: 'VALID.US', interval: '5m' },
|
|
signal
|
|
);
|
|
expect(valid.series).toEqual([
|
|
{
|
|
instrumentId: 'VALID.US',
|
|
interval: '5m',
|
|
rowCount: good.length,
|
|
firstTimestamp: 1727789400000,
|
|
lastTimestamp: 1727790000000,
|
|
invalidTimestampRows: 0,
|
|
},
|
|
]);
|
|
const empty = await storedSourceCoverage(
|
|
store,
|
|
{ instrumentId: 'MISSING.US', interval: '5m' },
|
|
signal
|
|
);
|
|
expect(empty.series).toEqual([]);
|
|
} finally {
|
|
await client.close();
|
|
}
|
|
});
|
|
});
|