Show a chat turn's changed files as a chip that opens its diff

The answer footer shows '3 files changed +9 -2'; tapping opens the turn's
files in the sheet (wide) or a full-screen page (narrow), and a file opens
the run Changes diff viewer. Patches load through the chat's own gateway.
This commit is contained in:
2026-11-09 16:08:35 +00:00
parent 0696ab849a
commit 3374bea2ef
15 changed files with 1155 additions and 36 deletions
@@ -0,0 +1,213 @@
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/ui/transcript/chat_message_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_turn_diff_chip.dart';
const _patch =
'--- "a/lib/a.dart"\n+++ "b/lib/a.dart"\n@@ -1,2 +1,2 @@\n-old\n+new\n same\n';
JsonMap _summary({bool truncated = false}) => {
'turn_id': 'turn_1',
'message_ids': ['a1'],
'files': 2,
'additions': 42,
'deletions': 7,
'status': truncated ? 'partial' : 'complete',
if (truncated) 'truncated': true,
};
JsonMap _reply({bool truncated = false}) => {
'session_id': 'ses_1',
'turn_id': 'turn_1',
'summary': _summary(truncated: truncated),
'changes': {
'status': truncated ? 'partial' : 'complete',
if (truncated)
'diagnostic': 'Some patches were omitted/truncated by capture limits.',
'files': [
{'path': 'lib/a.dart', 'kind': 'modified', 'patch': _patch},
{
'path': 'big.txt',
'kind': 'added',
if (truncated) 'truncated': true,
if (truncated) 'no_patch': true,
if (truncated) 'no_patch_reason': 'lines',
},
],
'skipped': 0,
},
};
Widget _app(Widget body, bool cupertino, {Size size = const Size(1000, 800)}) {
final media = MediaQueryData(size: size);
final home = MediaQuery(data: media, child: body);
return cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: home))
: MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(body: home),
);
}
final _chip = find.byKey(const Key('chat-turn-diff-chip'));
void main() {
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('the chip reads files and line counts, no inline diff '
'($host)', (tester) async {
final row = {
'kind': 'assistant',
'key': 'a1',
'messageIds': ['a1'],
'parts': [
{'type': 'text', 'text': 'Edited the files.'},
],
};
await tester.pumpWidget(
_app(
ListView(
children: [
ChatMessageView(
row: row,
footer: [
ChatTurnDiffChip(
summary: _summary(),
load: () async => _reply(),
),
],
),
],
),
cupertino,
),
);
await tester.pumpAndSettle();
expect(_chip, findsOneWidget);
expect(find.text('2 files changed +42 −7'), findsOneWidget);
expect(
find.bySemanticsLabel(
'2 files changed, 42 lines added, 7 lines removed, open changes',
),
findsOneWidget,
);
expect(find.textContaining('+new'), findsNothing);
});
testWidgets('tapping opens the file list and a file opens its patch '
'($host)', (tester) async {
var loads = 0;
await tester.pumpWidget(
_app(
Center(
child: ChatTurnDiffChip(
summary: _summary(),
load: () async {
loads++;
return _reply();
},
),
),
cupertino,
),
);
await tester.tap(_chip);
await tester.pumpAndSettle();
expect(loads, 1);
expect(find.byKey(const Key('chat-turn-diff-list')), findsOneWidget);
expect(find.text('lib/a.dart'), findsOneWidget);
expect(find.text('big.txt'), findsOneWidget);
expect(find.byKey(const Key('chat-turn-diff-truncated')), findsNothing);
await tester.tap(find.text('lib/a.dart'));
await tester.pumpAndSettle();
expect(
find.byKey(const ValueKey('file-diff-lib/a.dart')),
findsOneWidget,
);
expect(tester.takeException(), isNull);
});
testWidgets('a truncated turn says so and names the file without a patch '
'($host)', (tester) async {
await tester.pumpWidget(
_app(
Center(
child: ChatTurnDiffChip(
summary: _summary(truncated: true),
load: () async => _reply(truncated: true),
),
),
cupertino,
),
);
expect(
find.bySemanticsLabel(
'2 files changed, 42 lines added, 7 lines removed, partly shown, '
'open changes',
),
findsOneWidget,
);
await tester.tap(_chip);
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-turn-diff-truncated')), findsOneWidget);
expect(
find.text('Some patches were omitted/truncated by capture limits.'),
findsOneWidget,
);
expect(find.textContaining('truncated'), findsWidgets);
});
testWidgets('a narrow window opens the changes full screen ($host)', (
tester,
) async {
tester.view.physicalSize = const Size(400, 800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
await tester.pumpWidget(
_app(
Center(
child: ChatTurnDiffChip(
summary: _summary(),
load: () async => _reply(),
),
),
cupertino,
size: const Size(400, 800),
),
);
await tester.tap(_chip);
await tester.pumpAndSettle();
expect(find.text('Turn changes'), findsOneWidget);
expect(find.text('lib/a.dart'), findsOneWidget);
});
testWidgets('a failed load reports and retries ($host)', (tester) async {
var fail = true;
await tester.pumpWidget(
_app(
Center(
child: ChatTurnDiffChip(
summary: _summary(),
load: () async {
if (fail) throw StateError('no recorded changes for this turn');
return _reply();
},
),
),
cupertino,
),
);
await tester.tap(_chip);
await tester.pumpAndSettle();
expect(find.textContaining('no recorded changes'), findsOneWidget);
fail = false;
await tester.tap(find.text('Retry'));
await tester.pumpAndSettle();
expect(find.text('lib/a.dart'), findsOneWidget);
});
}
}