90 lines
3.3 KiB
TypeScript
90 lines
3.3 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import { M2_INTRADAY_TABLE_NAMES } from '@stock-bot/questdb';
|
|
import { resolveQuestDbInitM2Config, runQuestDbInitM2 } from '../questdb-init-m2';
|
|
|
|
describe('scripts/questdb-init-m2', () => {
|
|
test('rejects conflicting URL SSL settings before opening a QuestDB connection', async () => {
|
|
const secret = 'ddl_init_s3cr3t!';
|
|
const baseUrl = `postgresql://admin:${encodeURIComponent(secret)}@10.0.0.51:8812/qdb`;
|
|
|
|
const conflictingEnvs: Array<Record<string, string>> = [
|
|
{ QUESTDB_URL: `${baseUrl}?sslmode=no-verify`, QUESTDB_SSL: 'true' },
|
|
{ QUESTDB_URL: `${baseUrl}?sslmode=disable`, QUESTDB_SSL: 'true' },
|
|
{ QUESTDB_URL: `${baseUrl}?sslmode=disable`, QUESTDB_SSL: 'false' },
|
|
{ RESEARCH_MCP_QUESTDB_URL: `${baseUrl}?sslmode=disable`, RESEARCH_MCP_QUESTDB_SSL: 'true' },
|
|
{ QUESTDB_URL: `${baseUrl}?sslmode=require`, QUESTDB_SSL: 'true' },
|
|
{ QUESTDB_URL: `${baseUrl}?sslmode=require`, QUESTDB_SSL: 'false' },
|
|
{ QUESTDB_URL: `${baseUrl}?ssl=false`, QUESTDB_SSL: 'true' },
|
|
{ QUESTDB_URL: `${baseUrl}?ssl=false`, QUESTDB_SSL: 'false' },
|
|
{ QUESTDB_URL: `${baseUrl}?sslmode=prefer`, QUESTDB_SSL: 'true' },
|
|
{ QUESTDB_URL: `${baseUrl}?uselibpqcompat=true`, QUESTDB_SSL: 'true' },
|
|
];
|
|
|
|
let openCalls = 0;
|
|
for (const env of conflictingEnvs) {
|
|
let caught: unknown;
|
|
try {
|
|
await runQuestDbInitM2(env, async () => {
|
|
openCalls += 1;
|
|
throw new Error('openExecutor 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(baseUrl);
|
|
}
|
|
expect(openCalls).toBe(0);
|
|
});
|
|
|
|
test('rejects invalid SSL flag values and accepts single-source-of-truth URL or flag TLS configs', async () => {
|
|
expect(() =>
|
|
resolveQuestDbInitM2Config({
|
|
QUESTDB_HOST: '10.0.0.51',
|
|
QUESTDB_SSL: 'maybe',
|
|
})
|
|
).toThrow(RangeError);
|
|
|
|
const urlOnlyTls = resolveQuestDbInitM2Config({
|
|
QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require',
|
|
});
|
|
expect(urlOnlyTls).toEqual({
|
|
url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require',
|
|
});
|
|
|
|
const flagOnlyTls = resolveQuestDbInitM2Config({
|
|
QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?application_name=init-m2',
|
|
QUESTDB_SSL: 'true',
|
|
});
|
|
expect(flagOnlyTls).toEqual({
|
|
url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?application_name=init-m2',
|
|
ssl: true,
|
|
});
|
|
|
|
const executed: string[] = [];
|
|
let closed = false;
|
|
const result = await runQuestDbInitM2(
|
|
{
|
|
QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb',
|
|
QUESTDB_SSL: 'false',
|
|
},
|
|
async () => ({
|
|
executor: {
|
|
async query(sql: string) {
|
|
executed.push(sql);
|
|
return { rows: [] };
|
|
},
|
|
},
|
|
async close() {
|
|
closed = true;
|
|
},
|
|
})
|
|
);
|
|
|
|
expect(result.appliedTables).toEqual([...M2_INTRADAY_TABLE_NAMES]);
|
|
expect(executed).toHaveLength(3);
|
|
expect(closed).toBe(true);
|
|
});
|
|
});
|