38 lines
2.1 KiB
TypeScript
38 lines
2.1 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { loadServerConfig } from '../src/server/config.js';
|
|
|
|
test('JSON configuration preserves database and secrets independently of environment variables', async t => {
|
|
const previous = process.env.MONGODB_DATABASE;
|
|
process.env.MONGODB_DATABASE = 'must_not_override_json';
|
|
t.after(() => { if (previous === undefined) delete process.env.MONGODB_DATABASE; else process.env.MONGODB_DATABASE = previous; });
|
|
const dir = await mkdtemp(join(tmpdir(), 'dragon-config-'));
|
|
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
const path = join(dir, '.config.json');
|
|
const input = { MONGODB_URI: 'mongodb://localhost:27017', MONGODB_DATABASE: 'local_game', HOST: '127.0.0.1', PORT: 2100,
|
|
ADMIN_PASSWORD: ' spaces # $ remain ', TRUST_PROXY: 'loopback', STUN_URLS: ['stun:example.com:3478'] };
|
|
await writeFile(path, JSON.stringify(input));
|
|
const config = loadServerConfig(path);
|
|
assert.equal(config.MONGODB_DATABASE, 'local_game');
|
|
assert.equal(config.ADMIN_PASSWORD, input.ADMIN_PASSWORD);
|
|
assert.equal(config.PORT, 2100);
|
|
assert.deepEqual(config.STUN_URLS, input.STUN_URLS);
|
|
});
|
|
|
|
test('missing, malformed and invalid configuration produces safe actionable error codes', async t => {
|
|
const dir = await mkdtemp(join(tmpdir(), 'dragon-config-'));
|
|
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
const path = join(dir, '.config.json');
|
|
assert.throws(() => loadServerConfig(path), { code: 'CONFIG_NOT_FOUND' });
|
|
await writeFile(path, '{"password":"private');
|
|
assert.throws(() => loadServerConfig(path), error => (error as Error).message === 'INVALID_CONFIG');
|
|
for (const input of [null, {}, { MONGODB_URI: 'secret', MONGODB_DATABASE: 'db', PORT: '2100' },
|
|
{ MONGODB_URI: 'mongodb://localhost', MONGODB_DATABASE: 'db', STUN_URLS: 'stun:host' }]) {
|
|
await writeFile(path, JSON.stringify(input));
|
|
assert.throws(() => loadServerConfig(path), { code: 'INVALID_CONFIG' });
|
|
}
|
|
});
|