57 lines
2.5 KiB
Go
57 lines
2.5 KiB
Go
package run
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/model"
|
|
"gorm.io/datatypes"
|
|
)
|
|
|
|
func TestMatchCondition(t *testing.T) {
|
|
answers := map[string]interface{}{"weight": float64(6), "urgent": true, "symptom": "持续呕吐"}
|
|
tests := []struct {
|
|
name string
|
|
rule string
|
|
want bool
|
|
}{
|
|
{name: "default", rule: `{}`, want: true},
|
|
{name: "equals", rule: `{"field":"urgent","operator":"equals","value":true}`, want: true},
|
|
{name: "greater", rule: `{"field":"weight","operator":"greater_than","value":5}`, want: true},
|
|
{name: "contains", rule: `{"field":"symptom","operator":"contains","value":"呕吐"}`, want: true},
|
|
{name: "all", rule: `{"all":[{"field":"urgent","operator":"equals","value":true},{"field":"weight","operator":"greater_than","value":5}]}`, want: true},
|
|
{name: "nested groups", rule: `{"all":[{"field":"urgent","operator":"equals","value":true},{"any":[{"field":"weight","operator":"less_than","value":3},{"field":"symptom","operator":"contains","value":"呕吐"}]}]}`, want: true},
|
|
{name: "any false", rule: `{"any":[{"field":"urgent","operator":"equals","value":false},{"field":"weight","operator":"less_than","value":3}]}`, want: false},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got, err := matchCondition(json.RawMessage(test.rule), answers)
|
|
if err != nil {
|
|
t.Fatalf("matchCondition returned error: %v", err)
|
|
}
|
|
if got != test.want {
|
|
t.Fatalf("matchCondition() = %v, want %v", got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMatchConditionRejectsAmbiguousGroup(t *testing.T) {
|
|
_, err := matchCondition(json.RawMessage(`{"all":[{"field":"urgent","operator":"equals","value":true}],"any":[{"field":"urgent","operator":"equals","value":true}]}`), map[string]interface{}{"urgent": true})
|
|
if err == nil {
|
|
t.Fatal("matchCondition() should reject a condition containing both all and any")
|
|
}
|
|
}
|
|
|
|
func TestSortEdgesAlwaysPlacesDefaultLast(t *testing.T) {
|
|
edges := []model.SOPEdge{
|
|
{TargetNodeKey: "default", Condition: datatypes.JSON([]byte(`{}`)), Priority: 0},
|
|
{TargetNodeKey: "second", Condition: datatypes.JSON([]byte(`{"field":"urgent","operator":"equals","value":false}`)), Priority: 20},
|
|
{TargetNodeKey: "first", Condition: datatypes.JSON([]byte(`{"field":"urgent","operator":"equals","value":true}`)), Priority: 10},
|
|
}
|
|
sortEdges(edges)
|
|
if edges[0].TargetNodeKey != "first" || edges[1].TargetNodeKey != "second" || edges[2].TargetNodeKey != "default" {
|
|
t.Fatalf("sortEdges() order = %s, %s, %s", edges[0].TargetNodeKey, edges[1].TargetNodeKey, edges[2].TargetNodeKey)
|
|
}
|
|
}
|