1 Commits

5 changed files with 447 additions and 370 deletions

View File

@@ -5,17 +5,10 @@ export {
resolveDefaultNodeAttrs, resolveDefaultNodeAttrs,
} from "./defaults.js"; } from "./defaults.js";
export { typeCompat, type TypeCompatResult, type TypeMismatch } from "./type-compat.js"; export { typeCompat, type TypeCompatResult, type TypeMismatch } from "./type-compat.js";
export {
topologicalOrder,
parallelGroups,
criticalPath,
reachableFrom,
ancestors,
descendants,
} from "./ordering.js";
export { buildTypeEdges } from "../graph/construction.js"; export { buildTypeEdges } from "../graph/construction.js";
export { export {
validateSchema, validateSchema,
validateGraph, validateGraph,
validate, validate,
} from "../graph/validation.js"; } from "../graph/validation.js";
export { validatePreconditions, validateTemplate } from "./workflow.js";

View File

@@ -1,86 +0,0 @@
import { topologicalSort, topologicalGenerations, hasCycle } from "graphology-dag";
import type { FlowGraph } from "../graph/construction.js";
import { CycleError, NodeNotFoundError } from "../error/index.js";
import { findCycles as findCyclesQuery, reachableFrom as reachableFromQuery, ancestors as ancestorsQuery, descendants as descendantsQuery } from "../graph/queries.js";
export function topologicalOrder(graph: FlowGraph): string[] {
if (hasCycle(graph.graph)) {
const cycles = findCyclesQuery(graph.graph);
throw new CycleError(cycles);
}
return topologicalSort(graph.graph);
}
export function parallelGroups(graph: FlowGraph): string[][] {
if (hasCycle(graph.graph)) {
const cycles = findCyclesQuery(graph.graph);
throw new CycleError(cycles);
}
return topologicalGenerations(graph.graph);
}
export function criticalPath(graph: FlowGraph): string[] {
if (hasCycle(graph.graph)) {
const cycles = findCyclesQuery(graph.graph);
throw new CycleError(cycles);
}
const dg = graph.graph;
const depth = new Map<string, number>();
const topo = topologicalSort(dg);
for (const node of topo) {
const inNeighbors = dg.inNeighbors(node) ?? [];
let maxPredDepth = -1;
for (const pred of inNeighbors) {
const predDepth = depth.get(pred!) ?? 0;
if (predDepth > maxPredDepth) {
maxPredDepth = predDepth;
}
}
depth.set(node, maxPredDepth + 1);
}
let maxDepth = -1;
let endNode = "";
for (const node of topo) {
const d = depth.get(node) ?? 0;
if (d > maxDepth) {
maxDepth = d;
endNode = node;
}
}
if (topo.length === 0) return [];
const path: string[] = [endNode];
let current = endNode;
while (depth.get(current)! > 0) {
const inNeighbors = dg.inNeighbors(current) ?? [];
let bestPred = "";
let bestDepth = -1;
for (const pred of inNeighbors) {
const predDepth = depth.get(pred!) ?? 0;
if (predDepth > bestDepth) {
bestDepth = predDepth;
bestPred = pred!;
}
}
path.unshift(bestPred);
current = bestPred;
}
return path;
}
export function reachableFrom(graph: FlowGraph, nodeIds: string[]): Set<string> {
return reachableFromQuery(graph.graph, nodeIds);
}
export function ancestors(graph: FlowGraph, nodeId: string): string[] {
if (!graph.hasNode(nodeId)) {
throw new NodeNotFoundError(nodeId);
}
return ancestorsQuery(graph.graph, nodeId);
}
export function descendants(graph: FlowGraph, nodeId: string): string[] {
if (!graph.hasNode(nodeId)) {
throw new NodeNotFoundError(nodeId);
}
return descendantsQuery(graph.graph, nodeId);
}

View File

