T-210: M2.3s — Implement MarketDataStore.macroAsOf, crossSection, QuestDB fx('1m'), and copilot 1m routing

This commit is contained in:
Studio 2026-12-04 15:23:16 -05:00
parent c60365237f
commit 5e255d7e30
8 changed files with 587 additions and 37 deletions

View file

@ -1,6 +1,11 @@
import { beforeEach, describe, expect, it, mock } from 'bun:test';
import { EventEmitter } from 'node:events';
import { QuestDBHealthMonitor } from '../src/health';
import { QuestDBInfluxWriter } from '../src/influx-writer';
import {
openQuestDbPgExecutor,
type QuestDbPgPoolLike,
} from '../src/pg-executor';
import { QuestDBQueryBuilder } from '../src/query-builder';
import {
applyM2IntradayDDL,
@ -571,4 +576,143 @@ describe('QuestDB', () => {
monitor.stopMonitoring();
});
});
describe('openQuestDbPgExecutor', () => {
it('registers pool error listener before SELECT 1, redacts credentials, and recovers on next query after idle disconnect', async () => {
const secret = 'qdb_idle_s3cr3t!';
const url = `postgresql://admin:${encodeURIComponent(secret)}@10.0.0.51:8812/qdb`;
class FakePool extends EventEmitter implements QuestDbPgPoolLike {
connectionEpoch = 1;
probeSawErrorListener = false;
ended = false;
readonly queries: Array<{ readonly sql: string; readonly epoch: number }> = [];
async query(sql: string): Promise<{ readonly rows: readonly unknown[] }> {
if (sql === 'SELECT 1') {
this.probeSawErrorListener = this.listenerCount('error') > 0;
this.queries.push({ sql, epoch: this.connectionEpoch });
return { rows: [{ '?column?': 1 }] };
}
this.queries.push({ sql, epoch: this.connectionEpoch });
return { rows: [{ ok: true, epoch: this.connectionEpoch }] };
}
async end(): Promise<void> {
this.ended = true;
}
}
const fakePool = new FakePool();
const observedErrors: Error[] = [];
const handle = await openQuestDbPgExecutor(
{ url },
{
createPool: () => fakePool,
onPoolError: err => {
observedErrors.push(err);
},
}
);
try {
// Listener must be attached before the startup probe runs.
expect(fakePool.probeSawErrorListener).toBe(true);
expect(fakePool.listenerCount('error')).toBeGreaterThan(0);
// Simulate QuestDB restarting while the connection opened by SELECT 1 is idle.
const idleErr = Object.assign(
new Error(`idle connection dropped for ${url} (password=${secret})`),
{ code: 'ECONNRESET' }
);
expect(() => fakePool.emit('error', idleErr)).not.toThrow();
// Diagnostic callback receives a credential-free Error with the safe error code.
expect(observedErrors).toHaveLength(1);
expect(observedErrors[0]?.message).not.toContain(secret);
expect(observedErrors[0]?.message).not.toContain(url);
expect(observedErrors[0]?.message).toContain('ECONNRESET');
expect((observedErrors[0] as Error & { code?: string }).code).toBe('ECONNRESET');
// Pool replaces the failed idle client and serves the next query cleanly.
fakePool.connectionEpoch = 2;
const result = await handle.executor.query('SELECT pair FROM fx_1m LIMIT 1');
expect(result.rows).toEqual([{ ok: true, epoch: 2 }]);
expect(fakePool.queries).toEqual([
{ sql: 'SELECT 1', epoch: 1 },
{ sql: 'SELECT pair FROM fx_1m LIMIT 1', epoch: 2 },
]);
} finally {
await handle.close();
await handle.close();
}
expect(fakePool.ended).toBe(true);
});
it('rejects conflicting URL SSL query parameters and explicit ssl before creating a pool', async () => {
const secret = 'qdb_ssl_s3cr3t!';
const base = `postgresql://admin:${encodeURIComponent(secret)}@10.0.0.51:8812/qdb`;
const conflictingCases: ReadonlyArray<readonly [string, boolean]> = [
[`${base}?sslmode=disable`, true],
[`${base}?SSLMODE=DISABLE`, true],
[`${base}?sslmode=require`, false],
[`${base}?sslmode=verify-ca`, false],
[`${base}?sslmode=verify-full`, false],
[`${base}?sslmode=no-verify`, false],
[`${base}?sslmode=prefer`, true],
[`${base}?sslmode=prefer`, false],
[`${base}?sslmode=allow`, true],
[`${base}?ssl=false`, true],
[`${base}?ssl=0`, true],
[`${base}?ssl=true`, false],
[`${base}?ssl=1`, false],
[`${base}?sslrootcert=/etc/ssl/ca.pem`, false],
];
let createPoolCalls = 0;
for (const [url, ssl] of conflictingCases) {
let caught: unknown;
try {
await openQuestDbPgExecutor(
{ url, ssl },
{
createPool: () => {
createPoolCalls += 1;
throw new Error('createPool must not be called on conflicting SSL config');
},
}
);
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(RangeError);
expect((caught as Error).message).toContain('SSL');
expect((caught as Error).message).not.toContain(secret);
expect((caught as Error).message).not.toContain(url);
}
expect(createPoolCalls).toBe(0);
// Compatible combinations proceed to pool creation.
const compatibleCases: ReadonlyArray<readonly [string, boolean]> = [
[base, true],
[`${base}?sslmode=require`, true],
[`${base}?sslmode=disable`, false],
[`${base}?ssl=true`, true],
[`${base}?ssl=false`, false],
];
for (const [url, ssl] of compatibleCases) {
const pool = Object.assign(new EventEmitter(), {
query: async () => ({ rows: [{ '?column?': 1 }] }),
end: async () => {},
}) as unknown as QuestDbPgPoolLike;
const handle = await openQuestDbPgExecutor(
{ url, ssl },
{ createPool: () => pool }
);
await handle.close();
}
});
});
});