package engine import ( "testing" "aiaa-notification-service/internal/model" ) func TestPickEventRuleExact(t *testing.T) { rules := []model.Rule{ {ID: 1, Event: "trade.*", Enabled: 1}, {ID: 2, Event: "trade.open", Enabled: 1}, } got := PickEventRule("trade.open", rules) if got == nil || got.ID != 2 { t.Fatalf("want exact id=2, got %#v", got) } } func TestPickEventRuleWildcard(t *testing.T) { rules := []model.Rule{ {ID: 1, Event: "trade.*", Enabled: 1}, } got := PickEventRule("trade.close", rules) if got == nil || got.Event != "trade.*" { t.Fatalf("want trade.*, got %#v", got) } if PickEventRule("order.open", rules) != nil { t.Fatal("trade.* must not match order.open") } } func TestPickEventRuleMoreSpecificWildcardWins(t *testing.T) { rules := []model.Rule{ {ID: 1, Event: "*", Enabled: 1}, {ID: 2, Event: "trade.*", Enabled: 1}, } got := PickEventRule("trade.open", rules) if got == nil || got.ID != 2 { t.Fatalf("want trade.* id=2, got %#v", got) } } func TestPickEventRuleSkipsDisabled(t *testing.T) { rules := []model.Rule{ {ID: 1, Event: "trade.*", Enabled: 0}, {ID: 2, Event: "*", Enabled: 1}, } got := PickEventRule("trade.open", rules) if got == nil || got.ID != 2 { t.Fatalf("want * id=2, got %#v", got) } } func TestPickEventRuleNoMatch(t *testing.T) { rules := []model.Rule{ {ID: 1, Event: "trade.open", Enabled: 1}, } if PickEventRule("trade.close", rules) != nil { t.Fatal("expected no match") } } func TestPickEventRulesAllExactMatches(t *testing.T) { rules := []model.Rule{ {ID: 12, Event: "trade.*", Enabled: 1}, {ID: 17, Event: "trade.close", Enabled: 1}, {ID: 18, Event: "trade.close", Enabled: 1}, {ID: 19, Event: "trade.close", Enabled: 0}, } got := PickEventRules("trade.close", rules) if len(got) != 2 { t.Fatalf("want 2 exact trade.close, got %#v", got) } ids := []int{got[0].ID, got[1].ID} if ids[0] != 17 || ids[1] != 18 { t.Fatalf("ids=%v", ids) } } func TestPickEventRulesSameGlobBoth(t *testing.T) { rules := []model.Rule{ {ID: 1, Event: "trade.*", Enabled: 1}, {ID: 2, Event: "trade.*", Enabled: 1}, {ID: 3, Event: "*", Enabled: 1}, } got := PickEventRules("trade.open", rules) if len(got) != 2 { t.Fatalf("want both trade.*, got %#v", got) } }