Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/foundation/src/workflows-native.test.ts
T
2026-07-08 16:15:37 +02:00

181 lines
6.4 KiB
TypeScript

import { describe, expect, test } from "vitest";
import {
extractWorkflowReferences,
getNativeWorkflowTriggerCapabilityErrors,
getWorkflowBlockContract,
validateWorkflowRequiredInputs,
type WorkflowBlockType,
type WorkflowNativeCapabilityBlock,
type WorkflowPreflightBlock,
} from "./workflows.js";
function nativeBlock(overrides: Partial<WorkflowNativeCapabilityBlock>): WorkflowNativeCapabilityBlock {
return { id: "block_1", type: "manual", input: {}, conditionGroups: [], toBlocks: [], ...overrides };
}
const trigger = nativeBlock({ id: "trigger", type: "event", isTrigger: true, input: { event: "row.updated" } });
describe("getWorkflowBlockContract", () => {
test("throws on an unregistered block type", () => {
expect(() => getWorkflowBlockContract("nope" as WorkflowBlockType)).toThrow("Unknown workflow block contract: nope");
});
});
describe("native trigger capability: switch blocks", () => {
test("accepts a well-formed switch with matching case and default edges", () => {
expect(
getNativeWorkflowTriggerCapabilityErrors([
trigger,
nativeBlock({
id: "sw",
type: "switch",
conditionGroups: [{ index: 0, conditions: [{ variable: "{{$vars.plan}}", value: "pro" }] }],
toBlocks: [
{ id: "c1", condition: "case1" },
{ id: "d", condition: "default" },
],
}),
]),
).toEqual([]);
});
test("flags a missing default edge and a missing case edge", () => {
expect(
getNativeWorkflowTriggerCapabilityErrors([
trigger,
nativeBlock({
id: "sw",
type: "switch",
conditionGroups: [{ index: 0, conditions: [{ variable: "{{$vars.plan}}", value: "pro" }] }],
toBlocks: [],
}),
]),
).toEqual(["switch block sw is missing default edge", "switch block sw is missing case1 edge"]);
});
test("flags a switch condition group that has no conditions", () => {
expect(
getNativeWorkflowTriggerCapabilityErrors([
trigger,
nativeBlock({
id: "sw",
type: "switch",
conditionGroups: [{ index: 0, conditions: [] }],
toBlocks: [
{ id: "c1", condition: "case1" },
{ id: "d", condition: "default" },
],
}),
]),
).toEqual(["switch block sw condition group 1 must have at least one condition"]);
});
});
describe("native trigger capability: if blocks", () => {
test("flags an empty condition group and a missing true edge", () => {
expect(
getNativeWorkflowTriggerCapabilityErrors([
trigger,
nativeBlock({
id: "if1",
type: "if",
conditionGroups: [{ index: 0, conditions: [] }],
toBlocks: [{ id: "f", condition: "false" }],
}),
]),
).toEqual(["if block if1 must have at least one condition", "if block if1 is missing true edge"]);
});
test("reads condition groups from the legacy conditionsGroups alias", () => {
expect(
getNativeWorkflowTriggerCapabilityErrors([
trigger,
nativeBlock({
id: "if2",
type: "if",
conditionGroups: undefined,
conditionsGroups: [{ index: 0, conditions: [{ variable: "{{$vars.plan}}", value: "pro" }] }],
toBlocks: [
{ id: "t", condition: "true" },
{ id: "f", condition: "false" },
],
}),
]),
).toEqual([]);
});
});
describe("native trigger capability: persisted block input", () => {
test("parses a JSON-string block input and detects its credential references", () => {
expect(
getNativeWorkflowTriggerCapabilityErrors([
trigger,
nativeBlock({ id: "log", type: "log", input: '{"message":"{{$credentials.apiToken}}"}' }),
]),
).toEqual(['unsupported credential reference "apiToken" on block log']);
});
test("ignores unparseable JSON-string input instead of scanning the raw string", () => {
expect(getNativeWorkflowTriggerCapabilityErrors([trigger, nativeBlock({ id: "log", type: "log", input: "not json {{$credentials.x}}" })])).toEqual([]);
});
test("ignores JSON-string input that decodes to a non-object", () => {
expect(getNativeWorkflowTriggerCapabilityErrors([trigger, nativeBlock({ id: "log", type: "log", input: '["{{$credentials.x}}"]' })])).toEqual([]);
});
});
describe("native trigger capability: edges", () => {
test("flags a non-empty edge condition on a block type that has no branching semantics", () => {
expect(
getNativeWorkflowTriggerCapabilityErrors([
trigger,
nativeBlock({ id: "log", type: "log", input: { message: "hi" }, toBlocks: [{ id: "e1", condition: "weird" }] }),
]),
).toEqual(['unsupported conditional edge "e1" from block log']);
});
test("ignores blank edge conditions", () => {
expect(
getNativeWorkflowTriggerCapabilityErrors([
trigger,
nativeBlock({ id: "log", type: "log", input: { message: "hi" }, toBlocks: [{ id: "e1", condition: " " }, { id: "e2", condition: null }] }),
]),
).toEqual([]);
});
});
describe("validateWorkflowRequiredInputs edge cases", () => {
test("skips blocks whose type is not part of the contract registry", () => {
expect(validateWorkflowRequiredInputs({ blocks: [{ id: "x", type: "unknownType", input: {} }] })).toEqual([]);
});
test("treats a non-record block input as an empty input object", () => {
expect(validateWorkflowRequiredInputs({ blocks: [{ id: "http", type: "httpRequest", input: "not-an-object" }] })).toEqual([
{ blockId: "http", blockType: "httpRequest", missingInputs: ["url", "method"] },
]);
});
test("accepts non-string, non-null required input values as present", () => {
expect(
validateWorkflowRequiredInputs({
blocks: [{ id: "iter", type: "iterator", input: { variableName: 0 } }],
}),
).toEqual([]);
});
});
describe("extractWorkflowReferences variable definition edge cases", () => {
test("ignores variable blocks whose input is not a record when collecting local names", () => {
const references = extractWorkflowReferences({
blocks: [
{ id: "define", type: "variable", input: "not-a-record" } as WorkflowPreflightBlock,
{ id: "use", type: "log", input: { message: "{{$vars.localName}}" } },
],
});
// The malformed variable block defines no local name, so the reference stays unresolved/global.
expect(references.variables).toEqual(["localName"]);
});
});