metricBuilder.test.ts 1.02 KB
import { describe, expect, it } from "vitest";
import { buildExpr, parseExpr } from "@/features/kernel/MetricBuilder";

describe("指标构建器:表达式 ↔ 选项", () => {
  it("常见聚合能认回来,再拼出去不变", () => {
    for (const e of ["SUM(amount)", "COUNT(*)", "COUNT(DISTINCT sku_id)", "AVG(price)", "SUM(gap) / NULLIF(SUM(demand), 0)"]) {
      const p = parseExpr(e);
      expect(p.agg).not.toBe("custom");
      expect(buildExpr(p)).toBe(e);
    }
  });
  it("带反引号、大小写不同也认得", () => {
    expect(parseExpr("sum(`amount`)")).toEqual({ agg: "sum", a: "amount", b: "" });
  });
  it("复杂公式归到「自己写」,不会被改写", () => {
    expect(parseExpr("SUM(a) - SUM(b)").agg).toBe("custom");
    expect(parseExpr("SUM(CASE WHEN x > 0 THEN 1 ELSE 0 END)").agg).toBe("custom");
  });
  it("没选列时不产出半截表达式", () => {
    expect(buildExpr({ agg: "sum", a: "", b: "" })).toBe("");
    expect(buildExpr({ agg: "ratio", a: "x", b: "" })).toBe("");
  });
});