import { createEasyCMS, createRestHandler, defineConfig, jsonLogger } from '@easy-cms/core' import { describe, expect, it } from 'vitest' import { db, SECRET, tempProject } from './helpers.js' const CRON = 'logs-test-cron-secret-0123456789abcdef' const config = defineConfig({ secret: SECRET, cronSecret: CRON, db: db(), collections: [ { slug: 'posts', access: { read: () => true }, fields: [{ name: 'title', type: 'text' }], }, ], endpoints: [ { path: '/boom', method: 'get', handler: () => { throw new Error('endpoint exploded') }, }, ], jobs: [{ name: 'test:log', every: 3600, run: ({ cms }) => cms.logger.info('job ran') }], }) async function setup(logs: { requests?: boolean } = {}) { const lines: Record[] = [] const cms = await createEasyCMS( { ...config, logs }, { cwd: tempProject(), scheduler: false, logger: jsonLogger((line) => lines.push(JSON.parse(line))), }, ) const handle = createRestHandler(cms) const call = (path: string, headers: Record = {}) => handle(new Request(`http://cms.test/api/cms${path}`, { headers })) return { cms, lines, call } } describe('request ids and JSON logs', () => { it('gives every response a request id, keeping a plain one from the proxy', async () => { const { cms, call } = await setup() const generated = (await call('/posts')).headers.get('x-request-id') expect(generated).toMatch(/^[0-9a-f-]{36}$/) expect((await call('/posts')).headers.get('x-request-id')).not.toBe(generated) const kept = await call('/posts', { 'x-request-id': 'lb-7f3a.42' }) expect(kept.headers.get('x-request-id')).toBe('lb-7f3a.42') const replaced = await call('/posts', { 'x-request-id': 'bad id