Make spam example test deterministic

This commit is contained in:
ericz committed 2026-11-08 14:30:20 +01:00
1 parent a020eb30d0
commit cc12e2eb96
2 files changed
+16 -5

No files matched your search

@@ -107,6 +107,14 @@ class MessageFilterService extends ChangeNotifier {
@visibleForTesting
int get recentCleanCount => _recentClean.length;
@visibleForTesting
bool hasRecentCleanExample(String text) {
final key = MessageFilterText.normalize(text);
return _recentClean.any(
(example) => MessageFilterText.normalize(example.text) == key,
);
}
/// Loads persisted state and trains the classifier.
Future<void> load() async {
try {
@@ -232,7 +232,7 @@ void main() {
await _connect();
// Nobody called "Ghost" has posted, so the reply keeps its mention.
const text = '@[Ghost] cheap radios for sale, message me today';
final cleanBefore = _filter.recentCleanCount;
const filterText = 'cheap radios for sale, message me today';
_connector.handleFrameForTest(
ReviewFrames.channelMessage(
channelIndex: 2,
@@ -241,16 +241,19 @@ void main() {
timestampSecs: _nowSecs(),
),
);
await _waitFor(() => _find(2, text) != null);
await _waitFor(
() => _find(2, text) != null && _filter.hasRecentCleanExample(filterText),
);
final message = _find(2, text)!;
expect(message.text, startsWith('@[Ghost]'));
expect(_filter.recentCleanCount, cleanBefore + 1);
final spamBefore = _filter.spamExampleCount;
await _connector.markChannelMessageSpam(_channelAt(2), message);
expect(_filter.spamExampleCount, spamBefore + 1);
// The clean example recorded on arrival is gone, not contradicted.
expect(_filter.recentCleanCount, cleanBefore);
// The clean example recorded on arrival is gone, not contradicted. Check
// this example directly: other queued messages can teach the filter while
// this async test is awaiting the retrain.
expect(_filter.hasRecentCleanExample(filterText), isFalse);
});
test('spam actions only touch the message in the chosen channel', () async {