50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import assert from 'node:assert/strict';
|
|||
|
|
import { describe, it } from 'node:test';
|
||
|
|
import { EnvValidationError, parseEnv } from '../src/config/env.js';
|
||
|
|
|
||
|
|
describe('environment validation', () => {
|
||
|
|
it('accepts a valid configuration', () => {
|
||
|
|
const env = parseEnv({
|
||
|
|
NODE_ENV: 'test',
|
||
|
|
DATABASE_URL: 'mysql://user:pass@localhost:3306/novya_test',
|
||
|
|
LOG_LEVEL: 'silent',
|
||
|
|
});
|
||
|
|
|
||
|
|
assert.equal(env.NODE_ENV, 'test');
|
||
|
|
assert.equal(env.DATABASE_URL, 'mysql://user:pass@localhost:3306/novya_test');
|
||
|
|
assert.equal(env.PORT, 3001);
|
||
|
|
assert.equal(env.OPENAI_BASE_URL, 'https://api.gapgpt.app/v1');
|
||
|
|
assert.equal(env.OPENAI_MODEL, 'gpt-4o-mini');
|
||
|
|
assert.equal(env.OPENAI_API_KEY, '');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('fails clearly when DATABASE_URL is missing', () => {
|
||
|
|
assert.throws(
|
||
|
|
() =>
|
||
|
|
parseEnv({
|
||
|
|
NODE_ENV: 'test',
|
||
|
|
}),
|
||
|
|
(err: unknown) => {
|
||
|
|
assert.ok(err instanceof EnvValidationError);
|
||
|
|
assert.match(String(err.message), /DATABASE_URL.*Required/i);
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('fails clearly when DATABASE_URL is not MySQL', () => {
|
||
|
|
assert.throws(
|
||
|
|
() =>
|
||
|
|
parseEnv({
|
||
|
|
NODE_ENV: 'test',
|
||
|
|
DATABASE_URL: 'postgres://user:pass@localhost/db',
|
||
|
|
}),
|
||
|
|
(err: unknown) => {
|
||
|
|
assert.ok(err instanceof EnvValidationError);
|
||
|
|
assert.match(String(err.message), /MySQL connection string/);
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
);
|
||
|
|
});
|
||
|
|
});
|