forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.test.js
More file actions
130 lines (116 loc) · 3.54 KB
/
Copy pathconfig.test.js
File metadata and controls
130 lines (116 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const path = require('path');
const { spawnSync } = require('child_process');
const repoRoot = path.join(__dirname, '..');
function cleanProcessEnv(overrides) {
return {
PATH: process.env.PATH,
Path: process.env.Path,
SystemRoot: process.env.SystemRoot,
COMSPEC: process.env.COMSPEC,
TEMP: process.env.TEMP,
TMP: process.env.TMP,
...overrides,
};
}
function runConfig(script, env) {
return spawnSync(process.execPath, ['-e', script], {
cwd: repoRoot,
env: cleanProcessEnv(env),
encoding: 'utf8',
});
}
describe('configuration validation', () => {
test('exits before startup and reports every invalid production variable', () => {
const result = runConfig("require('./src/config')", {
NODE_ENV: 'production',
REDIS_URL: 'not-a-url',
LOG_LEVEL: 'verbose',
PRICE_CACHE_TTL_SECONDS: 'soon',
});
const output = `${result.stdout}\n${result.stderr}`;
expect(result.status).toBe(1);
expect(output).toContain('DATABASE_URL');
expect(output).toContain('REDIS_URL');
expect(output).toContain('LOG_LEVEL');
expect(output).toContain('PRICE_CACHE_TTL_SECONDS');
});
test('loads safe in-process defaults under NODE_ENV=test', () => {
const result = runConfig(
[
"const config = require('./src/config');",
'console.log(JSON.stringify({',
' port: config.port,',
' databaseUrl: config.databaseUrl,',
' redisUrl: config.redis.url,',
' price: config.price,',
' watchedAssets: config.watchedAssets,',
' airdrops: config.airdrops,',
'}));',
].join(' '),
{ NODE_ENV: 'test' }
);
expect(result.status).toBe(0);
const parsed = JSON.parse(result.stdout.trim());
expect(parsed).toEqual({
port: 3000,
databaseUrl: 'postgres://localhost/smartdrop_test',
redisUrl: 'redis://localhost:6379',
price: {
cacheTtl: 60,
refreshInterval: 30,
staleThresholdMinutes: 5,
anomalyThresholdPercent: 20,
minSources: 2,
anomalyAction: 'warn',
sourcePriority: [],
refreshMaxCycleMs: 90000,
circuitBreaker: {
failureThreshold: 3,
successThreshold: 1,
timeoutMs: 30000,
},
},
watchedAssets: [],
airdrops: {
expiryCheckIntervalSeconds: 60,
ledgerCacheTtlMs: 5000,
expiryScanBatchSize: 100,
csvMaxBytes: 5 * 1024 * 1024,
jsonMaxBytes: 2 * 1024 * 1024,
maxRecipients: 10000,
rateLimit: {
windowSeconds: 60,
max: 10,
},
},
});
});
test('parses watched assets from WATCHED_ASSETS', () => {
const issuer = 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
const result = runConfig(
[
"const config = require('./src/config');",
'console.log(JSON.stringify(config.watchedAssets));',
].join(' '),
{
NODE_ENV: 'test',
WATCHED_ASSETS: `XLM,USDC:${issuer},XLM`,
}
);
expect(result.status).toBe(0);
expect(JSON.parse(result.stdout.trim())).toEqual([
{ code: 'XLM', issuer: null },
{ code: 'USDC', issuer },
]);
});
test('rejects malformed watched assets during config loading', () => {
const result = runConfig("require('./src/config')", {
NODE_ENV: 'test',
WATCHED_ASSETS: 'usdc:not-a-stellar-address',
});
const output = `${result.stdout}\n${result.stderr}`;
expect(result.status).toBe(1);
expect(output).toContain('WATCHED_ASSETS');
});
});