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

166 lines
6.1 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),
);
void main() {
for (final cupertino in [false, true]) {
testWidgets('reasoning arriving after text opens live ($cupertino)', (
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 tester.pumpAndSettle();
expect(find.text('Only supplied reasoning'), findsOneWidget);
expect(find.text('Answer first'), findsOneWidget);
});
testWidgets('live reasoning survives text, collapse, paging and resync '
'(${cupertino ? 'Cupertino' : 'Material'})', (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 tester.pumpAndSettle();
// No opening tap required for a newly streaming reply.
expect(find.text('Provided thought'), findsOneWidget);
final rowElement = tester.element(find.byType(ChatMessageView));
gateway.emitEvent(_delta('text', 'Answer'));
gateway.emitEvent(_delta('reasoning', ' continues'));
await tester.pumpAndSettle();
expect(find.text('Provided thought continues'), findsOneWidget);
expect(find.text('Answer'), findsOneWidget);
expect(tester.element(find.byType(ChatMessageView)), same(rowElement));
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
gateway.emitEvent(_delta('reasoning', ' while collapsed'));
gateway.emitEvent(_delta('text', ' continues'));
await tester.pumpAndSettle();
expect(find.textContaining('while collapsed'), findsNothing);
expect(find.text('Answer continues'), findsOneWidget);
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(
find.text('Provided thought continues while collapsed'),
findsOneWidget,
);
// Prepending another assistant message folds into the same turn without
// replacing the row or closing the already-open reasoning disclosure.
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(find.text('Older provided thought'), findsOneWidget);
expect(
find.text('Provided thought continues while collapsed'),
findsOneWidget,
);
expect(tester.element(find.byType(ChatMessageView)), same(rowElement));
gateway.emitEvent(chatStepFrame('ended'));
await tester.pumpAndSettle();
expect(
find.text('Provided thought continues while collapsed'),
findsOneWidget,
);
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(find.text('Stored provided thought'), findsOneWidget);
expect(find.text('Stored answer'), findsOneWidget);
expect(find.textContaining('while collapsed'), findsNothing);
expect(tester.element(find.byType(ChatMessageView)), same(rowElement));
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(find.text('Stored provided thought'), findsNothing);
// A fresh history visit starts collapsed; 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(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(find.text('Stored provided thought'), findsOneWidget);
if (cupertino) expect(find.byType(Material), findsNothing);
expect(tester.takeException(), isNull);
});
}
}