import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_groups.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_external_open.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_gallery_page.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_visit_list.dart'; import '../domain/artifact_fixtures.dart'; import 'artifact_visit_list_fixture.dart'; const _endpoint = 'ws://127.0.0.1:8081/connect'; /// A real 1x1 PNG so the image preview decodes instead of only holding bytes. final Uint8List _png = base64Decode( 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAE' 'hQGAhKmMIQAAAABJRU5ErkJggg==', ); ArtifactGroup _group( List artifacts, { String stageKey = 'review', int visit = 1, }) => visitGroup(stageKey: stageKey, visit: visit, artifacts: artifacts); Future _pumpViewer( WidgetTester tester, { required List groups, required String selected, bool cupertino = false, ArtifactFetch? fetch, Widget Function(String)? markdownBuilder, ArtifactExternalOpen? openExternal, }) async { final page = ArtifactViewerPage( groups: groups, selectedIdentity: selected, configuredEndpoint: _endpoint, fetch: fetch ?? (_) async => ArtifactFetchResult(status: 200, bytes: _png), markdownBuilder: markdownBuilder ?? (value) => SelectableText(value), openExternal: openExternal ?? (_) async => true, ); await tester.pumpWidget( cupertino ? CupertinoApp(home: page) : MaterialApp(home: Scaffold(body: page)), ); await tester.pumpAndSettle(); } TextButton _textButton(WidgetTester tester, String label) { return tester.widget( find .ancestor(of: find.text(label), matching: find.byType(TextButton)) .first, ); } void main() { testWidgets('an image is previewed in-app without opening a tab', ( tester, ) async { final fetched = []; final opened = []; await tester.pumpWidget( MaterialApp( home: ArtifactGalleryPage( steps: [ step( stage: 'review', visit: 1, artifacts: [ artifact( 'shot.png', url: '/artifacts/1/1/shot.png', contentType: 'image/png', sizeBytes: 1024, ), ], ), ], configuredEndpoint: _endpoint, fetch: (uri) async { fetched.add(uri); return ArtifactFetchResult(status: 200, bytes: _png); }, openExternal: (uri) async { opened.add(uri); return true; }, ), ), ); await tester.pumpAndSettle(); await tester.tap(find.text('shot.png')); await tester.pumpAndSettle(); expect(find.byType(ArtifactViewerPage), findsOneWidget); expect(fetched, [ Uri.parse('http://127.0.0.1:8081/artifacts/1/1/shot.png'), ]); expect(find.byType(Image), findsOneWidget); expect(find.text('1 of 1 · image/png'), findsOneWidget); expect(find.text('image/png · 1.0\u00a0KiB'), findsOneWidget); expect(opened, isEmpty); expect(find.byType(AlertDialog), findsNothing); }); testWidgets('text/plain is fetched and shown as selectable text', ( tester, ) async { final text = recordedArtifact( 'build.log', url: '/artifacts/1/1/build.log', contentType: 'text/plain; charset=utf-8', ); await _pumpViewer( tester, groups: [ _group([text]), ], selected: artifactVisitIdentity(_group([text]), text), fetch: (_) async => ArtifactFetchResult(status: 200, bytes: utf8.encode('build ok\n')), ); expect(find.byType(SelectableText), findsOneWidget); expect(find.text('build ok\n'), findsOneWidget); expect(find.text('text/plain · 0\u00a0B'), findsOneWidget); }); testWidgets('markdown uses the injected builder and defaults to text', ( tester, ) async { final markdown = recordedArtifact( 'notes.md', url: '/artifacts/1/1/notes.md', contentType: 'text/markdown', ); final group = _group([markdown]); final identity = artifactVisitIdentity(group, markdown); await _pumpViewer( tester, groups: [group], selected: identity, fetch: (_) async => ArtifactFetchResult(status: 200, bytes: utf8.encode('# Title')), markdownBuilder: (value) => Text('MARKDOWN $value'), ); expect(find.text('MARKDOWN # Title'), findsOneWidget); await tester.pumpWidget(const SizedBox()); await _pumpViewer( tester, groups: [group], selected: identity, fetch: (_) async => ArtifactFetchResult(status: 200, bytes: utf8.encode('# Raw title')), ); expect(find.byType(SelectableText), findsOneWidget); expect(find.text('# Raw title'), findsOneWidget); }); testWidgets('an external origin is marked and never fetched', (tester) async { final fetched = []; final opened = []; final image = recordedArtifact( 'shot.png', url: 'https://elsewhere.test/shot.png', contentType: 'image/png', ); final group = _group([image]); await _pumpViewer( tester, groups: [group], selected: artifactVisitIdentity(group, image), fetch: (uri) async { fetched.add(uri); return ArtifactFetchResult(status: 200, bytes: _png); }, openExternal: (uri) async { opened.add(uri); return true; }, ); expect( find.text( 'This artifact is hosted on another origin and is not previewed here.', ), findsOneWidget, ); expect(fetched, isEmpty); expect(find.byType(Image), findsNothing); await tester.tap(find.text('Open')); await tester.pumpAndSettle(); expect(opened, [Uri.parse('https://elsewhere.test/shot.png')]); }); testWidgets('the gallery marks an external viewer artifact without fetching', ( tester, ) async { final fetched = []; await tester.pumpWidget( MaterialApp( home: ArtifactGalleryPage( steps: [ step( stage: 'review', visit: 1, artifacts: [ artifact( 'shot.png', url: 'https://elsewhere.test/shot.png', contentType: 'image/png', sizeBytes: 10, ), ], ), ], configuredEndpoint: _endpoint, fetch: (uri) async { fetched.add(uri); return ArtifactFetchResult(status: 200, bytes: _png); }, openExternal: (_) async => true, ), ), ); await tester.pumpAndSettle(); await tester.tap(find.text('shot.png')); await tester.pumpAndSettle(); expect(find.byType(ArtifactViewerPage), findsOneWidget); expect( find.text( 'This artifact is hosted on another origin and is not previewed here.', ), findsOneWidget, ); expect(fetched, isEmpty); expect(find.byType(Image), findsNothing); }); testWidgets('an unsupported type is explicit, with Open and Copy link', ( tester, ) async { final video = recordedArtifact( 'clip.mp4', url: '/artifacts/1/1/clip.mp4', contentType: 'video/mp4', ); final group = _group([video]); var fetches = 0; await _pumpViewer( tester, groups: [group], selected: artifactVisitIdentity(group, video), fetch: (_) async { fetches += 1; return ArtifactFetchResult(status: 200, bytes: _png); }, ); expect( find.text('This file type cannot be previewed here.'), findsOneWidget, ); expect(find.text('Open'), findsOneWidget); expect(find.text('Copy link'), findsOneWidget); expect(fetches, 0); expect(find.byType(Image), findsNothing); }); testWidgets('paging clamps at both ends and keeps the visit facts', ( tester, ) async { final first = recordedArtifact( 'a.png', url: '/artifacts/1/1/a.png', contentType: 'image/png', ); final second = recordedArtifact( 'b.png', url: '/artifacts/1/2/b.png', contentType: 'image/png', ); final third = recordedArtifact( 'c.png', url: '/artifacts/1/3/c.png', contentType: 'image/png', ); final groups = [ _group([first], visit: 1), _group([second], visit: 2), _group([third], visit: 3), ]; await _pumpViewer( tester, groups: groups, selected: artifactVisitIdentity(groups[0], first), ); expect(find.text('1 of 3 · image/png'), findsOneWidget); expect(_textButton(tester, 'Previous artifact').onPressed, isNull); expect(_textButton(tester, 'Next artifact').onPressed, isNotNull); await tester.tap(find.text('Next artifact')); await tester.pumpAndSettle(); expect(find.text('2 of 3 · image/png'), findsOneWidget); expect(find.text('review · visit 2'), findsOneWidget); expect(_textButton(tester, 'Previous artifact').onPressed, isNotNull); await tester.tap(find.text('Next artifact')); await tester.pumpAndSettle(); expect(find.text('3 of 3 · image/png'), findsOneWidget); expect(_textButton(tester, 'Next artifact').onPressed, isNull); await tester.tap(find.text('Next artifact')); await tester.pumpAndSettle(); expect(find.text('3 of 3 · image/png'), findsOneWidget); await tester.tap(find.text('Previous artifact')); await tester.pumpAndSettle(); expect(find.text('2 of 3 · image/png'), findsOneWidget); }); testWidgets('loading and HTTP failure are honest', (tester) async { final text = recordedArtifact( 'notes.txt', url: '/artifacts/1/1/notes.txt', contentType: 'text/plain', ); final group = _group([text]); final pending = Completer(); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ArtifactViewerPage( groups: [group], selectedIdentity: artifactVisitIdentity(group, text), configuredEndpoint: _endpoint, fetch: (_) => pending.future, openExternal: (_) async => true, ), ), ), ); await tester.pump(); expect(find.text('Loading preview…'), findsOneWidget); pending.complete(ArtifactFetchResult(status: 404, bytes: Uint8List(0))); await tester.pumpAndSettle(); expect(find.text('The server answered 404.'), findsOneWidget); expect(find.byType(SelectableText), findsNothing); }); testWidgets('a network failure is reported instead of an empty preview', ( tester, ) async { final text = recordedArtifact( 'notes.txt', url: '/artifacts/1/1/notes.txt', contentType: 'text/plain', ); final group = _group([text]); await _pumpViewer( tester, groups: [group], selected: artifactVisitIdentity(group, text), fetch: (_) async => throw const ArtifactFetchException('network down'), ); expect(find.textContaining('Could not load preview'), findsOneWidget); expect(find.textContaining('network down'), findsOneWidget); }); testWidgets('Cupertino draws the same text preview and controls', ( tester, ) async { final text = recordedArtifact( 'notes.txt', url: '/artifacts/1/1/notes.txt', contentType: 'text/plain', ); final group = _group([text]); await _pumpViewer( tester, cupertino: true, groups: [group], selected: artifactVisitIdentity(group, text), fetch: (_) async => ArtifactFetchResult( status: 200, bytes: utf8.encode('cupertino text'), ), ); expect(find.text('cupertino text'), findsOneWidget); expect(find.byType(CupertinoButton), findsWidgets); }); }