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> = [ { QUESTDB_URL: `${baseUrl}?sslmode=disable`, QUESTDB_SSL: 'true' }, { RESEARCH_MCP_QUESTDB_URL: `${baseUrl}?sslmode=disable`, RESEARCH_MCP_QUESTDB_SSL: 'true' }, { QUESTDB_URL: `${baseUrl}?sslmode=require`, QUESTDB_SSL: 'false' }, { QUESTDB_URL: `${baseUrl}?ssl=false`, QUESTDB_SSL: 'true' }, { QUESTDB_URL: `${baseUrl}?sslmode=prefer`, 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 consistent URL + SSL configs', async () => { expect(() => resolveQuestDbInitM2Config({ QUESTDB_HOST: '10.0.0.51', QUESTDB_SSL: 'maybe', }) ).toThrow(RangeError); const resolved = resolveQuestDbInitM2Config({ QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require', QUESTDB_SSL: 'true', }); expect(resolved).toEqual({ url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require', ssl: true, }); const executed: string[] = []; let closed = false; const result = await runQuestDbInitM2( { QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=disable', 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); }); });