feat(app): add native chat transcript message row
Render one transcriptRows row with native controls: role-labelled user and agent messages, verbatim user text, client and attachment names (own name, URI basename, else file), ordered agent parts, and collapsed reasoning, tool and unknown-part bodies using ExpansionTile or a Cupertino disclosure. Blank known parts draw nothing so they no longer fall through to the unknown-part disclosure; closed bodies build lazily. Adds focused widget tests, the fixture probe pair and the C2 evidence note.
This commit is contained in:
@@ -0,0 +1,321 @@
|
||||
import 'dart:ui' show Tristate;
|
||||
|
||||
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 _row(List<JsonMap> messages) => transcriptRows(messages).single;
|
||||
|
||||
JsonMap _assistant(List<JsonMap> parts) => _row([
|
||||
{'id': 'a1', 'role': 'assistant', 'parts': parts},
|
||||
]);
|
||||
|
||||
JsonMap _userRow() => _row([
|
||||
{
|
||||
'id': 'u1',
|
||||
'role': 'user',
|
||||
'client': 'Ada phone',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': ' Keep this exact\nline two '},
|
||||
{'type': 'file', 'name': 'plan.md', 'uri': '/tmp/plan.md'},
|
||||
{'type': 'file', 'uri': '/tmp/report.csv'},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
Widget _host(JsonMap row, {bool cupertino = false, double scale = 1}) {
|
||||
final body = SafeArea(
|
||||
child: ListView(children: [ChatMessageView(row: row)]),
|
||||
);
|
||||
Widget builder(BuildContext context, Widget? child) => MediaQuery(
|
||||
data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(scale)),
|
||||
child: child!,
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
theme: const CupertinoThemeData(brightness: Brightness.dark),
|
||||
builder: builder,
|
||||
home: CupertinoPageScaffold(child: body),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
theme: ThemeData(
|
||||
brightness: Brightness.dark,
|
||||
platform: TargetPlatform.android,
|
||||
),
|
||||
builder: builder,
|
||||
home: Scaffold(body: body),
|
||||
);
|
||||
}
|
||||
|
||||
void _phone(WidgetTester tester) {
|
||||
tester.view.physicalSize = const Size(390, 844);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('Material dark user row keeps exact text and attachments at 2x', (
|
||||
tester,
|
||||
) async {
|
||||
_phone(tester);
|
||||
await tester.pumpWidget(_host(_userRow(), scale: 2));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('User'), findsOneWidget);
|
||||
expect(find.text('Ada phone'), findsOneWidget);
|
||||
expect(find.text(' Keep this exact\nline two '), findsOneWidget);
|
||||
expect(find.text('plan.md'), findsOneWidget);
|
||||
expect(find.text('report.csv'), findsOneWidget);
|
||||
final role = tester.widget<Text>(find.text('User'));
|
||||
final theme = Theme.of(tester.element(find.byType(ChatMessageView)));
|
||||
expect(theme.brightness, Brightness.dark);
|
||||
expect(role.style?.color, theme.colorScheme.onSurfaceVariant);
|
||||
expect(role.style?.color?.computeLuminance(), greaterThan(0.4));
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'Cupertino dark user row keeps exact text and attachments at 2x',
|
||||
(tester) async {
|
||||
_phone(tester);
|
||||
await tester.pumpWidget(_host(_userRow(), cupertino: true, scale: 2));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('User'), findsOneWidget);
|
||||
expect(find.text(' Keep this exact\nline two '), findsOneWidget);
|
||||
expect(find.text('plan.md'), findsOneWidget);
|
||||
expect(find.text('report.csv'), findsOneWidget);
|
||||
final context = tester.element(find.byType(ChatMessageView));
|
||||
expect(CupertinoTheme.of(context).brightness, Brightness.dark);
|
||||
final role = tester.widget<Text>(find.text('User'));
|
||||
expect(role.style?.color?.computeLuminance(), greaterThan(0.4));
|
||||
expect(tester.takeException(), isNull);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('agent parts retain order and disclosures reveal native bodies', (
|
||||
tester,
|
||||
) async {
|
||||
_phone(tester);
|
||||
final row = _assistant([
|
||||
{'type': 'text', 'text': 'First answer'},
|
||||
{'type': 'reasoning', 'text': 'private reasoning'},
|
||||
{
|
||||
'type': 'tool',
|
||||
'name': 'shell',
|
||||
'state': {
|
||||
'status': 'completed',
|
||||
'input': {'command': 'ls'},
|
||||
},
|
||||
},
|
||||
{'type': 'text', 'text': 'Second answer'},
|
||||
{'type': 'future_text', 'text': 'plain future text'},
|
||||
{'type': 'future_part', 'payload': 7},
|
||||
]);
|
||||
await tester.pumpWidget(_host(row, scale: 2));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Assistant'), findsOneWidget);
|
||||
expect(find.text('private reasoning'), findsNothing);
|
||||
expect(find.textContaining('"command": "ls"'), findsNothing);
|
||||
expect(find.textContaining('"payload": 7'), findsNothing);
|
||||
expect(find.text('plain future text'), findsOneWidget);
|
||||
|
||||
double top(String text) => tester.getTopLeft(find.text(text)).dy;
|
||||
expect(top('First answer'), lessThan(top('Reasoning')));
|
||||
expect(top('Reasoning'), lessThan(top('shell')));
|
||||
expect(top('shell'), lessThan(top('Second answer')));
|
||||
expect(
|
||||
top('Second answer'),
|
||||
lessThan(top('Unrecognised part: future_part')),
|
||||
);
|
||||
|
||||
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.ensureVisible(find.text('shell'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('shell'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.textContaining('"command": "ls"'), findsOneWidget);
|
||||
|
||||
await tester.ensureVisible(find.text('Unrecognised part: future_part'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Unrecognised part: future_part'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.textContaining('"payload": 7'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'Material disclosure bodies start at the label edge at 1x and 2x',
|
||||
(tester) async {
|
||||
for (final scale in [1.0, 2.0]) {
|
||||
_phone(tester);
|
||||
// A fresh tree per scale so the disclosure starts collapsed again.
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
_assistant([
|
||||
{'type': 'reasoning', 'text': 'short thought'},
|
||||
]),
|
||||
scale: scale,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Reasoning'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final label = tester.getTopLeft(find.text('Reasoning'));
|
||||
final body = tester.getTopLeft(find.text('short thought'));
|
||||
expect(body.dx, label.dx, reason: 'short reasoning body at ${scale}x');
|
||||
expect(find.text('short thought'), findsOneWidget);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('unknown disclosure bodies also start at the label edge', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
_assistant([
|
||||
{'type': 'future_part', 'payload': 7},
|
||||
]),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
final label = find.text('Unrecognised part: future_part');
|
||||
await tester.tap(label);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
tester.getTopLeft(find.textContaining('"payload": 7')).dx,
|
||||
tester.getTopLeft(label).dx,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('blank known-type parts draw nothing but unknown parts stay', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
_assistant([
|
||||
{'type': 'text', 'text': ''},
|
||||
{'type': 'text', 'text': ' '},
|
||||
{'type': 'reasoning', 'text': ''},
|
||||
{'type': 'file'},
|
||||
{'type': 'future_part', 'payload': 7},
|
||||
]),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
for (final label in [
|
||||
'Unrecognised part: text',
|
||||
'Unrecognised part: reasoning',
|
||||
'Unrecognised part: file',
|
||||
]) {
|
||||
expect(find.text(label), findsNothing, reason: label);
|
||||
}
|
||||
expect(find.text('Unrecognised part: future_part'), findsOneWidget);
|
||||
// The blank parts leave only the visible role label and that disclosure.
|
||||
expect(find.byType(SelectableText), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('Cupertino disclosure expands with the native control', (
|
||||
tester,
|
||||
) async {
|
||||
final row = _assistant([
|
||||
{'type': 'reasoning', 'text': 'thought'},
|
||||
{
|
||||
'type': 'tool',
|
||||
'name': 'read',
|
||||
'state': {'status': 'error'},
|
||||
},
|
||||
]);
|
||||
await tester.pumpWidget(_host(row, cupertino: true));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('thought'), findsNothing);
|
||||
await tester.tap(find.text('Reasoning'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('thought'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('read'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.textContaining('"status": "error"'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Cupertino disclosure announces its expanded state', (
|
||||
tester,
|
||||
) async {
|
||||
final handle = tester.ensureSemantics();
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
_assistant([
|
||||
{'type': 'reasoning', 'text': 'thought'},
|
||||
]),
|
||||
cupertino: true,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
Tristate state() => tester
|
||||
.getSemantics(find.bySemanticsLabel('Reasoning'))
|
||||
.getSemanticsData()
|
||||
.flagsCollection
|
||||
.isExpanded;
|
||||
expect(state(), Tristate.isFalse);
|
||||
|
||||
await tester.tap(find.text('Reasoning'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(state(), Tristate.isTrue);
|
||||
handle.dispose();
|
||||
});
|
||||
|
||||
testWidgets('row semantics announce the role once', (tester) async {
|
||||
final handle = tester.ensureSemantics();
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
_assistant([
|
||||
{'type': 'text', 'text': 'Answer'},
|
||||
]),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.bySemanticsLabel('Assistant message'), findsOneWidget);
|
||||
expect(find.bySemanticsLabel('Assistant'), findsNothing);
|
||||
final answer = tester.getSemantics(find.text('Answer')).getSemanticsData();
|
||||
expect(answer.value, 'Answer');
|
||||
handle.dispose();
|
||||
});
|
||||
|
||||
testWidgets('the text builder seam replaces only assistant text', (
|
||||
tester,
|
||||
) async {
|
||||
final row = _assistant([
|
||||
{'type': 'text', 'text': '**markdown**'},
|
||||
]);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ChatMessageView(
|
||||
row: row,
|
||||
textBuilder: (context, text) => Text('Rendered $text'),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(find.text('Rendered **markdown**'), findsOneWidget);
|
||||
expect(find.text('**markdown**'), findsNothing);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/native_disclosure.dart';
|
||||
|
||||
Widget _material(WidgetBuilder body) => MaterialApp(
|
||||
theme: ThemeData(brightness: Brightness.dark),
|
||||
home: Scaffold(
|
||||
body: NativeDisclosure(label: 'Body', childBuilder: body),
|
||||
),
|
||||
);
|
||||
|
||||
Widget _cupertino(WidgetBuilder body) => CupertinoApp(
|
||||
theme: const CupertinoThemeData(brightness: Brightness.dark),
|
||||
home: CupertinoPageScaffold(
|
||||
child: NativeDisclosure(label: 'Body', childBuilder: body),
|
||||
),
|
||||
);
|
||||
|
||||
void main() {
|
||||
testWidgets('closed Material disclosure never builds its body', (
|
||||
tester,
|
||||
) async {
|
||||
var builds = 0;
|
||||
await tester.pumpWidget(
|
||||
_material((context) {
|
||||
builds++;
|
||||
return const Text('body');
|
||||
}),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(builds, 0);
|
||||
expect(find.text('body'), findsNothing);
|
||||
|
||||
await tester.tap(find.text('Body'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(builds, greaterThan(0));
|
||||
expect(find.text('body'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('closed Cupertino disclosure never builds its body', (
|
||||
tester,
|
||||
) async {
|
||||
var builds = 0;
|
||||
await tester.pumpWidget(
|
||||
_cupertino((context) {
|
||||
builds++;
|
||||
return const Text('body');
|
||||
}),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(builds, 0);
|
||||
expect(find.text('body'), findsNothing);
|
||||
|
||||
await tester.tap(find.text('Body'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(builds, 1);
|
||||
expect(find.text('body'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Body'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('body'), findsNothing);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user