-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_test.go
More file actions
64 lines (60 loc) · 2.17 KB
/
Copy pathnode_test.go
File metadata and controls
64 lines (60 loc) · 2.17 KB
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
package ruleflow
import "testing"
func TestParseNodes(t *testing.T) {
// README 四类节点示例
js := []byte(`[
{"id":"001","name":"每分钟触发一次","type":"trigger","config":{"interval":60},"outputs":{"ids":[]}},
{"id":"002","name":"等于","type":"condition","config":{"relation":"="},
"inputs":[{"key":"a","value":1,"position":1},{"key":"b","value":2,"position":2}],
"outputs":{"ids":[],"name":"a等于b","value":false}},
{"id":"003","name":"通知","type":"action","config":{"http":"url","method":"POST"},
"inputs":[{"key":"告警","value":"","position":1}]},
{"id":"004","name":"条件判断","type":"block","config":{"subflow":[]},
"inputs":[{"key":"温度","value":26,"position":1}],"outputs":{"ids":[],"value":false}}
]`)
nodes, err := ParseNodes(js)
if err != nil {
t.Fatalf("解析失败: %v", err)
}
if len(nodes) != 4 {
t.Fatalf("应解析 4 个节点, got %d", len(nodes))
}
if nodes[0].Type != TypeTrigger || nodes[1].Type != TypeCondition ||
nodes[2].Type != TypeAction || nodes[3].Type != TypeBlock {
t.Error("节点类型解析错误")
}
if nodes[1].Inputs[0].Key != "a" || nodes[1].Inputs[1].Key != "b" {
t.Error("输入项解析错误")
}
}
func TestParseNodesInvalid(t *testing.T) {
if _, err := ParseNodes([]byte(`not json`)); err == nil {
t.Error("非法 JSON 应返回错误")
}
// 空数组合法
if nodes, err := ParseNodes([]byte(`[]`)); err != nil || len(nodes) != 0 {
t.Errorf("空数组应合法且为空, err=%v len=%d", err, len(nodes))
}
}
func TestConfigAccessors(t *testing.T) {
n := &Node{Config: map[string]any{"relation": ">", "interval": 60, "flag": true}}
if n.ConfigString("relation") != ">" {
t.Error("ConfigString 取值错误")
}
if n.ConfigString("notexist") != "" {
t.Error("不存在的键应返回空串")
}
// 非字符串字段用 ConfigString 取应返回空(interval 是数字)
if n.ConfigString("interval") != "" {
t.Error("数字字段用 ConfigString 应返回空")
}
m := n.Config
if m["relation"] != ">" {
t.Error("Config 取值错误")
}
// 空 config
empty := &Node{}
if len(empty.Config) != 0 || empty.ConfigString("x") != "" {
t.Error("空 config 应返回空 map/串")
}
}