SPB Git

Repository Public

chartData.test.ts 2.4 KB 47 lines typescript

1import { describe, expect, it } from "vitest";2import { alignSeries, medianStep, summarize, withAlpha } from "./chartData";34describe("alignSeries", () => {5  it("aligns several series on one time axis with nulls where a series has no sample", () => {6    const a = alignSeries([7      { id: "n1", label: "M3U96a", points: [[1000, 10, 8, 12], [2000, 20, 18, 22], [3000, 30, 28, 32]] },8      { id: "n2", label: "m4ml", points: [[2000, 5, 4, 6], [3000, 6, 5, 7], [4000, 7, 6, 8]] },9    ]);10    expect(a.ts).toEqual([1000, 2000, 3000, 4000]);11    expect(a.series[0]).toMatchObject({ avg: [10, 20, 30, null], min: [8, 18, 28, null], max: [12, 22, 32, null] });12    expect(a.series[1]?.avg).toEqual([null, 5, 6, 7]);13  });14  it("inserts an explicit null after a hole so gaps are drawn as gaps", () => {15    const a = alignSeries([{ id: "n", label: "n", points: [[0, 1, 1, 1], [10, 2, 2, 2], [20, 3, 3, 3], [90, 4, 4, 4], [100, 5, 5, 5]] }]);16    expect(a.ts).toEqual([0, 10, 20, 30, 90, 100]);17    expect(a.series[0]?.avg).toEqual([1, 2, 3, null, 4, 5]);18  });19  it("does not break lines on regular spacing and tolerates [ts, value] points and unsorted input", () => {20    const a = alignSeries([{ id: "n", label: "n", points: [[30, 3], [10, 1], [20, 2]] }]);21    expect(a.ts).toEqual([10, 20, 30]);22    expect(a.series[0]).toMatchObject({ avg: [1, 2, 3], min: [1, 2, 3], max: [1, 2, 3] });23  });24  it("handles empty input", () => {25    expect(alignSeries([])).toEqual({ ts: [], series: [] });26    expect(alignSeries([{ id: "n", label: "n", points: [] }]).series[0]?.avg).toEqual([]);27    expect(medianStep([[1, 1]])).toBeUndefined();28  });29});3031describe("summarize", () => {32  it("computes min/avg/max from the envelope and the last value", () => {33    expect(summarize([{ id: "n", label: "n", points: [[1, 10, 5, 15], [2, 20, 18, 40], [3, 30, 25, 31]] }])).toEqual([{ id: "n", label: "n", count: 3, min: 5, max: 40, avg: 20, last: 30, lastTs: 3 }]);34  });35  it("reports an empty series without inventing zeros", () => {36    expect(summarize([{ id: "n", label: "n", points: [] }])).toEqual([{ id: "n", label: "n", count: 0 }]);37  });38});3940describe("withAlpha", () => {41  it("converts token hex colours to rgba", () => {42    expect(withAlpha("#7CB7FF", 0.16)).toBe("rgba(124, 183, 255, 0.16)");43    expect(withAlpha(" #000000B3 ", 0.5)).toBe("rgba(0, 0, 0, 0.5)");44    expect(withAlpha("rebeccapurple", 0.5)).toBe("rebeccapurple");45  });46});47
Files main
Menu

Theme