- proxy Go /api/stream through Nitro; drive /stage, /live, /plan and /forecast from scenario events - fall back to 5 s polling after SSE failures or heartbeat silence; show Live/Polling/Offline - authenticate /control and POST /api/scenario in Nitro with server-side control credentials - add big-screen stage with local QR and a no-login phone recommendation view - add SSE unit tests and a live smoke test; refresh README/Makefile
97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
liveAge,
|
|
liveStatus,
|
|
parseScenarioEvent,
|
|
scenarioChanged,
|
|
scenarioLabel,
|
|
SSE_FAILURES_BEFORE_POLLING,
|
|
} from '../shared/live'
|
|
import type { ScenarioState } from '../shared/types'
|
|
|
|
const state = (version: number, scenario: ScenarioState['scenario']['scenario'] = 'base'): ScenarioState => ({
|
|
version,
|
|
scenario: { scenario },
|
|
at: '2026-11-14T09:12:00Z',
|
|
})
|
|
|
|
describe('parseScenarioEvent', () => {
|
|
it('accepts the Go /api/stream scenario shape', () => {
|
|
expect(parseScenarioEvent(JSON.stringify(state(7, 'rainy-saturday')))).toEqual(
|
|
state(7, 'rainy-saturday'),
|
|
)
|
|
})
|
|
|
|
it('rejects malformed JSON, missing versions and unknown scenarios', () => {
|
|
expect(parseScenarioEvent('{')).toBeNull()
|
|
expect(parseScenarioEvent(JSON.stringify({ scenario: { scenario: 'base' }, at: 'x' }))).toBeNull()
|
|
expect(
|
|
parseScenarioEvent(JSON.stringify({ version: 1, at: 'x', scenario: { scenario: 'party' } })),
|
|
).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('scenarioChanged', () => {
|
|
it('refreshes on a new version or a new scenario id', () => {
|
|
expect(scenarioChanged(null, state(1))).toBe(true)
|
|
expect(scenarioChanged(state(1), state(1))).toBe(false)
|
|
expect(scenarioChanged(state(1), state(2))).toBe(true)
|
|
expect(scenarioChanged(state(1, 'base'), state(1, 'rainy-saturday'))).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('liveStatus', () => {
|
|
const base = {
|
|
online: true,
|
|
sseOpen: false,
|
|
sseFailures: 0,
|
|
polling: false,
|
|
pollFailures: 0,
|
|
}
|
|
|
|
it('shows connecting before the first event and live once SSE opens', () => {
|
|
expect(liveStatus(base)).toBe('connecting')
|
|
expect(liveStatus({ ...base, sseOpen: true })).toBe('live')
|
|
})
|
|
|
|
it('switches to polling after repeated SSE failures', () => {
|
|
expect(
|
|
liveStatus({
|
|
...base,
|
|
sseFailures: SSE_FAILURES_BEFORE_POLLING,
|
|
polling: true,
|
|
}),
|
|
).toBe('polling')
|
|
})
|
|
|
|
it('reports offline when the browser or both transports are down', () => {
|
|
expect(liveStatus({ ...base, online: false, sseOpen: true })).toBe('offline')
|
|
expect(
|
|
liveStatus({
|
|
...base,
|
|
sseFailures: SSE_FAILURES_BEFORE_POLLING,
|
|
polling: true,
|
|
pollFailures: 1,
|
|
}),
|
|
).toBe('offline')
|
|
})
|
|
})
|
|
|
|
describe('scenarioLabel and liveAge', () => {
|
|
it('labels every contract enum', () => {
|
|
expect(scenarioLabel('sunny-saturday-market')).toBe('Sunny Saturday + market')
|
|
expect(scenarioLabel('rainy-saturday')).toBe('Rainy Saturday')
|
|
expect(scenarioLabel('weekday-event')).toBe('Wednesday evening event')
|
|
expect(scenarioLabel('base')).toBe('Base week')
|
|
expect(scenarioLabel('unknown')).toBe('Base week')
|
|
})
|
|
|
|
it('renders a short age without a date library', () => {
|
|
const now = Date.parse('2026-11-14T09:12:10Z')
|
|
expect(liveAge('', now)).toBe('no event yet')
|
|
expect(liveAge('2026-11-14T09:12:08Z', now)).toBe('just now')
|
|
expect(liveAge('2026-11-14T09:11:40Z', now)).toBe('30s ago')
|
|
expect(liveAge('2026-11-14T09:07:10Z', now)).toBe('5 min ago')
|
|
})
|
|
})
|