import { describe, expect, it } from 'vitest' import type { CoolRouteBundle, CoolRouteOption } from '../shared/cool' import { buildGpx, compassDirection, isUseCaseId, prezeroReason, prezeroTotal, priorityShare, routeComparison, routeRationale, routeTitle, routeValuesFor, staffingSnapshot, } from '../shared/use-cases' import type { PrezeroBundle, PrezeroContainer } from '../shared/use-cases' import type { StaffingResponse } from '../shared/types' function route( id: CoolRouteOption['id'], durationMin: number, shadePct: number, directSunMin: number, rampInclinePct: number | null, ): CoolRouteOption { return { id, label: id, longName: id, description: `${id} description`, fullDescription: `${id} full description`, values: { '16:00': { shadePct, directSunMin, distanceM: 400, durationMin, stepFree: true, waterStops: 2, benchStops: 6, rampInclinePct, }, }, ramp: rampInclinePct ? { incline: `${rampInclinePct}%`, location: 'Marktplatz', wheelchair: 'yes' } : null, accessibilityNotes: [], geometry: [ [9.2184, 49.1402], [9.2189, 49.141], [9.2193, 49.1423], ], } } const bundle: CoolRouteBundle = { status: 'ready', generatedAt: '2026-08-14', hotDay: { date: '2026-08-14', temperature_max_c: 35, temperature_min_c: 18.2, precipitation_mm: 0, sunshine_hours: 13.9, source: 'Open-Meteo', }, label: 'modeled day', shadeLabel: 'modeled shade, not measured', origin: { name: 'Stadtgalerie', lat: 49.140269, lon: 9.218444 }, destination: { name: 'Marktplatz', lat: 49.142318, lon: 9.218659 }, times: ['10:00', '12:00', '14:00', '16:00'], routes: [ route('shortest', 4.8, 43.8, 2.7, 15), route('shaded', 9.8, 50, 4.9, 15), route('accessible', 5.8, 43.8, 3.3, null), ], recommendationByTime: { '10:00': 'shortest', '12:00': 'shortest', '14:00': 'accessible', '16:00': 'accessible' }, modelLabel: 'Accessible for Ruth', water: [], meta: { city: 'Heilbronn', shadeLabel: 'modeled shade, not measured', method: 'test method', solarPositions: { '16:00': { altitude_deg: 43.3, azimuth_deg: 235.1 }, }, dataGaps: ['test gap'], walkingSpeedMps: 1.25, }, } const container: PrezeroContainer = { location_id: 'eecbf6fa-decf-46da-b110-bf014e7c181d', priority: 'high', priority_score: 21.5, readings: 80, overfull_count: 21, overfull_rate: 0.2625, p95_fill: 100, max_fill: 100, mean_fill: 64.24, last_fill: null, last_is_overfull: true, last_date: '2026-09-29', days_since_last: 1, trend: 30, materials: {}, } const prezero: PrezeroBundle = { meta: { source: 'test.csv', generated_at: '2026-11-14T16:53:20Z', rows: 11701, containers: 449, material_series: 1338, date_from: '2025-10-17', date_to: '2026-09-30', reference_date: '2026-09-30', overfull_readings: 1192, priority_rule: 'high ...', limitations: ['No coordinates'], }, summary: { priority_counts: { high: 88, medium: 248, low: 113 }, by_material: { white: { readings: 4432, overfull: 731 }, green: { readings: 4169, overfull: 357 }, brown: { readings: 3100, overfull: 104 }, }, }, top: [container], } describe('use case ids', () => { it('accepts only the three implemented use cases', () => { expect(isUseCaseId('staffing')).toBe(true) expect(isUseCaseId('routing')).toBe(true) expect(isUseCaseId('prezero')).toBe(true) expect(isUseCaseId('uc4')).toBe(false) }) }) describe('route comparison', () => { it('uses the model recommendation and real duration penalties', () => { const rows = routeComparison(bundle, '16:00') expect(rows.map((row) => row.id)).toEqual(['shortest', 'shaded', 'accessible']) expect(rows.find((row) => row.recommended)?.id).toBe('accessible') expect(rows.find((row) => row.id === 'shaded')?.penaltyMin).toBe(5) expect(rows.find((row) => row.id === 'accessible')?.penaltyMin).toBe(1) expect(rows.find((row) => row.recommended)?.tag).toBe('Recommended') }) it('returns no rows when the bundle is missing', () => { expect(routeComparison(null, '16:00')).toEqual([]) }) it('keeps the real shade and sun values', () => { expect(routeValuesFor(bundle, 'accessible', '16:00')?.shadePct).toBe(43.8) expect(routeValuesFor(bundle, 'accessible', '16:00')?.rampInclinePct).toBe(null) expect(routeTitle('shaded')).toBe('Shaded route') }) }) describe('route rationale', () => { it('mentions the actual solar position, shade and the step-free trade-off', () => { const text = routeRationale(bundle, '16:00') expect(text).toContain('solar azimuth 235° SW') expect(text).toContain('43.8% shaded') expect(text).toContain('3.3 min of direct sun') expect(text).toContain('step-free with no ramp') expect(text).toContain('1 min longer') expect(text).not.toContain('4.8') }) it('never invents a route when the bundle is unavailable', () => { expect(routeRationale(null, '16:00')).toContain('unavailable') }) }) describe('compass and GPX', () => { it('maps solar azimuth to a compass label', () => { expect(compassDirection(0)).toBe('N') expect(compassDirection(110.8)).toBe('E') expect(compassDirection(235.1)).toBe('SW') }) it('exports the real geometry as GPX 1.1', () => { const xml = buildGpx(bundle, 'accessible', '16:00', '2026-11-14T20:00:00Z') expect(xml).toContain('') expect(xml).toContain('modeled shade, not measured') expect(xml).toContain('') }) it('refuses to export a route without geometry', () => { const empty = { ...bundle, routes: [{ ...bundle.routes[0]!, geometry: [] }] } expect(() => buildGpx(empty, 'shortest', '16:00')).toThrow('No geometry') }) }) describe('staffing snapshot', () => { const plan: StaffingResponse = { sensor: 'hn-stadtgalerie-01', scenario: 'base', standard: 'standard', assumptions: { visitors_per_staff_hour: 25, safety_factor: 1.15, min_staff: 1, shift_length_h: 4, labor_cost_eur_h: 16, store_hours: { open: '10:00', close: '20:00', tz: 'Europe/Berlin' }, }, hours: [ { t: '2026-11-16T09:00:00Z', footfall: 10, required_staff: 1, scheduled_staff: 2, coverage: 'over' }, { t: '2026-11-16T10:00:00Z', footfall: 90, required_staff: 5, scheduled_staff: 5, coverage: 'ok' }, { t: '2026-11-16T11:00:00Z', footfall: 50, required_staff: 3, scheduled_staff: 5, coverage: 'over' }, ], shifts: [ { date: '2026-11-16', start: '10:00', end: '14:00', staff: 5 }, { date: '2026-11-16', start: '14:00', end: '18:00', staff: 4 }, ], summary: { coverage_pct: 100, cost_eur: 192, flat_schedule_cost_eur: 320, overstaffed_hours: 2, understaffed_hours: 0, }, why: ['forecast peak'], cached: false, data_version: 'd', model_version: 'm', context_version: 'c', } it('finds the peak hour, peak shift, staff hours and savings', () => { const snapshot = staffingSnapshot(plan) expect(snapshot.peakHour?.footfall).toBe(90) expect(snapshot.peakShift?.staff).toBe(5) expect(snapshot.staffHours).toBe(12) expect(snapshot.savedEur).toBe(128) expect(snapshot.savedPct).toBe(40) expect(snapshot.days).toBe(1) }) it('returns a safe empty snapshot without a plan', () => { const snapshot = staffingSnapshot(null) expect(snapshot.peakHour).toBeNull() expect(snapshot.staffHours).toBe(0) }) }) describe('prezero helpers', () => { it('counts all locations and their shares', () => { expect(prezeroTotal(prezero)).toBe(449) expect(priorityShare(prezero, 'high')).toBeCloseTo(19.6, 1) expect(prezeroTotal(null)).toBe(0) }) it('builds a reason from the artifact fields only', () => { const reason = prezeroReason(container) expect(reason).toContain('21 overfull readings') expect(reason).toContain('26% of visits') expect(reason).toContain('p95 fill 100%') }) })