Make chat reasoning a footer chip that opens a live sheet like tools

This commit is contained in:
2026-10-24 18:13:03 +01:00
parent ef575898f2
commit 5cb43b6dc8
11 changed files with 646 additions and 165 deletions
@@ -112,7 +112,7 @@ void main() {
});
for (final cupertino in [false, true]) {
testWidgets('reasoning first arriving after a queued follow-up opens live '
testWidgets('reasoning first arriving after a queued follow-up stays live '
'($cupertino)', (tester) async {
gateway.prompt = (_, _) async => {'chat_queued': true};
await tester.pumpWidget(
@@ -145,7 +145,17 @@ void main() {
'part': {'id': 'live-reasoning-0', 'type': 'reasoning'},
'delta': 'Provided reasoning after the follow-up',
});
await tester.pumpAndSettle();
// The reasoning chip spins while its step is the newest part, so the
// frames are pumped rather than settled.
Future<void> frames() async {
for (var frame = 0; frame < 8; frame++) {
await tester.pump(const Duration(milliseconds: 100));
}
}
await frames();
await tester.tap(find.text('Reasoning'));
await frames();
expect(
find.text('Provided reasoning after the follow-up'),
findsOneWidget,
@@ -24,9 +24,31 @@ Widget _host(Widget child, bool cupertino) => cupertino
home: Scaffold(body: child),
);
/// A streaming reasoning chip spins for as long as reasoning is the newest
/// part, so pumpAndSettle would never return; this lets a sheet 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 _sheet = find.byKey(const Key('chat-reasoning-sheet'));
Finder _inSheet(String text) =>
find.descendant(of: _sheet, matching: find.text(text));
Future<void> _dismiss(WidgetTester tester) async {
await tester.tapAt(const Offset(10, 10));
await _settle(tester);
expect(_sheet, findsNothing);
}
void main() {
for (final cupertino in [false, true]) {
testWidgets('reasoning arriving after text opens live ($cupertino)', (
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('reasoning arriving after text is a spinning chip ($host)', (
tester,
) async {
final gateway = FakeChatGateway();
@@ -42,14 +64,39 @@ void main() {
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);
await _settle(tester);
// A chip in the footer like the tools chip, not an inline body.
expect(find.text('Only supplied reasoning'), findsNothing);
expect(find.text('Answer first'), findsOneWidget);
final chip = find.byKey(const Key('chat-reasoning-chip'));
final spinner = find.descendant(
of: chip,
matching: find.byType(
cupertino ? CupertinoActivityIndicator : CircularProgressIndicator,
),
);
expect(spinner, findsOneWidget);
expect(
find.bySemanticsLabel('Reasoning, expand for details'),
findsOneWidget,
);
await tester.tap(find.text('Reasoning'));
await _settle(tester);
expect(_inSheet('Only supplied reasoning'), findsOneWidget);
await _dismiss(tester);
// The step ends: the chip settles to its icon.
gateway.emitEvent(chatStepFrame('ended'));
await tester.pumpAndSettle();
expect(spinner, findsNothing);
expect(tester.takeException(), isNull);
});
testWidgets('live reasoning survives text, collapse, paging and resync '
'(${cupertino ? 'Cupertino' : 'Material'})', (tester) async {
testWidgets('an open reasoning sheet follows live deltas, paging and '
'resync ($host)', (tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async =>
@@ -63,34 +110,41 @@ void main() {
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);
await _settle(tester);
final rowElement = tester.element(find.byType(ChatMessageView));
await tester.tap(find.text('Reasoning'));
await _settle(tester);
expect(_inSheet('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 tester.pumpAndSettle();
expect(find.text('Provided thought continues'), findsOneWidget);
await _settle(tester);
expect(_inSheet('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(_inSheet('Provided thought continues live'), findsOneWidget);
await _dismiss(tester);
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
gateway.emitEvent(_delta('reasoning', ' while collapsed'));
// 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 collapsed'), findsNothing);
expect(find.textContaining('while closed'), findsNothing);
expect(find.text('Answer continues'), findsOneWidget);
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(
find.text('Provided thought continues while collapsed'),
_inSheet('Provided thought continues live while closed'),
findsOneWidget,
);
await _dismiss(tester);
// Prepending another assistant message folds into the same turn without
// replacing the row or closing the already-open reasoning disclosure.
// replacing the row; the chip counts both steps.
gateway.history = (_) async => chatPage([
chatMessage(
'older',
@@ -106,20 +160,18 @@ void main() {
]);
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(find.text('Reasoning · 2 steps'), findsOneWidget);
expect(tester.element(find.byType(ChatMessageView)), same(rowElement));
gateway.emitEvent(chatStepFrame('ended'));
await tester.tap(find.text('Reasoning · 2 steps'));
await tester.pumpAndSettle();
expect(_inSheet('Older provided thought'), findsOneWidget);
expect(
find.text('Provided thought continues while collapsed'),
_inSheet('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',
@@ -136,16 +188,14 @@ void main() {
]);
gateway.emitEvent({'session_id': 's', 'kind': 'resync'});
await tester.pumpAndSettle();
expect(find.text('Stored provided thought'), findsOneWidget);
expect(_inSheet('Stored provided thought'), findsOneWidget);
expect(find.textContaining('while closed'), findsNothing);
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);
await _dismiss(tester);
// A fresh history visit starts collapsed; missing reasoning is never
// fabricated. Cupertino keeps only its native controls.
// 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(
@@ -157,7 +207,7 @@ void main() {
expect(find.text('Stored provided thought'), findsNothing);
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(find.text('Stored provided thought'), findsOneWidget);
expect(_inSheet('Stored provided thought'), findsOneWidget);
if (cupertino) expect(find.byType(Material), findsNothing);
expect(tester.takeException(), isNull);
});
@@ -168,8 +168,8 @@ void main() {
);
await tester.pumpAndSettle();
// The turn's reasoning is one disclosure below its answer; it counts the
// steps of every message folded into the turn.
// The turn's reasoning is one footer chip below its answer; it counts
// the steps of every message folded into the turn.
expect(find.text('Reasoning · 2 steps'), findsOneWidget);
await tester.tap(find.text('Load older'));
@@ -182,10 +182,18 @@ void main() {
'NEWEST folded reasoning body.',
'OLDEST folded reasoning body.',
]) {
expect(find.text(body), findsOneWidget, reason: body);
expect(
find.descendant(
of: find.byKey(const Key('chat-reasoning-sheet')),
matching: find.text(body),
),
findsOneWidget,
reason: body,
);
}
await tester.tap(find.text('Reasoning · 4 steps'));
await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();
expect(find.text('OLDEST folded reasoning body.'), findsNothing);
expect(
find.text('Load older'),
keepsControl ? findsOneWidget : findsNothing,
@@ -124,7 +124,7 @@ void main() {
expect(find.textContaining('"payload": 7'), findsNothing);
expect(find.text('plain future text'), findsOneWidget);
// The answer keeps its own order; reasoning expands inline below it.
// The answer keeps its own order; the footer chips sit below it.
double top(String text) => tester.getTopLeft(find.text(text)).dy;
expect(top('First answer'), lessThan(top('Second answer')));
expect(
@@ -134,13 +134,21 @@ void main() {
expect(top('Unrecognised part: future_part'), lessThan(top('1 tool')));
expect(top('Unrecognised part: future_part'), lessThan(top('Reasoning')));
// Reasoning is a chip like the tools chip: it opens a native sheet.
await tester.ensureVisible(find.text('Reasoning'));
await tester.pumpAndSettle();
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(find.text('private reasoning'), findsOneWidget);
await tester.tap(find.text('Reasoning'));
expect(
find.descendant(
of: find.byKey(const Key('chat-reasoning-sheet')),
matching: find.text('private reasoning'),
),
findsOneWidget,
);
await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();
expect(find.text('private reasoning'), findsNothing);
// The chip opens the calls; a call opens to its formatted input.
await tester.ensureVisible(find.text('1 tool'));
@@ -267,11 +275,20 @@ void main() {
expect(reasoning.center.dy, closeTo(tools.center.dy, 2));
expect(reasoning.right, lessThan(tools.left));
// The inline disclosure opens with every recorded step.
// The sheet lists every recorded step, in order.
await tester.tap(find.text('Reasoning · 2 steps'));
await tester.pumpAndSettle();
expect(find.text('first thought'), findsOneWidget);
expect(find.text('second thought'), findsOneWidget);
final sheet = find.byKey(const Key('chat-reasoning-sheet'));
Finder inSheet(String text) =>
find.descendant(of: sheet, matching: find.text(text));
expect(inSheet('first thought'), findsOneWidget);
expect(inSheet('second thought'), findsOneWidget);
expect(
tester.getTopLeft(inSheet('first thought')).dy,
lessThan(tester.getTopLeft(inSheet('second thought')).dy),
);
expect(inSheet('STEP 1'), findsOneWidget);
expect(inSheet('STEP 2'), findsOneWidget);
});
}
@@ -352,7 +369,7 @@ void main() {
expect(find.byType(SelectableText), findsNothing);
});
testWidgets('Cupertino disclosure expands with the native control', (
testWidgets('Cupertino reasoning and tool chips open native popups', (
tester,
) async {
final row = _assistant([
@@ -369,9 +386,11 @@ void main() {
expect(find.text('thought'), findsNothing);
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(find.byType(CupertinoPopupSurface), findsOneWidget);
expect(find.text('thought'), findsOneWidget);
await tester.tap(find.text('Reasoning'));
await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();
expect(find.text('thought'), findsNothing);
await tester.tap(find.text('1 tool'));
await tester.pumpAndSettle();
@@ -0,0 +1,158 @@
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/domain/chat_transcript.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
JsonMap _turn(List<JsonMap> parts, {String model = ''}) => transcriptRows([
{
'id': 'a1',
'role': 'assistant',
if (model.isNotEmpty) 'model': model,
'parts': parts,
},
]).single;
Widget _app(Widget body, bool cupertino) => cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: body))
: MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(body: body),
);
final _sheet = find.byKey(const Key('chat-reasoning-sheet'));
Finder _inSheet(String text) =>
find.descendant(of: _sheet, matching: find.text(text));
void main() {
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('the footer reads reasoning, tools, permissions, model '
'($host)', (tester) async {
tester.view.physicalSize = const Size(900, 600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
_app(
ListView(
children: [
ChatMessageView(
row: _turn([
{'type': 'text', 'text': 'Answer'},
{'type': 'reasoning', 'text': 'thought'},
{
'type': 'tool',
'name': 'read',
'state': {'status': 'completed'},
},
], model: 'model-x'),
footer: const [Text('permissions')],
),
],
),
cupertino,
),
);
await tester.pumpAndSettle();
// The test font draws every glyph as a full square, so the row may wrap;
// each item then follows the previous one in reading order.
final rects = [
for (final text in ['Reasoning', '1 tool', 'permissions', 'model-x'])
tester.getRect(find.text(text)),
];
for (var index = 1; index < rects.length; index++) {
final before = rects[index - 1];
final after = rects[index];
final sameLine = (after.center.dy - before.center.dy).abs() < 3;
expect(
sameLine ? after.left > before.right : after.top >= before.bottom,
isTrue,
reason: 'footer item $index follows item ${index - 1}',
);
}
expect(
(rects[1].center.dy - rects[0].center.dy).abs(),
lessThan(3),
reason: 'reasoning and tools share a line',
);
// Same native chip as the tools: an icon and a chevron, one button node.
final chip = find.byKey(const Key('chat-reasoning-chip'));
expect(
find.descendant(
of: chip,
matching: find.byIcon(
cupertino ? CupertinoIcons.lightbulb : Icons.lightbulb_outline,
),
),
findsOneWidget,
);
expect(
find.descendant(
of: chip,
matching: find.byIcon(
cupertino ? CupertinoIcons.chevron_up : Icons.expand_less,
),
),
findsOneWidget,
);
final semantics = tester.getSemantics(
find.bySemanticsLabel('Reasoning, expand for details'),
);
expect(semantics.flagsCollection.isButton, isTrue);
if (cupertino) expect(find.byType(Material), findsNothing);
});
testWidgets('an open sheet follows a rebuilt turn and outlives its row '
'($host)', (tester) async {
final row = ValueNotifier<JsonMap?>(
_turn([
{'type': 'reasoning', 'id': 'r1', 'text': 'first'},
]),
);
addTearDown(row.dispose);
await tester.pumpWidget(
_app(
ValueListenableBuilder<JsonMap?>(
valueListenable: row,
builder: (context, value, _) => ListView(
children: [
if (value != null)
ChatMessageView(key: const Key('turn'), row: value),
],
),
),
cupertino,
),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Reasoning'));
await tester.pumpAndSettle();
expect(_inSheet('Reasoning'), findsOneWidget);
expect(_inSheet('first'), findsOneWidget);
row.value = _turn([
{'type': 'reasoning', 'id': 'r1', 'text': 'first, longer'},
{'type': 'text', 'text': 'Answer'},
{'type': 'reasoning', 'id': 'r2', 'text': 'second'},
]);
await tester.pumpAndSettle();
expect(_inSheet('Reasoning · 2 steps'), findsOneWidget);
expect(_inSheet('first, longer'), findsOneWidget);
expect(_inSheet('second'), findsOneWidget);
// The row leaves the transcript while the sheet is open: the sheet keeps
// its last steps and closes cleanly.
row.value = null;
await tester.pumpAndSettle();
expect(find.byKey(const Key('turn')), findsNothing);
expect(_inSheet('second'), findsOneWidget);
await tester.tapAt(const Offset(10, 10));
await tester.pumpAndSettle();
expect(_sheet, findsNothing);
expect(tester.takeException(), isNull);
});
}
}