94 lines
2.9 KiB
TypeScript
94 lines
2.9 KiB
TypeScript
/**
|
|
* Tests for APOPHIS_DEBUG mode.
|
|
*/
|
|
|
|
import { test } from 'node:test'
|
|
import assert from 'node:assert'
|
|
import Fastify from 'fastify'
|
|
import apophisPlugin from '../index.js'
|
|
|
|
test('APOPHIS_DEBUG=1 logs requests and responses', async () => {
|
|
const fastify = Fastify() as any
|
|
const originalEnv = process.env.APOPHIS_DEBUG
|
|
const logs: string[] = []
|
|
|
|
try {
|
|
process.env.APOPHIS_DEBUG = '1'
|
|
|
|
await fastify.register(await import('@fastify/swagger'), {})
|
|
await fastify.register(apophisPlugin, { validateRuntime: false })
|
|
|
|
fastify.get('/test', {
|
|
schema: {
|
|
'x-category': 'observer',
|
|
'x-ensures': ['status:200'],
|
|
response: { 200: { type: 'object', properties: { ok: { type: 'boolean' } } } }
|
|
}
|
|
}, async () => ({ ok: true }))
|
|
|
|
await fastify.ready()
|
|
|
|
// Monkey-patch log.debug to capture logs
|
|
const { log } = await import('../infrastructure/logger.js')
|
|
const originalDebug = log.debug.bind(log)
|
|
log.debug = (msg: string, _obj?: Record<string, unknown>) => {
|
|
logs.push(msg)
|
|
originalDebug(msg, _obj)
|
|
}
|
|
|
|
const result = await fastify.apophis.contract({ runs: 10 })
|
|
assert.ok(result.tests.length > 0, 'should have tests')
|
|
|
|
// Should have logged at least one request and one response
|
|
const requestLogs = logs.filter(l => l.startsWith('→'))
|
|
const responseLogs = logs.filter(l => l.startsWith('←'))
|
|
|
|
assert.ok(requestLogs.length > 0, 'should log requests')
|
|
assert.ok(responseLogs.length > 0, 'should log responses')
|
|
} finally {
|
|
process.env.APOPHIS_DEBUG = originalEnv
|
|
await fastify.close()
|
|
}
|
|
})
|
|
|
|
test('APOPHIS_DEBUG=0 does not log requests', async () => {
|
|
const fastify = Fastify() as any
|
|
const originalEnv = process.env.APOPHIS_DEBUG
|
|
const logs: string[] = []
|
|
|
|
try {
|
|
process.env.APOPHIS_DEBUG = '0'
|
|
|
|
await fastify.register(await import('@fastify/swagger'), {})
|
|
await fastify.register(apophisPlugin, { validateRuntime: false })
|
|
|
|
fastify.get('/test', {
|
|
schema: {
|
|
'x-category': 'observer',
|
|
'x-ensures': ['status:200'],
|
|
response: { 200: { type: 'object', properties: { ok: { type: 'boolean' } } } }
|
|
}
|
|
}, async () => ({ ok: true }))
|
|
|
|
await fastify.ready()
|
|
|
|
// Monkey-patch log.debug to capture logs
|
|
const { log } = await import('../infrastructure/logger.js')
|
|
const originalDebug = log.debug.bind(log)
|
|
log.debug = (msg: string, _obj?: Record<string, unknown>) => {
|
|
logs.push(msg)
|
|
originalDebug(msg, _obj)
|
|
}
|
|
|
|
const result = await fastify.apophis.contract({ runs: 10 })
|
|
assert.ok(result.tests.length > 0, 'should have tests')
|
|
|
|
// Should not have any request/response logs
|
|
const debugLogs = logs.filter(l => l.startsWith('→') || l.startsWith('←'))
|
|
assert.strictEqual(debugLogs.length, 0, 'should not log requests when APOPHIS_DEBUG=0')
|
|
} finally {
|
|
process.env.APOPHIS_DEBUG = originalEnv
|
|
await fastify.close()
|
|
}
|
|
})
|