foxygit / RPA-Remisser Log in
commits tags

/webui/frontend/src/lib/stepops.test.ts · 2.36 KB

raw
import { describe, expect, it } from "vitest";
import { newNode, retype } from "./stepops";
import type { Schema } from "./schema";

const SCHEMA = {
  step_types: [
    {
      type: "click", category: "interaction", label_sv: "", label_en: "", fields: [
        { key: "by", kind: "select", options: ["text", "css", "role"], default: "text", label_sv: "", label_en: "" },
        { key: "nth", kind: "number", default: 0, label_sv: "", label_en: "" },
      ],
    },
    { type: "if", category: "control", label_sv: "", label_en: "", control: "if", fields: [] },
    { type: "loop", category: "control", label_sv: "", label_en: "", control: "loop", fields: [] },
    { type: "call_flow", category: "control", label_sv: "", label_en: "", fields: [{ key: "target", kind: "text", label_sv: "", label_en: "" }] },
    { type: "desktop_click", category: "desktop", label_sv: "", label_en: "", fields: [] },
  ],
  condition_kinds: [],
  settings: [],
  input_fields: [],
  close_modes: [],
} as unknown as Schema;

describe("newNode", () => {
  it("applies schema defaults, an id and a position", () => {
    const n = newNode(SCHEMA, "click", { x: 5, y: 7 });
    expect(n.type).toBe("click");
    expect(n.by).toBe("text");
    expect(typeof n.id).toBe("string");
    expect(n.position).toEqual({ x: 5, y: 7 });
  });

  it("gives an if-node one starting branch and no steps", () => {
    const n = newNode(SCHEMA, "if");
    expect(n.branches).toHaveLength(1);
    expect(n.branches![0]).toEqual({ condition: { kind: "element_exists" } });
    expect((n as Record<string, unknown>).steps).toBeUndefined();
  });

  it("does not give a loop-node a steps array", () => {
    expect((newNode(SCHEMA, "loop") as Record<string, unknown>).steps).toBeUndefined();
  });

  it("gives a call_flow node empty target/in/out", () => {
    const n = newNode(SCHEMA, "call_flow");
    expect(n.target).toBe("");
    expect(n.in).toEqual([]);
    expect(n.out).toEqual([]);
  });
});

describe("retype", () => {
  it("keeps id/position/label but swaps fields", () => {
    const original = { ...newNode(SCHEMA, "click", { x: 1, y: 2 }), id: "keep", label: "L" };
    const next = retype(SCHEMA, original, "desktop_click");
    expect(next.id).toBe("keep");
    expect(next.label).toBe("L");
    expect(next.position).toEqual({ x: 1, y: 2 });
    expect(next.type).toBe("desktop_click");
    expect(next.by).toBeUndefined();
  });
});