mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
feat(server): add nest AppConfigModule bound to @nestjs/config
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* AppConfigModule wired into the real buildApp(): boot-stable registerAs
|
||||
* snapshots must re-derive on every app build (the env-mutate-then-rebuild
|
||||
* pattern the integration suite relies on), and RuntimeEnvService must stay
|
||||
* live within a single app's lifetime.
|
||||
*/
|
||||
import { describe, it, expect, vi, afterEach, beforeAll } from 'vitest';
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
|
||||
const { testDb, dbMock } = vi.hoisted(() => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const Database = require('better-sqlite3');
|
||||
const db = new Database(':memory:');
|
||||
db.exec('PRAGMA journal_mode = WAL');
|
||||
db.exec('PRAGMA foreign_keys = ON');
|
||||
db.exec('PRAGMA busy_timeout = 5000');
|
||||
const mock = {
|
||||
db,
|
||||
closeDb: () => {},
|
||||
reinitialize: () => {},
|
||||
getPlaceWithTags: () => null,
|
||||
canAccessTrip: () => undefined,
|
||||
isOwner: () => false,
|
||||
};
|
||||
return { testDb: db, dbMock: mock };
|
||||
});
|
||||
|
||||
vi.mock('../../src/db/database', () => dbMock);
|
||||
vi.mock('../../src/config', () => ({
|
||||
JWT_SECRET: 'test-jwt-secret-for-trek-testing-only',
|
||||
ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
|
||||
updateJwtSecret: () => {},
|
||||
SESSION_DURATION: '24h',
|
||||
SESSION_DURATION_MS: 86400000,
|
||||
SESSION_DURATION_SECONDS: 86400,
|
||||
DEFAULT_LANGUAGE: 'en',
|
||||
}));
|
||||
vi.mock('../../src/websocket', () => ({ broadcast: vi.fn(), broadcastToUser: vi.fn() }));
|
||||
|
||||
import { createTables } from '../../src/db/schema';
|
||||
import { runMigrations } from '../../src/db/migrations';
|
||||
import { resetTestDb } from '../helpers/test-db';
|
||||
import { buildApp } from '../../src/bootstrap';
|
||||
import { httpConfig, RuntimeEnvService } from '../../src/nest/app-config';
|
||||
import type { ConfigType } from '@nestjs/config';
|
||||
|
||||
describe('AppConfigModule in the real buildApp()', () => {
|
||||
let app: INestApplication | undefined;
|
||||
let prevForceHttps: string | undefined;
|
||||
let prevDemo: string | undefined;
|
||||
|
||||
beforeAll(() => {
|
||||
createTables(testDb);
|
||||
runMigrations(testDb);
|
||||
resetTestDb(testDb);
|
||||
prevForceHttps = process.env.FORCE_HTTPS;
|
||||
prevDemo = process.env.DEMO_MODE;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app?.close();
|
||||
app = undefined;
|
||||
if (prevForceHttps === undefined) delete process.env.FORCE_HTTPS;
|
||||
else process.env.FORCE_HTTPS = prevForceHttps;
|
||||
if (prevDemo === undefined) delete process.env.DEMO_MODE;
|
||||
else process.env.DEMO_MODE = prevDemo;
|
||||
});
|
||||
|
||||
it('boot-stable snapshots re-derive per app build (mutate → rebuild → new value)', async () => {
|
||||
process.env.FORCE_HTTPS = 'true';
|
||||
app = await buildApp();
|
||||
let http = app.get<ConfigType<typeof httpConfig>>(httpConfig.KEY);
|
||||
expect(http.forceHttps).toBe(true);
|
||||
await app.close();
|
||||
|
||||
process.env.FORCE_HTTPS = 'off';
|
||||
app = await buildApp();
|
||||
http = app.get<ConfigType<typeof httpConfig>>(httpConfig.KEY);
|
||||
expect(http.forceHttps).toBe(false);
|
||||
});
|
||||
|
||||
it('a snapshot does NOT move within one app lifetime, RuntimeEnvService does', async () => {
|
||||
delete process.env.FORCE_HTTPS;
|
||||
delete process.env.DEMO_MODE;
|
||||
app = await buildApp();
|
||||
const http = app.get<ConfigType<typeof httpConfig>>(httpConfig.KEY);
|
||||
const runtime = app.get(RuntimeEnvService);
|
||||
|
||||
process.env.FORCE_HTTPS = 'true';
|
||||
process.env.DEMO_MODE = 'true';
|
||||
expect(http.forceHttps).toBe(false); // frozen at build — by design
|
||||
expect(runtime.isDemoMode()).toBe(true); // live — by design
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { ConfigService, ConfigType } from '@nestjs/config';
|
||||
|
||||
import { AppConfigModule } from '../../../src/nest/app-config/app-config.module';
|
||||
import { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
||||
import { httpConfig, mcpConfig, BOOT_STABLE_TOKENS } from '../../../src/nest/app-config/tokens';
|
||||
|
||||
describe('RuntimeEnvService', () => {
|
||||
const service = new RuntimeEnvService();
|
||||
let prevDemo: string | undefined;
|
||||
|
||||
beforeEach(() => {
|
||||
prevDemo = process.env.DEMO_MODE;
|
||||
});
|
||||
afterEach(() => {
|
||||
if (prevDemo === undefined) delete process.env.DEMO_MODE;
|
||||
else process.env.DEMO_MODE = prevDemo;
|
||||
});
|
||||
|
||||
it('reads live — a mid-lifetime env mutation is visible on the next call', () => {
|
||||
process.env.DEMO_MODE = 'true';
|
||||
expect(service.isDemoMode()).toBe(true);
|
||||
process.env.DEMO_MODE = 'off';
|
||||
expect(service.isDemoMode()).toBe(false);
|
||||
delete process.env.DEMO_MODE;
|
||||
expect(service.isDemoMode()).toBe(false);
|
||||
});
|
||||
|
||||
it('env() exposes the full live-derived namespaces', () => {
|
||||
process.env.DEMO_MODE = 'yes';
|
||||
expect(service.env().demo.enabled).toBe(true);
|
||||
expect(service.env().app.port).toBeTypeOf('number');
|
||||
});
|
||||
});
|
||||
|
||||
describe('boot-stable tokens', () => {
|
||||
it('factories re-derive from the current env on each invocation', () => {
|
||||
const prev = process.env.FORCE_HTTPS;
|
||||
try {
|
||||
process.env.FORCE_HTTPS = 'on';
|
||||
expect(httpConfig().forceHttps).toBe(true);
|
||||
process.env.FORCE_HTTPS = 'off';
|
||||
expect(httpConfig().forceHttps).toBe(false);
|
||||
} finally {
|
||||
if (prev === undefined) delete process.env.FORCE_HTTPS;
|
||||
else process.env.FORCE_HTTPS = prev;
|
||||
}
|
||||
});
|
||||
|
||||
it('every token carries a namespaced KEY for @Inject', () => {
|
||||
for (const token of BOOT_STABLE_TOKENS) {
|
||||
expect(token.KEY).toMatch(/^CONFIGURATION\(.+\)$/);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('AppConfigModule', () => {
|
||||
it('provides ConfigService, RuntimeEnvService and the loaded namespaces', async () => {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [AppConfigModule] }).compile();
|
||||
try {
|
||||
const config = moduleRef.get(ConfigService);
|
||||
const runtime = moduleRef.get(RuntimeEnvService);
|
||||
const mcp = moduleRef.get<ConfigType<typeof mcpConfig>>(mcpConfig.KEY);
|
||||
expect(runtime).toBeInstanceOf(RuntimeEnvService);
|
||||
expect(mcp.rateLimitMax).toBe(300);
|
||||
// Plain get() falls through to live process.env (cache: false).
|
||||
expect(config.get('NODE_ENV')).toBe('test');
|
||||
} finally {
|
||||
await moduleRef.close();
|
||||
}
|
||||
});
|
||||
|
||||
// Guards `ignoreEnvFile: true`: a marker var present only in server/.env must
|
||||
// stay invisible. Skipped locally when a real server/.env exists (we won't
|
||||
// clobber it); CI has none, so the regression is always covered there.
|
||||
const envPath = path.resolve(process.cwd(), '.env');
|
||||
it.skipIf(fs.existsSync(envPath))('never reads a .env file (dotenv stays index.ts-only)', async () => {
|
||||
fs.writeFileSync(envPath, 'TREK_TEST_ENVFILE_MARKER=leaked\n');
|
||||
try {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [AppConfigModule] }).compile();
|
||||
try {
|
||||
const config = moduleRef.get(ConfigService);
|
||||
expect(config.get('TREK_TEST_ENVFILE_MARKER')).toBeUndefined();
|
||||
expect(process.env.TREK_TEST_ENVFILE_MARKER).toBeUndefined();
|
||||
} finally {
|
||||
await moduleRef.close();
|
||||
}
|
||||
} finally {
|
||||
fs.rmSync(envPath, { force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user