File size: 1,108 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
package uik

import (
	"testing"

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

func TestCompiledRule_Run(t *testing.T) {
	r := gadb.UIKRuleV1{
		ConditionExpr: "shouldRun",
		Actions: []gadb.UIKActionV1{
			{Params: map[string]string{"key": "someVar"}},
		},
	}

	var vm vm.VM
	compiledRule, err := NewCompiledRule(r)
	require.NoError(t, err, "should compile a valid rule")
	actions, matched, err := compiledRule.Run(&vm, map[string]any{"shouldRun": false})
	require.NoError(t, err, "should run a valid rule")
	require.Empty(t, actions, "should not return actions if condition evals false")
	require.False(t, matched, "should not return matched if condition evals false")

	actions, matched, err = compiledRule.Run(&vm, map[string]any{"shouldRun": true, "someVar": "value"})
	require.NoError(t, err, "should run a valid rule")
	require.True(t, matched, "should return matched if condition evals true")
	require.Len(t, actions, 1, "should run a valid rule")
	require.Equal(t, "value", actions[0].Params["key"], "should evaluate action params")
}