test(mock): enforce backend series metric rules

Port seriesValidMetric and the usage/series query checks into the mock so it
returns the backend 400 for an invalid scope, metric and window combination.
This commit is contained in:
2026-11-25 12:38:58 +00:00
parent fd9dc53241
commit b48ec65f94
4 changed files with 190 additions and 30 deletions
+6 -1
View File
@@ -1,5 +1,6 @@
import { matchFixture, nearestFixture, normaliseQuery, type Fixture } from './match.ts'
import { applyScenarios, errorEnvelope, hotUserIds, scenarioDelayMs } from './scenarios.ts'
import { seriesQueryError } from './series-rules.ts'
import { fitSeriesScopes, synthesise } from './synth.ts'
import { CAPTURE_AT, shiftTimestamps } from './time-shift.ts'
@@ -23,7 +24,7 @@ export interface MockResponse {
const SERIES = '/admin/api/usage/series'
const MODELS = '/admin/api/usage/models'
const SERIES_SCOPE = /^(model|provider|group|user):.+$|^total$/
const SERIES_SCOPE = /^(model|provider|provmodel|group|user):.+$|^total$/
const WINDOWS = ['hour', 'day', 'month']
const JITTER = 0.15
const RANGE_TOO_LARGE = 'range too large; narrow span or filter'
@@ -56,6 +57,10 @@ function limitRows(path: string, query: URLSearchParams, body: Obj): Obj {
function resolveBase(i: RespondInput): { status: number; body: unknown } {
const { manifest, path, query, now } = i
if (!manifest.some((f) => f.path === path)) return notFound(`unknown admin route ${path}`)
if (path === SERIES) {
const invalid = seriesQueryError(query)
if (invalid) return { status: 400, body: errorEnvelope(400, 'invalid_request_error', invalid) }
}
const bad = validQuery(path, query)
if (bad) return notFound(bad)
+78
View File
@@ -0,0 +1,78 @@
// Mirror of the backend's GET /admin/api/usage/series validation (stats_read.go, admin.go). Keep case-for-case in sync.
export const SERIES_MAX_SCOPES = 100
const HISTORY_MAX_SPAN: Record<string, number> = { hour: 48, day: 35, month: 13 }
const HISTORY_METRICS = ['req', 'tokin', 'tokout', 'cost']
const KNOWN_STATS_METRICS = ['rej', 'attempt', 'fail', 'timeout', 'fover', 'r402', 'chit', 'cmiss', 'csave']
export type SeriesScopeKind = 'user' | 'group' | 'total' | 'model' | 'prov' | 'provmodel'
/** parseSeriesScope: the scope kind a `scope` param names, or null when the backend would list it as unknown. */
export function parseSeriesScope(raw: string): SeriesScopeKind | null {
if (raw === 'total') return 'total'
const at = raw.indexOf(':')
if (at < 0 || at === raw.length - 1) return null
switch (raw.slice(0, at)) {
case 'user':
case 'group':
case 'model':
case 'provmodel':
return raw.slice(0, at) as SeriesScopeKind
case 'provider':
return 'prov'
default:
return null
}
}
/** seriesValidMetric: stats_read.go's metric x scope-kind x window matrix. */
export function seriesValidMetric(scopeKind: SeriesScopeKind, metric: string, window: string): boolean {
const hourOrDay = window === 'hour' || window === 'day'
switch (scopeKind) {
case 'user':
case 'group':
case 'total':
if (['req', 'tokin', 'tokout', 'cost'].includes(metric)) return true
if (metric === 'rej') return hourOrDay
if (scopeKind === 'total' && ['chit', 'cmiss', 'csave', 'r402'].includes(metric)) return hourOrDay
return false
case 'model':
if (['req', 'tokin', 'tokout', 'cost'].includes(metric)) return true
if (['chit', 'csave', 'r402'].includes(metric)) return window === 'day'
return false
case 'prov':
return ['attempt', 'fail', 'timeout', 'fover'].includes(metric) && hourOrDay
case 'provmodel':
return ['attempt', 'fail'].includes(metric) && hourOrDay
}
}
const isInt = (raw: string): boolean => /^[+-]?\d+$/.test(raw)
/** seriesQueryError: the 400 message serveAdminUsageSeries returns for this query, or null when the backend accepts it. Same check order as the Go handler. */
export function seriesQueryError(query: URLSearchParams): string | null {
const scopes = query.getAll('scope')
if (scopes.length === 0) return 'at least one scope is required'
if (scopes.length > SERIES_MAX_SCOPES) return 'too many scopes'
const window = query.get('window') ?? ''
const max = HISTORY_MAX_SPAN[window]
if (max === undefined) return 'unknown window'
const metric = query.get('metric') ?? ''
if (!HISTORY_METRICS.includes(metric) && !KNOWN_STATS_METRICS.includes(metric)) return 'unknown metric'
const rawSpan = query.get('span') ?? ''
let span = max
if (rawSpan !== '') {
span = Number(rawSpan)
if (!isInt(rawSpan) || span < 1 || span > max) return 'invalid span'
}
const rawOffset = query.get('offset') ?? ''
if (rawOffset !== '') {
const offset = Number(rawOffset)
if (!isInt(rawOffset) || offset < 0 || offset > max - span) return 'invalid offset'
}
for (const raw of scopes) {
const kind = parseSeriesScope(raw)
if (kind !== null && !seriesValidMetric(kind, metric, window)) return 'unknown metric for scope kind'
}
return null
}