-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJsonUtil.tojson.test.tsx
216 lines (205 loc) · 7.18 KB
/
JsonUtil.tojson.test.tsx
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// JsonUtil.tojson.test.tsx
import { describe, it, expect, vi } from 'vitest';
import { subGraphToJson, allSubGraphsToJson } from './JsonUtil';
import { SubGraph } from './GraphContext';
import { Node } from '@xyflow/react';
describe('JsonUtil', () => {
it('should convert a subGraph to JSON correctly', () => {
const mockSubGraph: SubGraph = {
graphName: "testGraph",
nodes: [
{
id: '1',
type: 'custom',
position: { x: 100, y: 100 },
width: 200,
height: 200,
data: { type: "START", name: "Start Node", description: "This is the start" } ,
} as Node,
{
id: '2',
type: 'custom',
position: { x: 200, y: 200 },
width: 250,
height: 150,
data: { type: "STEP", tool: "someTool", nexts:['3'], true_next: '3'} ,
} as Node,
{
id: '3',
type: 'custom',
position: {x: 300, y: 300},
data:{ type: "END"} ,
} as Node,
],
edges: [],
serial_number: 3,
};
const expectedJson = {
name: "testGraph",
nodes: [
{
uniq_id: '1',
type: 'START',
name: "Start Node",
description: "This is the start",
tool: '',
nexts: [],
true_next: null,
false_next: null,
ext: { pos_x: 100, pos_y: 100, width: 200, height: 200, info: null },
},
{
uniq_id: '2',
type: 'STEP',
name: "",
description: "",
tool: 'someTool',
nexts: [ '3' ],
true_next: '3',
false_next: null,
ext: { pos_x: 200, pos_y: 200, width: 250, height: 150, info: null },
},
{
uniq_id: '3',
type: 'END',
name: "",
description: "",
tool: "",
nexts: [],
true_next: null,
false_next: null,
ext: { pos_x: 300, pos_y: 300, width: 200, height: 200, info: null }
}
],
serial_number: 3,
};
const result = subGraphToJson(mockSubGraph);
console.log("subGraphToJson Output:", JSON.stringify(result, null, 2));
expect(result).toEqual(expectedJson);
});
it('should handle missing properties and default type for node.data', () => {
const mockSubGraph: SubGraph = {
graphName: "testGraph",
nodes: [
{
id: '1',
type: 'custom',
position: { x: 100, y: 100 },
data: { } as any, //missing type
} as Node,
{
id: '2',
type: 'custom',
position: { x: 200, y: 200 },
data: {type:"STEP", tool:"test"} ,
} as Node
],
edges: [],
serial_number: 1,
};
const expectedJson = {
name: "testGraph",
nodes:[
{
uniq_id: '1',
type: 'STEP',
name: "",
description: "",
tool: "",
nexts: [],
true_next: null,
false_next: null,
ext: { pos_x: 100, pos_y: 100, width: 200, height: 200, info: null }
},
{
uniq_id: '2',
type: 'STEP',
name: "",
description: "",
tool: "test",
nexts: [],
true_next: null,
false_next: null,
ext: { pos_x: 200, pos_y: 200, width: 200, height: 200, info: null }
}
],
serial_number: 1
}
// Mock console.error to check if it's called for invalid node data
const consoleErrorMock = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = subGraphToJson(mockSubGraph);
console.log("subGraphToJson Output (missing properties):", JSON.stringify(result, null, 2));
expect(result).toEqual(expectedJson);
expect(consoleErrorMock).toHaveBeenCalled();
consoleErrorMock.mockRestore();
});
it('should convert all subgraphs to JSON correctly', () => {
const mockSubGraphs: SubGraph[] = [
{
graphName: "graph1",
nodes: [
{
id: '1',
type: 'custom',
position: { x: 100, y: 100 },
data: { type: "START"} ,
} as Node
],
edges: [],
serial_number: 1,
},
{
graphName: "graph2",
nodes: [
{
id: '2',
type: 'custom',
position: { x: 200, y: 200 },
data: { type: "STEP", tool:"test"} ,
} as Node
],
edges: [],
serial_number: 2,
},
];
const expectedJson = [
{
name: "graph1",
nodes:[
{
uniq_id: '1',
type: 'START',
name: "",
description: "",
tool: "",
nexts: [],
true_next: null,
false_next: null,
ext: { pos_x: 100, pos_y: 100, width: 200, height: 200, info: null }
},
],
serial_number: 1,
},
{
name: "graph2",
nodes: [
{
uniq_id: '2',
type: 'STEP',
name: "",
description: "",
tool: "test",
nexts: [],
true_next: null,
false_next: null,
ext: { pos_x: 200, pos_y: 200, width: 200, height: 200, info: null }
},
],
serial_number: 2,
},
];
const result = allSubGraphsToJson(mockSubGraphs);
console.log("allSubGraphsToJson Output:", JSON.stringify(result, null, 2));
expect(result).toEqual(expectedJson);
});
});