import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/detached/detached_diff.dart'; import 'package:pipelines_app/application/detached/detached_route.dart'; import 'package:pipelines_app/application/detached/detached_window_body.dart'; import 'package:pipelines_app/features/changes/changes.dart'; import 'package:pipelines_app/shell/detached/detached_window_scope.dart'; import '../../application/detached/detached_fixtures.dart'; FileChange _file(String path) => FileChange( path: path, kind: ChangeKind.modified, patch: '--- a/$path\n+++ b/$path\n@@ -0,0 +1,1 @@\n+line of $path\n', beforeMode: '100644', afterMode: '100755', ).withSteps(const ['build']); void main() { setUp(() => diffShowsAllFiles.value = false); test('a file change survives the hand-over to another window', () { final file = _file('lib/a.dart'); final back = FileChange.fromHandover( jsonDecode(jsonEncode(file.toJson())), )!; expect(back.path, file.path); expect(back.kind, file.kind); expect(back.patch, file.patch); expect(back.beforeMode, '100644'); expect(back.afterMode, '100755'); expect(back.steps, ['build']); expect(back.added, 1); }); testWidgets('a diff page offers Open in new window and closes once open', ( tester, ) async { tester.view.physicalSize = const Size(1000, 700); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final opener = FakeOpener(); final files = [_file('lib/a.dart'), _file('lib/b.dart')]; await tester.pumpWidget( DetachedWindowScope( opener: opener, child: MaterialApp( home: Builder( builder: (context) => Scaffold( body: TextButton( onPressed: () => showFileDiffViewer(context, files[1], files: files), child: const Text('open'), ), ), ), ), ), ); await tester.tap(find.text('open')); await tester.pumpAndSettle(); expect(find.byType(FileDiffPage), findsOneWidget); await tester.tap(find.byTooltip('Open in new window')); await tester.pumpAndSettle(); expect(opener.diffs.single.map((f) => f['path']), [ 'lib/a.dart', 'lib/b.dart', ]); expect(find.byType(FileDiffPage), findsNothing); }); testWidgets('without platform windows the diff page has no such button', ( tester, ) async { await tester.pumpWidget( MaterialApp(home: FileDiffPage(files: [_file('a')])), ); expect(find.byTooltip('Open in new window'), findsNothing); }); testWidgets('a detached diff shows the handed files and gives them back', ( tester, ) async { tester.view.physicalSize = const Size(1000, 820); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final platform = FakeDetachedPlatform(); platform.stored['tok'] = jsonEncode({ 'files': [_file('lib/a.dart').toJson(), _file('lib/b.dart').toJson()], 'index': 1, }); final titles = []; final back = <(DetachedKind, String, int, int)>[]; await tester.pumpWidget( MaterialApp( home: DetachedDiff( token: 'tok', platform: platform, window: DetachedWindowControls( ready: ValueNotifier(true), sync: ValueNotifier(0), setTitle: titles.add, close: () {}, bringBackItem: (kind, id, files, index) => back.add((kind, id, files.length, index)), ), ), ), ); await tester.pumpAndSettle(); expect(find.byType(FileDiffPage), findsOneWidget); expect(titles.last, '2 files changed'); expect(find.text('b.dart'), findsWidgets); // No Back in a window of its own, and no window to open from it. expect(find.byType(BackButton), findsNothing); expect(find.byTooltip('Open in new window'), findsNothing); await tester.tap(find.byTooltip('Bring back to main window')); expect(back, [(DetachedKind.diff, 'tok', 2, 1)]); }); testWidgets('a diff handed over to nobody says it is gone', (tester) async { await tester.pumpWidget( MaterialApp( home: DetachedDiff( token: 'missing', platform: FakeDetachedPlatform(), window: DetachedWindowControls( ready: ValueNotifier(true), sync: ValueNotifier(0), setTitle: (_) {}, close: () {}, bringBackItem: (_, _, _, _) {}, ), ), ), ); await tester.pumpAndSettle(); expect(find.text('This diff is no longer available.'), findsOneWidget); }); }