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 input = const {}, Map? 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); }); }