import { toTimeSeriesPoints, type ElementHistorySeries } from "./elementHistory"; describe("element history display conversion", () => { it("converts every series through the shared unit utility", () => { const series: ElementHistorySeries[] = [ { element_id: "P-1", element_type: "pipe", device_id: null, metric: "flow", source: "realtime_simulation", source_unit: "MLD", display_unit: "m³/h", unit_inferred: false, points: [{ time: "2026-09-01T00:00:00Z", value: 2 }], }, { element_id: "J-1", element_type: "junction", device_id: "pressure-1", metric: "pressure", source: "scada_cleaned", source_unit: "KPA", display_unit: "m", unit_inferred: false, points: [{ time: "2026-09-01T00:00:00Z", value: 10 }], }, ]; const points = toTimeSeriesPoints(series); expect(points[0].timestamp).toBe("2026-09-01T00:00:00.000Z"); expect(points[0].values['["pipe","P-1","realtime_simulation"]']).toBeCloseTo(83.3333333334); expect(points[0].values["pressure-1_clean"]).toBeCloseTo(1.019716213); }); it("aligns equivalent timestamps and keeps same-name pipes and junctions separate", () => { const base: ElementHistorySeries = { element_id: "shared-id", element_type: "pipe", device_id: null, metric: "flow", source: "realtime_simulation", source_unit: "L/s", display_unit: "m³/h", unit_inferred: false, points: [{ time: "2026-09-01T08:00:00+08:00", value: 10 }], }; const points = toTimeSeriesPoints([ base, { ...base, element_type: "junction", metric: "pressure", source_unit: "m", points: [{ time: "2026-09-01T00:00:00Z", value: 20 }] }, { ...base, source: "analysis_simulation", points: [ { time: "2026-09-01T00:00:00.000Z", value: null }, ] }, ]); expect(points).toHaveLength(1); expect(points[0].values).toEqual({ '["pipe","shared-id","realtime_simulation"]': 36, '["junction","shared-id","realtime_simulation"]': 20, '["pipe","shared-id","analysis_simulation"]': null, }); }); it("keeps online IDs ending in _scheme distinct from another element's scheme series", () => { const base: ElementHistorySeries = { element_id: "P_scheme", element_type: "pipe", device_id: null, metric: "flow", source: "realtime_simulation", source_unit: "m3/h", display_unit: "m³/h", unit_inferred: false, points: [{ time: "2026-09-01T00:00:00Z", value: 100 }], }; const points = toTimeSeriesPoints([ base, { ...base, element_id: "P", source: "analysis_simulation", points: [{ time: "2026-09-01T00:00:00Z", value: 5 }] }, ]); expect(Object.values(points[0].values).sort((a, b) => Number(a) - Number(b))).toEqual([5, 100]); }); });