Files
pipelines/app/test/features/chat/conversation/chat_response_pending_test.dart
T
alan fbe1928f59 Remove Cupertino chat transcript branches
Keep the existing Material transcript path while preserving public Cupertino parameters for callers. Narrow transcript tests to the Material-only behavior and remove Cupertino-only fixtures.
2026-11-11 11:46:13 +00:00

222 lines
7.1 KiB
Dart

import 'dart:async';
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) 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(
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() {
testWidgets(
'response bubble exists before frames, through tools, then text',
(tester) async {
final gateway = _Gateway();
addTearDown(gateway.close);
await _host(tester, gateway);
expect(find.byType(ChatResponsePending), findsNothing);
await _send(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
expect(
find.bySemanticsLabel(RegExp(r'Thinking.*expand for details')),
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.textContaining('Thinking'), findsOneWidget);
expect(find.byType(ChatMessageView), findsNWidgets(2));
expect(
find.descendant(
of: find.byKey(const Key('chat-turn-activity')),
matching: find.byType(ChatResponsePending),
),
findsOneWidget,
);
gateway.emitEvent(chatTextFrame('Here is the answer.'));
await _pump(tester);
expect(find.byType(ChatResponsePending), findsOneWidget);
expect(find.text('Here is the answer.'), findsOneWidget);
expect(find.byKey(const Key('chat-turn-activity')), findsOneWidget);
gateway.emitEvent(chatStepFrame('ended'));
await tester.pumpAndSettle();
expect(find.byType(ChatResponsePending), findsNothing);
expect(find.text('Used 1 tool'), findsOneWidget);
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();
// Three steady dots, all fully drawn, with no animation scheduled.
final opacities = tester
.widgetList<Opacity>(
find.descendant(
of: find.byType(ChatResponsePending),
matching: find.byType(Opacity),
),
)
.map((widget) => widget.opacity);
expect(opacities, [1, 1, 1]);
expect(find.bySemanticsLabel('Assistant is responding'), findsOneWidget);
expect(tester.binding.hasScheduledFrame, isFalse);
});
testWidgets('the pending dots sit on the activity text\'s middle ', (
tester,
) async {
final gateway = _Gateway();
addTearDown(gateway.close);
await _host(tester, gateway);
await _send(tester);
final dots = tester.getRect(
find.byKey(const Key('chat-response-pending-dots')),
);
final label = tester.getRect(find.textContaining('Thinking'));
expect(dots.center.dy, closeTo(label.center.dy, 1));
expect(dots.height, lessThanOrEqualTo(label.height));
// Round dots, not baseline glyphs.
expect(
find.descendant(
of: find.byType(ChatResponsePending),
matching: find.text('.'),
),
findsNothing,
);
});
}