import 'dart:async'; 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'; 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, 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(MaterialApp(home: Scaffold(body: 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()); testWidgets('detail at 390 with 2x text', (tester) async { gateway.detail = runFixture(steps: 12); await controller.select(1); await pumpDetail(tester, controller, gateway, size: const Size(390, 1200)); expect(tester.takeException(), isNull); expect(find.text('Nightly build'), findsOneWidget); expect(find.textContaining('Original prompt'), findsNothing); // The title is the top bar's; the status kicker leads the body below // it, and the body does not repeat the title. expect(find.byKey(const Key('run-status-kicker')), findsOneWidget); expect( tester.getTopLeft(find.byKey(const Key('run-status-kicker'))).dy, greaterThan(tester.getBottomLeft(find.text('Nightly build')).dy), ); // Input and Output are sticky collapsibles. expect( find.byKey(const ValueKey('run-detail-summary-input')), findsOneWidget, ); Future tapTab(String label) async { final tab = find.byKey(ValueKey('run-detail-tab-${label.toLowerCase()}')); await tester.ensureVisible(tab); await tester.pumpAndSettle(); await tester.tap(tab); 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); // Output starts open: its value is there to scroll to, without a tap. await tester.scrollUntilVisible( find.text('ok=true'), 200, scrollable: find .descendant( of: find.byKey(const Key('run-detail-tab-body-summary')), matching: find.byType(Scrollable), ) .first, ); 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 1').last, 200, scrollable: timelineScroll, ); expect(find.textContaining('build · Visit 1').last, findsOneWidget); await tester.scrollUntilVisible( find.textContaining('test · Visit 1').last, 200, scrollable: timelineScroll, ); expect(find.textContaining('test · Visit 1').last, 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('loading and error stay honest', (tester) async { gateway.hold = Completer(); final pending = controller.select(1); await pumpDetail(tester, controller, gateway, settle: false); expect(find.text('Loading run details…'), findsOneWidget); expect(find.byType(CircularProgressIndicator), 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, controller, gateway, onBack: () => backs++); expect(find.textContaining('server unavailable'), findsOneWidget); await tester.tap(find.text('Back to list')); expect(backs, 1); }); }