import assert from 'node:assert/strict'; import { test } from 'node:test'; import { getServerConfig } from '../src/config/server-config'; const env = { JWT_SECRET: 'test-configuration-secret-12345678901234567890' }; test('启动配置必须提供有效 JWT 密钥,不使用共享默认密钥', () => { for (const secret of [ undefined, '', 'short', 'change-me-123456789012345678901234567890', ]) { assert.throws( () => getServerConfig( secret === undefined ? {} : { JWT_SECRET: secret }, ), /JWT_SECRET/, ); } }); test('Swagger 开发默认开启,生产默认关闭,支持显式开关', () => { assert.equal(getServerConfig(env).swaggerEnabled, true); assert.equal( getServerConfig({ ...env, NODE_ENV: 'production' }).swaggerEnabled, false, ); assert.equal( getServerConfig({ ...env, SWAGGER_ENABLED: 'false' }).swaggerEnabled, false, ); }); test('拒绝非法 body、JWT 有效期、日志级别、布尔开关和跨域配置', () => { for (const entry of [ { API_BODY_LIMIT_BYTES: '0' }, { API_BODY_LIMIT_BYTES: '1e6' }, { JWT_EXPIRES_IN_SECONDS: '0' }, { SWAGGER_ENABLED: 'yes' }, { LOG_LEVEL: 'all' }, { NODE_ENV: 'staging' }, { JWT_ISSUER: ' ' }, { CORS_ORIGINS: '*' }, { CORS_ORIGINS: 'https://example.com/path' }, { TRUSTED_PROXY_CIDRS: 'true' }, ]) assert.throws(() => getServerConfig({ ...env, ...entry })); assert.deepEqual( getServerConfig({ ...env, CORS_ORIGINS: 'https://example.com,http://localhost:3001', }).corsOrigins, ['https://example.com', 'http://localhost:3001'], ); });