T-117: M1.2ag — data-pipeline: read-only MongoDB → concatenated raw-BSON file exporter for the collections curate-etl consumes (raw: true cursor, byte pass-through, atomic publish) with tests and local benchmark

This commit is contained in:
Studio 2026-11-29 06:09:51 -05:00
parent c0afe5e3f0
commit 0962f66cc3
9 changed files with 2436 additions and 1 deletions

View file

@ -1,5 +1,5 @@
import { describe, expect, mock, test } from 'bun:test';
import { MongoClient } from 'mongodb';
import { BSON, MongoClient } from 'mongodb';
import { getLogger, setLoggerConfig } from '@stock-bot/logger';
import { MongoDBClient } from '@stock-bot/mongodb';
@ -1196,4 +1196,82 @@ describe('MongoDBClient read-only mode (offline)', () => {
expect(collectionStub.insertOne).toHaveBeenCalledTimes(2);
expect(dbStub.createCollection).toHaveBeenCalledTimes(1);
});
test('forwards find(filter, { raw: true, batchSize, limit }) through read-only collection/cursor and yields raw BSON buffers untouched', async () => {
const logger = getLogger('mongodb-read-only-raw-find-test');
const rawFrames = [
Buffer.from(BSON.serialize({ eodSearchCode: 'AAPL.US', date: '2025-01-02', close: 250.5 })),
Buffer.from(BSON.serialize({ eodSearchCode: 'RY.TO', date: '2025-01-02', close: 175.25 })),
];
const closeMock = mock(async () => {});
const rawCursorStub = {
close: closeMock,
[Symbol.asyncIterator]: async function* () {
for (const frame of rawFrames) {
yield frame;
}
},
};
const findCalls: Array<{ filter: unknown; options: unknown }> = [];
const collectionStub = {
collectionName: 'eodPrices',
dbName: 'stock',
namespace: 'stock.eodPrices',
find: mock((filter: unknown, options: unknown) => {
findCalls.push({ filter, options });
return rawCursorStub;
}),
insertOne: mock(async () => ({ insertedId: 'id-1' })),
drop: mock(async () => true),
};
const dbStub = {
databaseName: 'stock',
namespace: 'stock',
collection: mock(() => collectionStub),
};
const client = new MongoDBClient(
{ host: '127.0.0.1', port: 1, database: 'stock', readOnly: true },
logger
);
Object.assign(client, {
client: { db: mock(() => dbStub) },
db: dbStub,
isConnected: true,
});
const collection = client.getCollection('eodPrices');
const cursor = collection.find(
{ eodSearchCode: 'CAD.FOREX' },
{ raw: true, batchSize: 128, limit: 2 }
);
expect(findCalls).toEqual([
{
filter: { eodSearchCode: 'CAD.FOREX' },
options: { raw: true, batchSize: 128, limit: 2 },
},
]);
const yielded: unknown[] = [];
try {
for await (const frame of cursor) {
yielded.push(frame);
}
} finally {
await cursor.close();
}
expect(yielded).toHaveLength(2);
expect(yielded[0]).toBe(rawFrames[0]);
expect(yielded[1]).toBe(rawFrames[1]);
expect(yielded[0]).toBeInstanceOf(Uint8Array);
expect(closeMock).toHaveBeenCalledTimes(1);
// Write and DDL guards remain intact on the same read-only collection wrapper
expect(() => collection.insertOne({ a: 1 })).toThrow(/read-only mode/);
expect(() => collection.drop()).toThrow(/read-only mode/);
expect(collectionStub.insertOne).not.toHaveBeenCalled();
expect(collectionStub.drop).not.toHaveBeenCalled();
});
});