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 @visibleForTesting
int get recentCleanCount => _recentClean.length; 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. /// Loads persisted state and trains the classifier.
Future<void> load() async { Future<void> load() async {
try { try {
@@ -232,7 +232,7 @@ void main() {
await _connect(); await _connect();
// Nobody called "Ghost" has posted, so the reply keeps its mention. // Nobody called "Ghost" has posted, so the reply keeps its mention.
const text = '@[Ghost] cheap radios for sale, message me today'; 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( _connector.handleFrameForTest(
ReviewFrames.channelMessage( ReviewFrames.channelMessage(
channelIndex: 2, channelIndex: 2,
@@ -241,16 +241,19 @@ void main() {
timestampSecs: _nowSecs(), timestampSecs: _nowSecs(),
), ),
); );
await _waitFor(() => _find(2, text) != null); await _waitFor(
() => _find(2, text) != null && _filter.hasRecentCleanExample(filterText),
);
final message = _find(2, text)!; final message = _find(2, text)!;
expect(message.text, startsWith('@[Ghost]')); expect(message.text, startsWith('@[Ghost]'));
expect(_filter.recentCleanCount, cleanBefore + 1);
final spamBefore = _filter.spamExampleCount; final spamBefore = _filter.spamExampleCount;
await _connector.markChannelMessageSpam(_channelAt(2), message); await _connector.markChannelMessageSpam(_channelAt(2), message);
expect(_filter.spamExampleCount, spamBefore + 1); expect(_filter.spamExampleCount, spamBefore + 1);
// The clean example recorded on arrival is gone, not contradicted. // The clean example recorded on arrival is gone, not contradicted. Check
expect(_filter.recentCleanCount, cleanBefore); // 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 { test('spam actions only touch the message in the chosen channel', () async {