import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/application/detached/detached_platform.dart'; import 'package:pipelines_app/application/detached/detached_route.dart'; import 'package:pipelines_app/application/detached/detached_windows.dart'; import 'detached_fixtures.dart'; void main() { late FakeDetachedPlatform platform; late List returned; late GlobalKey messenger; late DetachedWindows windows; setUp(() { platform = FakeDetachedPlatform(); returned = []; messenger = GlobalKey(); windows = DetachedWindows( platform: platform, onReturn: returned.add, messenger: messenger, )..start(); }); tearDown(() => windows.dispose()); Future host(WidgetTester tester) => tester.pumpWidget( MaterialApp( scaffoldMessengerKey: messenger, home: const Scaffold(body: SizedBox()), ), ); testWidgets('an opened window runs onOpened; the item leaves after', ( tester, ) async { await host(tester); var opened = 0; windows.openChat('a', title: 'Chat a', onOpened: () => opened++); await tester.pump(); expect(platform.launches.single.route, DetachedRoute.chat('a')); expect(platform.launches.single.title, 'Chat a'); expect(opened, 1); windows.openTerminal(tabKey: 's/1', title: 'Terminal: zsh'); await tester.pump(); expect(platform.launches.last.route, DetachedRoute.terminal('s/1')); }); testWidgets('a blocked popup says so and offers a tab, which opens it', ( tester, ) async { await host(tester); platform.result = DetachedLaunch.blocked; var opened = 0; windows.openChat('a', title: 'Chat a', onOpened: () => opened++); await tester.pumpAndSettle(); expect(opened, 0, reason: 'nothing leaves the main window yet'); expect(find.textContaining('blocked the new window'), findsOneWidget); await tester.tap(find.text('Open in tab')); await tester.pumpAndSettle(); expect(platform.launches.last.tab, isTrue); expect(opened, 1); }); testWidgets('a failure is reported and the item stays', (tester) async { await host(tester); platform.result = DetachedLaunch.failed; var opened = 0; windows.openChat('a', title: 'Chat a', onOpened: () => opened++); await tester.pumpAndSettle(); expect(find.text('Could not open a new window.'), findsOneWidget); expect(opened, 0); }); testWidgets('a diff is handed over under its token before it opens', ( tester, ) async { await host(tester); windows.openDiff( [ {'path': 'lib/a.dart', 'kind': 'modified'}, ], initialIndex: 0, title: 'a.dart', ); await tester.pump(); final route = platform.launches.single.route; expect(route.kind, DetachedKind.diff); final stored = jsonDecode(platform.stored[route.id]!) as Map; expect((stored['files'] as List).single['path'], 'lib/a.dart'); }); testWidgets('items a detached window gives back reach the main window', ( tester, ) async { await host(tester); const diff = DetachedReturn( DetachedKind.diff, 'tok', files: [ {'path': 'a', 'kind': 'added'}, ], initialIndex: 0, ); platform.incoming ..add(const DetachedReturn(DetachedKind.chat, 'a').toMessage()) ..add(diff.toMessage()) ..add({'type': 'something else'}); await tester.pump(); expect(returned.map((r) => (r.kind, r.id)), [ (DetachedKind.chat, 'a'), (DetachedKind.diff, 'tok'), ]); expect(returned.last.files.single['path'], 'a'); }); }