@@ -1 +1,197 @@
export {}; import type { TSchema } from "@alkdev/typebox";
import { KindGuard } from "@alkdev/typebox";
import type { UNode } from "@alkdev/ujsx";
import { createHostRoot } from "@alkdev/ujsx";
import { hasCycle } from "graphology-dag";
import { DirectedGraph } from "graphology";
import type { FlowGraph } from "../graph/construction.js";
import type { OperationNodeAttrs } from "../schema/node.js";
import type { OperationEdgeAttrs } from "../schema/edge.js";
import type { ValidationError, AnyValidationError } from "../error/index.js";
import { GraphologyHostConfig } from "../host/graphology.js";
import { reachableFrom } from "../graph/queries.js";
function getRequiredTopLevelFields(schema: unknown): Set<string> {
const fields = new Set<string>();
if (schema === null || schema === undefined || typeof schema !== "object") return fields;
const s = schema as TSchema;
if (!KindGuard.IsObject(s)) return fields;
const props = s.properties as Record<string, TSchema> | undefined;
const required = s.required as string[] | undefined;
if (props && required) {
for (const key of required) {
fields.add(key);
}
}
return fields;
}
function getProvidedFields(schema: unknown): Set<string> {
const fields = new Set<string>();
if (schema === null || schema === undefined || typeof schema !== "object") return fields;
const s = schema as TSchema;
if (!KindGuard.IsObject(s)) return fields;
const props = s.properties as Record<string, TSchema> | undefined;
if (props) {
for (const key of Object.keys(props)) {
fields.add(key);
}
}
return fields;
}
export function validatePreconditions(
graph: FlowGraph<typeof import("../schema/node.js").OperationNodeAttrs, typeof import("../schema/edge.js").OperationEdgeAttrs>,
): ValidationError[] {
const errors: ValidationError[] = [];
const nodeKeys = graph.nodes();
for (const nodeKey of nodeKeys) {
const attrs = graph.getNodeAttributes(nodeKey) as unknown as OperationNodeAttrs;
const inputSchema = attrs.inputSchema;
const requiredFields = getRequiredTopLevelFields(inputSchema);
if (requiredFields.size === 0) continue;
const predecessors = graph.predecessors(nodeKey);
if (predecessors.length === 0) {
for (const field of requiredFields) {
errors.push({
type: "schema",
nodeKey,
field,
message: `Required input field "${field}" has no predecessor providing it`,
});
}
continue;
}
const providedFields = new Set<string>();
for (const predKey of predecessors) {
const predAttrs = graph.getNodeAttributes(predKey) as unknown as OperationNodeAttrs;
const predProvided = getProvidedFields(predAttrs.outputSchema);
for (const field of predProvided) {
providedFields.add(field);
}
}
for (const field of requiredFields) {
if (!providedFields.has(field)) {
errors.push({
type: "schema",
nodeKey,
field,
message: `Required input field "${field}" is not provided by any predecessor`,
});
}
}
}
return errors;
}
function collectOperationNodeKeys(dag: DirectedGraph): string[] {
const names: string[] = [];
dag.forEachNode((key) => {
if (!key.startsWith("__")) {
names.push(key);
}
});
return names;
}
export function validateTemplate(
template: UNode,
operationGraph: FlowGraph<typeof import("../schema/node.js").OperationNodeAttrs, typeof import("../schema/edge.js").OperationEdgeAttrs>,
): AnyValidationError[] {
const errors: AnyValidationError[] = [];
let renderedDag: DirectedGraph;
try {
const root = createHostRoot(GraphologyHostConfig, null);
root.render(template);
renderedDag = root.ctx.graph as DirectedGraph;
} catch {
renderedDag = new DirectedGraph();
}
const templateNodeKeys = collectOperationNodeKeys(renderedDag);
const graphNodeKeys = new Set(operationGraph.nodes());
for (const opKey of templateNodeKeys) {
if (!graphNodeKeys.has(opKey)) {
errors.push({
type: "graph",
category: "orphan-node",
details: { operation: opKey, message: `Operation "${opKey}" not found in operation graph` },
});
}
}
if (hasCycle(renderedDag)) {
errors.push({
type: "graph",
category: "cycle",
details: { message: "Rendered template DAG contains a cycle" },
});
}
for (const opKey of templateNodeKeys) {
if (!graphNodeKeys.has(opKey)) continue;
const outEdges = renderedDag.outEdges(opKey) ?? [];
for (const edge of outEdges) {
const target = renderedDag.target(edge);
if (target.startsWith("__")) continue;
if (!graphNodeKeys.has(target)) continue;
if (operationGraph.hasEdge(opKey, target)) {
const edgeAttrs = operationGraph.getEdgeAttributes(opKey, target) as unknown as OperationEdgeAttrs;
if (!edgeAttrs.compatible) {
errors.push({
type: "type-compat",
sourceKey: opKey,
targetKey: target,
compatible: false,
mismatches: edgeAttrs.mismatches ?? [],
});
}
}
}
}
if (templateNodeKeys.length > 1) {
const roots: string[] = [];
for (const key of templateNodeKeys) {
const inDegree = renderedDag.inDegree(key);
if (inDegree === 0) {
roots.push(key);
}
}
if (roots.length > 0) {
const reachable = reachableFrom(renderedDag, roots);
for (const nodeKey of templateNodeKeys) {
if (!reachable.has(nodeKey)) {
errors.push({
type: "graph",
category: "orphan-node",
details: { nodeKey, message: `Operation "${nodeKey}" is not reachable from start` },
});
}
}
}
}
for (const nodeKey of templateNodeKeys) {
const inDegree = renderedDag.inDegree(nodeKey);
const outDegree = renderedDag.outDegree(nodeKey);
if (inDegree === 0 && outDegree === 0 && templateNodeKeys.length > 1) {
errors.push({
type: "graph",
category: "orphan-node",
details: { nodeKey, message: `Operation "${nodeKey}" has no edges (orphan node)` },
});
}
}
return errors;
}

