Files
watchman/web/apps/console/src/mappings/condition.spec.ts
T
unknownandCursor 252709709c feat(mappings): 完成 M05 字段映射与规则判断
映射和判断规则共用一个不可变版本,试算可以直接取已选查询的结果。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-19 10:03:45 +08:00

46 lines
1.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { conditionSummary, operatorsFor, treeForApi, treeFromApi, type GroupNode } from "./condition";
const columns = [
{ source_column_name: "reading_value", value_type: "number" },
{ source_column_name: "metric", value_type: "string" },
];
describe("condition tree", () => {
it("shows a single saved leaf as one 并且 group and saves it back as a leaf", () => {
const group = treeFromApi({ column: "reading_value", operator: "gt", value: 85 }, "reading_value");
expect(group).toEqual({ kind: "group", op: "and", items: [{ kind: "leaf", column: "reading_value", operator: "gt", value: "85" }] });
expect(treeForApi(group, columns)).toEqual({ column: "reading_value", operator: "gt", value: 85 });
});
it("keeps an and/or group and a nested group", () => {
const saved = {
op: "and",
items: [
{ column: "metric", operator: "eq", value: "temp_c" },
{ op: "or", items: [{ column: "reading_value", operator: "gt", value: 85 }, { column: "reading_value", operator: "lt", value: 10 }] },
],
};
const group = treeFromApi(saved, "metric");
expect(group.items[1]).toMatchObject({ kind: "group", op: "or" });
expect(treeForApi(group, columns)).toEqual(saved);
});
it("limits string columns to equal and not-equal", () => {
expect(operatorsFor("string").map((item) => item.value)).toEqual(["eq", "ne"]);
expect(operatorsFor("number").map((item) => item.value)).toContain("gt");
});
it("writes the abnormal condition in words", () => {
const group: GroupNode = {
kind: "group",
op: "and",
items: [
{ kind: "leaf", column: "metric", operator: "eq", value: "temp_c" },
{ kind: "leaf", column: "reading_value", operator: "gt", value: "85" },
],
};
expect(conditionSummary(group)).toBe("(metric 等于 temp_c 且 reading_value 大于 85)");
});
});