Files
pipelines/app/test/features/attention/notify/chat_question_notices_test.dart
T

102 lines
3.0 KiB
Dart

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<JsonMap>.broadcast();
@override
Stream<JsonMap> get frames => controller.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async => {};
}
Map<String, dynamic> _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<AppRoute> 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<void>.delayed(Duration.zero);
gateway.controller.add(_event(id: 'form-2', session: 'unknown'));
await Future<void>.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<void>.delayed(Duration.zero);
active = null;
allowed = false;
gateway.controller.add(_event(id: 'form-2'));
await Future<void>.delayed(Duration.zero);
allowed = true;
gateway.controller.add(_event(id: 'form-3', resolved: true));
await Future<void>.delayed(Duration.zero);
gateway.controller.add(_event(id: 'form-4', cancelled: true));
await Future<void>.delayed(Duration.zero);
gateway.controller.add(_event(id: 'form-5', kind: 'approval'));
await Future<void>.delayed(Duration.zero);
expect(bridge.shown, isEmpty);
});
}