File size: 5,595 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { describe, it, expect } from "vitest";
import { Edge } from "@xyflow/react";
import {
  validateSchema,
  allNodeValidate,
  inputNodeValidate,
  outputNodeValidate,
  llmNodeValidate,
} from "./node-validate";
import { UINode, NodeKind } from "./workflow.interface";

describe("node-validate", () => {
  const createInputNodeData = (
    id: string,
    name: string,
    outputSchema = {
      type: "object" as const,
      properties: { input: { type: "string" as const } },
    },
  ): UINode<NodeKind.Input> => ({
    id,
    type: "default",
    position: { x: 0, y: 0 },
    data: {
      id,
      name,
      kind: NodeKind.Input,
      outputSchema,
    },
  });

  const createOutputNodeData = (
    id: string,
    name: string,
    outputData: any[] = [],
  ): UINode<NodeKind.Output> => ({
    id,
    type: "default",
    position: { x: 0, y: 0 },
    data: {
      id,
      name,
      kind: NodeKind.Output,
      outputSchema: { type: "object", properties: {} },
      outputData,
    },
  });

  const createLLMNodeData = (
    id: string,
    name: string,
    model: any = { id: "gpt-4", name: "GPT-4" },
    messages: any[] = [{ role: "user", content: { type: "doc", content: [] } }],
  ): UINode<NodeKind.LLM> => ({
    id,
    type: "default",
    position: { x: 0, y: 0 },
    data: {
      id,
      name,
      kind: NodeKind.LLM,
      outputSchema: { type: "object", properties: {} },
      model,
      messages,
    },
  });

  const createEdge = (id: string, source: string, target: string): Edge => ({
    id,
    source,
    target,
  });

  describe("validateSchema", () => {
    it("should validate valid string schema", () => {
      expect(() => {
        validateSchema("test", { type: "string" });
      }).not.toThrow();
    });

    it("should throw error for invalid variable name", () => {
      expect(() => {
        validateSchema("", { type: "string" });
      }).toThrow();
    });

    it("should throw error for schema without type", () => {
      expect(() => {
        validateSchema("test", {});
      }).toThrow();
    });
  });

  describe("inputNodeValidate", () => {
    it("should validate start node with edge and inputs", () => {
      const startNode = createInputNodeData("start", "Start Node");
      const edges = [createEdge("edge1", "start", "end")];

      expect(() => {
        inputNodeValidate({ node: startNode.data, nodes: [], edges });
      }).not.toThrow();
    });

    it("should throw error when start node has no edge", () => {
      const startNode = createInputNodeData("start", "Start Node");

      expect(() => {
        inputNodeValidate({ node: startNode.data, nodes: [], edges: [] });
      }).toThrow();
    });
  });

  describe("outputNodeValidate", () => {
    it("should validate end node with proper source", () => {
      const startNode = createInputNodeData("start", "Start Node");
      const endNode = createOutputNodeData("end", "End Node", [
        {
          key: "result",
          source: { nodeId: "start", path: ["input"] },
        },
      ]);
      const nodes = [startNode, endNode];
      const edges = [createEdge("edge1", "start", "end")];

      expect(() => {
        outputNodeValidate({ node: endNode.data, nodes, edges });
      }).not.toThrow();
    });

    it("should throw error when end node has duplicate output keys", () => {
      const endNode = createOutputNodeData("end", "End Node", [
        { key: "result", source: { nodeId: "start", path: ["input"] } },
        { key: "result", source: { nodeId: "start", path: ["input"] } },
      ]);

      expect(() => {
        outputNodeValidate({ node: endNode.data, nodes: [], edges: [] });
      }).toThrow();
    });
  });

  describe("llmNodeValidate", () => {
    it("should validate LLM node with model and messages", () => {
      const llmNode = createLLMNodeData("llm", "LLM Node");

      expect(() => {
        llmNodeValidate({ node: llmNode.data, nodes: [], edges: [] });
      }).not.toThrow();
    });

    it("should throw error when LLM node has no model", () => {
      const llmNode = createLLMNodeData("llm", "LLM Node", null);

      expect(() => {
        llmNodeValidate({ node: llmNode.data, nodes: [], edges: [] });
      }).toThrow();
    });

    it("should throw error when LLM node has no messages", () => {
      const llmNode = createLLMNodeData(
        "llm",
        "LLM Node",
        { id: "gpt-4", name: "GPT-4" },
        [],
      );

      expect(() => {
        llmNodeValidate({ node: llmNode.data, nodes: [], edges: [] });
      }).toThrow();
    });
  });

  describe("allNodeValidate", () => {
    it("should validate workflow with start and end nodes", () => {
      const startNode = createInputNodeData("start", "Start Node");
      const endNode = createOutputNodeData("end", "End Node", [
        {
          key: "result",
          source: { nodeId: "start", path: ["input"] },
        },
      ]);
      const nodes = [startNode, endNode];
      const edges = [createEdge("edge1", "start", "end")];

      const result = allNodeValidate({ nodes, edges });
      expect(result).toBe(true);
    });

    it("should return error when nodes have duplicate names", () => {
      const startNode1 = createInputNodeData("start1", "Duplicate Name");
      const startNode2 = createOutputNodeData("start1", "Duplicate Name");
      const nodes = [startNode1, startNode2];
      const edges = [];

      const result = allNodeValidate({ nodes, edges });
      expect(result).not.toBe(true);
      expect(result).toHaveProperty("errorMessage");
    });
  });
});