prevision / apps /prevision-api /src /config /app-config.spec.ts
github-actions[bot]
Automated Deploy: 23110475c742a5d4f03930b55bca09042215c1dd
1fe7075
Raw History Blame Contribute Delete
2.48 kB
import { isOriginAllowed, loadConfig } from './app-config.js';
const base = {
PREVISION_DATABASE_URL: 'postgresql://u:p@db.test:5432/app',
CLERK_SECRET_KEY: 'sk_test_x',
PROCESSING_TOKEN_SECRET: 'x'.repeat(40),
STRIPE_SECRET_KEY: 'sk_test_x',
STRIPE_WEBHOOK_SECRET: 'whsec_x',
STRIPE_PRICE_STARTER: 'price_s',
STRIPE_PRICE_GROWTH: 'price_g',
};
describe('loadConfig', () => {
it('applies defaults', () => {
const config = loadConfig(base);
expect(config.port).toBe(8001);
expect(config.freeGenerations).toBe(10);
expect(config.processingToken).toMatchObject({ issuer: 'prevision-api', audience: 'prevision-processing', ttlSeconds: 600 });
expect(config.stripe.plans).toEqual({
starter: { priceId: 'price_s', monthlyGenerations: 150 },
growth: { priceId: 'price_g', monthlyGenerations: 600 },
});
expect(config.appUrl).toBe('http://localhost:5173');
});
it('accepts a comma-separated list of Clerk secret keys', () => {
expect(loadConfig({ ...base, CLERK_SECRET_KEY: 'sk_live_a, sk_test_b' }).clerk.secretKeys).toEqual(['sk_live_a', 'sk_test_b']);
expect(() => loadConfig({ ...base, ENVIRONMENT: 'staging', CLERK_SECRET_KEY: 'sk_live_a,pk_live_b' })).toThrow(/Clerk secret keys/);
});
it('matches exact origins and the preview regex', () => {
const config = loadConfig({
...base,
PREVISION_ALLOWED_ORIGINS: 'https://a.test/, https://b.test',
PREVISION_ALLOWED_ORIGIN_REGEX: '^https://prevision-[a-z0-9]+-team\\.vercel\\.app$',
});
expect(isOriginAllowed(config, 'https://a.test')).toBe(true);
expect(isOriginAllowed(config, 'https://prevision-abc-team.vercel.app')).toBe(true);
expect(isOriginAllowed(config, 'https://prevision-abc-team.vercel.app.evil.test')).toBe(false);
expect(isOriginAllowed(config, undefined)).toBe(false);
});
it('refuses missing or weak settings', () => {
expect(() => loadConfig({})).toThrow(/PREVISION_DATABASE_URL is required.*CLERK_SECRET_KEY is required/);
expect(() => loadConfig({ ...base, ENVIRONMENT: 'staging', PROCESSING_TOKEN_SECRET: 'short' })).toThrow(/at least 32/);
expect(() => loadConfig({ ...base, FREE_GENERATIONS: 'many' })).toThrow(/whole number/);
expect(() => loadConfig({ ...base, STRIPE_PRICE_GROWTH: '' })).toThrow(/STRIPE_PRICE_GROWTH is required/);
expect(() => loadConfig({ ...base, ENVIRONMENT: 'staging', STRIPE_WEBHOOK_SECRET: 'nope' })).toThrow(/webhook signing secret/);
});
});