Files
lolympus-tournament-support/apps/api/src/newrelic-config.spec.ts
T

91 lines
2.9 KiB
TypeScript

import { createRequire } from 'node:module';
const CONFIG_PATH = '../newrelic.cjs';
const requireConfig = createRequire(__filename);
interface NewRelicConfig {
app_name: string[];
license_key: string;
agent_enabled: boolean;
ai_monitoring: {
enabled: boolean;
};
custom_insights_events: {
max_samples_stored: number;
};
span_events: {
max_samples_stored: number;
};
}
interface NewRelicConfigModule {
config: NewRelicConfig;
}
function loadConfig(): NewRelicConfig {
jest.resetModules();
const module = requireConfig(CONFIG_PATH) as unknown as NewRelicConfigModule;
return module.config;
}
describe('New Relic config', () => {
let originalEnv: NodeJS.ProcessEnv;
beforeEach(() => {
originalEnv = { ...process.env };
delete process.env.NEW_RELIC_ENABLED;
delete process.env.NEW_RELIC_LICENSE_KEY;
delete process.env.NEW_RELIC_APP_NAME;
delete process.env.NEW_RELIC_AI_MONITORING_ENABLED;
delete process.env.NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED;
delete process.env.NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED;
jest.resetModules();
});
afterEach(() => {
process.env = originalEnv;
jest.resetModules();
});
it('uses safe defaults and stays disabled without a license key', () => {
const config = loadConfig();
expect(config.app_name).toEqual(['lolympus']);
expect(config.agent_enabled).toBe(false);
expect(config.ai_monitoring.enabled).toBe(true);
expect(config.custom_insights_events.max_samples_stored).toBe(100000);
expect(config.span_events.max_samples_stored).toBe(10000);
});
it('enables the agent when a license key exists and honors runtime overrides', () => {
process.env.NEW_RELIC_LICENSE_KEY = 'test-license';
process.env.NEW_RELIC_APP_NAME = 'lolympus-api-test';
process.env.NEW_RELIC_AI_MONITORING_ENABLED = 'false';
process.env.NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED = '5000';
process.env.NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED = '4000';
const config = loadConfig();
expect(config.app_name).toEqual(['lolympus-api-test']);
expect(config.license_key).toBe('test-license');
expect(config.agent_enabled).toBe(true);
expect(config.ai_monitoring.enabled).toBe(false);
expect(config.custom_insights_events.max_samples_stored).toBe(5000);
expect(config.span_events.max_samples_stored).toBe(4000);
});
it('normalizes shorthand sample sizes before New Relic applies env overrides', () => {
process.env.NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED = '100k';
process.env.NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED = '10k';
const config = loadConfig();
expect(config.custom_insights_events.max_samples_stored).toBe(100000);
expect(config.span_events.max_samples_stored).toBe(10000);
expect(
process.env.NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_SAMPLES_STORED,
).toBe('100000');
expect(process.env.NEW_RELIC_SPAN_EVENTS_MAX_SAMPLES_STORED).toBe('10000');
});
});