Files
aiaa-notification-server/internal/engine/event_match_test.go
T
ryan d2e8476398 feat(规则): 支持同一事件多规则按条件区分并全部发送
Motivation:
止盈、止损等不同策略信号会映射到同一事件(如 trade.close),但原有唯一约束要求每个事件只能有一条规则,无法按策略区分处理。放开该约束后,同一事件可配置多条规则,通过规则条件与精确/通配优先级区分,命中条件的规则全部发送;同时将去重键从消息原文改为信号维度,避免同一信号因时间戳等无关字段差异被误判为重复。

Changes:

* 移除规则 source_id+event 的唯一约束,改为普通索引
* 事件匹配改为返回命中优先级内所有启用规则,并按 ID 逐条派发
* 通知服务遍历多条规则,按条件过滤后聚合发送渠道
* 去重键由消息 body 哈希改为策略/币种/周期/方向/价格信号维度哈希
2026-08-17 01:20:49 +08:00

92 lines
2.2 KiB
Go

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)
}
}