import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_question_card.dart'; import 'package:pipelines_app/features/chat/conversation/chat_question_card_model.dart'; import 'package:pipelines_app/features/chat/conversation/chat_question_validation.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'chat_thread_fixtures.dart'; Widget materialHost(Widget child) => MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: Scaffold(body: child), ); Widget scrollHost(Widget child) => materialHost(SingleChildScrollView(child: child)); Widget cupertinoHost(Widget child) => CupertinoApp( theme: const CupertinoThemeData(brightness: Brightness.dark), home: CupertinoPageScaffold( child: SingleChildScrollView( padding: const EdgeInsets.all(16), child: child, ), ), ); List fields() => [ { 'key': 'colour', 'title': 'Colour', 'type': 'string', 'required': true, 'options': [ {'value': 'red', 'label': 'Red'}, {'value': 'blue', 'label': 'Blue'}, ], }, { 'key': 'extras', 'title': 'Extras', 'type': 'multiselect', 'options': [ {'value': 'extra-a', 'label': 'Extra A'}, {'value': 'extra-b', 'label': 'Extra B'}, ], }, { 'key': 'note', 'title': 'Notes', 'type': 'string', 'required': true, 'min_length': 3, }, ]; JsonMap question({ bool resolved = false, bool cancelled = false, Map? answers, }) => { 'id': 'form-1', 'session_id': 's', 'kind': 'question', 'title': 'A demo question', 'fields': fields(), 'actions': ['accept', 'decline', 'cancel'], if (resolved) 'resolved': true, if (cancelled) 'cancelled': true, 'answers': ?answers, }; void main() { test('field visibility keeps false and zero as answers', () { final fields = [ {'key': 'enabled', 'type': 'boolean'}, { 'key': 'count', 'type': 'integer', 'when': [ {'key': 'enabled', 'op': 'eq', 'value': true}, ], }, ]; final hidden = {'enabled': false, 'count': 0}; expect(chatFormFieldVisible(fields[1], hidden), isFalse); expect(chatFormAnswerError(fields, hidden), ''); expect(chatVisibleFormAnswers(fields, hidden), {'enabled': false}); }); test('required free text and numeric bounds use the web wording', () { final fields = [ {'key': 'note', 'title': 'Notes', 'type': 'string', 'required': true}, {'key': 'count', 'type': 'integer', 'minimum': 1, 'maximum': 5}, ]; expect( chatFormAnswerError(fields, {'count': 2}), 'Answer Notes before sending.', ); expect( chatFormAnswerError(fields, {'note': 'ok', 'count': 1.5}), 'count needs a valid integer.', ); expect( chatFormAnswerError(fields, {'note': 'ok', 'count': 6}), 'count must be at most 5.', ); expect( chatFormAnswerError(fields, {'note': 'ok', 'count': 0}), 'count must be at least 1.', ); expect(chatFormAnswerError(fields, {'note': 'ok', 'count': 5}), ''); }); testWidgets('a pending question answers single, multi and required free text ' 'exactly as recorded', (tester) async { tester.view.physicalSize = const Size(800, 1200); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); Map? sent; await tester.pumpWidget( scrollHost( ChatQuestionCard( form: question(), onSubmit: (answers, action) async { sent = answers; }, ), ), ); expect(find.text('A demo question'), findsOneWidget); expect(find.text(chatQuestionWaiting), findsOneWidget); await tester.tap(find.text('Red')); await tester.pump(); await tester.tap(find.text('Extra A')); await tester.pump(); await tester.tap(find.text('Send answer')); await tester.pump(); expect(find.text('Answer Notes before sending.'), findsOneWidget); expect(sent, isNull); await tester.enterText( find.byKey(const ValueKey('chat-question-input-note')), ' ready ', ); await tester.pump(); await tester.tap(find.text('Send answer')); await tester.pumpAndSettle(); expect(sent, { 'colour': 'red', 'extras': ['extra-a'], 'note': ' ready ', }); }); testWidgets('a single-select option replaces the previous choice and a ' 'multi-select toggles', (tester) async { Map? sent; await tester.pumpWidget( scrollHost( ChatQuestionCard( form: question(), onSubmit: (answers, action) async { sent = answers; }, ), ), ); await tester.tap(find.text('Red')); await tester.pump(); await tester.tap(find.text('Blue')); await tester.pump(); await tester.tap(find.text('Extra A')); await tester.pump(); await tester.tap(find.text('Extra B')); await tester.pump(); await tester.tap(find.text('Extra A')); await tester.pump(); await tester.enterText( find.byKey(const ValueKey('chat-question-input-note')), 'done', ); await tester.tap(find.text('Send answer')); await tester.pumpAndSettle(); expect(sent, { 'colour': 'blue', 'extras': ['extra-b'], 'note': 'done', }); }); testWidgets('answered questions show the recorded answer read-only', ( tester, ) async { await tester.pumpWidget( scrollHost( ChatQuestionCard( form: question( resolved: true, answers: { 'colour': 'red', 'extras': ['extra-b'], 'note': 'Recorded note', }, ), onSubmit: (_, _) async {}, ), ), ); expect(find.text(chatQuestionAnswered), findsOneWidget); expect(find.text('Red'), findsOneWidget); expect(find.text('Extra B'), findsOneWidget); expect(find.text('Recorded note'), findsOneWidget); expect(find.text('Send answer'), findsNothing); }); testWidgets('an expired question keeps recorded answers and offers no ' 'controls', (tester) async { await tester.pumpWidget( scrollHost( ChatQuestionCard( form: question(cancelled: true, answers: {'colour': 'red'}), onSubmit: (_, _) async {}, ), ), ); expect(find.text(chatQuestionExpired), findsOneWidget); expect(find.text('Red'), findsOneWidget); expect(find.text('Send answer'), findsNothing); }); testWidgets('Cupertino field labels stay subordinate to the card title', ( tester, ) async { await tester.pumpWidget( cupertinoHost( ChatQuestionCard(form: question(), onSubmit: (_, _) async {}), ), ); final title = tester.widget(find.text('A demo question')); final label = tester.widget(find.text('Colour')); expect(label.style!.fontSize, lessThan(title.style!.fontSize!)); }); testWidgets('a free-text input carries its visual label in semantics', ( tester, ) async { final semantics = tester.ensureSemantics(); await tester.pumpWidget( scrollHost( ChatQuestionCard(form: question(), onSubmit: (_, _) async {}), ), ); final node = tester.getSemantics( find.byKey(const ValueKey('chat-question-input-note')), ); expect(node.label, contains('Notes')); semantics.dispose(); }); testWidgets('integer input sends an int, never a decimal double', ( tester, ) async { Map? sent; await tester.pumpWidget( scrollHost( ChatQuestionCard( form: { 'id': 'form-int', 'title': 'How many?', 'actions': ['accept'], 'fields': [ {'key': 'count', 'title': 'Count', 'type': 'integer'}, ], }, onSubmit: (answers, action) async { sent = answers; }, ), ), ); await tester.enterText( find.byKey(const ValueKey('chat-question-input-count')), '1.0', ); await tester.pump(); await tester.tap(find.text('Send answer')); await tester.pumpAndSettle(); expect(sent, {'count': 1}); expect(sent!['count'], isA()); }); testWidgets('decline and cancel send their recorded actions with no answers', ( tester, ) async { final actions = []; final answers = >[]; await tester.pumpWidget( scrollHost( ChatQuestionCard( form: question(), onSubmit: (sent, action) async { actions.add(action); answers.add(sent); }, ), ), ); await tester.tap(find.text('Decline')); await tester.pumpAndSettle(); await tester.tap(find.text('Cancel question')); await tester.pumpAndSettle(); expect(actions, ['decline', 'cancel']); expect(answers, [{}, {}]); }); testWidgets('an all-external form confirms completion without field answers', ( tester, ) async { Map? sent; String? action; await tester.pumpWidget( scrollHost( ChatQuestionCard( form: { 'id': 'form-external', 'title': 'Open the form', 'actions': ['accept'], 'fields': [ { 'key': 'form', 'title': 'External form', 'type': 'external', 'url': 'https://example.com/complete', }, ], }, onSubmit: (answers, next) async { sent = answers; action = next; }, ), ), ); expect(find.text('Confirm completion'), findsOneWidget); expect(find.text('https://example.com/complete'), findsOneWidget); await tester.tap(find.text('Confirm completion')); await tester.pumpAndSettle(); expect(sent, isEmpty); expect(action, 'accept'); }); testWidgets('an unsupported external URL shows the web alert wording', ( tester, ) async { await tester.pumpWidget( scrollHost( ChatQuestionCard( form: { 'id': 'form-external', 'title': 'Open the form', 'actions': ['accept'], 'fields': [ {'key': 'form', 'type': 'external', 'url': 'http://'}, ], }, onSubmit: (_, _) async {}, ), ), ); expect( find.text('This form has an unsupported external URL.'), findsOneWidget, ); }); testWidgets('a resolved question without answers says none was recorded', ( tester, ) async { await tester.pumpWidget( scrollHost( ChatQuestionCard( form: question(resolved: true, answers: const {}), onSubmit: (_, _) async {}, ), ), ); expect(find.text('No answer was recorded.'), findsOneWidget); }); testWidgets('recorded booleans use Yes/No and blank titles fall back to the ' 'key', (tester) async { await tester.pumpWidget( scrollHost( ChatQuestionCard( form: { 'id': 'form-recorded', 'title': 'Recorded', 'resolved': true, 'answers': {'enabled': true, 'extra': 'kept'}, 'fields': [ {'key': 'enabled', 'title': '', 'description': '', 'type': 'boolean'}, ], }, onSubmit: (_, _) async {}, ), ), ); expect(find.text('enabled'), findsOneWidget); expect(find.text('Yes'), findsOneWidget); expect(find.text('true'), findsNothing); expect(find.text('extra'), findsOneWidget); expect(find.text('kept'), findsOneWidget); }); testWidgets('320px at 2x in dark Cupertino keeps native controls', ( tester, ) async { tester.view.physicalSize = const Size(640, 1600); tester.view.devicePixelRatio = 2; tester.platformDispatcher.textScaleFactorTestValue = 2; tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue); await tester.pumpWidget( cupertinoHost( ChatQuestionCard(form: question(), onSubmit: (_, _) async {}), ), ); expect(tester.takeException(), isNull); expect(find.text('A demo question'), findsOneWidget); expect(find.text('Send answer'), findsOneWidget); expect(find.byType(Material), findsNothing); }); testWidgets('a pushed form renders and sends one exact chat_answer', ( tester, ) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage(const []); final held = Completer(); gateway.answer = (_) => held.future; await tester.pumpWidget( materialHost( ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}), ), ); await tester.pumpAndSettle(); gateway.emitEvent({'session_id': 's', 'kind': 'form', 'form': question()}); await tester.pumpAndSettle(); expect(find.text('A demo question'), findsOneWidget); await tester.tap(find.text('Red')); await tester.pump(); await tester.enterText( find.byKey(const ValueKey('chat-question-input-note')), 'sent once', ); await tester.tap(find.text('Send answer')); await tester.tap(find.text('Send answer')); await tester.pump(); final answers = gateway.calls.where((call) => call.type == 'chat_answer'); expect(answers, hasLength(1)); expect(answers.single.fields, { 'chat_answer': { 'session_id': 's', 'form_id': 'form-1', 'answers': {'colour': 'red', 'note': 'sent once'}, 'action': 'accept', }, }); held.complete(const {}); await tester.pumpAndSettle(); expect(find.text(chatQuestionAnswered), findsOneWidget); }); testWidgets('a refused question keeps the card open with the server reason', ( tester, ) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage(const []); gateway.answer = (_) async => throw StateError('form already closed'); await tester.pumpWidget( materialHost( ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}), ), ); await tester.pumpAndSettle(); gateway.emitEvent({'session_id': 's', 'kind': 'form', 'form': question()}); await tester.pumpAndSettle(); await tester.tap(find.text('Red')); await tester.pump(); await tester.enterText( find.byKey(const ValueKey('chat-question-input-note')), 'retry me', ); await tester.tap(find.text('Send answer')); await tester.pumpAndSettle(); expect(find.textContaining('form already closed'), findsOneWidget); expect(find.text('Send answer'), findsOneWidget); expect(find.text(chatQuestionWaiting), findsOneWidget); }); testWidgets('unknown transcript parts remain available beside cards', ( tester, ) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage([ chatMessage( 'a1', 'assistant', parts: [ {'type': 'text', 'text': 'Recorded answer with a fallback part.'}, {'type': 'future-content', 'future_value': 'still preserved'}, ], ), ]); await tester.pumpWidget( materialHost( ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}), ), ); await tester.pumpAndSettle(); expect(find.text('Recorded answer with a fallback part.'), findsOneWidget); await tester.tap(find.text('Unrecognised part: future-content')); await tester.pumpAndSettle(); expect(find.textContaining('still preserved'), findsOneWidget); }); }