Files
AndreaSlopFotografie/web/src/monitoring/config.test.ts
T
SluijsensandClaude Sonnet 5.5 49b1d5f0ba Monitoring settings (Sentry, Umami) and the CSP origins come from the build environment
One build per environment: PROD_* variables for the production artifact,
TEST_* for the test build and the CSP check. The build fails when a variable
is empty. Tests use the production values from config/monitoringTestEnv.ts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-18 19:29:51 +01:00

125 lines
3.9 KiB
TypeScript

import fc from 'fast-check';
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
monitoredHostnames,
monitoringConfig,
monitoringFor,
monitoringOrigins,
} from './config';
const dsn = 'https://key@o1.ingest.de.sentry.io/2';
const script = 'https://analytics.example.nl/script.js';
const raw = (extra: Record<string, unknown> = {}) => ({
environment: 'production',
sentryDsn: dsn,
umamiScriptUrl: script,
umamiWebsiteId: 'site-1',
...extra,
});
// Alles wat geen test- of productiehost is
const otherHostname = fc
.oneof(
fc.domain(),
fc.ipV4(),
fc.ipV6(),
fc.string(),
fc.constantFrom(
'localhost',
'127.0.0.1',
'www.andreaslop.nl',
'andreaslop.nl.',
'test.andreaslop.nl.evil.com',
'__proto__',
'constructor',
),
)
.filter((host) => !monitoredHostnames.includes(host));
afterEach(() => vi.unstubAllEnvs());
describe('monitoringConfig', () => {
it('leest de waarden uit de omgeving van de build', () => {
// De testomgeving (vite.config.ts) is de productiebuild
expect(monitoringConfig()).toMatchObject({
environment: 'production',
umamiWebsiteId: '3edbb7d1-4388-4f2d-9cef-11ade88ace96',
});
vi.stubEnv('VITE_APP_ENV', 'test');
vi.stubEnv('VITE_UMAMI_WEBSITE_ID', ' abc ');
expect(monitoringConfig()).toMatchObject({
environment: 'test',
umamiWebsiteId: 'abc',
});
});
it('geeft null als er iets ontbreekt of ongeldig is', () => {
expect(monitoringConfig(raw())).not.toBeNull();
for (const bad of [
{ environment: 'staging' },
{ environment: undefined },
{ sentryDsn: undefined },
{ sentryDsn: 'http://key@host/1' },
{ sentryDsn: 'geen url' },
{ umamiScriptUrl: '' },
{ umamiScriptUrl: 'http://analytics.example.nl/script.js' },
{ umamiWebsiteId: ' ' },
{ umamiWebsiteId: 5 },
]) {
expect(monitoringConfig(raw(bad))).toBeNull();
}
});
});
describe('monitoringFor', () => {
it('productiebuild meet alleen op de productiehost', () => {
const config = monitoringConfig(raw());
expect(monitoringFor('andreaslop.nl', config)).toEqual({
environment: 'production',
sentryDsn: dsn,
umamiScriptUrl: script,
umamiWebsiteId: 'site-1',
});
expect(monitoringFor('test.andreaslop.nl', config)).toBeNull();
});
it('testbuild meet alleen op de testhost', () => {
const config = monitoringConfig(raw({ environment: 'test' }));
expect(monitoringFor('test.andreaslop.nl', config)?.environment).toBe(
'test',
);
expect(monitoringFor('andreaslop.nl', config)).toBeNull();
});
it('meet niets zonder instellingen', () => {
expect(monitoringFor('andreaslop.nl', null)).toBeNull();
});
it('elke andere host meet niets (M1)', () => {
fc.assert(
fc.property(otherHostname, (host) => {
expect(monitoringFor(host)).toBeNull();
expect(
monitoringFor(
host,
monitoringConfig(raw({ environment: 'test' })),
),
).toBeNull();
}),
);
});
});
describe('monitoringOrigins', () => {
it('zijn de herkomst van het Umami-script en van de Sentry-ingest, zonder sleutel', () => {
expect(monitoringOrigins(monitoringConfig(raw()))).toEqual({
umami: ['https://analytics.example.nl'],
sentry: ['https://o1.ingest.de.sentry.io'],
});
});
it('zijn leeg zonder instellingen', () => {
expect(monitoringOrigins(null)).toEqual({ umami: [], sentry: [] });
});
});