Files
pipelines/app/test/features/chat/conversation/chat_response_pending_test.dart
T

197 lines
6.4 KiB
Dart

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_draft_store.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_response_pending.dart';
import 'chat_thread_fixtures.dart';
class _Gateway extends FakeChatGateway {
Completer<JsonMap> prompt = Completer();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_prompt') return prompt.future;
return super.call(type, fields);
}
}
Future<void> _pump(WidgetTester tester) async {
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
await tester.pump();
}
Future<void> _host(
WidgetTester tester,
_Gateway gateway, {
bool cupertino = false,
}) async {
gateway.history = (_) async => chatPage([]);
final thread = ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
drafts: MemoryChatDraftStore(),
newOperationId: () => 'test-pending',
);
Widget animations(BuildContext context, Widget? child) => MediaQuery(
data: MediaQuery.of(context).copyWith(disableAnimations: false),
child: child!,
);
await tester.pumpWidget(
cupertino
? CupertinoApp(
builder: animations,
home: CupertinoPageScaffold(child: thread),
)
: MaterialApp(
builder: animations,
home: Scaffold(body: thread),
),
);
await tester.pumpAndSettle();
}
Future<void> _send(WidgetTester tester) async {
await tester.enterText(find.byKey(const Key('chat-composer-field')), 'Hello');
await tester.pump();
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await _pump(tester);
}
void main() {
for (final cupertino in [false, true]) {
testWidgets(
'response bubble exists before frames, through tools, then text '
'($cupertino)',
(tester) async {
final gateway = _Gateway();
addTearDown(gateway.close);
await _host(tester, gateway, cupertino: cupertino);
expect(find.byType(ChatResponsePending), findsNothing);
await _send(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
expect(
find.bySemanticsLabel('Assistant is responding'),
findsOneWidget,
);
expect(find.byType(ChatMessageView), findsNWidgets(2));
final opacities = tester
.widgetList<Opacity>(
find.descendant(
of: find.byType(ChatResponsePending),
matching: find.byType(Opacity),
),
)
.map((widget) => widget.opacity)
.toList();
await tester.pump(const Duration(milliseconds: 200));
expect(
tester
.widgetList<Opacity>(
find.descendant(
of: find.byType(ChatResponsePending),
matching: find.byType(Opacity),
),
)
.map((widget) => widget.opacity)
.toList(),
isNot(opacities),
);
gateway.prompt.complete({});
await _pump(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
gateway.emitEvent(chatStepFrame('running'));
gateway.emitEvent({
'session_id': 's',
'kind': 'reasoning',
'message_id': 'a',
'part': {'id': 'reasoning', 'type': 'reasoning'},
'delta': 'Thinking through this.',
});
await _pump(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
gateway.emitEvent({
'session_id': 's',
'kind': 'tool',
'message_id': 'a',
'part': {
'id': 'tool-1',
'type': 'tool',
'name': 'read',
'state': {'status': 'completed'},
},
});
await _pump(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
expect(find.text('1 tool'), findsOneWidget);
expect(find.byType(ChatMessageView), findsNWidgets(2));
expect(
tester.getTopLeft(find.byType(ChatResponsePending)).dy,
lessThan(tester.getTopLeft(find.text('1 tool')).dy),
);
gateway.emitEvent(chatTextFrame('Here is the answer.'));
await _pump(tester);
expect(find.byType(ChatResponsePending), findsNothing);
expect(find.text('Here is the answer.'), findsOneWidget);
expect(find.text('1 tool'), findsOneWidget);
gateway.emitEvent(chatStepFrame('ended'));
await tester.pumpAndSettle();
await tester.pumpWidget(const SizedBox.shrink());
},
);
}
for (final result in ['refused', 'queued', 'ended']) {
testWidgets(
'$result clears a response shell without leaving fake history',
(tester) async {
final gateway = _Gateway();
addTearDown(gateway.close);
await _host(tester, gateway);
await _send(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
if (result == 'refused') {
gateway.prompt.completeError(StateError('Send refused'));
} else {
gateway.prompt.complete({'chat_queued': result == 'queued'});
if (result == 'ended') gateway.emitEvent(chatStepFrame('ended'));
}
await tester.pumpAndSettle();
expect(find.byType(ChatResponsePending), findsNothing);
expect(
tester
.widgetList<ChatMessageView>(find.byType(ChatMessageView))
.where((view) => view.row['kind'] == 'assistant'),
isEmpty,
);
},
);
}
testWidgets('reduced motion keeps a static accessible ellipsis', (
tester,
) async {
await tester.pumpWidget(
const MaterialApp(
home: MediaQuery(
data: MediaQueryData(disableAnimations: true),
child: ChatResponsePending(),
),
),
);
await tester.pumpAndSettle();
expect(find.text('.'), findsNWidgets(3));
expect(find.bySemanticsLabel('Assistant is responding'), findsOneWidget);
expect(tester.binding.hasScheduledFrame, isFalse);
});
}