flowModel.test.ts 5.03 KB
import { describe, expect, it } from "vitest";
import type { FlowOutput, FlowTree } from "@/lib/onto";
import {
  allSteps, allowedIn, condText, findStep, insertStep, locate, loopOf, moveStep, newStep, refLabel, removeStep,
  summarize, updateStep, visibleOutputs,
} from "@/features/sop/flowModel";

const tree = (): FlowTree => ({
  params: [], priority: "P2", domain: "",
  steps: [
    { id: "gap", kind: "metric", title: "核对库存缺口", var: "gap", metric: "库存缺口", by: ["仓库", "商品"], filters: {}, where: null },
    { id: "each", kind: "foreach", title: "逐条补货", var: "each", items: "$gap", filter: null, limit: 20, steps: [
      { id: "big", kind: "branch", title: "缺口大吗", otherwise: [
        { id: "tell", kind: "notify", title: "知会", to: { kind: "role", value: "采购员" }, subject: "$item.商品名称 缺 $item.value" }],
        branches: [{ label: "大", when: { mode: "all", items: [{ left: "$item.value", op: "ge", right: 100 }] }, steps: [
          { id: "po", kind: "action", title: "建采购单", var: "po", action: "创建补货采购单", target: "$item.商品", params: {}, on_reject: "stop" }] }] }] },
    { id: "fin", kind: "end", title: "完成", outcome: "已处理" },
  ],
});

const outputs: FlowOutput[] = [
  { id: "gap", var: "gap", title: "核对库存缺口", kind: "metric", scope: "", fields: [{ path: "rows", label: "每一行", type: "rows" }],
    row_fields: [{ path: "仓库", label: "仓库", type: "text" }, { path: "商品名称", label: "商品名称", type: "text" }, { path: "value", label: "库存缺口(件)", type: "number" }] },
  { id: "each:item", var: "item", title: "当前这一条", kind: "item", scope: "each",
    fields: [{ path: "商品名称", label: "商品名称", type: "text" }, { path: "value", label: "库存缺口(件)", type: "number" }], row_fields: [] },
  { id: "po", var: "po", title: "建采购单", kind: "action", scope: "each", fields: [{ path: "status", label: "动作结果", type: "text" }], row_fields: [] },
  { id: "each", var: "each", title: "逐条补货", kind: "foreach", scope: "", fields: [{ path: "ok", label: "完成几条", type: "number" }], row_fields: [] },
];

describe("流程卡步骤树", () => {
  it("遍历出所有步骤,包括分支和循环体里的", () => {
    expect(allSteps(tree().steps).map((s) => s.id)).toEqual(["gap", "each", "big", "po", "tell", "fin"]);
  });

  it("在任意位置插入一步,id 不重复", () => {
    const t = tree();
    const s = newStep(t, "notify");
    const next = insertStep(t, { at: "branch", step: "big", index: 0 }, 1, s);
    const branch = findStep(next, "big")!.branches![0].steps;
    expect(branch.map((x) => x.id)).toEqual(["po", s.id]);
    expect(newStep(next, "notify").id).not.toBe(s.id);
    expect(findStep(t, "big")!.branches![0].steps).toHaveLength(1);       // 原树没被改
  });

  it("改一步、删一步、同一串里上下移动", () => {
    let t = updateStep(tree(), "po", { title: "下采购单" });
    expect(findStep(t, "po")!.title).toBe("下采购单");
    t = removeStep(t, "tell");
    expect(findStep(t, "tell")).toBeUndefined();
    t = moveStep(t, "fin", -1);
    expect(t.steps.map((s) => s.id)).toEqual(["gap", "fin", "each"]);
    expect(moveStep(t, "gap", -1).steps.map((s) => s.id)).toEqual(["gap", "fin", "each"]);   // 已在最前,不动
  });

  it("知道一步在哪儿、在不在循环体里", () => {
    const t = tree();
    expect(locate(t, "tell")).toEqual({ path: { at: "otherwise", step: "big" }, index: 0 });
    expect(loopOf(t, "po")).toBe("each");
    expect(loopOf(t, "fin")).toBe("");
    expect(allowedIn("foreach", true)).toBe(false);
    expect(allowedIn("action", true)).toBe(true);
  });

  it("每一步只能引用排在前面、且看得见的数据", () => {
    const t = tree();
    const at = (id: string) => visibleOutputs(t, id, outputs).map((o) => o.id);
    expect(at("gap")).toEqual([]);
    expect(at("each")).toEqual(["gap"]);
    expect(at("po")).toEqual(["gap", "each:item"]);                       // 循环体里多一个「当前这一条」
    expect(at("tell")).toEqual(["gap", "each:item"]);                     // 「建采购单」在另一个分支里,这条路没走到它
    expect(at("fin")).toEqual(["gap", "each"]);                           // 出了循环体,里面的结果看不到,只有汇总
  });

  it("把引用和步骤说成人话", () => {
    const t = tree();
    expect(refLabel("$gap.value", outputs)).toBe("核对库存缺口 · 库存缺口(件)");
    expect(refLabel("$nope.x", outputs)).toBe("$nope.x");
    expect(condText(findStep(t, "big")!.branches![0].when, outputs)).toBe("库存缺口(件) 大于等于 100");
    expect(summarize(findStep(t, "gap")!, outputs)).toBe("查 库存缺口,按 仓库、商品");
    expect(summarize(findStep(t, "each")!, outputs)).toBe("对 核对库存缺口 的每一条(最多 20 条)");
    expect(summarize(findStep(t, "tell")!, outputs)).toContain("通知 采购员");
    expect(summarize(newStep(t, "metric"), outputs)).toBe("还没选指标");
  });
});