import { expect, test } from 'bun:test'; import type { PoolConfig } from 'pg'; import { baseAppSchema } from '@stock-bot/config'; import { ServiceContainerBuilder } from '../../libs/core/di/src/container/builder'; function configuration(enabled = true) { return baseAppSchema.parse({ name: 'store-configuration', version: '1.0.0', service: { name: 'store-configuration', port: 3000 }, log: { level: 'fatal' }, database: { dragonfly: { enabled, host: 'redis.test', password: 'fixture', db: 3 }, mongodb: { enabled, uri: 'mongodb://fixture:fixture@mongo.test:27017/store?authSource=admin', database: 'store', poolSize: 17, }, postgres: { enabled, host: 'pg.test', port: 15432, database: 'store', user: 'fixture', password: 'fixture', ssl: true, poolSize: 19, connectionTimeout: 4321, idleTimeout: 8765, }, questdb: { enabled: false }, }, }); } for (const enabled of [true, false]) { test(`explicit disable options win over enabled=${enabled} configuration`, async () => { const c = await new ServiceContainerBuilder() .withConfig(configuration(enabled)) .withOptions({ enableCache: false, enableMongoDB: false, enablePostgres: false }) .skipInitialization() .build(); try { for (const name of ['cache', 'mongoClient', 'postgresClient']) expect(c.resolve(name)).toBeNull(); const config = c.resolve('config'); expect(config.redis.enabled).toBe(false); expect(config.mongodb.enabled).toBe(false); expect(config.postgres.enabled).toBe(false); } finally { await c.dispose(); } }); } test('enable options do not override disabled configurations', async () => { const c = await new ServiceContainerBuilder() .withConfig(configuration(false)) .withOptions({ enableCache: true, enableMongoDB: true, enablePostgres: true }) .skipInitialization() .build(); try { for (const name of ['cache', 'mongoClient', 'postgresClient']) expect(c.resolve(name)).toBeNull(); } finally { await c.dispose(); } }); test('PostgreSQL nondefault fields reach wire configuration and Mongo retains URI/pool settings', async () => { const c = await new ServiceContainerBuilder() .withConfig(configuration()) .skipInitialization() .build(); try { const wire = ( c.resolve('postgresClient') as unknown as { buildPoolConfig(): PoolConfig } ).buildPoolConfig(); expect(wire).toMatchObject({ host: 'pg.test', port: 15432, database: 'store', user: 'fixture', password: 'fixture', ssl: { rejectUnauthorized: true }, max: 19, connectionTimeoutMillis: 4321, idleTimeoutMillis: 8765, }); const mongo = c.resolve('mongoClient') as unknown as { config: { uri: string; poolSettings: { maxPoolSize: number } }; }; expect(mongo.config.uri).toBe(configuration().database!.mongodb!.uri); expect(mongo.config.poolSettings.maxPoolSize).toBe(17); } finally { await c.dispose(); } }); test('flat QuestDB alias takes precedence without discarding credentials or schema settings', async () => { const c = await new ServiceContainerBuilder() .withConfig({ ...configuration(false), questdb: { enabled: true, host: 'quest.test', ilpPort: 19009, influxPort: 29009, user: 'fixture', password: 'fixture', bufferSize: 1234, flushInterval: 3456, }, } as never) .skipInitialization() .build(); try { expect(c.resolve('config').questdb).toMatchObject({ influxPort: 29009, bufferSize: 1234, flushInterval: 3456, user: 'fixture', password: 'fixture', }); } finally { await c.dispose(); } });