Files
pipelines/app/test/features/artifacts/ui/artifact_viewer_page_test.dart
T

620 lines
20 KiB
Dart

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_viewer_body.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<ArtifactRecord> artifacts, {
String stageKey = 'review',
int visit = 1,
}) => visitGroup(stageKey: stageKey, visit: visit, artifacts: artifacts);
Future<void> _pumpViewer(
WidgetTester tester, {
required List<ArtifactGroup> 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();
}
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<void> _openMarkdownFromGallery(
WidgetTester tester, {
required bool cupertino,
String name = 'notes.md',
String contentType = 'text/markdown',
}) async {
final page = ArtifactGalleryPage(
steps: <JsonMap>[
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();
}
/// The recorded facts sit behind the viewer's Details button.
Future<void> showFacts(WidgetTester tester) async {
await tester.tap(find.byKey(const Key('artifact-info')));
await tester.pumpAndSettle();
}
Future<void> hideFacts(WidgetTester tester) async {
await tester.tapAt(const Offset(5, 5));
await tester.pumpAndSettle();
}
void main() {
testWidgets('an image is previewed in-app without opening a tab', (
tester,
) async {
final fetched = <Uri>[];
final opened = <Uri>[];
await tester.pumpWidget(
MaterialApp(
home: ArtifactGalleryPage(
steps: <JsonMap>[
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);
// The gallery's thumbnail and the viewer read the same recorded file.
expect(fetched, isNotEmpty);
expect(fetched.toSet(), {
Uri.parse('http://127.0.0.1:8081/artifacts/1/1/shot.png'),
});
expect(find.byType(Image), findsOneWidget);
// The page itself does not scroll; the image zooms inside its frame.
expect(find.byKey(const Key('artifact-image-zoom')), findsOneWidget);
expect(
find.ancestor(of: find.byType(Image), matching: find.byType(Scrollable)),
findsNothing,
);
expect(find.text('1 of 1 · image/png'), findsOneWidget);
await showFacts(tester);
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);
await showFacts(tester);
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 = <Uri>[];
final opened = <Uri>[];
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.byKey(const Key('artifact-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 = <Uri>[];
await tester.pumpWidget(
MaterialApp(
home: ArtifactGalleryPage(
steps: <JsonMap>[
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.byKey(const Key('artifact-open')), findsOneWidget);
expect(find.byKey(const Key('artifact-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(
tester.widget<IconButton>(find.byKey(artifactPreviousKey)).onPressed,
isNull,
);
expect(
tester.widget<IconButton>(find.byKey(artifactNextKey)).onPressed,
isNotNull,
);
await tester.tap(find.byKey(artifactNextKey));
await tester.pumpAndSettle();
expect(find.text('2 of 3 · image/png'), findsOneWidget);
await showFacts(tester);
expect(find.text('review · visit 2'), findsOneWidget);
await hideFacts(tester);
expect(
tester.widget<IconButton>(find.byKey(artifactPreviousKey)).onPressed,
isNotNull,
);
await tester.tap(find.byKey(artifactNextKey));
await tester.pumpAndSettle();
expect(find.text('3 of 3 · image/png'), findsOneWidget);
expect(
tester.widget<IconButton>(find.byKey(artifactNextKey)).onPressed,
isNull,
);
await tester.tap(find.byKey(artifactNextKey));
await tester.pumpAndSettle();
expect(find.text('3 of 3 · image/png'), findsOneWidget);
await tester.tap(find.byKey(artifactPreviousKey));
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<ArtifactFetchResult>();
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 position stays on the top edge; the facts are one tap away.
expect(find.text('1 of 1 · text/markdown'), findsOneWidget);
await showFacts(tester);
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);
await showFacts(tester);
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('a swipe moves to the next and previous artifact', (
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 groups = [
_group([first], visit: 1),
_group([second], visit: 2),
];
await _pumpViewer(
tester,
groups: groups,
selected: artifactVisitIdentity(groups[0], first),
);
final swipe = find.byKey(const Key('artifact-viewer-swipe'));
await tester.fling(swipe, const Offset(-300, 0), 1000);
await tester.pumpAndSettle();
expect(find.textContaining('2 of 2'), findsWidgets);
await tester.fling(swipe, const Offset(300, 0), 1000);
await tester.pumpAndSettle();
expect(find.textContaining('1 of 2'), findsWidgets);
});
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 chevrons are whole and on screen at the bottom right.
for (final (label, key) in [
('Previous artifact', artifactPreviousKey),
('Next artifact', artifactNextKey),
]) {
final rect = tester.getRect(find.byKey(key));
expect(rect.left, greaterThanOrEqualTo(0), reason: label);
expect(rect.right, lessThanOrEqualTo(320), reason: label);
expect(rect.width, greaterThan(0), reason: label);
}
});
}