flowCanvasModel.test.ts 3.78 KB
import { describe, expect, it } from "vitest";
import type { FlowOutput, SopSpec } from "@/lib/onto";
import { addStep, autoLayout, connect, edgesOf, newStep, portsOf, refLabel, removeStep, ROOT, visibleOutputs } from "@/features/sop/canvas/model";

const spec = (): SopSpec => ({
  params: [{ name: "wo", label: "工单", type: "object", object_type: "维修工单", calendar: "", required: true }],
  start: "q", evidence: [],
  steps: [
    { id: "q", type: "query", title: "查工单", next: "gate", output: "q" },
    { id: "gate", type: "decision", title: "有缺件吗", branches: [{ label: "有", cond: null, next: "each" }], default: "none" },
    { id: "each", type: "foreach", title: "每个缺件", items: "$q.缺件", next: "fin", output: "each",
      body: { start: "w", steps: [{ id: "w", type: "write", title: "下单", next: "e", output: "w" }, { id: "e", type: "end", title: "完" }] } },
    { id: "fin", type: "end", title: "完成" }, { id: "none", type: "end", title: "没有" },
  ],
});

const outputs: FlowOutput[] = [
  { id: "param:wo", var: "wo", title: "工单", kind: "param", scope: "", schema: { type: "object", fields: [{ name: "key", label: "主键", type: "string" }] } },
  { id: "q", var: "q", title: "查工单", kind: "query", scope: "", schema: { type: "object", fields: [
    { name: "缺件", label: "缺件", type: "array", items: { type: "object", fields: [{ name: "qty_short", label: "缺件数量", type: "number" }] } }] } },
  { id: "each", var: "each", title: "每个缺件", kind: "foreach", scope: "", schema: { type: "object", fields: [] } },
  { id: "each:item", var: "item", title: "这一条", kind: "item", scope: "each", schema: { type: "object", fields: [{ name: "qty_short", label: "缺件数量", type: "number" }] } },
  { id: "w", var: "w", title: "下单", kind: "write", scope: "each", schema: { type: "object", fields: [] } },
];

describe("流程画布模型", () => {
  it("条件节点每种情况一个出口,加上否则;动作 / 写数据多一个被驳回", () => {
    const s = spec();
    expect(portsOf(s.steps[1]).map((p) => p.id)).toEqual(["b0", "default"]);
    expect(portsOf(s.steps[2].body.steps[0]).map((p) => p.id)).toEqual(["next", "reject"]);
  });

  it("连线、断开、删节点会把指向它的线一起去掉", () => {
    let s = connect(spec(), ROOT, "gate", "default", "fin");
    expect(s.steps[1].default).toBe("fin");
    s = removeStep(s, "fin");
    expect(s.steps.find((x) => x.id === "each")!.next).toBe("");
    expect(s.steps[1].default).toBe("");
    expect(edgesOf(s, ROOT).some((e) => e.target === "fin")).toBe(false);
  });

  it("循环体是独立的容器,开始节点连到循环体第一步", () => {
    const s = spec();
    expect(edgesOf(s, "each")).toEqual([
      { source: "__start", port: "next", target: "w", label: "" }, { source: "w", port: "next", target: "e", label: "", tone: undefined }]);
    const n = newStep(s, "notify");
    const s2 = connect(addStep(s, "each", n), "each", "__start", "next", n.id);
    expect(s2.steps[2].body.start).toBe(n.id);
  });

  it("自动排版:从上往下分层,分支并排", () => {
    const s = autoLayout(spec());
    const y = (id: string) => s.steps.find((x) => x.id === id)!.ui!.y;
    expect(y("q")).toBeLessThan(y("gate"));
    expect(y("each")).toBe(y("none"));
    expect(s.steps.find((x) => x.id === "each")!.body.steps.every((b: any) => b.ui)).toBe(true);
  });

  it("只看得到上游的数据;循环体里还看得到当前这一条", () => {
    const s = spec();
    expect(visibleOutputs(s, "gate", outputs).map((o) => o.var)).toEqual(["wo", "q"]);
    expect(visibleOutputs(s, "e", outputs).map((o) => o.var)).toEqual(["wo", "q", "item", "w"]);
    expect(refLabel("$q.缺件[0].qty_short", outputs)).toBe("查工单 · 缺件 · 第 1 条 · 缺件数量");
  });
});