import { describe, expect, it } from "vitest";
import { normalizeSafeChartData, parseChartSpec, pointToLabelValue } from "./chart-data";

describe("chart data normalization", () => {
  it("parses chart spec aliases", () => {
    expect(parseChartSpec({ title: "压力", chart_type: "bar", x_axis_name: "时间" })).toEqual({
      title: "压力",
      chartType: "bar",
      xAxisName: "时间",
      yAxisName: undefined
    });
  });

  it("normalizes series with x_data and numeric strings", () => {
    expect(
      normalizeSafeChartData({
        x_data: ["00:00", "01:00"],
        series: [{ name: "压力", data: ["1.2", 1.4] }]
      })
    ).toEqual({
      xData: ["00:00", "01:00"],
      series: [{ name: "压力", data: [1.2, 1.4], type: undefined }]
    });
  });

  it("normalizes raw point arrays into a default series", () => {
    expect(
      normalizeSafeChartData({
        series: [
          ["A", 12],
          ["B", "13"]
        ]
      })
    ).toEqual({
      xData: ["A", "B"],
      series: [{ name: "数据", data: [12, 13], type: undefined }]
    });
  });

  it("normalizes object points", () => {
    expect(pointToLabelValue({ time: "10:00", value: "2.4" }, "fallback")).toEqual({
      label: "10:00",
      value: 2.4
    });
  });

  it("clips chart points to the safe limit", () => {
    const data = normalizeSafeChartData(
      {
        x_data: Array.from({ length: 30 }, (_, index) => String(index)),
        series: [{ name: "流量", data: Array.from({ length: 30 }, (_, index) => index) }]
      },
      5
    );

    expect(data?.xData).toHaveLength(5);
    expect(data?.series[0].data).toEqual([0, 1, 2, 3, 4]);
  });
});
