feat: show edit and write tools as line diffs
Expanding an edit, write or patch call now renders the change as tinted diff lines instead of an old/new text blob: added lines green, removed lines red, @@ hunk headers marked, and file header noise hidden. The patch comes from the server's metadata.files[].patch; older servers fall back to a line diff of the edit's old/new strings, and a write with only content shows as added lines. The tool row also shows +N −M for these calls, as dsh-Go does, and a phone-sized preview stops at 600 lines. 0.4.12 (412).
This commit is contained in:
1 parent
0f850b6191
commit
d533bc95ef
7 files changed
+541
-6
No files matched your search
@@ -0,0 +1,129 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:openchamber/api/models.dart';
|
||||
import 'package:openchamber/widgets/diff_view.dart';
|
||||
|
||||
ContentPart editPart({
|
||||
String name = 'edit',
|
||||
Map<String, dynamic> input = const {},
|
||||
Map<String, dynamic>? metadata,
|
||||
String status = 'completed',
|
||||
String? error,
|
||||
}) {
|
||||
return ContentPart(
|
||||
type: 'tool',
|
||||
name: name,
|
||||
state: ToolState(
|
||||
status: status,
|
||||
input: input,
|
||||
metadata: metadata,
|
||||
error: error,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('an edit uses the server patch from metadata.files', () {
|
||||
final part = editPart(
|
||||
input: {'path': 'PLAN.md'},
|
||||
metadata: {
|
||||
'files': [
|
||||
{
|
||||
'file': 'PLAN.md',
|
||||
'patch': '--- PLAN.md\n'
|
||||
'+++ PLAN.md\n'
|
||||
'@@ -1,3 +1,4 @@\n'
|
||||
' same\n'
|
||||
'-old line\n'
|
||||
'+new line\n'
|
||||
'+added line\n'
|
||||
' tail\n',
|
||||
},
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
final files = toolDiffFiles(part);
|
||||
expect(files, hasLength(1));
|
||||
expect(files.single.path, 'PLAN.md');
|
||||
expect(files.single.added, 2);
|
||||
expect(files.single.removed, 1);
|
||||
expect(files.single.lines.first.kind, DiffLineKind.meta);
|
||||
expect(
|
||||
files.single.lines.where((line) => line.kind == DiffLineKind.hunk),
|
||||
hasLength(1),
|
||||
);
|
||||
expect(
|
||||
files.single.lines.map((line) => line.text),
|
||||
containsAll(['-old line', '+new line', '+added line']),
|
||||
);
|
||||
});
|
||||
|
||||
test('an edit without a patch is diffed from its old and new text', () {
|
||||
final part = editPart(input: {
|
||||
'path': '/tmp/a.txt',
|
||||
'oldString': 'one\ntwo',
|
||||
'newString': 'one\ntwo\nthree',
|
||||
});
|
||||
|
||||
final files = toolDiffFiles(part);
|
||||
expect(files, hasLength(1));
|
||||
expect(files.single.added, 1);
|
||||
expect(files.single.removed, 0);
|
||||
expect(files.single.lines.last.text, '+three');
|
||||
});
|
||||
|
||||
test('a new write without a patch shows its content as added lines', () {
|
||||
final part = editPart(
|
||||
name: 'write',
|
||||
input: {'path': 'hello.txt', 'content': 'hello\nworld'},
|
||||
);
|
||||
|
||||
final files = toolDiffFiles(part);
|
||||
expect(files, hasLength(1));
|
||||
expect(files.single.added, 2);
|
||||
expect(files.single.removed, 0);
|
||||
expect(files.single.lines.map((line) => line.text),
|
||||
['+hello', '+world']);
|
||||
});
|
||||
|
||||
test('a failed tool and a non-diff tool have no diff', () {
|
||||
expect(
|
||||
toolDiffFiles(editPart(
|
||||
metadata: {
|
||||
'files': [
|
||||
{'file': 'a', 'patch': '@@ -1 +1 @@\n-a\n+b\n'},
|
||||
],
|
||||
},
|
||||
error: 'file not found',
|
||||
)),
|
||||
isEmpty,
|
||||
);
|
||||
expect(
|
||||
toolDiffFiles(editPart(name: 'bash', input: {'command': 'ls'})),
|
||||
isEmpty,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('the diff view tints added and removed lines', (tester) async {
|
||||
final files = toolDiffFiles(editPart(
|
||||
metadata: {
|
||||
'files': [
|
||||
{
|
||||
'file': 'a.txt',
|
||||
'patch': '@@ -1,2 +1,2 @@\n old\n-removed\n+added\n',
|
||||
},
|
||||
],
|
||||
},
|
||||
));
|
||||
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: Scaffold(body: DiffView(files: files)),
|
||||
));
|
||||
|
||||
expect(find.text('@@ -1,2 +1,2 @@'), findsOneWidget);
|
||||
expect(find.text(' old'), findsOneWidget);
|
||||
expect(find.text('-removed'), findsOneWidget);
|
||||
expect(find.text('+added'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user