Files
web-nuxt/scripts/smoke-api.mjs
Noel Sabia b33e6ff001 feat: wire Nitro API to Go, real staffing plan and train proxy
- Replace mock Nitro routes with server-side proxies to the Go API
- /plan reads Go /api/staffing; local assumption edits are a labeled preview
- /simulate reads base and scenario staffing from Go instead of local multipliers
- Route train upload and scenario POST through Nitro with server-only credentials
- Keep NUXT_PUBLIC_USE_MOCK=1 as an explicit dev fallback only
- Match Go's global-peak flat schedule and add a captured Go parity fixture
- Run the web runtime as numeric UID/GID 10001:10001
- Add make check, Gitea CI workflow and a proxy smoke script
2026-11-14 06:13:00 +01:00

134 lines
4.8 KiB
JavaScript

#!/usr/bin/env node
// Smoke-test the Nitro -> Go proxy chain against a running web server.
// Usage: WEB_URL=http://localhost:3000 node scripts/smoke-api.mjs
const base = (process.env.WEB_URL ?? 'http://localhost:3000').replace(/\/+$/, '')
let failures = 0
async function request(path, expectedStatus = 200) {
const response = await fetch(`${base}${path}`, { headers: { accept: 'application/json' } })
const text = await response.text()
let body
try {
body = JSON.parse(text)
} catch {
throw new Error(`${path} returned non-JSON (${response.status}): ${text.slice(0, 120)}`)
}
if (response.status !== expectedStatus) {
throw new Error(`${path} returned ${response.status}, expected ${expectedStatus}: ${text.slice(0, 200)}`)
}
return body
}
async function check(name, fn) {
try {
const detail = await fn()
console.log(`PASS ${name}${detail ? ` — ${detail}` : ''}`)
} catch (error) {
failures += 1
console.log(`FAIL ${name} — ${error.message}`)
}
}
await check('/api/healthz', async () => {
const body = await request('/api/healthz')
if (body.ok !== true) throw new Error('ok is not true')
return 'ok'
})
let sensors = []
await check('/api/sensors', async () => {
sensors = await request('/api/sensors')
if (!Array.isArray(sensors) || sensors.length === 0) throw new Error('empty sensor list')
if (!sensors[0].id || !sensors[0].name) throw new Error('sensor missing id/name')
return `${sensors.length} sensors, first=${sensors[0].id}`
})
const sensor = sensors[0]?.id ?? 'hn-stadtgalerie-01'
await check('/api/context', async () => {
const body = await request('/api/context')
if (!body.weather?.now?.t) throw new Error('weather.now missing')
if (!Array.isArray(body.holidays)) throw new Error('holidays missing')
return `${body.holidays.length} holidays, updated=${body.updated_at}`
})
await check('/api/meta', async () => {
const body = await request('/api/meta')
if (!body.model_version || !body.data_version) throw new Error('versions missing')
return `model=${body.model_version}`
})
await check('/api/series', async () => {
const body = await request(`/api/series?sensor=${encodeURIComponent(sensor)}&step=1h`)
if (!Array.isArray(body.points) || body.points.length === 0) throw new Error('no points')
return `${body.points.length} hourly points`
})
await check('/api/forecast 7d base', async () => {
const body = await request(
`/api/forecast?sensor=${encodeURIComponent(sensor)}&horizon=7d&scenario=base`,
)
if (!Array.isArray(body.points) || body.points.length === 0) throw new Error('no points')
return `${body.points.length} points, cached=${body.cached}`
})
await check('/api/forecast 7d rainy-saturday', async () => {
const body = await request(
`/api/forecast?sensor=${encodeURIComponent(sensor)}&horizon=7d&scenario=rainy-saturday`,
)
if (body.scenario !== 'rainy-saturday') throw new Error('scenario not echoed')
return `${body.points.length} points`
})
await check('/api/staffing standard', async () => {
const body = await request(
`/api/staffing?sensor=${encodeURIComponent(sensor)}&scenario=base&standard=standard`,
)
if (!Array.isArray(body.hours) || body.hours.length === 0) throw new Error('no plan hours')
if (!Array.isArray(body.shifts) || body.shifts.length === 0) throw new Error('no shifts')
if (!body.summary || typeof body.summary.coverage_pct !== 'number') {
throw new Error('summary missing')
}
return `${body.hours.length} hours, ${body.shifts.length} shifts, cost=${body.summary.cost_eur}`
})
await check('/api/staffing premium', async () => {
const body = await request(
`/api/staffing?sensor=${encodeURIComponent(sensor)}&scenario=base&standard=premium`,
)
if (body.standard !== 'premium') throw new Error('standard not echoed')
return `cost=${body.summary.cost_eur}`
})
await check('/api/scenarios local presets', async () => {
const body = await request('/api/scenarios')
if (!Array.isArray(body) || body.length < 4) throw new Error('preset list missing')
return `${body.length} presets`
})
await check('unknown sensor propagates 404', async () => {
const body = await request('/api/staffing?sensor=does-not-exist', 404)
if (!body.error?.message && !body.statusMessage && !body.message) {
throw new Error('no error message')
}
return body.statusMessage ?? body.message ?? body.error?.message ?? '404'
})
await check('invalid standard propagates 400', async () => {
const body = await request(
`/api/staffing?sensor=${encodeURIComponent(sensor)}&standard=luxury`,
400,
)
if (!body.error?.message && !body.statusMessage && !body.message) {
throw new Error('no error message')
}
return body.statusMessage ?? body.message ?? body.error?.message ?? '400'
})
if (failures > 0) {
console.error(`\n${failures} smoke check(s) failed against ${base}`)
process.exit(1)
}
console.log(`\nAll smoke checks passed against ${base}`)