-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.test.ts
163 lines (151 loc) · 3.88 KB
/
config.test.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import test from 'ava'
import { AdapterConfig, getEnv, SettingDefinition, SettingsDefinitionMap } from '../src/config'
import { validator } from '../src/validation/utils'
test.afterEach(async () => {
process.env = {}
})
test.serial('Test config validator', async (t) => {
process.env['MAX_COMMON_KEY_SIZE'] = '1000'
try {
const config = new AdapterConfig({})
config.initialize()
config.validate()
t.fail()
} catch (e: unknown) {
t.pass()
}
})
test.serial('Test good enum config', async (t) => {
process.env['CACHE_TYPE'] = 'local'
const config = new AdapterConfig({})
config.initialize()
config.validate()
t.is(config.settings.CACHE_TYPE, 'local')
})
test.serial('Test bad enum config', async (t) => {
process.env['CACHE_TYPE'] = 'test'
try {
const config = new AdapterConfig({})
config.initialize()
config.validate()
t.fail()
} catch (e: unknown) {
t.pass()
}
})
test.serial('Test bad env name', async (t) => {
try {
getEnv('$#%', {} as SettingDefinition)
t.fail()
} catch (e: unknown) {
t.pass()
}
})
test.serial('Test custom settings', async (t) => {
process.env['CUSTOM_KEY'] = 'test'
const customSettings = {
CUSTOM_KEY: {
description: 'Test custom env var',
type: 'string',
},
} satisfies SettingsDefinitionMap
const config = new AdapterConfig(customSettings)
config.initialize()
config.validate()
t.is(config.settings.CUSTOM_KEY, 'test')
})
test.serial('Test missing custom settings (required)', async (t) => {
const customSettings: SettingsDefinitionMap = {
CUSTOM_KEY_1: {
description: 'Test custom env var',
type: 'string',
required: true,
},
}
try {
const config = new AdapterConfig(customSettings)
config.initialize()
config.validate()
t.fail()
} catch (e: unknown) {
t.pass()
}
})
test.serial('Test custom settings (overlap base)', async (t) => {
process.env['BASE_URL'] = 'test'
const customSettings: SettingsDefinitionMap = {
BASE_URL: {
description: 'Test custom env var overlapping base',
type: 'string',
},
}
try {
const config = new AdapterConfig(customSettings)
config.initialize()
config.validate()
t.fail()
} catch (e: unknown) {
t.pass()
}
})
test.serial('Test prefix settings', async (t) => {
process.env['TEST_PREFIX_BASE_URL'] = 'TEST_BASE_URL'
const envVarsPrefix = 'TEST_PREFIX'
const config = new AdapterConfig({}, { envVarsPrefix })
config.initialize()
t.is(config.settings['BASE_URL'], 'TEST_BASE_URL')
})
test.serial('Test validate function (out of bounds)', async (t) => {
process.env['CUSTOM_KEY'] = '11'
const customSettings: SettingsDefinitionMap = {
CUSTOM_KEY: {
description: 'Test custom env var',
type: 'number',
validate: validator.integer({ min: 1, max: 10 }),
},
}
const config = new AdapterConfig(customSettings)
config.initialize()
try {
config.validate()
t.fail()
} catch (e) {
t.pass()
}
})
test.serial('Test validate function (decimal)', async (t) => {
process.env['CUSTOM_KEY'] = '5.1'
const customSettings: SettingsDefinitionMap = {
CUSTOM_KEY: {
description: 'Test custom env var',
type: 'number',
validate: validator.integer({ min: 1, max: 10 }),
},
}
const config = new AdapterConfig(customSettings)
config.initialize()
try {
config.validate()
t.fail()
} catch (e) {
t.pass()
}
})
test.serial('Test validate function (scientific notation)', async (t) => {
process.env['CUSTOM_KEY'] = '3.5e+6'
const customSettings: SettingsDefinitionMap = {
CUSTOM_KEY: {
description: 'Test custom env var',
type: 'number',
validate: validator.integer({ min: 2_000_000, max: 5_000_000 }),
},
}
const config = new AdapterConfig(customSettings)
config.initialize()
try {
config.validate()
t.pass()
} catch (e) {
t.fail()
}
})