import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/attention/notify/notification_ledger.dart'; import 'notification_test_support.dart'; void main() { group('the identity is the run and kind', () { test('two failures of one run are the same announced state', () { final first = transitionFor(7, finishedAt: '2026-10-22T09:10:00Z'); final second = transitionFor(7, finishedAt: '2026-10-22T11:30:00Z'); expect(notificationIdentity(first), '7:Failed'); expect(notificationIdentity(second), '7:Failed'); }); test('a waiting run needs no finish time to have an identity', () { final entry = transitionFor(7, status: 'waiting', finishedAt: ''); expect(notificationIdentity(entry), '7:Decision'); }); test('the kind separates a decision from a failure of one run', () { expect( notificationIdentity(transitionFor(7)), isNot(notificationIdentity(transitionFor(7, status: 'waiting'))), ); }); }); group('the qualifying set is the attention surface\'s own keys', () { test('only failed and human-waiting runs qualify', () { expect( notificationQualifyingKeys([ runSummary(1, 'failed'), runSummary(2, 'waiting'), runSummary(3, 'running'), runSummary(4, 'succeeded'), runSummary(5, 'queued'), runSummary(6, 'waiting', exportPending: 2), ]), {'1:Failed', '2:Decision'}, ); }); test('a duplicate row and a malformed id add nothing', () { expect( notificationQualifyingKeys([ runSummary(1, 'failed'), runSummary(1, 'failed'), {'id': 0, 'status': 'failed'}, {'id': 'not a run', 'status': 'failed'}, ]), {'1:Failed'}, ); expect(notificationQualifyingKeys(null), isEmpty); }); test('the frame key form is read too', () { expect(notificationQualifyingKeys([runFrame(9, 'failed')]), {'9:Failed'}); }); }); group('the ledger records identities', () { test('recording is idempotent and never mutates the original', () { const base = NotificationLedger(); final once = base.recording('7:Failed'); final twice = once.recording('7:Failed'); expect(base.notified, isEmpty); expect(once.hasNotified('7:Failed'), isTrue); expect(twice.notified, {'7:Failed'}); }); test('asked is carried without touching the identities', () { final asked = const NotificationLedger(notified: {'a'}).withAsked(true); expect(asked.asked, isTrue); expect(asked.notified, {'a'}); }); }); group('pruning keeps only the current qualifying run', () { test('a run that left the state is forgotten; asked survives', () { final ledger = const NotificationLedger( notified: {'7:Decision', '8:Failed'}, ).withAsked(true); final pruned = ledger.pruning({'8:Failed'}); expect(pruned.notified, {'8:Failed'}); expect(pruned.asked, isTrue); }); test('nothing to drop returns the same ledger', () { const ledger = NotificationLedger(notified: {'7:Failed'}); expect(identical(ledger.pruning({'7:Failed'}), ledger), isTrue); }); test('a timestamped identity from an older build is dropped', () { final old = decodeNotificationLedger( '{"asked":false,"notified":["7:Decision@2026-10-22T11:00:00.000"]}', ); expect(old.notified, hasLength(1)); expect(old.pruning({'7:Decision'}).notified, isEmpty); }); }); group('persistence is total', () { test('a round trip keeps the asked flag and the identities', () { final ledger = const NotificationLedger( notified: {'b', 'a'}, ).withAsked(true); final decoded = decodeNotificationLedger( encodeNotificationLedger(ledger), ); expect(decoded.asked, isTrue); expect(decoded.notified, {'a', 'b'}); }); test('an unchanged ledger always encodes to the same bytes', () { final first = encodeNotificationLedger( const NotificationLedger(notified: {'b', 'a'}), ); final second = encodeNotificationLedger( const NotificationLedger(notified: {'a', 'b'}), ); expect(first, second); }); test('blank, malformed and wrong-typed values never throw', () { expect(decodeNotificationLedger(null).notified, isEmpty); expect(decodeNotificationLedger('').asked, isFalse); expect(decodeNotificationLedger('not json').notified, isEmpty); expect(decodeNotificationLedger('[]').notified, isEmpty); expect(decodeNotificationLedger('"text"').notified, isEmpty); expect( decodeNotificationLedger( '{"asked":"yes","notified":[1,null,"a",""]}', ).notified, {'a'}, ); expect( decodeNotificationLedger('{"asked":"yes"}').asked, isFalse, reason: 'only a real true counts as asked', ); }); }); }