import test from 'node:test'
import assert from 'node:assert/strict'
import fixtures from './fixtures.json' with { type: 'json' }
import { key, replay } from './replay.js'
const WIN = { from: '-1h', to: 'now' }
test('every key the UI derives is a key the capture recorded', () => {
const asked = [
key('/api/v1/alerts', {}),
key('/api/v1/entities', WIN),
key('/api/v1/map', WIN),
key('/api/v1/metrics/names', WIN),
key('/api/v1/query', { signal: 'logs', ...WIN, where: [] }),
key('/api/v1/query', { signal: 'traces', ...WIN, where: [] }),
key('/api/v1/query', { signal: 'logs', ...WIN, where: [], after: 'cursor' }),
key('/api/v1/query', { signal: 'traces', ...WIN, where: [], after: 'cursor' }),
key('/api/v1/correlate', { signal: 'logs', ...WIN }),
key('/api/v1/correlate', { signal: 'traces', ...WIN }),
]
for (const k of asked) assert.ok(fixtures[k], `no fixture for ${k}`)
})
test('a trace the capture never saw still opens a waterfall', () => {
const unseen = { signal: 'traces', where: [{ field: 'trace_id', eq: 'ff'.repeat(16) }] }
assert.equal(key('/api/v1/query', unseen), `query:trace:${'ff'.repeat(16)}`)
assert.ok(replay('/api/v1/query', unseen).rows.length > 0)
})
test('a metric the capture never saw still draws a chart', () => {
assert.ok(replay('/api/v1/metrics/query', { name: 'no.such.metric' }).series.length > 0)
})
test('the last page carries no cursor', () => {
for (const sig of ['logs', 'traces']) {
const last = replay('/api/v1/query', { signal: sig, where: [], after: 'cursor' })
assert.equal(last.next, '', `query:${sig}:2 still has a cursor`)
assert.ok(last.rows.length > 0)
}
})
test('a request nobody recorded answers empty rather than failing', () => {
const empty = replay('/api/v1/nothing-like-this', {})
assert.deepEqual(empty.rows, [])
assert.equal(empty.stats.elapsed_us, 0)
})
test('a replayed response is a copy, because components mutate what they get', () => {
const first = replay('/api/v1/query', { signal: 'logs', where: [] })
const n = first.rows.length
first.rows.push({ body: 'not from the capture' })
assert.equal(replay('/api/v1/query', { signal: 'logs', where: [] }).rows.length, n)
})