screen-locker/web/src/render-health.test.ts
Krzysztof kuhy Rudnicki f3bd94f646 web: migrate status page from React to Astro
One static Astro page, vanilla TS poller and renderer,
@kuhyx/web-ui 0.5.0 for tokens only. Same web/dist output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYdwRAtMg7ki6TKxuU
2026-11-01 20:12:46 +01:00

80 lines
2.5 KiB
TypeScript

import { within } from '@testing-library/dom'
import { describe, expect, it } from 'vitest'
import { renderErrors, renderHealth } from './render'
import { makeSlot } from './test/dom'
import { makeHealth } from './test/factories'
import type { HealthPayload } from './types'
function show(over: Partial<HealthPayload> = {}) {
const slot = makeSlot()
renderHealth(slot, makeHealth(over))
return within(slot)
}
describe('renderHealth', () => {
it('reports an armed locker', () => {
const view = show()
expect(view.getByText('armed')).toHaveClass('state-ok')
expect(view.getByText('enabled and scheduled')).toBeInTheDocument()
})
it('says an unchecked arming state is NOT a pass', () => {
const view = show({ timers_checked: false, timers: [], armed: false })
expect(view.getByText(/This is not a pass/)).toBeInTheDocument()
})
it('shows the disarm marker when enforcement is switched off', () => {
const view = show({ disarmed: true, armed: false })
expect(view.getByText(/present at/)).toBeInTheDocument()
expect(view.getByText('NOT armed')).toHaveClass('state-lock')
})
it('shows a disarmed timer with its own reason', () => {
const view = show({
armed: false,
timers: [
{
name: 'workout-locker.timer',
enabled: false,
scheduled: false,
armed: false,
describe: 'DISARMED workout-locker.timer: not enabled',
},
],
})
expect(
view.getByText('DISARMED workout-locker.timer: not enabled'),
).toBeInTheDocument()
})
it('renders unknown ages rather than hiding them', () => {
const view = show({ log_age_seconds: null, last_decision_age_seconds: null })
expect(view.getAllByText('unknown ago')).toHaveLength(2)
})
it('formats known ages', () => {
const view = show()
expect(view.getByText('1h ago')).toBeInTheDocument()
expect(view.getByText('2m ago')).toBeInTheDocument()
})
})
describe('renderErrors', () => {
it('shows each distinct message once and unhides the slot', () => {
const slot = makeSlot()
slot.hidden = true
renderErrors(slot, ['boom', 'boom', 'API returned 503 Unavailable'])
expect(slot.hidden).toBe(false)
expect(within(slot).getAllByText('boom')).toHaveLength(1)
expect(slot).toHaveTextContent('API returned 503 Unavailable')
})
it('hides and empties the slot once everything recovers', () => {
const slot = makeSlot()
renderErrors(slot, ['boom'])
renderErrors(slot, [])
expect(slot.hidden).toBe(true)
expect(slot).toBeEmptyDOMElement()
})
})