From cc12e2eb963988b4bd3916cde9cc9f2902e26f2a Mon Sep 17 00:00:00 2001 From: ericz Date: Sun, 8 Nov 2026 14:30:20 +0100 Subject: [PATCH] Make spam example test deterministic --- .../message_filter/message_filter_service.dart | 8 ++++++++ test/connector/message_filter_ingest_test.dart | 13 ++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/services/message_filter/message_filter_service.dart b/lib/services/message_filter/message_filter_service.dart index 5f679c7d..42891bf2 100644 --- a/lib/services/message_filter/message_filter_service.dart +++ b/lib/services/message_filter/message_filter_service.dart @@ -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 load() async { try { diff --git a/test/connector/message_filter_ingest_test.dart b/test/connector/message_filter_ingest_test.dart index a9a36fbf..da9c03bf 100644 --- a/test/connector/message_filter_ingest_test.dart +++ b/test/connector/message_filter_ingest_test.dart @@ -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 {