import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/runs/data/runs_repository.dart'; import 'package:pipelines_app/features/runs/ui/run_detail_view.dart'; import 'package:pipelines_app/features/runs/ui/runs_controller.dart'; enum AppPlatform { material, cupertino } class FakeGateway implements ApiGateway { FakeGateway(this.detail); JsonMap detail; Object? failure; Completer? hold; @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) { if (type != 'get_run') throw UnsupportedError(type); if (hold != null) return hold!.future; if (failure != null) return Future.error(failure!); return Future.value({'run_detail': detail}); } } const prompt = ' keep\n\nspacing '; JsonMap runFixture({int steps = 2}) { final run = { 'id': 7, 'title': 'Nightly build', 'name': 'release', 'status': 'succeeded', 'input': { 'prompt': prompt, 'nested': { 'alpha': 1, 'items': [1, 2], }, }, 'output': { 'result': {'ok': true}, }, 'steps': [ for (var i = 0; i < steps; i++) { 'stage': i == 0 ? 'build' : 'test', 'sequence': i + 3, 'status': 'completed', 'outcome': 'success', 'target': '#success', }, ], }; return run; } Future pumpDetail( WidgetTester tester, AppPlatform platform, RunsController controller, FakeGateway gateway, { VoidCallback? onBack, bool settle = true, Size size = const Size(390, 1600), }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final view = RunDetailView( controller: controller, gateway: gateway, onBack: onBack ?? () {}, ); final page = MediaQuery( data: MediaQueryData.fromView( tester.view, ).copyWith(textScaler: const TextScaler.linear(2)), child: view, ); await tester.pumpWidget( platform == AppPlatform.material ? MaterialApp(home: Scaffold(body: page)) : CupertinoApp( home: CupertinoPageScaffold(child: SafeArea(child: page)), ), ); if (settle) { await tester.pumpAndSettle(); } else { await tester.pump(); } } void mockClipboard( WidgetTester tester, List copied, { bool Function()? denied, }) { tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, (call) async { if (call.method != 'Clipboard.setData') return null; if (denied?.call() ?? false) { throw PlatformException(code: 'clipboard-denied'); } copied.add((call.arguments as Map)['text'] as String); return null; }, ); addTearDown( () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, null, ), ); } void main() { late FakeGateway gateway; late RunsController controller; setUp(() { gateway = FakeGateway(runFixture()); controller = RunsController(RunsRepository(gateway)); }); tearDown(() => controller.dispose()); for (final platform in AppPlatform.values) { testWidgets('${platform.name} detail at 390 with 2x text', (tester) async { gateway.detail = runFixture(steps: 12); await controller.select(1); await pumpDetail( tester, platform, controller, gateway, size: const Size(390, 1200), ); expect(tester.takeException(), isNull); expect(find.text('Nightly build'), findsOneWidget); expect(find.textContaining('Original prompt'), findsNothing); // Both hosts lead with the web's status kicker above the title. expect(find.byKey(const Key('run-status-kicker')), findsOneWidget); expect( tester.getTopLeft(find.byKey(const Key('run-status-kicker'))).dy, lessThan(tester.getTopLeft(find.text('Nightly build')).dy), ); // Input and Output are sticky collapsibles on both hosts. expect( find.byKey(const ValueKey('run-detail-summary-input')), findsOneWidget, ); if (platform == AppPlatform.cupertino) { expect(find.byType(Material), findsNothing); expect(find.byType(Chip), findsNothing); } Future tapTab(String label) async { await tester.ensureVisible(find.text(label)); await tester.pumpAndSettle(); await tester.tap(find.text(label)); await tester.pumpAndSettle(); } Future openSummarySection(String label) async { final summaryScroll = find.descendant( of: find.byKey(const Key('run-detail-tab-body-summary')), matching: find.byType(Scrollable), ); await tester.scrollUntilVisible( find.text(label), 200, scrollable: summaryScroll.first, ); await tester.pumpAndSettle(); await tester.tap(find.text(label)); await tester.pumpAndSettle(); } await openSummarySection('Input'); // Each field reads under its own name as data, never as JSON. expect(find.text('nested'), findsOneWidget); expect(find.text('alpha=1, items=1, 2'), findsOneWidget); expect(find.textContaining('"alpha"'), findsNothing); await openSummarySection('Output'); expect(find.text('ok=true'), findsOneWidget); await tapTab('Timeline'); final timelineScroll = find.descendant( of: find.byKey(const Key('run-detail-tab-body-timeline')), matching: find.byType(Scrollable), ); await tester.scrollUntilVisible( find.textContaining('build · visit 3'), 200, scrollable: timelineScroll, ); expect(find.textContaining('build · visit 3'), findsOneWidget); await tester.scrollUntilVisible( find.textContaining('test · visit 4'), 200, scrollable: timelineScroll, ); expect(find.textContaining('test · visit 4'), findsOneWidget); final before = tester .state(timelineScroll) .position .pixels; expect(before, greaterThan(0)); gateway.detail = runFixture(steps: 13); await controller.select(1); await tester.pumpAndSettle(); expect( tester.state(timelineScroll).position.pixels, closeTo(before, 0.1), ); }); testWidgets('${platform.name} loading and error stay honest', ( tester, ) async { gateway.hold = Completer(); final pending = controller.select(1); await pumpDetail(tester, platform, controller, gateway, settle: false); expect(find.text('Loading run details…'), findsOneWidget); expect( find.byType( platform == AppPlatform.material ? CircularProgressIndicator : CupertinoActivityIndicator, ), findsOneWidget, ); gateway.hold!.complete({'run_detail': runFixture()}); await pending; await tester.pumpAndSettle(); expect(find.text('Nightly build'), findsOneWidget); controller.dispose(); gateway = FakeGateway(runFixture()) ..failure = StateError('server unavailable'); controller = RunsController(RunsRepository(gateway)); await controller.select(1); var backs = 0; await pumpDetail( tester, platform, controller, gateway, onBack: () => backs++, ); expect(find.textContaining('server unavailable'), findsOneWidget); await tester.tap(find.text('Back to list')); expect(backs, 1); }); } }