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:
@@ -0,0 +1,73 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/servers/chat/chat_identity.dart';
|
||||
import 'package:pipelines_app/core/servers/chat/chat_server_gateway.dart';
|
||||
import 'package:pipelines_app/core/servers/chat/federated_chat_gateway.dart';
|
||||
import 'package:pipelines_app/core/servers/server_catalog.dart';
|
||||
|
||||
class _Machine implements ApiGateway {
|
||||
final events = StreamController<JsonMap>.broadcast();
|
||||
final calls = <(String, JsonMap)>[];
|
||||
@override
|
||||
Stream<JsonMap> get frames => events.stream;
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add((type, fields));
|
||||
return {
|
||||
'type': type,
|
||||
'chat_turn_diff': {
|
||||
'session_id': 'native',
|
||||
'turn_id': 'turn_1',
|
||||
'changes': {'status': 'complete', 'files': <JsonMap>[]},
|
||||
},
|
||||
'chat_turn_diffs': {'session_id': 'native', 'turn_diffs': <JsonMap>[]},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
SavedServer _server(String id) => SavedServer.validated(
|
||||
id: id,
|
||||
name: 'Machine $id',
|
||||
endpoints: ['ws://$id.test/connect'],
|
||||
);
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'turn diffs go to the chat\'s own machine and come back namespaced',
|
||||
() async {
|
||||
final a = _Machine(), b = _Machine();
|
||||
final sources = {
|
||||
'a': ChatServerGateway(_server('a'), a),
|
||||
'b': ChatServerGateway(_server('b'), b),
|
||||
};
|
||||
final all = FederatedChatGateway(
|
||||
() => sources,
|
||||
() => 'a',
|
||||
const Stream.empty(),
|
||||
);
|
||||
final remote = federatedChatId('b', 'native');
|
||||
final reply = await all.call('chat_turn_diff', {
|
||||
'chat_turn_diff': {'session_id': remote, 'turn_id': 'turn_1'},
|
||||
});
|
||||
expect(a.calls, isEmpty);
|
||||
expect(b.calls.single.$2['chat_turn_diff'], {
|
||||
'session_id': 'native',
|
||||
'turn_id': 'turn_1',
|
||||
});
|
||||
expect((reply['chat_turn_diff'] as Map)['session_id'], remote);
|
||||
expect((reply['chat_turn_diffs'] as Map)['session_id'], remote);
|
||||
|
||||
// A pushed summary list is re-keyed to the machine's namespaced id.
|
||||
final pushed = sources['b']!.frames.first;
|
||||
b.events.add({
|
||||
'type': 'chat_turn_diffs',
|
||||
'chat_turn_diffs': {'session_id': 'native', 'turn_diffs': <JsonMap>[]},
|
||||
});
|
||||
expect(((await pushed)['chat_turn_diffs'] as Map)['session_id'], remote);
|
||||
await a.events.close();
|
||||
await b.events.close();
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
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 'chat_thread_fixtures.dart';
|
||||
|
||||
/// Answers chat_turn_diff with one recorded file for the asked turn.
|
||||
class _DiffGateway extends FakeChatGateway {
|
||||
final asked = <JsonMap>[];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
if (type != 'chat_turn_diff') return super.call(type, fields);
|
||||
final body = Map<String, dynamic>.from(fields['chat_turn_diff'] as Map);
|
||||
asked.add(body);
|
||||
return {
|
||||
'type': 'chat_turn_diff',
|
||||
'chat_turn_diff': {
|
||||
...body,
|
||||
'changes': {
|
||||
'status': 'complete',
|
||||
'files': [
|
||||
{
|
||||
'path': 'lib/${body['turn_id']}.dart',
|
||||
'kind': 'added',
|
||||
'patch': '@@ -0,0 +1 @@\n+x\n',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
JsonMap _summary(String turn, String message) => {
|
||||
'turn_id': turn,
|
||||
'message_ids': [message],
|
||||
'files': 1,
|
||||
'additions': 1,
|
||||
'deletions': 0,
|
||||
'status': 'complete',
|
||||
};
|
||||
|
||||
void main() {
|
||||
testWidgets('history and live summaries draw a change chip per turn; the '
|
||||
'chip fetches that turn through the chat gateway', (tester) async {
|
||||
final gateway = _DiffGateway();
|
||||
addTearDown(gateway.close);
|
||||
gateway.history = (_) async => {
|
||||
...chatPage([
|
||||
chatMessage(
|
||||
'u1',
|
||||
'user',
|
||||
parts: [
|
||||
{'type': 'text', 'text': 'Edit it'},
|
||||
],
|
||||
),
|
||||
chatMessage(
|
||||
'a1',
|
||||
'assistant',
|
||||
parts: [
|
||||
{'type': 'text', 'text': 'Edited.'},
|
||||
],
|
||||
),
|
||||
chatMessage(
|
||||
'u2',
|
||||
'user',
|
||||
parts: [
|
||||
{'type': 'text', 'text': 'Again'},
|
||||
],
|
||||
),
|
||||
chatMessage(
|
||||
'a2',
|
||||
'assistant',
|
||||
parts: [
|
||||
{'type': 'text', 'text': 'Edited again.'},
|
||||
],
|
||||
),
|
||||
]),
|
||||
'chat_turn_diffs': {
|
||||
'session_id': 's',
|
||||
'turn_diffs': [_summary('turn_a', 'a1')],
|
||||
},
|
||||
};
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
home: Scaffold(
|
||||
body: ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
final chips = find.byKey(const Key('chat-turn-diff-chip'));
|
||||
expect(chips, findsOneWidget);
|
||||
expect(find.text('1 file changed +1 −0'), findsOneWidget);
|
||||
|
||||
// Another session's push is ignored; this session's push adds a chip.
|
||||
gateway.emitRaw({
|
||||
'type': 'chat_turn_diffs',
|
||||
'chat_turn_diffs': {
|
||||
'session_id': 'other',
|
||||
'turn_diffs': [_summary('turn_x', 'a2')],
|
||||
},
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
expect(chips, findsOneWidget);
|
||||
gateway.emitRaw({
|
||||
'type': 'chat_turn_diffs',
|
||||
'chat_turn_diffs': {
|
||||
'session_id': 's',
|
||||
'turn_diffs': [_summary('turn_b', 'a2')],
|
||||
},
|
||||
});
|
||||
await tester.pumpAndSettle();
|
||||
expect(chips, findsNWidgets(2));
|
||||
|
||||
await tester.tap(chips.last);
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.asked, [
|
||||
{'session_id': 's', 'turn_id': 'turn_b'},
|
||||
]);
|
||||
expect(find.text('lib/turn_b.dart'), findsOneWidget);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_turn_diffs.dart';
|
||||
|
||||
void main() {
|
||||
test('summaries apply only for their session and attach by message id', () {
|
||||
final diffs = ChatTurnDiffs();
|
||||
expect(
|
||||
diffs.apply({
|
||||
'session_id': 'other',
|
||||
'turn_diffs': [
|
||||
{'turn_id': 't0', 'files': 1},
|
||||
],
|
||||
}, 'ses_1'),
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
diffs.apply({
|
||||
'session_id': 'ses_1',
|
||||
'turn_diffs': [
|
||||
{
|
||||
'turn_id': 't1',
|
||||
'message_ids': ['m2'],
|
||||
'files': 1,
|
||||
'additions': 3,
|
||||
'deletions': 1,
|
||||
},
|
||||
{'turn_id': 'empty', 'files': 0},
|
||||
],
|
||||
}, 'ses_1'),
|
||||
isTrue,
|
||||
);
|
||||
final rows = [
|
||||
{
|
||||
'kind': 'assistant',
|
||||
'messageIds': ['m1'],
|
||||
},
|
||||
{'kind': 'user'},
|
||||
{
|
||||
'kind': 'assistant',
|
||||
'messageIds': ['m2', 'm3'],
|
||||
},
|
||||
];
|
||||
final attached = attachChatTurnDiffs(rows, diffs);
|
||||
expect(attached[0].containsKey('turnDiff'), isFalse);
|
||||
expect((attached[2]['turnDiff'] as Map)['turn_id'], 't1');
|
||||
expect(diffs.summaries, hasLength(1));
|
||||
expect(chatTurnDiffLabel(diffs.summaries.single), '1 file changed +3 −1');
|
||||
});
|
||||
|
||||
test('a summary without a held message id falls back to its time span', () {
|
||||
final diffs = ChatTurnDiffs()
|
||||
..apply({
|
||||
'session_id': 's',
|
||||
'turn_diffs': [
|
||||
{
|
||||
'turn_id': 't',
|
||||
'message_ids': ['gone'],
|
||||
'started_at': 1000,
|
||||
'ended_at': 2000,
|
||||
'files': 2,
|
||||
},
|
||||
],
|
||||
}, 's');
|
||||
final rows = [
|
||||
{
|
||||
'kind': 'assistant',
|
||||
'messageIds': ['a'],
|
||||
'created_at': 500,
|
||||
},
|
||||
{
|
||||
'kind': 'assistant',
|
||||
'messageIds': ['b'],
|
||||
'created_at': 1500,
|
||||
},
|
||||
];
|
||||
final attached = attachChatTurnDiffs(rows, diffs);
|
||||
expect(attached[0].containsKey('turnDiff'), isFalse);
|
||||
expect(attached[1].containsKey('turnDiff'), isTrue);
|
||||
// A summary for a turn not held at all draws nothing.
|
||||
expect(
|
||||
attachChatTurnDiffs([
|
||||
{
|
||||
'kind': 'assistant',
|
||||
'messageIds': ['x'],
|
||||
'created_at': 9000,
|
||||
},
|
||||
], diffs).single.containsKey('turnDiff'),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user