Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

7.3 KB
Raw Blame
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.

import { describe, expect, it } from "vitest";
import { createFixtureCatalog } from "../definition/catalog";
import type { ActionNode } from "../definition/schema";
import { t } from "../definition/types";
import { actionExecutor } from "./action";
import { createRuntimeContext } from "./fixtures";
import type { RuntimeValue, WorkflowServices } from "./types";
import { entityValue, listValue, primitiveValue } from "./values";

const catalog = createFixtureCatalog();

const title = {
  kind: "literal" as const,
  type: t.string,
  value: "Scrap is over budget"
};

const node = (
  action = "createIssue",
  inputs: ActionNode["data"]["inputs"] = { title }
): ActionNode => ({
  id: "act",
  name: "act",
  type: "action",
  position: { x: 0, y: 0 },
  data: { action, inputs }
});

const recipients = listValue({ kind: "entity", of: "user" }, [
  entityValue("user", "u1"),
  entityValue("user", "u2")
]).value;

const contextWith = (runAction: WorkflowServices["runAction"]) =>
  createRuntimeContext({
    outputs: { find: { result: recipients } },
    services: { runAction }
  });

const unreachable: WorkflowServices["runAction"] = async () => {
  throw new Error("the service should not have been called");
};

describe("actionExecutor", () => {
  it("returns the action's outputs and summary on the success handle", async () => {
    const result = await actionExecutor.execute(
      node(),
      contextWith(async () => ({
        ok: true,
        outputs: { issue: entityValue("issue", "i1") },
        summary: "Raised NCR-1."
      }))
    );
    expect(result).toEqual({
      status: "Succeeded",
      outputs: { issue: entityValue("issue", "i1") },
      handle: "success",
      summary: "Raised NCR-1."
    });
  });

  it("fails on the failure handle so a wired recovery path can run", async () => {
    const result = await actionExecutor.execute(
      node(),
      contextWith(async () => ({
        ok: false,
        error: "The issue could not be created."
      }))
    );
    expect(result).toEqual({
      status: "Failed",
      error: "The issue could not be created.",
      handle: "failure"
    });
  });

  it("skips with the resolver's own reason when an input cannot be resolved", async () => {
    const result = await actionExecutor.execute(
      node("createIssue", {
        title: { kind: "ref", nodeId: "gone", output: "result", path: [] }
      }),
      contextWith(unreachable)
    );
    expect(result).toEqual({
      status: "Skipped",
      reason: "The step that produces this value did not run."
    });
  });

  it("reports the action's declared permission", () => {
    expect(actionExecutor.permission(node(), catalog)).toEqual({
      module: "quality",
      action: "create"
    });
    expect(actionExecutor.permission(node("banish"), catalog)).toBeUndefined();
  });

  it("skips when the action is no longer in the catalog", async () => {
    const result = await actionExecutor.execute(
      node("banish"),
      contextWith(unreachable)
    );
    expect(result).toEqual({
      status: "Skipped",
      reason: "This step is no longer available."
    });
  });

  it("calls the service once even when an input is a list", async () => {
    const calls: Array<[string, Record<string, RuntimeValue>]> = [];
    const ctx = contextWith(async (actionId, inputs) => {
      calls.push([actionId, inputs]);
      return { ok: true, outputs: {} };
    });

    const result = await actionExecutor.execute(
      node("notify", {
        recipient: {
          kind: "ref",
          nodeId: "find",
          output: "result",
          path: []
        },
        message: { kind: "literal", type: t.string, value: "Please review" }
      }),
      ctx
    );

    expect(result).toEqual({
      status: "Succeeded",
      outputs: {},
      handle: "success"
    });
    expect(calls).toEqual([
      [
        "notify",
        {
          recipient: recipients,
          message: primitiveValue("string", "Please review")
        }
      ]
    ]);
  });

  it("records each resolved input as it resolves, even when the service fails", async () => {
    const recorded: Record<string, RuntimeValue> = {};
    const ctx = {
      ...contextWith(async () => ({
        ok: false as const,
        error: "The message could not be sent."
      })),
      record: (key: string, value: RuntimeValue) => {
        recorded[key] = value;
      }
    };

    const result = await actionExecutor.execute(
      node("notify", {
        recipient: { kind: "ref", nodeId: "find", output: "result", path: [] },
        message: { kind: "literal", type: t.string, value: "Please review" }
      }),
      ctx
    );

    expect(result).toEqual({
      status: "Failed",
      error: "The message could not be sent.",
      handle: "failure"
    });
    expect(recorded).toEqual({
      recipient: recipients,
      message: primitiveValue("string", "Please review")
    });
  });
});

// The webhook-body guarantee: a markdown link shipped to someone else's API would be
// literal text there, so only an input the catalog declares `links` for may be wrapped.
describe("actionExecutor and links", () => {
  const orderRef = {
    kind: "template" as const,
    parts: [
      { kind: "text" as const, text: "Check " },
      { kind: "ref" as const, nodeId: "trigger", output: "record", path: [] }
    ]
  };

  const withOrder = (runAction: WorkflowServices["runAction"]) =>
    createRuntimeContext({
      outputs: {
        trigger: {
          record: entityValue("purchaseOrder", "po_1", {
            purchaseOrderId: "PO000123"
          })
        }
      },
      services: { runAction },
      linkFor: (of: string, id: string) => `https://erp.test/${of}/${id}`
    });

  it("leaves an input without links plain, even with a resolver present", async () => {
    let seen: Record<string, RuntimeValue> = {};
    const ctx = withOrder(async (_id, inputs) => {
      seen = inputs;
      return { ok: true, outputs: {} };
    });

    // `createIssue.title` is a plain template input in the fixture catalog.
    await actionExecutor.execute(node("createIssue", { title: orderRef }), ctx);

    expect(seen.title).toEqual(primitiveValue("string", "Check PO000123"));
  });

  it("wraps a record on an input that declares links, in the declared format", async () => {
    let seen: Record<string, RuntimeValue> = {};
    const ctx = withOrder(async (_id, inputs) => {
      seen = inputs;
      return { ok: true, outputs: {} };
    });
    const base = ctx.catalog.getAction("createIssue");
    if (base === undefined) throw new Error("fixture action missing");
    const linked = {
      ...base,
      inputs: {
        ...base.inputs,
        title: {
          ...base.inputs.title!,
          links: { format: "markdown" as const }
        }
      }
    };

    await actionExecutor.execute(node("createIssue", { title: orderRef }), {
      ...ctx,
      catalog: {
        ...ctx.catalog,
        getAction: (id) =>
          id === "createIssue" ? linked : ctx.catalog.getAction(id)
      }
    });

    expect(seen.title).toEqual(
      primitiveValue(
        "string",
        "Check [PO000123](https://erp.test/purchaseOrder/po_1)"
      )
    );
  });
});