@flusys/nestjs-core 6.0.2 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,325 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ const _envconfigservice = require("./env-config.service");
6
+ describe('EnvConfigService', ()=>{
7
+ const ORIGINAL_ENV = {
8
+ ...process.env
9
+ };
10
+ afterEach(()=>{
11
+ for (const key of Object.keys(process.env)){
12
+ if (!(key in ORIGINAL_ENV)) delete process.env[key];
13
+ }
14
+ Object.assign(process.env, ORIGINAL_ENV);
15
+ });
16
+ describe('tryGetValue', ()=>{
17
+ it('should return env value for an unrestricted key', ()=>{
18
+ process.env.CUSTOM_KEY = 'custom-value';
19
+ expect(_envconfigservice.envConfig.tryGetValue('CUSTOM_KEY')).toBe('custom-value');
20
+ });
21
+ it('should return empty string when missing and throwOnMissing is false', ()=>{
22
+ delete process.env.MISSING_KEY;
23
+ expect(_envconfigservice.envConfig.tryGetValue('MISSING_KEY', false)).toBe('');
24
+ });
25
+ it('should throw when missing and throwOnMissing is true', ()=>{
26
+ delete process.env.MISSING_KEY;
27
+ expect(()=>_envconfigservice.envConfig.tryGetValue('MISSING_KEY', true)).toThrow('Config error - missing env.MISSING_KEY');
28
+ });
29
+ it.each([
30
+ [
31
+ 'FRONTEND_URL',
32
+ 'getFrontendUrl'
33
+ ],
34
+ [
35
+ 'ALLOW_ORIGINS',
36
+ 'getOrigins'
37
+ ],
38
+ [
39
+ 'PORT',
40
+ 'getPort'
41
+ ],
42
+ [
43
+ 'MODE',
44
+ 'isProduction'
45
+ ],
46
+ [
47
+ 'DB_TYPE',
48
+ 'getTypeOrmConfig'
49
+ ],
50
+ [
51
+ 'DB_HOST',
52
+ 'getTypeOrmConfig'
53
+ ],
54
+ [
55
+ 'DB_PORT',
56
+ 'getTypeOrmConfig'
57
+ ],
58
+ [
59
+ 'DB_USER',
60
+ 'getTypeOrmConfig'
61
+ ],
62
+ [
63
+ 'DB_PASSWORD',
64
+ 'getTypeOrmConfig'
65
+ ],
66
+ [
67
+ 'DB_NAME',
68
+ 'getTypeOrmConfig'
69
+ ]
70
+ ])('should deny direct access to restricted key %s and point to %s()', (key, method)=>{
71
+ expect(()=>_envconfigservice.envConfig.tryGetValue(key)).toThrow(`Access denied for env key "${key}". Use the method \`${method}()\` instead.`);
72
+ });
73
+ });
74
+ describe('getNumber', ()=>{
75
+ it('should coerce a numeric env value', ()=>{
76
+ process.env.SOME_NUMBER = '42';
77
+ expect(_envconfigservice.envConfig.getNumber('SOME_NUMBER')).toBe(42);
78
+ });
79
+ it('should return NaN for a non-numeric value', ()=>{
80
+ process.env.SOME_NUMBER = 'not-a-number';
81
+ expect(_envconfigservice.envConfig.getNumber('SOME_NUMBER')).toBeNaN();
82
+ });
83
+ });
84
+ describe('getBoolean', ()=>{
85
+ it.each([
86
+ [
87
+ 'true',
88
+ true
89
+ ],
90
+ [
91
+ 'TRUE',
92
+ true
93
+ ],
94
+ [
95
+ 'True',
96
+ true
97
+ ],
98
+ [
99
+ 'false',
100
+ false
101
+ ],
102
+ [
103
+ 'yes',
104
+ false
105
+ ],
106
+ [
107
+ '',
108
+ false
109
+ ]
110
+ ])('should parse %s as %s', (value, expected)=>{
111
+ process.env.SOME_BOOL = value;
112
+ expect(_envconfigservice.envConfig.getBoolean('SOME_BOOL', false)).toBe(expected);
113
+ });
114
+ });
115
+ describe('getPort', ()=>{
116
+ it('should default to 3000 when PORT is unset', ()=>{
117
+ delete process.env.PORT;
118
+ expect(_envconfigservice.envConfig.getPort()).toBe(3000);
119
+ });
120
+ it('should return the configured PORT', ()=>{
121
+ process.env.PORT = '2002';
122
+ expect(_envconfigservice.envConfig.getPort()).toBe(2002);
123
+ });
124
+ });
125
+ describe('isProduction', ()=>{
126
+ it('should be true when MODE is unset', ()=>{
127
+ delete process.env.MODE;
128
+ expect(_envconfigservice.envConfig.isProduction()).toBe(true);
129
+ });
130
+ it('should be false when MODE is "dev" (case-insensitive)', ()=>{
131
+ process.env.MODE = 'dev';
132
+ expect(_envconfigservice.envConfig.isProduction()).toBe(false);
133
+ process.env.MODE = 'DEV';
134
+ expect(_envconfigservice.envConfig.isProduction()).toBe(false);
135
+ });
136
+ it('should be true for any non-DEV mode', ()=>{
137
+ process.env.MODE = 'production';
138
+ expect(_envconfigservice.envConfig.isProduction()).toBe(true);
139
+ });
140
+ });
141
+ describe('getOrigins', ()=>{
142
+ it('should split and trim a comma-separated origin list', ()=>{
143
+ process.env.ALLOW_ORIGINS = 'http://localhost:2001, http://localhost:3000 ,http://example.com';
144
+ expect(_envconfigservice.envConfig.getOrigins()).toEqual([
145
+ 'http://localhost:2001',
146
+ 'http://localhost:3000',
147
+ 'http://example.com'
148
+ ]);
149
+ });
150
+ it('should throw when unset since getOrigins requires the value', ()=>{
151
+ delete process.env.ALLOW_ORIGINS;
152
+ expect(()=>_envconfigservice.envConfig.getOrigins()).toThrow('Config error - missing env.ALLOW_ORIGINS');
153
+ });
154
+ });
155
+ describe('getTypeOrmConfig', ()=>{
156
+ it('should apply mysql defaults when DB_TYPE is unset', ()=>{
157
+ delete process.env.DB_TYPE;
158
+ delete process.env.DB_HOST;
159
+ delete process.env.DB_PORT;
160
+ delete process.env.DB_USER;
161
+ delete process.env.DB_PASSWORD;
162
+ delete process.env.DB_NAME;
163
+ expect(_envconfigservice.envConfig.getTypeOrmConfig()).toEqual({
164
+ type: 'mysql',
165
+ host: 'localhost',
166
+ port: 3306,
167
+ username: 'root',
168
+ password: '',
169
+ database: 'flusys_main'
170
+ });
171
+ });
172
+ it('should default the postgres port to 5432', ()=>{
173
+ process.env.DB_TYPE = 'postgres';
174
+ delete process.env.DB_PORT;
175
+ expect(_envconfigservice.envConfig.getTypeOrmConfig().port).toBe(5432);
176
+ });
177
+ it('should use explicit connection values when provided', ()=>{
178
+ process.env.DB_TYPE = 'postgres';
179
+ process.env.DB_HOST = 'db.internal';
180
+ process.env.DB_PORT = '5555';
181
+ process.env.DB_USER = 'flusys';
182
+ process.env.DB_PASSWORD = 'secret';
183
+ process.env.DB_NAME = 'flusys_prod';
184
+ expect(_envconfigservice.envConfig.getTypeOrmConfig()).toEqual({
185
+ type: 'postgres',
186
+ host: 'db.internal',
187
+ port: 5555,
188
+ username: 'flusys',
189
+ password: 'secret',
190
+ database: 'flusys_prod'
191
+ });
192
+ });
193
+ });
194
+ describe('getJwtConfig', ()=>{
195
+ it('should throw when JWT_SECRET is missing', ()=>{
196
+ delete process.env.JWT_SECRET;
197
+ process.env.REFRESH_TOKEN_SECRET = 'refresh';
198
+ expect(()=>_envconfigservice.envConfig.getJwtConfig()).toThrow('Config error - missing env.JWT_SECRET');
199
+ });
200
+ it('should throw when REFRESH_TOKEN_SECRET is missing', ()=>{
201
+ process.env.JWT_SECRET = 'secret';
202
+ delete process.env.REFRESH_TOKEN_SECRET;
203
+ expect(()=>_envconfigservice.envConfig.getJwtConfig()).toThrow('Config error - missing env.REFRESH_TOKEN_SECRET');
204
+ });
205
+ it('should apply default expirations when unset', ()=>{
206
+ process.env.JWT_SECRET = 'secret';
207
+ process.env.REFRESH_TOKEN_SECRET = 'refresh-secret';
208
+ delete process.env.JWT_EXPIRATION;
209
+ delete process.env.REFRESH_TOKEN_EXPIRATION;
210
+ expect(_envconfigservice.envConfig.getJwtConfig()).toEqual({
211
+ secret: 'secret',
212
+ expiration: '1h',
213
+ refreshSecret: 'refresh-secret',
214
+ refreshExpiration: '7d'
215
+ });
216
+ });
217
+ it('should use explicit expirations when set', ()=>{
218
+ process.env.JWT_SECRET = 'secret';
219
+ process.env.JWT_EXPIRATION = '15m';
220
+ process.env.REFRESH_TOKEN_SECRET = 'refresh-secret';
221
+ process.env.REFRESH_TOKEN_EXPIRATION = '30d';
222
+ expect(_envconfigservice.envConfig.getJwtConfig()).toEqual({
223
+ secret: 'secret',
224
+ expiration: '15m',
225
+ refreshSecret: 'refresh-secret',
226
+ refreshExpiration: '30d'
227
+ });
228
+ });
229
+ });
230
+ describe('getRedisUrl', ()=>{
231
+ it('should default to localhost redis', ()=>{
232
+ delete process.env.REDIS_URL;
233
+ expect(_envconfigservice.envConfig.getRedisUrl()).toBe('redis://localhost:6379');
234
+ });
235
+ it('should return the configured redis url', ()=>{
236
+ process.env.REDIS_URL = 'redis://redis-host:6380';
237
+ expect(_envconfigservice.envConfig.getRedisUrl()).toBe('redis://redis-host:6380');
238
+ });
239
+ });
240
+ describe('getMailConfig', ()=>{
241
+ it('should default to empty strings when unset', ()=>{
242
+ delete process.env.MAIL_FROM;
243
+ delete process.env.MAIL_APP_PASSWORD;
244
+ expect(_envconfigservice.envConfig.getMailConfig()).toEqual({
245
+ MAIL_FROM: '',
246
+ MAIL_APP_PASSWORD: ''
247
+ });
248
+ });
249
+ it('should return configured mail settings', ()=>{
250
+ process.env.MAIL_FROM = 'noreply@flusys.io';
251
+ process.env.MAIL_APP_PASSWORD = 'app-pass';
252
+ expect(_envconfigservice.envConfig.getMailConfig()).toEqual({
253
+ MAIL_FROM: 'noreply@flusys.io',
254
+ MAIL_APP_PASSWORD: 'app-pass'
255
+ });
256
+ });
257
+ });
258
+ describe('getTenantId', ()=>{
259
+ it('should return null when unset', ()=>{
260
+ delete process.env.TENANT_ID;
261
+ expect(_envconfigservice.envConfig.getTenantId()).toBeNull();
262
+ });
263
+ it('should return the configured tenant id', ()=>{
264
+ process.env.TENANT_ID = 'tenant-a';
265
+ expect(_envconfigservice.envConfig.getTenantId()).toBe('tenant-a');
266
+ });
267
+ });
268
+ describe('useTenantMode', ()=>{
269
+ it('should be false by default', ()=>{
270
+ delete process.env.USE_TENANT_MODE;
271
+ expect(_envconfigservice.envConfig.useTenantMode()).toBe(false);
272
+ });
273
+ it('should be true when explicitly set', ()=>{
274
+ process.env.USE_TENANT_MODE = 'true';
275
+ expect(_envconfigservice.envConfig.useTenantMode()).toBe(true);
276
+ });
277
+ });
278
+ describe('getLogConfig', ()=>{
279
+ it('should default log level to info in production', ()=>{
280
+ delete process.env.MODE;
281
+ delete process.env.LOG_LEVEL;
282
+ expect(_envconfigservice.envConfig.getLogConfig().level).toBe('info');
283
+ });
284
+ it('should default log level to debug outside production', ()=>{
285
+ process.env.MODE = 'dev';
286
+ delete process.env.LOG_LEVEL;
287
+ expect(_envconfigservice.envConfig.getLogConfig().level).toBe('debug');
288
+ });
289
+ it('should apply all defaults when unset', ()=>{
290
+ process.env.MODE = 'dev';
291
+ delete process.env.LOG_DIR;
292
+ delete process.env.LOG_LEVEL;
293
+ delete process.env.LOG_MAX_SIZE;
294
+ delete process.env.LOG_MAX_FILES;
295
+ delete process.env.DISABLE_HTTP_LOGGING;
296
+ expect(_envconfigservice.envConfig.getLogConfig()).toEqual({
297
+ dir: 'logs',
298
+ level: 'debug',
299
+ maxSize: '20m',
300
+ maxFiles: '14d',
301
+ disableHttpLogging: false
302
+ });
303
+ });
304
+ it('should honor explicit overrides', ()=>{
305
+ process.env.LOG_DIR = '/var/log/flusys';
306
+ process.env.LOG_LEVEL = 'warn';
307
+ process.env.LOG_MAX_SIZE = '50m';
308
+ process.env.LOG_MAX_FILES = '30d';
309
+ process.env.DISABLE_HTTP_LOGGING = 'true';
310
+ expect(_envconfigservice.envConfig.getLogConfig()).toEqual({
311
+ dir: '/var/log/flusys',
312
+ level: 'warn',
313
+ maxSize: '50m',
314
+ maxFiles: '30d',
315
+ disableHttpLogging: true
316
+ });
317
+ });
318
+ });
319
+ describe('getEnv', ()=>{
320
+ it('should return the full process.env map', ()=>{
321
+ process.env.SOME_FLAG = 'x';
322
+ expect(_envconfigservice.envConfig.getEnv()).toBe(process.env);
323
+ });
324
+ });
325
+ });