|
1 | 1 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
2 | | -import { getClickHouseConfig, validateStorageConfig, STORAGE_ENGINE } from '../../database/storage-config.js'; |
| 2 | +import { getClickHouseConfig, getMongoDBConfig, validateStorageConfig, STORAGE_ENGINE } from '../../database/storage-config.js'; |
3 | 3 |
|
4 | 4 | describe('storage-config', () => { |
5 | 5 | describe('STORAGE_ENGINE', () => { |
@@ -91,5 +91,132 @@ describe('storage-config', () => { |
91 | 91 | // STORAGE_ENGINE is 'timescale' in test env by default |
92 | 92 | expect(() => validateStorageConfig()).not.toThrow(); |
93 | 93 | }); |
| 94 | + |
| 95 | + // Dynamic import tests for non-default engine values |
| 96 | + it('should throw for invalid engine value', async () => { |
| 97 | + vi.resetModules(); |
| 98 | + vi.stubEnv('STORAGE_ENGINE', 'oracle'); |
| 99 | + const { validateStorageConfig: validate } = await import('../../database/storage-config.js'); |
| 100 | + expect(() => validate()).toThrow('Invalid STORAGE_ENGINE'); |
| 101 | + vi.unstubAllEnvs(); |
| 102 | + vi.resetModules(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should throw for clickhouse without CLICKHOUSE_HOST', async () => { |
| 106 | + vi.resetModules(); |
| 107 | + vi.stubEnv('STORAGE_ENGINE', 'clickhouse'); |
| 108 | + const { validateStorageConfig: validate } = await import('../../database/storage-config.js'); |
| 109 | + expect(() => validate()).toThrow('Missing CLICKHOUSE_HOST'); |
| 110 | + vi.unstubAllEnvs(); |
| 111 | + vi.resetModules(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should throw for clickhouse with HOST but without CLICKHOUSE_DATABASE', async () => { |
| 115 | + vi.resetModules(); |
| 116 | + vi.stubEnv('STORAGE_ENGINE', 'clickhouse'); |
| 117 | + vi.stubEnv('CLICKHOUSE_HOST', 'localhost'); |
| 118 | + const { validateStorageConfig: validate } = await import('../../database/storage-config.js'); |
| 119 | + expect(() => validate()).toThrow('Missing CLICKHOUSE_DATABASE'); |
| 120 | + vi.unstubAllEnvs(); |
| 121 | + vi.resetModules(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should not throw for clickhouse with both required vars', async () => { |
| 125 | + vi.resetModules(); |
| 126 | + vi.stubEnv('STORAGE_ENGINE', 'clickhouse'); |
| 127 | + vi.stubEnv('CLICKHOUSE_HOST', 'localhost'); |
| 128 | + vi.stubEnv('CLICKHOUSE_DATABASE', 'logtide'); |
| 129 | + const { validateStorageConfig: validate } = await import('../../database/storage-config.js'); |
| 130 | + expect(() => validate()).not.toThrow(); |
| 131 | + vi.unstubAllEnvs(); |
| 132 | + vi.resetModules(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should throw for mongodb without MONGODB_URI or MONGODB_HOST', async () => { |
| 136 | + vi.resetModules(); |
| 137 | + vi.stubEnv('STORAGE_ENGINE', 'mongodb'); |
| 138 | + const { validateStorageConfig: validate } = await import('../../database/storage-config.js'); |
| 139 | + expect(() => validate()).toThrow('Missing MONGODB_URI or MONGODB_HOST'); |
| 140 | + vi.unstubAllEnvs(); |
| 141 | + vi.resetModules(); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should not throw for mongodb with MONGODB_URI', async () => { |
| 145 | + vi.resetModules(); |
| 146 | + vi.stubEnv('STORAGE_ENGINE', 'mongodb'); |
| 147 | + vi.stubEnv('MONGODB_URI', 'mongodb://localhost:27017/test'); |
| 148 | + const { validateStorageConfig: validate } = await import('../../database/storage-config.js'); |
| 149 | + expect(() => validate()).not.toThrow(); |
| 150 | + vi.unstubAllEnvs(); |
| 151 | + vi.resetModules(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should not throw for mongodb with MONGODB_HOST', async () => { |
| 155 | + vi.resetModules(); |
| 156 | + vi.stubEnv('STORAGE_ENGINE', 'mongodb'); |
| 157 | + vi.stubEnv('MONGODB_HOST', 'localhost'); |
| 158 | + const { validateStorageConfig: validate } = await import('../../database/storage-config.js'); |
| 159 | + expect(() => validate()).not.toThrow(); |
| 160 | + vi.unstubAllEnvs(); |
| 161 | + vi.resetModules(); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('getMongoDBConfig()', () => { |
| 166 | + afterEach(() => { |
| 167 | + vi.unstubAllEnvs(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should return defaults when no env vars are set', () => { |
| 171 | + const config = getMongoDBConfig(); |
| 172 | + expect(config.host).toBe('localhost'); |
| 173 | + expect(config.port).toBe(27017); |
| 174 | + expect(config.database).toBe('logtide'); |
| 175 | + expect(config.username).toBe(''); |
| 176 | + expect(config.password).toBe(''); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should parse MONGODB_URI for host, port, database, credentials', () => { |
| 180 | + vi.stubEnv('MONGODB_URI', 'mongodb://admin:secret@mongo.example.com:27018/mydb'); |
| 181 | + const config = getMongoDBConfig(); |
| 182 | + expect(config.host).toBe('mongo.example.com'); |
| 183 | + expect(config.port).toBe(27018); |
| 184 | + expect(config.database).toBe('mydb'); |
| 185 | + expect(config.username).toBe('admin'); |
| 186 | + expect(config.password).toBe('secret'); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should parse authSource from MONGODB_URI query string', () => { |
| 190 | + vi.stubEnv('MONGODB_URI', 'mongodb://user:pass@localhost:27017/mydb?authSource=admin'); |
| 191 | + const config = getMongoDBConfig(); |
| 192 | + expect((config as any).options?.authSource).toBe('admin'); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should fall back to individual env vars when MONGODB_URI is not set', () => { |
| 196 | + vi.stubEnv('MONGODB_HOST', 'mongo.internal'); |
| 197 | + vi.stubEnv('MONGODB_PORT', '27019'); |
| 198 | + vi.stubEnv('MONGODB_DATABASE', 'proddb'); |
| 199 | + vi.stubEnv('MONGODB_USERNAME', 'dbuser'); |
| 200 | + vi.stubEnv('MONGODB_PASSWORD', 'dbpass'); |
| 201 | + const config = getMongoDBConfig(); |
| 202 | + expect(config.host).toBe('mongo.internal'); |
| 203 | + expect(config.port).toBe(27019); |
| 204 | + expect(config.database).toBe('proddb'); |
| 205 | + expect(config.username).toBe('dbuser'); |
| 206 | + expect(config.password).toBe('dbpass'); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should include authSource in options when MONGODB_AUTH_SOURCE is set', () => { |
| 210 | + vi.stubEnv('MONGODB_HOST', 'localhost'); |
| 211 | + vi.stubEnv('MONGODB_AUTH_SOURCE', 'admin'); |
| 212 | + const config = getMongoDBConfig(); |
| 213 | + expect((config as any).options?.authSource).toBe('admin'); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should not include options when MONGODB_AUTH_SOURCE is not set', () => { |
| 217 | + vi.stubEnv('MONGODB_HOST', 'localhost'); |
| 218 | + const config = getMongoDBConfig(); |
| 219 | + expect((config as any).options).toBeUndefined(); |
| 220 | + }); |
94 | 221 | }); |
95 | 222 | }); |
0 commit comments