merge(app): land in-app video preview

This commit is contained in:
2026-10-23 14:42:30 +01:00
9 changed files with 345 additions and 17 deletions
@@ -8,7 +8,11 @@ enum ArtifactPreviewKind {
text,
markdown,
/// A recorded file the viewer cannot draw (video, CSV, archives, ...).
/// A same-origin video, played in the viewer from its resolved url (never
/// fetched as bytes, never autoplayed), as the web viewer does.
video,
/// A recorded file the viewer cannot draw (CSV, archives, ...).
unsupported,
/// No safe same-origin url to fetch: absent, unsafe or another origin.
@@ -52,9 +56,9 @@ const String artifactPreviewExternalUrl =
/// Classifies [artifact] against the configured server origin.
///
/// Images are drawn from the resolved bytes; `text/plain` is selectable text; a
/// `.md`/`.markdown` name or `text/markdown` is handed to the injected markdown
/// builder. Everything else is explicitly unsupported rather than faked.
/// Images are drawn from the resolved bytes; a video plays from the resolved
/// url; `text/plain` is selectable text; a `.md`/`.markdown` name or
/// `text/markdown` is handed to the injected markdown builder. Everything else is explicitly unsupported rather than faked.
ArtifactPreviewPlan planArtifactPreview({
required ArtifactRecord artifact,
required String configuredEndpoint,
@@ -89,6 +93,13 @@ ArtifactPreviewPlan planArtifactPreview({
message: '',
);
}
if (isVideoContentType(mime)) {
return ArtifactPreviewPlan(
kind: ArtifactPreviewKind.video,
resolved: resolved,
message: '',
);
}
if (mime == 'text/markdown' || _isMarkdownName(artifact.name)) {
return ArtifactPreviewPlan(
kind: ArtifactPreviewKind.markdown,
@@ -0,0 +1,145 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import '../../../ui/adaptive/host.dart';
/// Creates the player for one video url; tests inject a fake controller.
typedef ArtifactVideoControllerFactory =
VideoPlayerController Function(Uri uri);
VideoPlayerController defaultArtifactVideoController(Uri uri) =>
VideoPlayerController.networkUrl(uri);
const String artifactVideoPlayLabel = 'Play video';
const String artifactVideoPauseLabel = 'Pause video';
const String artifactVideoError = 'The video could not be played.';
/// Plays one recorded same-origin video in the viewer, as the web viewer's
/// `<video controls>`: it never autoplays, and the operator starts it.
class ArtifactVideoPlayer extends StatefulWidget {
const ArtifactVideoPlayer({
super.key,
required this.uri,
this.controllerFactory = defaultArtifactVideoController,
});
final Uri uri;
final ArtifactVideoControllerFactory controllerFactory;
@override
State<ArtifactVideoPlayer> createState() => _ArtifactVideoPlayerState();
}
class _ArtifactVideoPlayerState extends State<ArtifactVideoPlayer> {
late VideoPlayerController _controller;
bool _failed = false;
@override
void initState() {
super.initState();
_open();
}
@override
void didUpdateWidget(covariant ArtifactVideoPlayer oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.uri != widget.uri) {
_controller.dispose();
_open();
}
}
void _open() {
_failed = false;
final controller = widget.controllerFactory(widget.uri);
_controller = controller;
controller.initialize().then(
(_) {
if (mounted && identical(controller, _controller)) setState(() {});
},
onError: (Object _) {
if (mounted && identical(controller, _controller)) {
setState(() => _failed = true);
}
},
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _toggle() {
final playing = _controller.value.isPlaying;
playing ? _controller.pause() : _controller.play();
}
@override
Widget build(BuildContext context) {
if (_failed) return const Center(child: Text(artifactVideoError));
final cupertino = usesCupertinoHost(context);
return ValueListenableBuilder<VideoPlayerValue>(
valueListenable: _controller,
builder: (context, value, _) {
if (value.hasError) {
return const Center(child: Text(artifactVideoError));
}
if (!value.isInitialized) {
return Center(
child: cupertino
? const CupertinoActivityIndicator()
: const CircularProgressIndicator(),
);
}
final label = value.isPlaying
? artifactVideoPauseLabel
: artifactVideoPlayLabel;
final icon = cupertino
? (value.isPlaying
? CupertinoIcons.pause_fill
: CupertinoIcons.play_fill)
: (value.isPlaying ? Icons.pause : Icons.play_arrow);
return Column(
key: const Key('artifact-video'),
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: AspectRatio(
aspectRatio: value.aspectRatio,
child: VideoPlayer(_controller),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
child: Row(
children: [
cupertino
? CupertinoButton(
key: const Key('artifact-video-toggle'),
onPressed: _toggle,
child: Icon(icon, semanticLabel: label),
)
: IconButton(
key: const Key('artifact-video-toggle'),
tooltip: label,
onPressed: _toggle,
icon: Icon(icon),
),
Expanded(
child: VideoProgressIndicator(
_controller,
allowScrubbing: true,
),
),
],
),
),
],
);
},
);
}
}
@@ -3,6 +3,7 @@ import 'dart:typed_data';
import 'package:flutter/material.dart';
import '../domain/artifact_preview.dart';
import 'artifact_video_player.dart';
/// Renders fetched markdown text for the viewer. It takes the build context so
/// a themed markdown renderer can be supplied; the viewer's own default stays
@@ -16,7 +17,8 @@ Widget defaultArtifactMarkdown(BuildContext context, String text) =>
/// The viewer's preview surface for one page.
///
/// It draws only what was actually fetched: image bytes as an [Image], text as
/// selectable text, markdown through [markdownBuilder], and an explicit message
/// selectable text, markdown through [markdownBuilder], a same-origin video
/// through [ArtifactVideoPlayer] (streamed, never autoplayed), and an explicit message
/// for unsupported/unavailable pages. It never substitutes a placeholder that
/// looks like a preview.
class ArtifactPreviewBody extends StatelessWidget {
@@ -28,6 +30,7 @@ class ArtifactPreviewBody extends StatelessWidget {
this.text,
this.bytes,
this.error,
this.videoControllerFactory = defaultArtifactVideoController,
});
final ArtifactPreviewPlan plan;
@@ -37,6 +40,9 @@ class ArtifactPreviewBody extends StatelessWidget {
final Uint8List? bytes;
final String? error;
/// The video player's controller source; tests inject a fake.
final ArtifactVideoControllerFactory videoControllerFactory;
@override
Widget build(BuildContext context) {
if (plan.kind == ArtifactPreviewKind.unavailable ||
@@ -65,6 +71,11 @@ class ArtifactPreviewBody extends StatelessWidget {
return _PreviewPadding(child: SelectableText(text ?? ''));
case ArtifactPreviewKind.markdown:
return _PreviewPadding(child: markdownBuilder(context, text ?? ''));
case ArtifactPreviewKind.video:
return ArtifactVideoPlayer(
uri: plan.resolved!.uri,
controllerFactory: videoControllerFactory,
);
case ArtifactPreviewKind.unsupported:
case ArtifactPreviewKind.unavailable:
return const SizedBox.shrink();
+56
View File
@@ -57,6 +57,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.7"
csslib:
dependency: transitive
description:
name: csslib
sha256: "09bad715f418841f976c77db72d5398dc1253c21fb9c0c7f0b0b985860b2d58e"
url: "https://pub.dev"
source: hosted
version: "1.0.2"
cupertino_icons:
dependency: "direct main"
description:
@@ -112,6 +120,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
html:
dependency: transitive
description:
name: html
sha256: "43b67b8f43321ab066817dfac5619596c98bb1b61624e77203bb4351785f9699"
url: "https://pub.dev"
source: hosted
version: "0.15.7"
jni:
dependency: transitive
description:
@@ -365,6 +381,46 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.0"
video_player:
dependency: "direct main"
description:
name: video_player
sha256: "07ae1e03f2d4c8e2e9a75ee678f90acbc4506d4458a9c628512d119a1eee5754"
url: "https://pub.dev"
source: hosted
version: "2.14.1"
video_player_android:
dependency: transitive
description:
name: video_player_android
sha256: d27054dea34d748a44f06d433a57005d2aac69944485b0abbaed3303dbcfa3f1
url: "https://pub.dev"
source: hosted
version: "2.12.2"
video_player_avfoundation:
dependency: transitive
description:
name: video_player_avfoundation
sha256: "4f8a845173377ab250a50f9b9dfe4a43500cc76a7c49dd8410d02d8fc29dfd8b"
url: "https://pub.dev"
source: hosted
version: "2.12.1"
video_player_platform_interface:
dependency: transitive
description:
name: video_player_platform_interface
sha256: "92c0fbabe20c788e71fd10d26cea998d0d253282e65d145aed0818731cf593ce"
url: "https://pub.dev"
source: hosted
version: "6.9.0"
video_player_web:
dependency: transitive
description:
name: video_player_web
sha256: "9f3c00be2ef9b76a95d94ac5119fb843dca6f2c69e6c9968f6f2b6c9e7afbdeb"
url: "https://pub.dev"
source: hosted
version: "2.4.0"
vm_service:
dependency: transitive
description:
+1
View File
@@ -39,6 +39,7 @@ dependencies:
flutter_web_plugins:
sdk: flutter
web: ^1.1.1
video_player: ^2.14.1
dev_dependencies:
flutter_test:
@@ -86,7 +86,6 @@ void main() {
test('unsupported types are explicit instead of a fake preview', () {
for (final record in <ArtifactRecord>[
_record('result.csv', contentType: 'text/csv'),
_record('clip.mp4', contentType: 'video/mp4'),
_record('bundle.zip', contentType: 'application/zip'),
]) {
final plan = _plan(record);
@@ -96,4 +95,12 @@ void main() {
expect(plan.resolved, isNotNull);
}
});
test('a same-origin video plays in the viewer without a byte fetch', () {
final plan = _plan(_record('clip.mp4', contentType: 'video/mp4'));
expect(plan.kind, ArtifactPreviewKind.video);
expect(plan.canFetch, isFalse);
expect(plan.message, isEmpty);
expect(plan.resolved, isNotNull);
});
}
@@ -0,0 +1,71 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_preview.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_url.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_video_player.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_content.dart';
import 'package:video_player/video_player.dart';
/// A controller whose platform work is scripted: [initialize] completes or
/// fails only when the test says so, and nothing reaches a video plugin.
class _ScriptedController extends VideoPlayerController {
_ScriptedController(super.uri) : super.networkUrl();
final ready = Completer<void>();
int plays = 0;
@override
Future<void> initialize() => ready.future;
@override
Future<void> play() async => plays++;
}
Widget _body(ArtifactVideoControllerFactory factory) => MaterialApp(
home: Scaffold(
body: ArtifactPreviewBody(
plan: ArtifactPreviewPlan(
kind: ArtifactPreviewKind.video,
resolved: ResolvedArtifactUrl(
uri: Uri.parse('http://127.0.0.1:8080/artifacts/7/clip.mp4'),
external: false,
),
message: '',
),
loading: false,
markdownBuilder: defaultArtifactMarkdown,
videoControllerFactory: factory,
),
),
);
void main() {
testWidgets('a video preview streams its resolved url and never autoplays', (
tester,
) async {
final opened = <Uri>[];
late _ScriptedController controller;
await tester.pumpWidget(
_body((uri) {
opened.add(uri);
return controller = _ScriptedController(uri);
}),
);
expect(find.byType(ArtifactVideoPlayer), findsOneWidget);
expect(opened.single.path, '/artifacts/7/clip.mp4');
expect(find.byType(CircularProgressIndicator), findsOneWidget);
expect(controller.plays, 0);
});
testWidgets('a video that cannot be opened says so', (tester) async {
late _ScriptedController controller;
await tester.pumpWidget(
_body((uri) => controller = _ScriptedController(uri)),
);
controller.ready.completeError(StateError('unsupported codec'));
await tester.pump();
expect(find.text(artifactVideoError), findsOneWidget);
});
}
@@ -5,6 +5,7 @@ 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';
@@ -300,7 +301,7 @@ void main() {
expect(find.byType(Image), findsNothing);
});
testWidgets('an unsupported type is explicit, with Open and Copy link', (
testWidgets('a video plays from its url, with Open and Copy link', (
tester,
) async {
final video = recordedArtifact(
@@ -320,14 +321,12 @@ void main() {
},
);
expect(
find.text('This file type cannot be previewed here.'),
findsOneWidget,
);
// 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);
expect(find.byType(Image), findsNothing);
});
testWidgets('paging clamps at both ends and keeps the visit facts', (
+32 -5
View File
@@ -93,6 +93,23 @@ const probePng = () => {
const png = probePng();
const probeCsv = Buffer.from('id,name\n1,alpha\n2,beta\n');
// A real one-second H.264 clip, so the in-app player is exercised against a
// playable file (ffmpeg is required for this probe).
async function probeMp4() {
const dir = await mkdtemp(join(tmpdir(), 'gallery-mp4-'));
const file = join(dir, 'clip.mp4');
const ffmpeg = spawn('ffmpeg', [
'-loglevel', 'error', '-f', 'lavfi', '-i', 'testsrc=size=320x180:rate=10',
'-t', '1', '-pix_fmt', 'yuv420p', '-c:v', 'libx264', '-movflags', '+faststart', file,
]);
const [code] = await once(ffmpeg, 'exit');
if (code !== 0) throw new Error('ffmpeg could not create the probe video');
const bytes = await readFile(file);
await rm(dir, { recursive: true, force: true });
return bytes;
}
const mp4 = await probeMp4();
const server = createServer(async (req, res) => {
const pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname);
if (pathname.startsWith('/fixture/')) {
@@ -114,6 +131,11 @@ const server = createServer(async (req, res) => {
res.end(png);
return;
}
if (pathname.endsWith('.mp4')) {
res.writeHead(200, { 'content-type': 'video/mp4', 'content-length': mp4.length });
res.end(mp4);
return;
}
res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' });
res.end(`recorded artifact ${pathname}\n`);
return;
@@ -566,18 +588,23 @@ try {
if (!videoBody.includes('4 of 4 · video/mp4')) {
throw new Error(`Viewer did not page to the video: ${JSON.stringify(videoBody)}`);
}
if (!videoBody.includes('This file type cannot be previewed here.')) {
throw new Error(`Video was not marked unpresentable: ${JSON.stringify(videoBody)}`);
// The video plays in-app: its player loads the real clip and offers Play.
for (let i = 0; i < 20 && !(await hasLabel('Play video')); i++) await delay(250);
if (!(await hasLabel('Play video'))) {
throw new Error(`Video did not load in the player: ${JSON.stringify(await bodyText())}`);
}
if ((await bodyText()).includes('The video could not be played.')) {
throw new Error('The playable probe clip was reported as unplayable');
}
if (!(await hasLabel('Open')) || !(await hasLabel('Copy link'))) {
throw new Error(`Unsupported page lost its actions: ${JSON.stringify(await labels())}`);
throw new Error(`Video page lost its actions: ${JSON.stringify(await labels())}`);
}
await capture('a1w-viewer-video');
await clickExact('Next artifact');
if (!(await bodyText()).includes('4 of 4 · video/mp4')) {
throw new Error('Next past the last artifact changed the page');
}
evidence.checks.push({ check: 'unsupported types are explicit with Open/Copy and paging clamps', value: true });
await capture('a1w-viewer-unsupported');
evidence.checks.push({ check: 'a video plays in-app with Open/Copy and paging clamps', value: true });
await clickExact('Previous artifact');
await clickExact('Previous artifact');