View File

@@ -1,270 +0,0 @@
import { describe, it, expect } from "vitest";
import { FlowGraph } from "../../src/graph/construction.js";
import {
topologicalOrder,
parallelGroups,
criticalPath,
reachableFrom,
ancestors,
descendants,
} from "../../src/analysis/ordering.js";
import { CycleError, NodeNotFoundError } from "../../src/error/index.js";
function buildDiamondDag(): FlowGraph {
const fg = new FlowGraph();
fg.addNode("top", { name: "top" });
fg.addNode("left", { name: "left" });
fg.addNode("right", { name: "right" });
fg.addNode("bottom", { name: "bottom" });
fg.addEdge("top", "left");
fg.addEdge("top", "right");
fg.addEdge("left", "bottom");
fg.addEdge("right", "bottom");
return fg;
}
function buildChainDag(): FlowGraph {
const fg = new FlowGraph();
fg.addNode("a", { name: "a" });
fg.addNode("b", { name: "b" });
fg.addNode("c", { name: "c" });
fg.addNode("d", { name: "d" });
fg.addEdge("a", "b");
fg.addEdge("b", "c");
fg.addEdge("c", "d");
return fg;
}
function buildWideDag(): FlowGraph {
const fg = new FlowGraph();
fg.addNode("root", { name: "root" });
fg.addNode("x", { name: "x" });
fg.addNode("y", { name: "y" });
fg.addNode("z", { name: "z" });
fg.addEdge("root", "x");
fg.addEdge("root", "y");
fg.addEdge("root", "z");
return fg;
}
function buildDisconnectedDag(): FlowGraph {
const fg = new FlowGraph();
fg.addNode("a", { name: "a" });
fg.addNode("b", { name: "b" });
fg.addNode("c", { name: "c" });
fg.addEdge("a", "b");
return fg;
}
describe("topologicalOrder (analysis)", () => {
it("returns topological order for a chain", () => {
const fg = buildChainDag();
expect(topologicalOrder(fg)).toEqual(["a", "b", "c", "d"]);
});
it("returns topological order for a diamond DAG", () => {
const fg = buildDiamondDag();
const order = topologicalOrder(fg);
expect(order[0]).toBe("top");
expect(order[3]).toBe("bottom");
expect(order).toContain("left");
expect(order).toContain("right");
});
it("returns empty array for empty graph", () => {
const fg = new FlowGraph();
expect(topologicalOrder(fg)).toEqual([]);
});
it("returns single node for graph with one node", () => {
const fg = new FlowGraph();
fg.addNode("only", { name: "only" });
expect(topologicalOrder(fg)).toEqual(["only"]);
});
it("throws CycleError when graph has cycles", () => {
const fg = new FlowGraph();
fg.graph.addNode("a");
fg.graph.addNode("b");
fg.graph.addNode("c");
fg.graph.addEdgeWithKey("a->b", "a", "b");
fg.graph.addEdgeWithKey("b->c", "b", "c");
fg.graph.addEdgeWithKey("c->a", "c", "a");
expect(() => topologicalOrder(fg)).toThrow(CycleError);
});
});
describe("parallelGroups (analysis)", () => {
it("groups chain nodes into sequential groups", () => {
const fg = buildChainDag();
const groups = parallelGroups(fg);
expect(groups).toEqual([["a"], ["b"], ["c"], ["d"]]);
});
it("groups diamond DAG correctly", () => {
const fg = buildDiamondDag();
const groups = parallelGroups(fg);
expect(groups.length).toBe(3);
expect(groups[0]).toEqual(["top"]);
expect(groups[1]!.sort()).toEqual(["left", "right"]);
expect(groups[2]).toEqual(["bottom"]);
});
it("groups wide DAG with root and leaves", () => {
const fg = buildWideDag();
const groups = parallelGroups(fg);
expect(groups.length).toBe(2);
expect(groups[0]).toEqual(["root"]);
expect(groups[1]!.sort()).toEqual(["x", "y", "z"]);
});
it("groups disconnected DAG correctly", () => {
const fg = buildDisconnectedDag();
const groups = parallelGroups(fg);
expect(groups.length).toBe(2);
expect(groups[0]!.sort()).toEqual(["a", "c"]);
expect(groups[1]).toEqual(["b"]);
});
it("returns empty array for empty graph", () => {
const fg = new FlowGraph();
expect(parallelGroups(fg)).toEqual([]);
});
it("returns single group for single node with no edges", () => {
const fg = new FlowGraph();
fg.addNode("only", { name: "only" });
expect(parallelGroups(fg)).toEqual([["only"]]);
});
it("throws CycleError when graph has cycles", () => {
const fg = new FlowGraph();
fg.graph.addNode("a");
fg.graph.addNode("b");
fg.graph.addEdgeWithKey("a->b", "a", "b");
fg.graph.addEdgeWithKey("b->a", "b", "a");
expect(() => parallelGroups(fg)).toThrow(CycleError);
});
});
describe("criticalPath (analysis)", () => {
it("returns full chain for a chain DAG", () => {
const fg = buildChainDag();
expect(criticalPath(fg)).toEqual(["a", "b", "c", "d"]);
});
it("returns a longest path for diamond DAG", () => {
const fg = buildDiamondDag();
const path = criticalPath(fg);
expect(path.length).toBe(3);
expect(path[0]).toBe("top");
expect(path[2]).toBe("bottom");
expect(path[1] === "left" || path[1] === "right").toBe(true);
});
it("returns single node for graph with one node", () => {
const fg = new FlowGraph();
fg.addNode("only", { name: "only" });
expect(criticalPath(fg)).toEqual(["only"]);
});
it("returns empty array for empty graph", () => {
const fg = new FlowGraph();
expect(criticalPath(fg)).toEqual([]);
});
it("returns correct path for wide DAG", () => {
const fg = buildWideDag();
const path = criticalPath(fg);
expect(path.length).toBe(2);
expect(path[0]).toBe("root");
});
it("throws CycleError when graph has cycles", () => {
const fg = new FlowGraph();
fg.graph.addNode("a");
fg.graph.addNode("b");
fg.graph.addEdgeWithKey("a->b", "a", "b");
fg.graph.addEdgeWithKey("b->a", "b", "a");
expect(() => criticalPath(fg)).toThrow(CycleError);
});
});
describe("reachableFrom (analysis)", () => {
it("returns all reachable nodes from a single start node", () => {
const fg = buildChainDag();
expect(reachableFrom(fg, ["a"])).toEqual(new Set(["a", "b", "c", "d"]));
});
it("returns only the start node if it is a leaf", () => {
const fg = buildChainDag();
expect(reachableFrom(fg, ["d"])).toEqual(new Set(["d"]));
});
it("returns union of reachable nodes from multiple start nodes", () => {
const fg = buildDiamondDag();
expect(reachableFrom(fg, ["left", "right"])).toEqual(new Set(["left", "right", "bottom"]));
});
it("returns empty set for empty input", () => {
const fg = buildChainDag();
expect(reachableFrom(fg, [])).toEqual(new Set());
});
it("returns full reachable set from root in diamond", () => {
const fg = buildDiamondDag();
expect(reachableFrom(fg, ["top"])).toEqual(new Set(["top", "left", "right", "bottom"]));
});
});
describe("ancestors (analysis)", () => {
it("returns all ancestors for a node in a chain", () => {
const fg = buildChainDag();
expect(ancestors(fg, "d")).toEqual(["c", "b", "a"]);
});
it("returns all ancestors for a node in a diamond DAG", () => {
const fg = buildDiamondDag();
const anc = ancestors(fg, "bottom");
expect(anc).toContain("left");
expect(anc).toContain("right");
expect(anc).toContain("top");
expect(anc.length).toBe(3);
});
it("returns empty array for a root node", () => {
const fg = buildChainDag();
expect(ancestors(fg, "a")).toEqual([]);
});
it("throws NodeNotFoundError for missing node", () => {
const fg = new FlowGraph();
expect(() => ancestors(fg, "missing")).toThrow(NodeNotFoundError);
});
});
describe("descendants (analysis)", () => {
it("returns all descendants for a node in a chain", () => {
const fg = buildChainDag();
expect(descendants(fg, "a")).toEqual(["b", "c", "d"]);
});
it("returns all descendants for a node in a diamond DAG", () => {
const fg = buildDiamondDag();
const desc = descendants(fg, "top");
expect(desc).toContain("left");
expect(desc).toContain("right");
expect(desc).toContain("bottom");
expect(desc.length).toBe(3);
});
it("returns empty array for a leaf node", () => {
const fg = buildChainDag();
expect(descendants(fg, "d")).toEqual([]);
});
it("throws NodeNotFoundError for missing node", () => {
const fg = new FlowGraph();
expect(() => descendants(fg, "missing")).toThrow(NodeNotFoundError);
});
});

