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

223 lines
8.3 KiB
Dart

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_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
import 'chat_thread_fixtures.dart';
// Matches the normalized OpenCode V2 protocol: text and reasoning each start
// at ordinal zero but have distinct stable IDs (ChatDeltaPartID in Go).
JsonMap _delta(String kind, String text) => {
'session_id': 's',
'message_id': 'a',
'kind': kind,
'part': {'id': 'a#$kind#0', 'type': kind},
'delta': text,
};
Widget _host(Widget child, bool cupertino) => cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: child))
: MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(body: child),
);
/// The streaming activity control animates, so pumpAndSettle would never
/// return; this lets its dialog finish its
/// entrance or exit instead.
Future<void> _settle(WidgetTester tester) async {
for (var frame = 0; frame < 8; frame++) {
await tester.pump(const Duration(milliseconds: 100));
}
}
final _activity = find.byKey(const Key('chat-turn-activity'));
final _dialog = find.text('Activity details');
Finder _inDialog(String text) => find.text(text);
Future<void> _dismiss(WidgetTester tester) async {
await tester.tapAt(const Offset(10, 10));
await _settle(tester);
expect(_dialog, findsNothing);
}
void main() {
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets(
'reasoning arriving after text keeps one streaming activity control ($host)',
(tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage(const []);
await tester.pumpWidget(
_host(
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
cupertino,
),
);
await tester.pumpAndSettle();
gateway.emitEvent(_delta('text', 'Answer first'));
await tester.pumpAndSettle();
expect(find.text('Reasoning'), findsNothing);
gateway.emitEvent(_delta('reasoning', 'Only supplied reasoning'));
await _settle(tester);
// Reasoning is private until the operator opens the combined activity.
expect(find.text('Only supplied reasoning'), findsNothing);
expect(find.text('Answer first'), findsOneWidget);
expect(_activity, findsOneWidget);
expect(find.textContaining('Thinking'), findsOneWidget);
expect(find.byKey(const Key('chat-reasoning-chip')), findsNothing);
await tester.tap(_activity);
await _settle(tester);
await tester.tap(find.text('Reasoning 1'));
await _settle(tester);
expect(_inDialog('Only supplied reasoning'), findsOneWidget);
await _dismiss(tester);
// The step ends: the chip settles to its icon.
gateway.emitEvent(chatStepFrame('ended'));
await tester.pumpAndSettle();
expect(_activity, findsOneWidget);
expect(tester.takeException(), isNull);
},
);
testWidgets('an open activity dialog follows live deltas, paging and '
'resync ($host)', (tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async =>
chatPage(const [], hasMore: true, cursor: 'old');
await tester.pumpWidget(
_host(
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
cupertino,
),
);
await tester.pumpAndSettle();
gateway.emitEvent(_delta('reasoning', 'Provided thought'));
await _settle(tester);
final rowElement = tester.element(find.byType(ChatMessageView));
await tester.tap(_activity);
await _settle(tester);
await tester.tap(find.text('Reasoning 1'));
await _settle(tester);
expect(_inDialog('Provided thought'), findsOneWidget);
// The open sheet repaints with each delta while the answer streams too;
// the row and its chip are the same elements throughout.
gateway.emitEvent(_delta('text', 'Answer'));
gateway.emitEvent(_delta('reasoning', ' continues'));
await _settle(tester);
expect(_inDialog('Provided thought continues'), findsOneWidget);
expect(find.text('Answer'), findsOneWidget);
expect(tester.element(find.byType(ChatMessageView)), same(rowElement));
gateway.emitEvent(_delta('reasoning', ' live'));
await _settle(tester);
expect(_inDialog('Provided thought continues live'), findsOneWidget);
await _dismiss(tester);
// A closed sheet draws nothing; reopening shows the newest text.
gateway.emitEvent(_delta('reasoning', ' while closed'));
gateway.emitEvent(_delta('text', ' continues'));
await tester.pumpAndSettle();
expect(find.textContaining('while closed'), findsNothing);
expect(find.text('Answer continues'), findsOneWidget);
await tester.tap(_activity);
await tester.pumpAndSettle();
expect(find.text('Reasoning 1'), findsOneWidget);
await tester.tap(find.text('Reasoning 1'));
await tester.pumpAndSettle();
expect(
_inDialog('Provided thought continues live while closed'),
findsOneWidget,
);
await _dismiss(tester);
// Prepending another assistant message folds into the same turn without
// replacing the row; the one activity control keeps both steps.
gateway.history = (_) async => chatPage([
chatMessage(
'older',
'assistant',
parts: [
{
'type': 'reasoning',
'id': 'older-r',
'text': 'Older provided thought',
},
],
),
]);
await tester.tap(find.text('Load older'));
await tester.pumpAndSettle();
expect(_activity, findsOneWidget);
expect(tester.element(find.byType(ChatMessageView)), same(rowElement));
await tester.tap(_activity);
await tester.pumpAndSettle();
expect(find.text('Reasoning 1'), findsOneWidget);
expect(find.text('Reasoning 2'), findsOneWidget);
await tester.tap(find.text('Reasoning 1'));
await tester.pumpAndSettle();
await tester.tap(find.text('Reasoning 2'));
await tester.pumpAndSettle();
expect(_inDialog('Older provided thought'), findsOneWidget);
expect(
_inDialog('Provided thought continues live while closed'),
findsOneWidget,
);
// A resync replaces the stored turn under the open sheet.
gateway.emitEvent(chatStepFrame('ended'));
gateway.history = (_) async => chatPage([
chatMessage(
'a',
'assistant',
parts: [
{
'type': 'reasoning',
'id': 'a#reasoning#0',
'text': 'Stored provided thought',
},
{'type': 'text', 'id': 'a#text#0', 'text': 'Stored answer'},
],
),
]);
gateway.emitEvent({'session_id': 's', 'kind': 'resync'});
await tester.pumpAndSettle();
expect(_inDialog('Stored provided thought'), findsOneWidget);
expect(find.textContaining('while closed'), findsNothing);
expect(find.text('Stored answer'), findsOneWidget);
expect(tester.element(find.byType(ChatMessageView)), same(rowElement));
await _dismiss(tester);
// A fresh history visit draws the chip closed; missing reasoning is
// never fabricated. Cupertino keeps only its native controls.
await tester.pumpWidget(const SizedBox.shrink());
await tester.pumpWidget(
_host(
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
cupertino,
),
);
await tester.pumpAndSettle();
expect(find.text('Stored provided thought'), findsNothing);
await tester.tap(_activity);
await tester.pumpAndSettle();
await tester.tap(find.text('Reasoning 1'));
await tester.pumpAndSettle();
expect(_inDialog('Stored provided thought'), findsOneWidget);
if (cupertino) expect(find.byType(Material), findsNothing);
expect(tester.takeException(), isNull);
});
}
}