import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/application/app_route.dart'; import 'package:pipelines_app/features/attention/notify/chat_question_notices.dart'; import 'notification_test_support.dart'; class _Gateway implements ApiGateway { final controller = StreamController.broadcast(); @override Stream get frames => controller.stream; @override Future call(String type, [JsonMap fields = const {}]) async => {}; } Map _event({ String id = 'form-1', String session = 'session_1', String kind = 'question', bool resolved = false, bool cancelled = false, }) => { 'type': 'chat_event', 'chat_event': { 'session_id': session, 'kind': 'form', 'form': { 'id': id, 'kind': kind, 'title': 'Choose an option', 'resolved': resolved, 'cancelled': cancelled, }, }, }; void main() { late _Gateway gateway; late FakeNotificationBridge bridge; late bool allowed; late String? active; late List opened; late ChatQuestionNotices notices; setUp(() { gateway = _Gateway(); bridge = FakeNotificationBridge(permission: 'granted'); allowed = true; active = null; opened = []; notices = ChatQuestionNotices( gateway: gateway, canNotify: () => allowed, activeChatSessionId: () => active, sessionTitle: (id) async => id == 'session_1' ? ' Design chat ' : null, openChat: (id) => opened.add(AppRoute.chatSession(id)), bridge: bridge, ); }); tearDown(() async { notices.dispose(); await gateway.controller.close(); }); test('notifies titled and unknown sessions; tap opens the chat route', () async { gateway.controller.add(_event()); await Future.delayed(Duration.zero); gateway.controller.add(_event(id: 'form-2', session: 'unknown')); await Future.delayed(Duration.zero); expect(bridge.shown.map((message) => message.title), [ 'Design chat needs your answer', 'Chat needs your answer', ]); expect(bridge.shown.first.body, 'Choose an option'); expect(bridge.shown.first.tag, 'chat-question:form-1'); bridge.taps.first?.call(); expect(opened, [AppRoute.chatSession('session_1')]); }); test('suppresses active, unavailable, resolved, cancelled and non-question frames', () async { active = 'session_1'; gateway.controller.add(_event()); await Future.delayed(Duration.zero); active = null; allowed = false; gateway.controller.add(_event(id: 'form-2')); await Future.delayed(Duration.zero); allowed = true; gateway.controller.add(_event(id: 'form-3', resolved: true)); await Future.delayed(Duration.zero); gateway.controller.add(_event(id: 'form-4', cancelled: true)); await Future.delayed(Duration.zero); gateway.controller.add(_event(id: 'form-5', kind: 'approval')); await Future.delayed(Duration.zero); expect(bridge.shown, isEmpty); }); }