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/features/artifacts/ui/artifact_video_player.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 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.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(BuildContext, String)? markdownBuilder, ArtifactExternalOpen? openExternal, }) async { final page = ArtifactViewerPage( groups: groups, selectedIdentity: selected, configuredEndpoint: _endpoint, fetch: fetch ?? (_) async => ArtifactFetchResult(status: 200, bytes: _png), markdownBuilder: markdownBuilder ?? (context, 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, ); } const String _markdownBody = '# Report\n' '\n' 'A paragraph with **bold** and `code`.\n' '\n' '| Step | State |\n' '| --- | --- |\n' '| build | ok |\n' '\n' '```dart\n' 'final n = 1;\n' '```'; /// Opens the gallery's markdown row so the default builder composes the real /// viewer exactly as the run detail does. Future _openMarkdownFromGallery( WidgetTester tester, { required bool cupertino, String name = 'notes.md', String contentType = 'text/markdown', }) async { final page = ArtifactGalleryPage( steps: [ step( stage: 'review', visit: 1, artifacts: [ artifact( name, url: '/artifacts/1/1/$name', contentType: contentType, sizeBytes: 4096, ), ], ), ], configuredEndpoint: _endpoint, fetch: (_) async => ArtifactFetchResult(status: 200, bytes: utf8.encode(_markdownBody)), ); await tester.pumpWidget( cupertino ? CupertinoApp(home: page) : MaterialApp(home: page), ); await tester.pumpAndSettle(); await tester.tap(find.text(name)); await tester.pumpAndSettle(); } 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: (context, 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('a video plays from its url, 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); }, ); // The player streams the url itself; the viewer never fetches its bytes. expect(find.byType(ArtifactVideoPlayer), findsOneWidget); expect(find.text('This file type cannot be previewed here.'), findsNothing); expect(find.text('Open'), findsOneWidget); expect(find.text('Copy link'), findsOneWidget); expect(fetches, 0); }); 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); }); testWidgets('the gallery previews markdown through the chat renderer', ( tester, ) async { await _openMarkdownFromGallery(tester, cupertino: false); expect(find.byType(ArtifactViewerPage), findsOneWidget); expect(find.byType(ChatMarkdownView), findsOneWidget); // The subset renders as native blocks, not raw markers. expect(find.text('Report'), findsOneWidget); expect(find.text('build'), findsOneWidget); expect(find.text('ok'), findsOneWidget); expect(find.text('dart'), findsOneWidget); expect(find.text('final n = 1;'), findsOneWidget); expect(find.text('# Report'), findsNothing); expect(find.text('A paragraph with bold and code.'), findsOneWidget); // The recorded facts stay visible above the preview. expect(find.text('1 of 1 · text/markdown'), findsOneWidget); expect(find.text('text/markdown · 4.0\u00a0KiB'), findsOneWidget); expect(find.text('review · visit 1'), findsOneWidget); }); testWidgets('a .markdown name previews without a recorded MIME', ( tester, ) async { await _openMarkdownFromGallery( tester, cupertino: false, name: 'notes.markdown', contentType: '', ); expect(find.byType(ArtifactViewerPage), findsOneWidget); expect(find.byType(ChatMarkdownView), findsOneWidget); expect(find.text('Report'), findsOneWidget); expect(find.text('dart'), findsOneWidget); expect(find.text('1 of 1 · artifact'), findsOneWidget); expect(find.text('type not recorded · 4.0\u00a0KiB'), findsOneWidget); }); testWidgets('Cupertino markdown preview is native at 320px and 2x text', ( tester, ) async { tester.view.physicalSize = const Size(640, 1200); tester.view.devicePixelRatio = 2; tester.platformDispatcher.textScaleFactorTestValue = 2; tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue); await _openMarkdownFromGallery(tester, cupertino: true); expect(tester.takeException(), isNull); expect(find.byType(ChatMarkdownView), findsOneWidget); expect(find.text('Report'), findsOneWidget); expect(find.text('dart'), findsOneWidget); expect(find.text('1 of 1 · text/markdown'), findsOneWidget); // The viewer and its markdown add no Material control to a Cupertino host. expect(find.byType(Material), findsNothing); }); testWidgets('Material paging stays inside 320px at 2x text', (tester) async { tester.view.physicalSize = const Size(640, 1200); tester.view.devicePixelRatio = 2; tester.platformDispatcher.textScaleFactorTestValue = 2; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); 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 groups = [ _group([first], visit: 1), _group([second], visit: 2), ]; await _pumpViewer( tester, groups: groups, selected: artifactVisitIdentity(groups[0], first), ); expect(tester.takeException(), isNull); // Both labelled controls are whole: a clipped Row overflowed the viewport. for (final label in ['Previous artifact', 'Next artifact']) { final rect = tester.getRect(find.text(label)); expect(rect.left, greaterThanOrEqualTo(0), reason: label); expect(rect.right, lessThanOrEqualTo(320), reason: label); expect(rect.width, greaterThan(0), reason: label); } }); }