MCPcopy Create free account
hub / github.com/daodst/chat / TestRuleMatches

Function TestRuleMatches

internal/pushrules/evaluate_test.go:55–98  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

53}
54
55func TestRuleMatches(t *testing.T) {
56 emptyRule := Rule{Enabled: true}
57 tsts := []struct {
58 Name string
59 Kind Kind
60 Rule Rule
61 EventJSON string
62 Want bool
63 }{
64 {"emptyOverride", OverrideKind, emptyRule, `{}`, true},
65 {"emptyContent", ContentKind, emptyRule, `{}`, false},
66 {"emptyRoom", RoomKind, emptyRule, `{}`, true},
67 {"emptySender", SenderKind, emptyRule, `{}`, true},
68 {"emptyUnderride", UnderrideKind, emptyRule, `{}`, true},
69
70 {"disabled", OverrideKind, Rule{}, `{}`, false},
71
72 {"overrideConditionMatch", OverrideKind, Rule{Enabled: true}, `{}`, true},
73 {"overrideConditionNoMatch", OverrideKind, Rule{Enabled: true, Conditions: []*Condition{{}}}, `{}`, false},
74
75 {"underrideConditionMatch", UnderrideKind, Rule{Enabled: true}, `{}`, true},
76 {"underrideConditionNoMatch", UnderrideKind, Rule{Enabled: true, Conditions: []*Condition{{}}}, `{}`, false},
77
78 {"contentMatch", ContentKind, Rule{Enabled: true, Pattern: "b"}, `{"content":{"body":"abc"}}`, true},
79 {"contentNoMatch", ContentKind, Rule{Enabled: true, Pattern: "d"}, `{"content":{"body":"abc"}}`, false},
80
81 {"roomMatch", RoomKind, Rule{Enabled: true, RuleID: "!room@example.com"}, `{"room_id":"!room@example.com"}`, true},
82 {"roomNoMatch", RoomKind, Rule{Enabled: true, RuleID: "!room@example.com"}, `{"room_id":"!otherroom@example.com"}`, false},
83
84 {"senderMatch", SenderKind, Rule{Enabled: true, RuleID: "@user@example.com"}, `{"sender":"@user@example.com"}`, true},
85 {"senderNoMatch", SenderKind, Rule{Enabled: true, RuleID: "@user@example.com"}, `{"sender":"@otheruser@example.com"}`, false},
86 }
87 for _, tst := range tsts {
88 t.Run(tst.Name, func(t *testing.T) {
89 got, err := ruleMatches(&tst.Rule, tst.Kind, mustEventFromJSON(t, tst.EventJSON), nil)
90 if err != nil {
91 t.Fatalf("ruleMatches failed: %v", err)
92 }
93 if got != tst.Want {
94 t.Errorf("ruleMatches: got %v, want %v", got, tst.Want)
95 }
96 })
97 }
98}
99
100func TestConditionMatches(t *testing.T) {
101 tsts := []struct {

Callers

nothing calls this directly

Calls 3

ruleMatchesFunction · 0.85
mustEventFromJSONFunction · 0.85
RunMethod · 0.80

Tested by

no test coverage detected