import assert from "node:assert/strict"; import { test } from "node:test"; import { aggregateDays } from "../src/lib/days.ts"; const ID = "6f1a2b3c-4d5e-4f60-8a9b-0c1d2e3f4a5b"; const chunk = (date: string, at: number, km: number, kwh: number, sohPercent?: number) => ({ installationId: ID, date, recordedAt: new Date(at), day: { km, kwh, hours: km / 50, socDrop: kwh / 0.6 }, ...(sohPercent === undefined ? {} : { sohPercent }), }); test("adds chunks up per day, newest day first, with a running lifetime", () => { const days = aggregateDays([ chunk("2026-10-16", 1_000, 10, 2), chunk("2026-10-17", 3_000, 5, 1), chunk("2026-10-16", 2_000, 20, 3), ]); assert.deepEqual(days.map((d) => d.date), ["2026-10-17", "2026-10-16"]); assert.equal(days[1].day.km, 30); assert.equal(days[1].day.kwh, 5); assert.equal(days[1].recordedAt.getTime(), 2_000); assert.equal(days[1].lifetime?.km, 30); assert.equal(days[0].lifetime?.km, 35); assert.equal(days[0].lifetime?.kwh, 6); }); test("the newest manual SoH of a day wins, and a day without one has none", () => { const days = aggregateDays([ chunk("2026-10-16", 2_000, 1, 1, 96), chunk("2026-10-16", 1_000, 1, 1, 98), chunk("2026-10-17", 3_000, 1, 1), ]); assert.equal(days[1].sohPercent, 96); assert.equal("sohPercent" in days[0], false); });