File size: 2,083 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package uik

import (
	"testing"

	"github.com/expr-lang/expr/vm"
	"github.com/stretchr/testify/require"
	"github.com/target/goalert/gadb"
)

func TestCompiledConfig_Run(t *testing.T) {
	cfg := gadb.UIKConfigV1{
		Rules: []gadb.UIKRuleV1{
			{
				Name:          "rule1",
				ConditionExpr: "shouldRun1",
				Actions: []gadb.UIKActionV1{
					// Lets use a string literal to simplify this test.
					{Params: map[string]string{"key": `"value1"`}},
				},
			},
			{
				Name:               "rule2",
				ConditionExpr:      "shouldRun2",
				ContinueAfterMatch: true,
				Actions: []gadb.UIKActionV1{
					{Params: map[string]string{"key": `"value2"`}},
				},
			},
			{
				Name:          "rule3",
				ConditionExpr: "shouldRun3",
				Actions: []gadb.UIKActionV1{
					{Params: map[string]string{"key": `"value3"`}},
				},
			},
		},
		DefaultActions: []gadb.UIKActionV1{
			{Params: map[string]string{"key": `"valueDefault"`}},
		},
	}
	cmp, err := NewCompiledConfig(cfg)
	require.NoError(t, err, "should compile a valid config")
	var vm vm.VM

	check := func(desc string, env any, expValues []string) {
		t.Helper()
		t.Run(desc, func(t *testing.T) {
			res, err := cmp.Run(&vm, env)
			require.NoError(t, err, "should run a valid config")
			require.Len(t, res, len(expValues), "should evaluate all actions")

			for i, a := range res {
				require.Equal(t, expValues[i], a.Params["key"], "should evaluate action params")
			}
		})
	}

	check("default actions",
		// since no rules match, we should only see the default action
		map[string]any{
			"shouldRun1": false,
			"shouldRun2": false,
			"shouldRun3": false,
		},
		[]string{"valueDefault"})

	check("stop at first match",
		// by default, processing stops at first match
		map[string]any{
			"shouldRun1": true,
			"shouldRun2": true,
			"shouldRun3": true,
		},
		[]string{"value1"})

	check("stop at first match",
		// rule #2 has ContinueAfterMatch set to true, so rule #3 should also run
		map[string]any{
			"shouldRun1": false,
			"shouldRun2": true,
			"shouldRun3": true,
		},
		[]string{"value2", "value3"})
}