145 lines
4.1 KiB
TypeScript
145 lines
4.1 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
||
import type { Point } from '../shared/types'
|
||
import {
|
||
buildHeatmap,
|
||
formatBand,
|
||
scenarioDelta,
|
||
summarizeForecast,
|
||
} from '../app/utils/playground'
|
||
import type { ForecastSummary } from '../app/utils/playground'
|
||
|
||
const hourly: Point[] = [
|
||
{ t: '2026-11-16T07:00:00Z', v: 10, lo: 8, hi: 12 },
|
||
{ t: '2026-11-16T08:00:00Z', v: 20, lo: 16, hi: 24 },
|
||
{ t: '2026-11-16T09:00:00Z', v: 30, lo: 24, hi: 36 },
|
||
]
|
||
|
||
describe('summarizeForecast', () => {
|
||
it('returns peak, mean and modeled device-hours for hourly points', () => {
|
||
const summary = summarizeForecast(hourly)
|
||
|
||
expect(summary.count).toBe(3)
|
||
expect(summary.peak?.t).toBe('2026-11-16T09:00:00Z')
|
||
expect(summary.peak?.v).toBe(30)
|
||
expect(summary.mean).toBe(20)
|
||
expect(summary.total).toBe(60)
|
||
expect(summary.intervalHours).toBe(1)
|
||
})
|
||
|
||
it('scales total by the real 15-minute spacing', () => {
|
||
const points: Point[] = [
|
||
{ t: '2026-11-16T07:00:00Z', v: 10 },
|
||
{ t: '2026-11-16T07:15:00Z', v: 20 },
|
||
{ t: '2026-11-16T07:30:00Z', v: 30 },
|
||
]
|
||
const summary = summarizeForecast(points)
|
||
|
||
expect(summary.intervalHours).toBe(0.25)
|
||
expect(summary.total).toBe(15)
|
||
expect(summary.mean).toBe(20)
|
||
})
|
||
|
||
it('handles an empty forecast without inventing a point', () => {
|
||
const summary = summarizeForecast([])
|
||
|
||
expect(summary.count).toBe(0)
|
||
expect(summary.peak).toBeNull()
|
||
expect(summary.mean).toBe(0)
|
||
expect(summary.total).toBe(0)
|
||
})
|
||
})
|
||
|
||
describe('buildHeatmap', () => {
|
||
const points: Point[] = [
|
||
{ t: '2026-11-16T07:00:00Z', v: 10 },
|
||
{ t: '2026-11-16T09:00:00Z', v: 20 },
|
||
{ t: '2026-11-16T09:30:00Z', v: 40 },
|
||
{ t: '2026-11-17T07:00:00Z', v: 55 },
|
||
{ t: '2026-11-17T08:00:00Z', v: 99 },
|
||
]
|
||
|
||
it('groups by Berlin local date and hour and leaves gaps empty', () => {
|
||
const heatmap = buildHeatmap(points)
|
||
|
||
expect(heatmap.hours).toEqual([8, 10, 12, 14, 16, 18])
|
||
expect(heatmap.rows.map((row) => row.date)).toEqual([
|
||
'2026-11-16',
|
||
'2026-11-17',
|
||
])
|
||
|
||
const monday = heatmap.rows[0]!
|
||
expect(monday.cells.map((cell) => cell.value)).toEqual([
|
||
10,
|
||
30,
|
||
null,
|
||
null,
|
||
null,
|
||
null,
|
||
])
|
||
expect(monday.cells[2]!.value).toBeNull()
|
||
expect(monday.cells[2]!.intensity).toBe(0)
|
||
})
|
||
|
||
it('averages every point inside the same local hour bucket', () => {
|
||
const heatmap = buildHeatmap(points)
|
||
const mondayTen = heatmap.rows[0]!.cells[1]!
|
||
|
||
expect(mondayTen.hour).toBe(10)
|
||
expect(mondayTen.value).toBe(30)
|
||
})
|
||
|
||
it('scales intensity against the busiest filled bucket', () => {
|
||
const heatmap = buildHeatmap(points)
|
||
|
||
expect(heatmap.max).toBe(55)
|
||
expect(heatmap.rows[1]!.cells[0]!.intensity).toBe(1)
|
||
expect(heatmap.rows[0]!.cells[0]!.intensity).toBeCloseTo(10 / 55)
|
||
})
|
||
|
||
it('returns no rows for an empty forecast', () => {
|
||
const heatmap = buildHeatmap([])
|
||
|
||
expect(heatmap.rows).toEqual([])
|
||
expect(heatmap.max).toBe(0)
|
||
expect(heatmap.hours).toEqual([8, 10, 12, 14, 16, 18])
|
||
})
|
||
})
|
||
|
||
describe('formatBand', () => {
|
||
it('formats the empirical band with one optional decimal', () => {
|
||
expect(formatBand(9, 13)).toBe('9 – 13')
|
||
expect(formatBand(7.4, 17.2)).toBe('7.4 – 17.2')
|
||
expect(formatBand(undefined, 17.2)).toBe('—')
|
||
})
|
||
})
|
||
|
||
describe('scenarioDelta', () => {
|
||
function summary(peak: number, mean: number): ForecastSummary {
|
||
return {
|
||
count: 2,
|
||
peak: { t: '2026-11-16T09:00:00Z', v: peak },
|
||
mean,
|
||
total: mean * 2,
|
||
intervalHours: 1,
|
||
}
|
||
}
|
||
|
||
it('compares peak and mean against base', () => {
|
||
const delta = scenarioDelta(summary(120, 90), summary(100, 80))
|
||
|
||
expect(delta.peakDelta).toBe(20)
|
||
expect(delta.peakDeltaPct).toBeCloseTo(20)
|
||
expect(delta.meanDelta).toBe(10)
|
||
expect(delta.meanDeltaPct).toBeCloseTo(12.5)
|
||
})
|
||
|
||
it('returns a null percentage when base is zero', () => {
|
||
const delta = scenarioDelta(summary(12, 6), summary(0, 0))
|
||
|
||
expect(delta.peakDelta).toBe(12)
|
||
expect(delta.peakDeltaPct).toBeNull()
|
||
expect(delta.meanDelta).toBe(6)
|
||
expect(delta.meanDeltaPct).toBeNull()
|
||
})
|
||
})
|