fix(chat): keep provided reasoning visible during live replies

This commit is contained in:
2026-10-24 01:59:52 +01:00
parent 7f88f14a52
commit fa818341cf
10 changed files with 330 additions and 96 deletions
@@ -0,0 +1,165 @@
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);
});
}
}
@@ -168,7 +168,7 @@ void main() {
);
await tester.pumpAndSettle();
// The turn's reasoning is one chip below its answer; it counts the
// The turn's reasoning is one disclosure below its answer; it counts the
// steps of every message folded into the turn.
expect(find.text('Reasoning · 2 steps'), findsOneWidget);
@@ -184,9 +184,7 @@ void main() {
]) {
expect(find.text(body), findsOneWidget, reason: body);
}
Navigator.of(
tester.element(find.text('NEWEST folded reasoning body.')),
).pop();
await tester.tap(find.text('Reasoning · 4 steps'));
await tester.pumpAndSettle();
expect(
find.text('Load older'),
@@ -0,0 +1,70 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
void main() {
test('only the newest assistant turn is marked streaming', () {
final messages = <JsonMap>[
{
'id': 'old',
'role': 'assistant',
'parts': [
{'type': 'reasoning', 'text': 'Stored thought'},
],
},
{'id': 'user', 'role': 'user', 'parts': []},
];
expect(transcriptRows(messages, running: true).first['streaming'], isNull);
final activityOnly = <JsonMap>[
...messages,
{
'id': 'activity-only',
'role': 'assistant',
'parts': [
{'type': 'activity', 'kind': 'thinking', 'state': 'running'},
],
},
];
expect(
transcriptRows(activityOnly, running: true).first['streaming'],
isNull,
);
messages.add({
'id': 'new',
'role': 'assistant',
'parts': [
{'type': 'text', 'text': 'Answer'},
],
});
final rows = transcriptRows(messages, running: true);
expect(rows.first['streaming'], isNull);
expect(rows.last['streaming'], isTrue);
expect(transcriptRows(messages).last['streaming'], isNull);
});
test('same ordinal text and reasoning stay independent across deltas', () {
var thread = <String, dynamic>{'sessionId': 's', 'messages': <JsonMap>[]};
for (final (kind, delta) in [
('text', 'Answer'),
('reasoning', 'Provided thought'),
('text', ' continued'),
('reasoning', ' continued'),
]) {
thread = applyChatEvent(thread, {
'session_id': 's',
'message_id': 'a',
'kind': kind,
'part': {'type': kind, 'id': 'a#$kind#0'},
'delta': delta,
});
}
expect((thread['messages'] as List).single['parts'], [
{'type': 'text', 'id': 'a#text#0', 'text': 'Answer continued'},
{
'type': 'reasoning',
'id': 'a#reasoning#0',
'text': 'Provided thought continued',
},
]);
});
}
@@ -124,8 +124,7 @@ void main() {
expect(find.textContaining('"payload": 7'), findsNothing);
expect(find.text('plain future text'), findsOneWidget);
// The answer keeps its own order in the bubble; reasoning and the tool
// calls are two chips in one row below it.
// The answer keeps its own order; reasoning expands inline below it.
double top(String text) => tester.getTopLeft(find.text(text)).dy;
expect(top('First answer'), lessThan(top('Second answer')));
expect(
@@ -140,7 +139,7 @@ void main() {
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(find.text('private reasoning'), findsOneWidget);
await tester.tapAt(const Offset(10, 10));
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
// The chip opens the calls; a call opens to its formatted input.
@@ -162,7 +161,7 @@ void main() {
});
for (final cupertino in [false, true]) {
testWidgets('reasoning and tool chips share one row below the answer '
testWidgets('reasoning disclosure and tool chip follow the answer '
'(${cupertino ? 'cupertino' : 'material'})', (tester) async {
// The test font draws every glyph as a full square, so the chips need
// more room than real text does on a phone.
@@ -192,10 +191,9 @@ void main() {
reasoning.top,
greaterThan(tester.getRect(find.text('The answer')).bottom),
);
expect(reasoning.center.dy, moreOrLessEquals(tools.center.dy));
expect(reasoning.right, lessThan(tools.left));
expect(reasoning.bottom, lessThan(tools.top));
// Like the tool chip, it opens a sheet with every step.
// The inline disclosure opens with every recorded step.
await tester.tap(find.text('Reasoning · 2 steps'));
await tester.pumpAndSettle();
expect(find.text('first thought'), findsOneWidget);
@@ -298,7 +296,7 @@ void main() {
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(find.text('thought'), findsOneWidget);
await tester.tapAt(const Offset(10, 10));
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
await tester.tap(find.text('1 tool'));