-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathNotificationCategorizerTests.cs
More file actions
406 lines (362 loc) · 14.1 KB
/
NotificationCategorizerTests.cs
File metadata and controls
406 lines (362 loc) · 14.1 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System.Collections.Generic;
using Xunit;
using OpenClaw.Shared;
namespace OpenClaw.Shared.Tests;
public class NotificationCategorizerTests
{
private readonly NotificationCategorizer _categorizer = new();
// --- Keyword fallback (backward compatibility) ---
[Theory]
[InlineData("Your blood sugar is high", "health")]
[InlineData("Glucose level: 180 mg/dl", "health")]
[InlineData("CGM reading available", "health")]
[InlineData("URGENT: Action required", "urgent")]
[InlineData("This is critical", "urgent")]
[InlineData("Emergency situation", "urgent")]
[InlineData("Reminder: Meeting at 3pm", "reminder")]
[InlineData("Item is in stock", "stock")]
[InlineData("Available now!", "stock")]
[InlineData("New email in inbox", "email")]
[InlineData("Gmail notification", "email")]
[InlineData("Meeting starting soon", "calendar")]
[InlineData("Calendar event: Team standup", "calendar")]
[InlineData("Build failed", "error")]
[InlineData("Exception occurred", "error")]
[InlineData("Build succeeded", "build")]
[InlineData("CI pipeline completed", "build")]
[InlineData("Deploy finished", "build")]
[InlineData("Hello world", "info")]
public void KeywordFallback_BackwardCompatible(string message, string expectedType)
{
var notification = new OpenClawNotification { Message = message };
var (_, type) = _categorizer.Classify(notification);
Assert.Equal(expectedType, type);
}
[Fact]
public void KeywordFallback_IsCaseInsensitive()
{
var notification = new OpenClawNotification { Message = "URGENT: test" };
Assert.Equal("urgent", _categorizer.Classify(notification).type);
notification = new OpenClawNotification { Message = "urgent: test" };
Assert.Equal("urgent", _categorizer.Classify(notification).type);
}
// --- Structured metadata takes priority ---
[Fact]
public void Intent_TakesPriority_OverKeywords()
{
// Message says "email" but intent says "build"
var notification = new OpenClawNotification
{
Message = "New email notification",
Intent = "build"
};
var (_, type) = _categorizer.Classify(notification);
Assert.Equal("build", type);
}
[Theory]
[InlineData("health", "health")]
[InlineData("urgent", "urgent")]
[InlineData("alert", "urgent")]
[InlineData("reminder", "reminder")]
[InlineData("email", "email")]
[InlineData("calendar", "calendar")]
[InlineData("build", "build")]
[InlineData("stock", "stock")]
[InlineData("error", "error")]
public void Intent_MapsCorrectly(string intent, string expectedType)
{
var notification = new OpenClawNotification { Message = "test", Intent = intent };
Assert.Equal(expectedType, _categorizer.Classify(notification).type);
}
[Fact]
public void Channel_TakesPriority_OverKeywords()
{
// Message says "email" but channel is "calendar"
var notification = new OpenClawNotification
{
Message = "Check your email",
Channel = "calendar"
};
Assert.Equal("calendar", _categorizer.Classify(notification).type);
}
[Theory]
[InlineData("calendar", "calendar")]
[InlineData("email", "email")]
[InlineData("ci", "build")]
[InlineData("build", "build")]
[InlineData("stock", "stock")]
[InlineData("inventory", "stock")]
[InlineData("health", "health")]
[InlineData("alerts", "urgent")]
public void Channel_MapsCorrectly(string channel, string expectedType)
{
var notification = new OpenClawNotification { Message = "test", Channel = channel };
Assert.Equal(expectedType, _categorizer.Classify(notification).type);
}
[Fact]
public void Intent_TakesPriority_OverChannel()
{
var notification = new OpenClawNotification
{
Message = "test",
Channel = "email",
Intent = "build"
};
Assert.Equal("build", _categorizer.Classify(notification).type);
}
[Fact]
public void UnknownChannel_FallsThrough_ToKeywords()
{
var notification = new OpenClawNotification
{
Message = "Your blood sugar is high",
Channel = "unknown-channel"
};
Assert.Equal("health", _categorizer.Classify(notification).type);
}
[Fact]
public void UnknownIntent_FallsThrough_ToChannel()
{
var notification = new OpenClawNotification
{
Message = "test",
Intent = "unknown-intent",
Channel = "email"
};
Assert.Equal("email", _categorizer.Classify(notification).type);
}
// --- User-defined rules ---
[Fact]
public void UserRule_KeywordMatch_Categorizes()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = "invoice", Category = "email", Enabled = true }
};
var notification = new OpenClawNotification { Message = "New invoice received" };
Assert.Equal("email", _categorizer.Classify(notification, rules).type);
}
[Fact]
public void UserRule_RegexMatch_Categorizes()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = @"PR\s*#\d+", IsRegex = true, Category = "build", Enabled = true }
};
var notification = new OpenClawNotification { Message = "PR #42 merged" };
Assert.Equal("build", _categorizer.Classify(notification, rules).type);
}
[Fact]
public void UserRule_DisabledRule_IsSkipped()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = "invoice", Category = "email", Enabled = false }
};
var notification = new OpenClawNotification { Message = "New invoice received" };
// Falls through to keyword "info" since no keyword matches
Assert.Equal("info", _categorizer.Classify(notification, rules).type);
}
[Fact]
public void UserRule_InvalidRegex_IsSkipped()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = "[invalid", IsRegex = true, Category = "build", Enabled = true }
};
var notification = new OpenClawNotification { Message = "Hello world" };
Assert.Equal("info", _categorizer.Classify(notification, rules).type);
}
[Fact]
public void UserRule_TakesPriority_OverKeywords()
{
// Message contains "email" keyword, but user rule maps "inbox" to "stock"
var rules = new List<UserNotificationRule>
{
new() { Pattern = "inbox", Category = "stock", Enabled = true }
};
var notification = new OpenClawNotification { Message = "New items in your inbox" };
Assert.Equal("stock", _categorizer.Classify(notification, rules).type);
}
[Fact]
public void UserRule_StructuredMetadata_TakesPriority_OverUserRules()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = "anything", Category = "stock", Enabled = true }
};
var notification = new OpenClawNotification
{
Message = "anything here",
Intent = "build"
};
Assert.Equal("build", _categorizer.Classify(notification, rules).type);
}
[Fact]
public void UserRule_FirstMatchWins()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = "report", Category = "email", Enabled = true },
new() { Pattern = "report", Category = "build", Enabled = true }
};
var notification = new OpenClawNotification { Message = "Daily report ready" };
Assert.Equal("email", _categorizer.Classify(notification, rules).type);
}
[Fact]
public void UserRule_MatchesAgainstTitleAndMessage()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = "special-title", Category = "urgent", Enabled = true }
};
var notification = new OpenClawNotification
{
Title = "special-title",
Message = "generic message"
};
Assert.Equal("urgent", _categorizer.Classify(notification, rules).type);
}
// --- Pipeline order verification ---
[Fact]
public void PipelineOrder_Intent_Channel_UserRules_Keywords()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = "test", Category = "stock", Enabled = true }
};
// All layers match — intent wins
var notification = new OpenClawNotification
{
Message = "test email urgent blood sugar",
Intent = "calendar",
Channel = "email"
};
Assert.Equal("calendar", _categorizer.Classify(notification, rules).type);
// Remove intent — channel wins
notification.Intent = null;
Assert.Equal("email", _categorizer.Classify(notification, rules).type);
// Remove channel — user rule wins
notification.Channel = null;
Assert.Equal("stock", _categorizer.Classify(notification, rules).type);
// Remove user rules — keyword wins
Assert.Equal("health", _categorizer.Classify(notification).type);
}
// --- PreferStructuredCategories = false ---
[Fact]
public void PreferStructuredCategories_False_SkipsIntent()
{
// Intent says "build" but with preferStructuredCategories=false it should be ignored;
// message keyword "email" drives the result.
var notification = new OpenClawNotification
{
Message = "New email notification",
Intent = "build"
};
var (_, type) = _categorizer.Classify(notification, preferStructuredCategories: false);
Assert.Equal("email", type);
}
[Fact]
public void PreferStructuredCategories_False_SkipsChannel()
{
// Channel says "calendar" but with preferStructuredCategories=false it should be ignored;
// message keyword "email" drives the result.
var notification = new OpenClawNotification
{
Message = "Check your email",
Channel = "calendar"
};
var (_, type) = _categorizer.Classify(notification, preferStructuredCategories: false);
Assert.Equal("email", type);
}
[Fact]
public void PreferStructuredCategories_False_UserRulesStillApply()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = "invoice", Category = "email", Enabled = true }
};
// Intent would win when preferStructuredCategories=true, but is skipped here;
// user rule matches the message.
var notification = new OpenClawNotification
{
Message = "New invoice received",
Intent = "urgent"
};
var (_, type) = _categorizer.Classify(notification, rules, preferStructuredCategories: false);
Assert.Equal("email", type);
}
[Fact]
public void PreferStructuredCategories_False_FallsBackToKeywords()
{
// No user rules, no keywords for "hello world" → "info"
var notification = new OpenClawNotification
{
Message = "Hello world",
Intent = "build",
Channel = "email"
};
var (_, type) = _categorizer.Classify(notification, preferStructuredCategories: false);
Assert.Equal("info", type);
}
[Fact]
public void PreferStructuredCategories_True_Default_BehaviourUnchanged()
{
// Ensure the default (true) still gives structured metadata priority.
var notification = new OpenClawNotification
{
Message = "New email notification",
Intent = "build"
};
Assert.Equal("build", _categorizer.Classify(notification).type);
Assert.Equal("build", _categorizer.Classify(notification, preferStructuredCategories: true).type);
}
[Fact]
public void ClassifyByKeywords_DefaultsToInfo()
{
var (title, type) = NotificationCategorizer.ClassifyByKeywords("Hello world");
Assert.Equal("info", type);
Assert.Equal("🤖 OpenClaw", title);
}
[Fact]
public void ClassifyByKeywords_ReturnsCorrectTitles()
{
Assert.Equal("🩸 Blood Sugar Alert", NotificationCategorizer.ClassifyByKeywords("blood sugar high").title);
Assert.Equal("🚨 Urgent Alert", NotificationCategorizer.ClassifyByKeywords("urgent message").title);
Assert.Equal("⏰ Reminder", NotificationCategorizer.ClassifyByKeywords("reminder").title);
Assert.Equal("📦 Stock Alert", NotificationCategorizer.ClassifyByKeywords("in stock").title);
Assert.Equal("📧 Email", NotificationCategorizer.ClassifyByKeywords("email notification").title);
Assert.Equal("📅 Calendar", NotificationCategorizer.ClassifyByKeywords("calendar event").title);
Assert.Equal("⚠️ Error", NotificationCategorizer.ClassifyByKeywords("error occurred").title);
Assert.Equal("🔨 Build", NotificationCategorizer.ClassifyByKeywords("deploy finished").title);
}
// --- Empty/null edge cases ---
[Fact]
public void EmptyMessage_DefaultsToInfo()
{
var notification = new OpenClawNotification { Message = "" };
Assert.Equal("info", _categorizer.Classify(notification).type);
}
[Fact]
public void NullUserRules_FallsToKeywords()
{
var notification = new OpenClawNotification { Message = "urgent alert" };
Assert.Equal("urgent", _categorizer.Classify(notification, null).type);
}
[Fact]
public void EmptyUserRules_FallsToKeywords()
{
var notification = new OpenClawNotification { Message = "urgent alert" };
Assert.Equal("urgent", _categorizer.Classify(notification, new List<UserNotificationRule>()).type);
}
[Fact]
public void EmptyPattern_RuleIsSkipped()
{
var rules = new List<UserNotificationRule>
{
new() { Pattern = "", Category = "urgent", Enabled = true }
};
var notification = new OpenClawNotification { Message = "Hello world" };
Assert.Equal("info", _categorizer.Classify(notification, rules).type);
}
}