View File

@@ -1,7 +1,251 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from "vitest";
import { Type } from "@alkdev/typebox";
import { h, createHostRoot } from "@alkdev/ujsx";
import { Operation, Sequential, Parallel, Conditional } from "../../src/component/index.js";
import { validatePreconditions, validateTemplate } from "../../src/analysis/workflow.js";
import { FlowGraph } from "../../src/graph/construction.js";
import type { OperationNodeAttrs, OperationEdgeAttrs } from "../../src/schema/index.js";
describe('analysis workflow', () => { type OpGraph = FlowGraph<typeof import("../../src/schema/node.js").OperationNodeAttrs, typeof import("../../src/schema/edge.js").OperationEdgeAttrs>;
it('placeholder', () => {
expect(true).toBe(true); function createOperationGraph(
specs: Array<{
name: string;
namespace?: string;
inputSchema?: Record<string, unknown>;
outputSchema?: Record<string, unknown>;
}>,
): OpGraph {
const graph = new FlowGraph() as OpGraph;
for (const spec of specs) {
const ns = spec.namespace ?? "test";
const key = `${ns}.${spec.name}`;
graph.addNode(key, {
name: spec.name,
namespace: ns,
version: "1.0.0",
type: "query",
inputSchema: spec.inputSchema ?? Type.Object({}),
outputSchema: spec.outputSchema ?? Type.Object({}),
} as OperationNodeAttrs);
}
return graph;
}
function createOperationGraphWithEdges(
specs: Array<{
name: string;
namespace?: string;
inputSchema?: Record<string, unknown>;
outputSchema?: Record<string, unknown>;
}>,
edges?: Array<{ source: string; target: string; compatible: boolean; mismatches?: Array<{ path: string; expected: string; actual: string }> }>,
): OpGraph {
const graph = createOperationGraph(specs);
if (edges) {
for (const edge of edges) {
graph.addTypedEdge(edge.source, edge.target, {
compatible: edge.compatible,
mismatches: edge.mismatches,
});
}
}
return graph;
}
describe("validatePreconditions", () => {
it("returns empty for valid graph with no required fields", () => {
const graph = createOperationGraph([
{ name: "a", outputSchema: Type.Object({ x: Type.Number() }) },
{ name: "b", inputSchema: Type.Object({}) },
]);
graph.addEdge("test.a", "test.b");
const errors = validatePreconditions(graph);
expect(errors).toEqual([]);
});
it("returns empty when all required input fields are provided by predecessors", () => {
const graph = createOperationGraph([
{ name: "a", outputSchema: Type.Object({ x: Type.Number(), y: Type.String() }) },
{ name: "b", inputSchema: Type.Object({ x: Type.Number() }) },
]);
graph.addEdge("test.a", "test.b");
const errors = validatePreconditions(graph);
expect(errors).toEqual([]);
});
it("returns errors when required input field is not provided by any predecessor", () => {
const graph = createOperationGraph([
{ name: "a", outputSchema: Type.Object({ x: Type.Number() }) },
{ name: "b", inputSchema: Type.Object({ x: Type.Number(), y: Type.String() }) },
]);
graph.addEdge("test.a", "test.b");
const errors = validatePreconditions(graph);
expect(errors.length).toBeGreaterThan(0);
const fieldNames = errors.map((e) => e.field);
expect(fieldNames).toContain("y");
});
it("returns errors when node with required fields has no predecessors", () => {
const graph = createOperationGraph([
{ name: "a", inputSchema: Type.Object({ x: Type.Number() }), outputSchema: Type.Object({}) },
]);
const errors = validatePreconditions(graph);
expect(errors.length).toBeGreaterThan(0);
expect(errors[0]!.message).toContain("no predecessor");
});
it("collects provided fields from multiple predecessors", () => {
const graph = createOperationGraph([
{ name: "a", outputSchema: Type.Object({ x: Type.Number() }) },
{ name: "c", outputSchema: Type.Object({ y: Type.String() }) },
{ name: "b", inputSchema: Type.Object({ x: Type.Number(), y: Type.String() }) },
]);
graph.addEdge("test.a", "test.b");
graph.addEdge("test.c", "test.b");
const errors = validatePreconditions(graph);
expect(errors).toEqual([]);
});
it("returns empty for graph with no nodes", () => {
const graph = new FlowGraph() as OpGraph;
const errors = validatePreconditions(graph);
expect(errors).toEqual([]);
});
});
describe("validateTemplate", () => {
it("returns empty for valid template with all operations in graph", () => {
const graph = createOperationGraphWithEdges([
{ name: "a", outputSchema: Type.Object({ x: Type.Number() }) },
{ name: "b", inputSchema: Type.Object({ x: Type.Number() }) },
], [
{ source: "test.a", target: "test.b", compatible: true },
]);
const template = h(Sequential, {},
h(Operation, { name: "test.a" }),
h(Operation, { name: "test.b" }),
);
const errors = validateTemplate(template, graph);
expect(errors).toEqual([]);
});
it("returns error when operation name is not in operation graph", () => {
const graph = createOperationGraph([
{ name: "a" },
]);
const template = h(Sequential, {},
h(Operation, { name: "test.a" }),
h(Operation, { name: "test.missing" }),
);
const errors = validateTemplate(template, graph);
expect(errors.length).toBeGreaterThan(0);
const missingErrors = errors.filter(
(e) => e.type === "graph" && (e as { type: string; category: string; details: unknown }).category === "orphan-node"
&& JSON.stringify((e as { details: unknown }).details).includes("missing"),
);
expect(missingErrors.length).toBeGreaterThan(0);
});
it("returns error for type incompatibility between sequential operations", () => {
const graph = createOperationGraphWithEdges([
{ name: "a", outputSchema: Type.Object({ x: Type.String() }) },
{ name: "b", inputSchema: Type.Object({ x: Type.Number() }) },
], [
{ source: "test.a", target: "test.b", compatible: false, mismatches: [{ path: "/x", expected: "number", actual: "string" }] },
]);
const template = h(Sequential, {},
h(Operation, { name: "test.a" }),
h(Operation, { name: "test.b" }),
);
const errors = validateTemplate(template, graph);
const typeErrors = errors.filter((e) => e.type === "type-compat");
expect(typeErrors.length).toBeGreaterThan(0);
});
it("returns empty for single-operation template", () => {
const graph = createOperationGraph([
{ name: "a" },
]);
const template = h(Operation, { name: "test.a" });
const errors = validateTemplate(template, graph);
expect(errors).toEqual([]);
});
it("detects unreachable nodes", () => {
const graph = createOperationGraphWithEdges([
{ name: "a" },
{ name: "b" },
{ name: "c" },
]);
const template = h(Sequential, {},
h(Operation, { name: "test.a" }),
h(Operation, { name: "test.b" }),
);
const errors = validateTemplate(template, graph);
const reachableErrors = errors.filter(
(e) => e.type === "graph" && JSON.stringify((e as { details: unknown }).details).includes("not reachable"),
);
expect(reachableErrors.length).toBe(0);
});
it("returns empty for valid parallel template", () => {
const graph = createOperationGraphWithEdges([
{ name: "a" },
{ name: "b" },
{ name: "c" },
]);
const template = h(Sequential, {},
h(Operation, { name: "test.a" }),
h(Parallel, {},
h(Operation, { name: "test.b" }),
h(Operation, { name: "test.c" }),
),
);
const errors = validateTemplate(template, graph);
expect(errors).toEqual([]);
});
it("handles template with conditional", () => {
const graph = createOperationGraphWithEdges([
{ name: "a", outputSchema: Type.Object({ result: Type.Boolean() }) },
{ name: "b" },
{ name: "c" },
]);
const template = h(Sequential, {},
h(Operation, { name: "test.a" }),
h(Conditional, { test: "test.a" },
h(Operation, { name: "test.b" }),
),
h(Operation, { name: "test.c" }),
);
const errors = validateTemplate(template, graph);
expect(errors).toEqual([]);
});
it("template validation is advisory - never throws", () => {
const graph = new FlowGraph() as OpGraph;
const template = h(Sequential, {},
h(Operation, { name: "nonexistent" }),
);
const errors = validateTemplate(template, graph);
expect(Array.isArray(errors)).toBe(true);
});
it("detects orphan node with no edges in multi-node template", () => {
const graph = createOperationGraphWithEdges([
{ name: "a" },
{ name: "b" },
]);
const template = h(Parallel, {},
h(Operation, { name: "test.a" }),
h(Operation, { name: "test.b" }),
);
const errors = validateTemplate(template, graph);
const orphanErrors = errors.filter(
(e) => e.type === "graph" && (e as { type: string; category: string }).category === "orphan-node"
&& JSON.stringify((e as { details: unknown }).details).includes("no edges"),
);
expect(orphanErrors.length).toBeGreaterThan(0);
}); });
}); });