import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/attention/notify/agent_notice.dart'; import 'package:pipelines_app/features/attention/notify/agent_notices.dart'; import 'package:pipelines_app/features/attention/notify/notification_controller.dart'; import 'package:pipelines_app/features/attention/notify/notification_ledger.dart'; import 'package:pipelines_app/features/attention/notify/notification_store.dart'; import 'package:pipelines_app/features/attention/notify/page_signals.dart'; import 'notification_test_support.dart'; final _now = DateTime.utc(2026, 12, 2, 12); class _Gateway implements ApiGateway { final controller = StreamController.broadcast(); List backlog = []; final calls = []; @override Stream get frames => controller.stream; @override Future call(String type, [JsonMap fields = const {}]) async { calls.add(type); return { 'type': 'notifications_subscribed', 'notifications': backlog, 'ids': [], }; } } class _Page implements PageSignals { PagePresence current = PagePresence.focused; final badges = []; int chimes = 0; final focus = StreamController.broadcast(); @override PagePresence get presence => current; @override Stream get focused => focus.stream; @override void setBadge(int count) => badges.add(count); @override void chime() => chimes++; @override void dispose() {} } JsonMap _notice( String id, { String session = 'ses_1', String kind = 'chat_notify', int priority = 3, Duration age = Duration.zero, }) => { 'id': 'chat:$session:notify:$id', 'kind': kind, 'title': 'Build done', 'message': 'All tests pass', 'click': 'pipelines://chat/$session', 'priority': priority, 'tags': [], 'created_at': _now.subtract(age).toIso8601String(), 'group': 'chat:$session', }; JsonMap _push(JsonMap notice) => { 'type': 'notification', 'notification': notice, }; void main() { late _Gateway gateway; late _Page page; late FakeNotificationBridge bridge; late MemoryNotificationStore store; late NotificationController notifications; late AgentNotices notices; late List toasts; late List opened; late String? active; AgentNotices build() => AgentNotices( gateway: gateway, notifications: notifications, page: page, activeChatSessionId: () => active, sessionTitle: (id) async => id == 'ses_1' ? 'Release chat' : null, openChat: opened.add, showToast: toasts.add, now: () => _now, ); Future setUpWith({ String permission = 'granted', bool secure = true, }) async { gateway = _Gateway(); page = _Page(); bridge = FakeNotificationBridge( permission: permission, secureContext: secure, ); store = MemoryNotificationStore(); notifications = NotificationController(store: store, bridge: bridge); await notifications.load(); toasts = []; opened = []; active = null; notices = build(); } Future pump() async { for (var i = 0; i < 10; i++) { await Future.delayed(Duration.zero); } } tearDown(() async { notices.dispose(); notifications.dispose(); await gateway.controller.close(); }); test('a focused page shows a toast that opens the chat', () async { await setUpWith(); gateway.controller.add(_push(_notice('a'))); await pump(); expect(bridge.shown, isEmpty); expect(toasts.single.headline, 'Release chat: Build done'); expect(toasts.single.message, 'All tests pass'); expect(page.badges, isEmpty); toasts.single.open(); expect(opened, ['ses_1']); }); test('nothing shows when the chat is already in view', () async { await setUpWith(); active = 'ses_1'; gateway.controller.add(_push(_notice('a'))); await pump(); expect(toasts, isEmpty); expect(bridge.shown, isEmpty); expect( notifications.claimAgentNotice('chat:ses_1:notify:a', _now), isFalse, ); }); test('an unfocused page posts a system notification and badges the title; ' 'focus clears the badge', () async { await setUpWith(); page.current = PagePresence.hidden; active = 'ses_1'; // In view but not looked at: still notified. gateway.controller.add(_push(_notice('a'))); gateway.controller.add(_push(_notice('b', session: 'ses_2'))); await pump(); expect(bridge.shown.map((m) => m.title), [ 'Release chat: Build done', 'Chat: Build done', ]); expect(bridge.shown.first.tag, 'chat:ses_1:notify:a'); expect(bridge.shown.first.body, 'All tests pass'); expect(toasts, isEmpty); expect(page.badges, [1, 2]); expect(notices.badge, 2); expect(page.chimes, 0); bridge.taps.first!(); expect(opened, ['ses_1']); page.current = PagePresence.focused; page.focus.add(null); await pump(); expect(page.badges.last, 0); expect(notices.badge, 0); }); test('a low priority notice waits as a toast on a visible page and posts ' 'only when the page is hidden', () async { await setUpWith(); page.current = PagePresence.visible; gateway.controller.add(_push(_notice('low', priority: 2))); await pump(); expect(bridge.shown, isEmpty); expect(toasts, hasLength(1)); expect(page.badges, [1]); expect(page.chimes, 0); page.current = PagePresence.hidden; gateway.controller.add(_push(_notice('low2', priority: 2))); await pump(); expect(bridge.shown, hasLength(1)); }); test( 'an insecure page falls back to a toast, a title count and a chime', () async { await setUpWith(permission: 'default', secure: false); page.current = PagePresence.hidden; gateway.controller.add(_push(_notice('a', priority: 4))); await pump(); expect(notifications.canNotify, isFalse); expect(bridge.shown, isEmpty); expect(toasts.single.headline, 'Release chat: Build done'); expect(page.badges, [1]); expect(page.chimes, 1); }, ); test('a refused system notification falls back to the toast', () async { await setUpWith(); bridge.accepts = false; page.current = PagePresence.hidden; gateway.controller.add(_push(_notice('a'))); await pump(); expect(bridge.shown, hasLength(1)); expect(toasts, hasLength(1)); }); test('other notification kinds are left to their own surfaces', () async { await setUpWith(); gateway.controller.add(_push(_notice('r', kind: 'chat_reply'))); gateway.controller.add({'type': 'chat_event', 'chat_event': {}}); await pump(); expect(toasts, isEmpty); expect(bridge.shown, isEmpty); }); test( 'a repeated push and a replay of a shown notice are deduplicated', () async { await setUpWith(); gateway.controller.add(_push(_notice('a'))); gateway.controller.add(_push(_notice('a'))); await pump(); gateway.backlog = [_notice('a')]; await notices.connected(); await pump(); expect(toasts, hasLength(1)); }, ); test( 'a reconnect replays recent missed notices once, across a reload', () async { await setUpWith(); gateway.backlog = [ _notice('old', age: const Duration(hours: 2)), _notice('recent', age: const Duration(minutes: 20)), _notice('reply', kind: 'chat_reply'), ]; await notices.connected(); await pump(); expect(gateway.calls, ['subscribe_notifications']); expect(toasts.map((t) => t.headline), ['Release chat: Build done']); // The ledger persisted both chat_notify ids, so a reload shows neither. final stored = decodeNotificationLedger(store.stored); expect(stored.agentShown.keys, { 'chat:ses_1:notify:old', 'chat:ses_1:notify:recent', }); notices.dispose(); notifications.dispose(); notifications = NotificationController( store: MemoryNotificationStore(stored: store.stored), bridge: bridge, ); await notifications.load(); toasts.clear(); notices = build(); await notices.connected(); await pump(); expect(toasts, isEmpty); }, ); test('with a send-only path the subscribe reply is read from the frames, ' 'so it never holds the request lane', () async { await setUpWith(); notices.dispose(); final sent = []; notices = AgentNotices( gateway: gateway, notifications: notifications, page: page, activeChatSessionId: () => active, sessionTitle: (id) async => 'Release chat', openChat: opened.add, showToast: toasts.add, sendOnly: (type, fields) { sent.add(type); return 'flutter-1'; }, now: () => _now, ); await notices.connected(); expect(sent, ['subscribe_notifications']); expect(gateway.calls, isEmpty); gateway.controller.add({ 'type': 'notifications_subscribed', 'id': 'flutter-1', 'notifications': [_notice('missed', age: const Duration(minutes: 5))], }); await pump(); expect(toasts.single.headline, 'Release chat: Build done'); }); test('a replay shows at most the newest few and never chimes', () async { await setUpWith(permission: 'default', secure: false); page.current = PagePresence.hidden; gateway.backlog = [ for (var i = 0; i < 8; i++) _notice('n$i', age: Duration(minutes: 30 - i)), ]; await notices.connected(); await pump(); expect(toasts, hasLength(agentNoticeReplayLimit)); expect(page.chimes, 0); expect(page.badges.last, agentNoticeReplayLimit); }); }