import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/attention/domain/attention_models.dart'; import 'package:pipelines_app/features/attention/notify/notification_bridge.dart'; import 'package:pipelines_app/features/attention/notify/notification_controller.dart'; import 'package:pipelines_app/features/attention/notify/notification_models.dart'; import 'package:pipelines_app/features/attention/notify/notification_store.dart'; import 'notification_test_support.dart'; void main() { group('a dismissed entry never notifies', () { test('the detector suppresses it and nothing is posted', () async { final attention = attentionController(); final bridge = FakeNotificationBridge(permission: 'granted'); final notify = NotificationController( store: MemoryNotificationStore(), bridge: bridge, ); addTearDown(attention.dispose); addTearDown(notify.dispose); await attention.load(); await notify.load(); attention.seedRows([runSummary(1, 'running')]); await attention.dismiss(attentionEntryFor(runSummary(1, 'failed'))!); attention.observeFrame(runFrame(1, 'failed')); expect(attention.transitions, isEmpty); expect(publish(notify, attention), 0); expect(bridge.shown, isEmpty); }); test( 'the publish guard records but does not show a dismissed key', () async { final bridge = FakeNotificationBridge(permission: 'granted'); final notify = NotificationController( store: MemoryNotificationStore(), bridge: bridge, ); addTearDown(notify.dispose); await notify.load(); final transition = transitionFor(4); expect( notify.publishTransitions( [transition], qualifying: {transition.key}, dismissed: {transition.key}, ), 0, ); expect(notify.notified, contains(transition.key)); expect(bridge.shown, isEmpty); }, ); }); group('batching and the tap seam', () { test('a burst posts a bounded batch in order, then the rest', () async { final bridge = FakeNotificationBridge(permission: 'granted'); final notify = NotificationController( store: MemoryNotificationStore(), bridge: bridge, ); addTearDown(notify.dispose); await notify.load(); final transitions = [for (var id = 1; id <= 7; id++) transitionFor(id)]; final qualifying = {for (final t in transitions) t.key}; expect(notify.publishTransitions(transitions, qualifying: qualifying), 5); expect(bridge.shown.map((m) => m.runId), [1, 2, 3, 4, 5]); expect(notify.publishTransitions(transitions, qualifying: qualifying), 2); expect(bridge.shown.map((m) => m.runId), [1, 2, 3, 4, 5, 6, 7]); }); test('a tap reports the run and never navigates here', () async { final bridge = FakeNotificationBridge(permission: 'granted'); final notify = NotificationController( store: MemoryNotificationStore(), bridge: bridge, ); addTearDown(notify.dispose); await notify.load(); final opened = []; notify.onOpenRun = opened.add; expect( notify.publishTransitions([transitionFor(3)], qualifying: {'3:Failed'}), 1, ); bridge.taps.single!.call(); expect(opened, [3]); }); }); group('the ledger cannot be read into by a caller', () { test('notified is an unmodifiable copy', () async { final bridge = FakeNotificationBridge(permission: 'granted'); final notify = NotificationController( store: MemoryNotificationStore(), bridge: bridge, ); addTearDown(notify.dispose); await notify.load(); expect( notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'}), 1, ); expect(notify.notified, {'1:Failed'}); expect(() => notify.notified.add('9:Failed'), throwsUnsupportedError); expect(notify.notified, {'1:Failed'}); }); }); group('an unavailable store cannot reach the UI', () { test('a failed read and write still notify and never throw', () async { final store = MemoryNotificationStore() ..failReads = true ..failWrites = true; final bridge = FakeNotificationBridge(permission: 'granted'); final notify = NotificationController(store: store, bridge: bridge); addTearDown(notify.dispose); await expectLater(notify.load(), completes); expect(notify.loaded, isTrue); expect(notify.notified, isEmpty); expect( notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'}), 1, ); await settle(); expect(bridge.shown, hasLength(1)); }); test('a throwing platform post is reported, not propagated', () async { final bridge = _ThrowingBridge(); final notify = NotificationController( store: MemoryNotificationStore(), bridge: bridge, ); addTearDown(notify.dispose); await notify.load(); expect( notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'}), 0, ); expect(notify.notified, contains('1:Failed')); }); test('ledger writes land in call order, one at a time', () async { final store = _SlowStore(); final bridge = FakeNotificationBridge(permission: 'granted'); final notify = NotificationController(store: store, bridge: bridge); addTearDown(notify.dispose); await notify.load(); notify.publishTransitions([transitionFor(1)], qualifying: {'1:Failed'}); notify.publishTransitions( [transitionFor(2)], qualifying: {'1:Failed', '2:Failed'}, ); await Future.delayed(const Duration(milliseconds: 40)); expect(store.maxActive, 1, reason: 'the writes must be serialized'); expect(store.stored, contains('"1:Failed"')); expect(store.stored, contains('"2:Failed"')); expect(store.writes.last, store.stored); }); }); group('the platform seam', () { test('the non-web bridge reports unsupported honestly', () async { final bridge = createNotificationBridge(); expect(bridge.environment().supported, isFalse); final notify = NotificationController( store: MemoryNotificationStore(), bridge: bridge, ); addTearDown(notify.dispose); await notify.load(); expect(notify.permission, NotificationPermissionState.unsupported); expect( bridge.show( const NotificationMessage( runId: 1, title: 'A run failed', body: 'release', tag: '1:Failed', ), ), isFalse, ); }); }); } /// A store whose writes take a tick, so a racing implementation would overlap /// them and the last value to land would be the older snapshot. class _SlowStore implements NotificationStore { String? stored; int active = 0; int maxActive = 0; final List writes = []; @override Future read() async => stored; @override Future write(String value) async { active++; if (active > maxActive) maxActive = active; await Future.delayed(const Duration(milliseconds: 2)); stored = value; writes.add(value); active--; return true; } } class _ThrowingBridge implements NotificationBridge { @override NotificationEnvironment environment() => const NotificationEnvironment( supported: true, secureContext: true, permission: 'granted', ); @override Future requestPermission() async => 'granted'; @override bool show(NotificationMessage message, {void Function()? onTap}) => throw StateError('the browser refused to construct a notification'); }