Repository navigation
Expand file tree
/
Copy pathconfig-async.test.ts
More file actions
245 lines (196 loc) · 7.22 KB
/
Copy pathconfig-async.test.ts
File metadata and controls
245 lines (196 loc) · 7.22 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// tests/config-async.test.ts
import { test, describe, beforeEach, afterEach } from 'node:test'
import assert from 'node:assert/strict'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import fs from 'node:fs'
import { getConfig, loadConfigAsync, resetConfigForTesting } from '../dist/app/config.js'
import { cleanupEnvironmentVariables, storeEnvironmentVariables, restoreEnvironmentVariables } from './test-helpers.js'
import type { TestEnvironment } from './types/index.js'
import { ENV_TEST_VALUES, TEST_SECRET_LONG } from './test-constants.js'
// Ensure clean state at module load
resetConfigForTesting()
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
describe('Config Module - Async Tests', () => {
const configPath = join(__dirname, '..', 'config.json')
const backupPath = join(__dirname, '..', 'config.json.backup')
let originalEnv: TestEnvironment
beforeEach(() => {
// Store original environment variables
originalEnv = storeEnvironmentVariables()
// Clean up all environment variables
cleanupEnvironmentVariables()
// Backup existing config if it exists
if (fs.existsSync(configPath)) {
fs.copyFileSync(configPath, backupPath)
}
// Reset config instance for fresh testing
resetConfigForTesting()
})
afterEach(() => {
// Restore original environment variables
restoreEnvironmentVariables(originalEnv)
// Restore original config
try {
if (fs.existsSync(backupPath)) {
fs.copyFileSync(backupPath, configPath)
fs.unlinkSync(backupPath)
} else if (fs.existsSync(configPath)) {
fs.unlinkSync(configPath)
}
} catch (error) {
// Ignore cleanup errors in tests
console.warn('Test cleanup warning:', (error as Error).message)
}
})
test('loadConfigAsync loads default config when config.json is missing', async () => {
// Ensure config.json doesn't exist
if (fs.existsSync(configPath)) {
fs.unlinkSync(configPath)
}
const config = await loadConfigAsync()
assert.equal(config.listen.ip, '0.0.0.0')
assert.equal(config.listen.port, 2222)
assert.equal(config.ssh.port, 22)
assert.equal(config.ssh.term, 'xterm-color')
assert.equal(config.session.name, 'webssh2.sid')
assert.ok(config.session.secret)
})
test('loadConfigAsync loads and merges custom config from config.json', async () => {
const customConfig = {
listen: {
port: 3333
},
ssh: {
host: 'test.example.com'
},
header: {
text: 'Test Header'
}
}
fs.writeFileSync(configPath, JSON.stringify(customConfig, null, 2))
const config = await loadConfigAsync()
// Custom values should be merged
assert.equal(config.listen.port, 3333)
assert.equal(config.ssh.host, 'test.example.com')
assert.equal(config.header.text, 'Test Header')
// Default values should still be present
assert.equal(config.listen.ip, '0.0.0.0')
assert.equal(config.ssh.port, 22)
assert.equal(config.ssh.term, 'xterm-color')
})
test('loadConfigAsync overrides port with PORT environment variable', async () => {
const customConfig = {
listen: {
port: 3333
}
}
fs.writeFileSync(configPath, JSON.stringify(customConfig, null, 2))
process.env.PORT = '4444'
const config = await loadConfigAsync()
assert.equal(config.listen.port, 4444)
})
test('loadConfigAsync handles malformed JSON gracefully', async () => {
// Write invalid JSON
fs.writeFileSync(configPath, '{ invalid json }')
const config = await loadConfigAsync()
// Should fall back to defaults
assert.equal(config.listen.ip, '0.0.0.0')
assert.equal(config.listen.port, 2222)
})
test('getConfig returns the same config instance on multiple calls', async () => {
const config1 = await getConfig()
const config2 = await getConfig()
assert.strictEqual(config1, config2)
assert.ok(typeof config1.getCorsConfig === 'function')
})
test('getConfig works with custom configuration file', async () => {
const customConfig = {
listen: {
port: 5555
},
ssh: {
algorithms: {
cipher: ['aes256-gcm@openssh.com']
}
}
}
fs.writeFileSync(configPath, JSON.stringify(customConfig, null, 2))
const config = await getConfig()
assert.equal(config.listen.port, 5555)
assert.ok(config.ssh.algorithms?.cipher?.includes('aes256-gcm@openssh.com'))
assert.ok(typeof config.getCorsConfig === 'function')
})
test('async config loading uses literal JSON values (no env var substitution)', async () => {
// Native JSON parsing doesn't support environment variable substitution
// This tests that literal values are preserved
process.env.TEST_SECRET = TEST_SECRET_LONG
const customConfig = {
session: {
secret: ENV_TEST_VALUES.secret
}
}
fs.writeFileSync(configPath, JSON.stringify(customConfig, null, 2))
try {
const config = await loadConfigAsync()
// Native JSON parsing should preserve the literal string
assert.equal(config.session.secret, ENV_TEST_VALUES.secret)
} finally {
delete process.env.TEST_SECRET
}
})
test('async config loading validates configuration schema', async () => {
const validConfig = {
listen: {
ip: '127.0.0.1',
port: 3000
},
ssh: {
port: 22,
term: 'xterm-256color'
}
}
fs.writeFileSync(configPath, JSON.stringify(validConfig, null, 2))
const config = await loadConfigAsync()
// Should pass validation and merge successfully
assert.equal(config.listen.ip, '127.0.0.1')
assert.equal(config.listen.port, 3000)
assert.equal(config.ssh.term, 'xterm-256color')
})
test('async config preserves all SSH algorithms', async () => {
const customConfig = {
ssh: {
algorithms: {
cipher: ['aes256-gcm@openssh.com', 'aes128-ctr'],
kex: ['ecdh-sha2-nistp256'],
hmac: ['hmac-sha2-512']
}
}
}
fs.writeFileSync(configPath, JSON.stringify(customConfig, null, 2))
const config = await loadConfigAsync()
assert.ok(config.ssh.algorithms?.cipher?.includes('aes256-gcm@openssh.com'))
assert.ok(config.ssh.algorithms?.cipher?.includes('aes128-ctr'))
assert.ok(config.ssh.algorithms?.kex?.includes('ecdh-sha2-nistp256'))
assert.ok(config.ssh.algorithms?.hmac?.includes('hmac-sha2-512'))
// Should still have other default algorithms
assert.ok(config.ssh.algorithms?.serverHostKey && config.ssh.algorithms.serverHostKey.length > 0)
assert.ok(config.ssh.algorithms?.compress && config.ssh.algorithms.compress.length > 0)
})
test('concurrent calls to getConfig return the same instance', async () => {
const customConfig = {
listen: { port: 6666 }
}
fs.writeFileSync(configPath, JSON.stringify(customConfig, null, 2))
// Make multiple concurrent calls
const [config1, config2, config3] = await Promise.all([
getConfig(),
getConfig(),
getConfig()
])
assert.strictEqual(config1, config2)
assert.strictEqual(config2, config3)
assert.equal(config1.listen.port, 6666)
})
})