feat(app): download and share artifacts as named files
This commit is contained in:
@@ -0,0 +1,403 @@
|
||||
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/actions/artifact_file.dart';
|
||||
import 'package:pipelines_app/features/artifacts/actions/artifact_file_actions.dart';
|
||||
import 'package:pipelines_app/features/artifacts/actions/artifact_file_bridge.dart';
|
||||
import 'package:pipelines_app/features/artifacts/actions/artifact_transfer.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/domain/artifact_url.dart';
|
||||
import 'package:pipelines_app/features/artifacts/ui/artifact_gallery_page.dart';
|
||||
import 'package:pipelines_app/features/artifacts/ui/artifact_visit_list.dart';
|
||||
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart';
|
||||
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart';
|
||||
|
||||
import '../domain/artifact_fixtures.dart';
|
||||
import '../ui/artifact_visit_list_fixture.dart';
|
||||
|
||||
const _endpoint = 'ws://127.0.0.1:8081/connect';
|
||||
final Uint8List _bytes = Uint8List.fromList(<int>[7, 8, 9]);
|
||||
|
||||
final ArtifactRecord _record = ArtifactRecord.fromJson(<String, dynamic>{
|
||||
'name': 'report.csv',
|
||||
'url': '/artifacts/1/1/report.csv',
|
||||
'content_type': 'text/csv',
|
||||
'size_bytes': 3,
|
||||
});
|
||||
|
||||
ArtifactFileBridge _bridge({
|
||||
List<ArtifactFile>? saved,
|
||||
List<ArtifactFile>? shared,
|
||||
bool downloadSupported = true,
|
||||
bool shareSupported = true,
|
||||
}) => ArtifactFileBridge(
|
||||
downloadSupported: downloadSupported,
|
||||
shareSupported: shareSupported,
|
||||
downloadUnsupportedMessage: 'No download on this host.',
|
||||
shareUnsupportedMessage: 'No share on this host.',
|
||||
download: (file) async {
|
||||
saved?.add(file);
|
||||
return ArtifactTransferResult(
|
||||
ArtifactTransferState.saved,
|
||||
'Downloaded ${file.name}.',
|
||||
);
|
||||
},
|
||||
share: (file) async {
|
||||
shared?.add(file);
|
||||
return const ArtifactTransferResult(
|
||||
ArtifactTransferState.shared,
|
||||
'File shared.',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Future<ArtifactFetchResult> _fetch(Uri uri) async =>
|
||||
ArtifactFetchResult(status: 200, bytes: _bytes);
|
||||
|
||||
Widget _row({
|
||||
required bool cupertino,
|
||||
ArtifactFileBridge? bridge,
|
||||
ArtifactRecord? artifact,
|
||||
ArtifactFetch? fetch,
|
||||
}) {
|
||||
final actions = ArtifactFileActions(
|
||||
artifact: artifact ?? _record,
|
||||
resolved: resolveArtifactUrl(_endpoint, _record.url)!,
|
||||
fetch: fetch ?? _fetch,
|
||||
bridge: bridge ?? _bridge(),
|
||||
);
|
||||
return cupertino
|
||||
? CupertinoApp(
|
||||
home: CupertinoPageScaffold(
|
||||
child: SafeArea(
|
||||
child: Padding(padding: const EdgeInsets.all(8), child: actions),
|
||||
),
|
||||
),
|
||||
)
|
||||
: MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Padding(padding: const EdgeInsets.all(8), child: actions),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
TextButton _materialButton(WidgetTester tester, String label) =>
|
||||
tester.widget<TextButton>(
|
||||
find
|
||||
.ancestor(of: find.text(label), matching: find.byType(TextButton))
|
||||
.first,
|
||||
);
|
||||
|
||||
CupertinoButton _cupertinoButton(WidgetTester tester, String label) =>
|
||||
tester.widget<CupertinoButton>(
|
||||
find
|
||||
.ancestor(
|
||||
of: find.text(label),
|
||||
matching: find.byType(CupertinoButton),
|
||||
)
|
||||
.first,
|
||||
);
|
||||
|
||||
void main() {
|
||||
testWidgets('Download saves the exact named bytes and reports it', (
|
||||
tester,
|
||||
) async {
|
||||
final saved = <ArtifactFile>[];
|
||||
await tester.pumpWidget(
|
||||
_row(cupertino: false, bridge: _bridge(saved: saved)),
|
||||
);
|
||||
|
||||
await tester.tap(find.text('Download'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(saved, hasLength(1));
|
||||
expect(saved.single.name, 'report.csv');
|
||||
expect(saved.single.mimeType, 'text/csv');
|
||||
expect(saved.single.bytes, _bytes);
|
||||
expect(find.text('Downloaded report.csv.'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Share starts one file transfer, not a link', (tester) async {
|
||||
final shared = <ArtifactFile>[];
|
||||
await tester.pumpWidget(
|
||||
_row(cupertino: false, bridge: _bridge(shared: shared)),
|
||||
);
|
||||
|
||||
await tester.tap(find.text('Share'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(shared.single.name, 'report.csv');
|
||||
expect(shared.single.byteLength, 3);
|
||||
expect(find.text('File shared.'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('unsupported share is explained but download stays enabled', (
|
||||
tester,
|
||||
) async {
|
||||
final saved = <ArtifactFile>[];
|
||||
await tester.pumpWidget(
|
||||
_row(
|
||||
cupertino: false,
|
||||
bridge: _bridge(saved: saved, shareSupported: false),
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.text('No share on this host.'), findsOneWidget);
|
||||
expect(_materialButton(tester, 'Share').onPressed, isNull);
|
||||
expect(_materialButton(tester, 'Download').onPressed, isNotNull);
|
||||
|
||||
await tester.tap(find.text('Download'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(saved, hasLength(1));
|
||||
});
|
||||
|
||||
testWidgets('a slow fetch shows progress and cancel, then reports cancel', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
_row(
|
||||
cupertino: false,
|
||||
bridge: _bridge(),
|
||||
fetch: (_) => Future<ArtifactFetchResult>.delayed(
|
||||
const Duration(seconds: 30),
|
||||
() => ArtifactFetchResult(status: 200, bytes: _bytes),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.text('Download'));
|
||||
await tester.pump();
|
||||
expect(find.text('Preparing file…'), findsOneWidget);
|
||||
expect(find.byType(CircularProgressIndicator), findsOneWidget);
|
||||
expect(_materialButton(tester, 'Download').onPressed, isNull);
|
||||
expect(find.text('Cancel'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Cancel'));
|
||||
await tester.pump();
|
||||
expect(find.text('Download canceled.'), findsOneWidget);
|
||||
expect(find.text('Cancel'), findsNothing);
|
||||
await tester.pump(const Duration(seconds: 31));
|
||||
});
|
||||
|
||||
testWidgets('both hosts keep the file actions whole at 320/2x dark', (
|
||||
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);
|
||||
|
||||
for (final cupertino in <bool>[false, true]) {
|
||||
await tester.pumpWidget(_row(cupertino: cupertino));
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(find.text('Download'), findsOneWidget);
|
||||
expect(find.text('Share'), findsOneWidget);
|
||||
if (cupertino) {
|
||||
expect(_cupertinoButton(tester, 'Download').onPressed, isNotNull);
|
||||
expect(find.byType(TextButton), findsNothing);
|
||||
} else {
|
||||
expect(_materialButton(tester, 'Download').onPressed, isNotNull);
|
||||
expect(find.byType(CupertinoButton), findsNothing);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('the gallery selection offers both file actions in its dialog', (
|
||||
tester,
|
||||
) async {
|
||||
final saved = <ArtifactFile>[];
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: ArtifactGalleryPage(
|
||||
steps: <JsonMap>[
|
||||
step(
|
||||
stage: 'review',
|
||||
visit: 1,
|
||||
artifacts: <JsonMap>[
|
||||
artifact('report.csv', contentType: 'text/csv', sizeBytes: 3),
|
||||
],
|
||||
),
|
||||
],
|
||||
configuredEndpoint: _endpoint,
|
||||
fetch: _fetch,
|
||||
fileBridge: _bridge(saved: saved),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.text('report.csv'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(find.text('Download'), findsOneWidget);
|
||||
expect(find.text('Share'), findsOneWidget);
|
||||
expect(find.text('Open'), findsOneWidget);
|
||||
expect(find.text('Copy link'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Download'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(saved.single.bytes, _bytes);
|
||||
expect(find.text('Downloaded report.csv.'), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('a host without file capabilities explains and disables both', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: ArtifactFileActions(
|
||||
artifact: _record,
|
||||
resolved: resolveArtifactUrl(_endpoint, _record.url)!,
|
||||
fetch: _fetch,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.text('Download'), findsOneWidget);
|
||||
expect(find.text('Share'), findsOneWidget);
|
||||
expect(_materialButton(tester, 'Download').onPressed, isNull);
|
||||
expect(_materialButton(tester, 'Share').onPressed, isNull);
|
||||
expect(
|
||||
find.text('Downloading files is not supported on this platform yet.'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.text('File sharing is not supported on this platform yet.'),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('gallery selection keeps both actions at 320/2x dark', (
|
||||
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);
|
||||
|
||||
final steps = <JsonMap>[
|
||||
step(
|
||||
stage: 'review',
|
||||
visit: 1,
|
||||
artifacts: <JsonMap>[
|
||||
artifact('report.csv', contentType: 'text/csv', sizeBytes: 3),
|
||||
],
|
||||
),
|
||||
];
|
||||
for (final cupertino in <bool>[false, true]) {
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(
|
||||
theme: const CupertinoThemeData(brightness: Brightness.dark),
|
||||
home: ArtifactGalleryPage(
|
||||
steps: steps,
|
||||
configuredEndpoint: _endpoint,
|
||||
fetch: _fetch,
|
||||
fileBridge: _bridge(),
|
||||
),
|
||||
)
|
||||
: MaterialApp(
|
||||
theme: ThemeData(brightness: Brightness.dark),
|
||||
home: ArtifactGalleryPage(
|
||||
steps: steps,
|
||||
configuredEndpoint: _endpoint,
|
||||
fetch: _fetch,
|
||||
fileBridge: _bridge(),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('report.csv'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(find.text('Download'), findsOneWidget);
|
||||
expect(find.text('Share'), findsOneWidget);
|
||||
expect(
|
||||
cupertino
|
||||
? find.byType(CupertinoAlertDialog)
|
||||
: find.byType(AlertDialog),
|
||||
findsOneWidget,
|
||||
);
|
||||
await tester.tap(find.text('Close'));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'the viewer offers file actions only for a same-origin artifact',
|
||||
(tester) async {
|
||||
final local = recordedArtifact(
|
||||
'clip.mp4',
|
||||
url: '/artifacts/1/1/clip.mp4',
|
||||
contentType: 'video/mp4',
|
||||
);
|
||||
final external = recordedArtifact(
|
||||
'elsewhere.png',
|
||||
url: 'https://elsewhere.test/elsewhere.png',
|
||||
contentType: 'image/png',
|
||||
);
|
||||
final localGroup = visitGroup(artifacts: <ArtifactRecord>[local]);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ArtifactViewerPage(
|
||||
key: const ValueKey<String>('local'),
|
||||
groups: <ArtifactGroup>[localGroup],
|
||||
selectedIdentity: artifactVisitIdentity(localGroup, local),
|
||||
configuredEndpoint: _endpoint,
|
||||
fetch: _fetch,
|
||||
fileBridge: _bridge(),
|
||||
openExternal: (_) async => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Download'), findsOneWidget);
|
||||
expect(find.text('Share'), findsOneWidget);
|
||||
|
||||
final externalGroup = visitGroup(artifacts: <ArtifactRecord>[external]);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ArtifactViewerPage(
|
||||
key: const ValueKey<String>('external'),
|
||||
groups: <ArtifactGroup>[externalGroup],
|
||||
selectedIdentity: artifactVisitIdentity(externalGroup, external),
|
||||
configuredEndpoint: _endpoint,
|
||||
fetch: _fetch,
|
||||
fileBridge: _bridge(),
|
||||
openExternal: (_) async => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Download'), findsNothing);
|
||||
expect(find.text('Share'), findsNothing);
|
||||
expect(find.text('Open'), findsOneWidget);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/artifacts/actions/artifact_file.dart';
|
||||
import 'package:pipelines_app/features/artifacts/actions/artifact_file_bridge.dart';
|
||||
import 'package:pipelines_app/features/artifacts/actions/artifact_file_controller.dart';
|
||||
import 'package:pipelines_app/features/artifacts/actions/artifact_transfer.dart';
|
||||
import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart';
|
||||
import 'package:pipelines_app/features/artifacts/domain/artifact_url.dart';
|
||||
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart';
|
||||
|
||||
const _url = 'http://127.0.0.1:8081/artifacts/1/1/report.csv';
|
||||
final Uint8List _bytes = Uint8List.fromList(<int>[10, 20, 30, 40]);
|
||||
|
||||
final ArtifactRecord _record = ArtifactRecord.fromJson(<String, dynamic>{
|
||||
'name': 'public/reports/report.csv',
|
||||
'url': '/artifacts/1/1/report.csv',
|
||||
'content_type': 'text/csv; charset=utf-8',
|
||||
'size_bytes': 4,
|
||||
});
|
||||
|
||||
ArtifactFileActionsController _controller({
|
||||
ArtifactFetch? fetch,
|
||||
ArtifactFileBridge bridge = const ArtifactFileBridge(),
|
||||
}) => ArtifactFileActionsController(
|
||||
artifact: _record,
|
||||
resolved: resolveArtifactUrl('ws://127.0.0.1:8081/connect', _record.url)!,
|
||||
fetch: fetch ?? (_) async => ArtifactFetchResult(status: 200, bytes: _bytes),
|
||||
bridge: bridge,
|
||||
);
|
||||
|
||||
ArtifactFileBridge _bridge({
|
||||
List<ArtifactFile>? saved,
|
||||
List<ArtifactFile>? shared,
|
||||
ArtifactTransferResult shareResult = const ArtifactTransferResult(
|
||||
ArtifactTransferState.shared,
|
||||
'File shared.',
|
||||
),
|
||||
bool downloadSupported = true,
|
||||
bool shareSupported = true,
|
||||
}) => ArtifactFileBridge(
|
||||
downloadSupported: downloadSupported,
|
||||
shareSupported: shareSupported,
|
||||
downloadUnsupportedMessage: 'No download here.',
|
||||
shareUnsupportedMessage: 'No share here.',
|
||||
download: (file) async {
|
||||
saved?.add(file);
|
||||
return ArtifactTransferResult(
|
||||
ArtifactTransferState.saved,
|
||||
'Downloaded ${file.name}.',
|
||||
);
|
||||
},
|
||||
share: (file) async {
|
||||
shared?.add(file);
|
||||
return shareResult;
|
||||
},
|
||||
);
|
||||
|
||||
void main() {
|
||||
test('download carries the exact named, typed bytes', () async {
|
||||
final saved = <ArtifactFile>[];
|
||||
final controller = _controller(bridge: _bridge(saved: saved));
|
||||
|
||||
await controller.download();
|
||||
|
||||
expect(saved, hasLength(1));
|
||||
expect(saved.single.name, 'report.csv');
|
||||
expect(saved.single.mimeType, 'text/csv');
|
||||
expect(saved.single.bytes, _bytes);
|
||||
expect(controller.status, 'Downloaded report.csv.');
|
||||
expect(controller.busy, isFalse);
|
||||
});
|
||||
|
||||
test('share uses a file, not a link, and reports when shared', () async {
|
||||
final shared = <ArtifactFile>[];
|
||||
final fetched = <Uri>[];
|
||||
final controller = _controller(
|
||||
bridge: _bridge(shared: shared),
|
||||
fetch: (uri) async {
|
||||
fetched.add(uri);
|
||||
return ArtifactFetchResult(status: 200, bytes: _bytes);
|
||||
},
|
||||
);
|
||||
|
||||
await controller.share();
|
||||
|
||||
expect(fetched, [Uri.parse(_url)]);
|
||||
expect(shared, hasLength(1));
|
||||
expect(shared.single.name, 'report.csv');
|
||||
expect(shared.single.mimeType, 'text/csv');
|
||||
expect(shared.single.bytes, _bytes);
|
||||
expect(controller.canShare, isTrue);
|
||||
expect(controller.status, 'File shared.');
|
||||
});
|
||||
|
||||
test('unsupported sharing leaves download working', () async {
|
||||
final saved = <ArtifactFile>[];
|
||||
final shared = <ArtifactFile>[];
|
||||
var fetches = 0;
|
||||
final controller = _controller(
|
||||
fetch: (_) async {
|
||||
fetches += 1;
|
||||
return ArtifactFetchResult(status: 200, bytes: _bytes);
|
||||
},
|
||||
bridge: _bridge(saved: saved, shared: shared, shareSupported: false),
|
||||
);
|
||||
|
||||
await controller.share();
|
||||
expect(shared, isEmpty);
|
||||
expect(fetches, 0);
|
||||
expect(controller.status, 'No share here.');
|
||||
expect(controller.canDownload, isTrue);
|
||||
|
||||
await controller.download();
|
||||
expect(saved, hasLength(1));
|
||||
expect(controller.status, 'Downloaded report.csv.');
|
||||
});
|
||||
|
||||
test(
|
||||
'an HTTP or network fetch failure is reported, never transferred',
|
||||
() async {
|
||||
final saved = <ArtifactFile>[];
|
||||
final bridge = _bridge(saved: saved);
|
||||
final notFound = _controller(
|
||||
fetch: (_) async =>
|
||||
ArtifactFetchResult(status: 404, bytes: Uint8List(0)),
|
||||
bridge: bridge,
|
||||
);
|
||||
|
||||
await notFound.download();
|
||||
expect(notFound.status, 'Could not fetch the artifact (HTTP 404).');
|
||||
expect(saved, isEmpty);
|
||||
expect(notFound.busy, isFalse);
|
||||
|
||||
final offline = _controller(
|
||||
fetch: (_) async => throw const ArtifactFetchException('network down'),
|
||||
bridge: bridge,
|
||||
);
|
||||
await offline.download();
|
||||
expect(offline.status, contains('network down'));
|
||||
expect(saved, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'cancel during a slow fetch starts no transfer and ignores the result',
|
||||
() async {
|
||||
final pending = Completer<ArtifactFetchResult>();
|
||||
final saved = <ArtifactFile>[];
|
||||
final controller = _controller(
|
||||
fetch: (_) => pending.future,
|
||||
bridge: _bridge(saved: saved),
|
||||
);
|
||||
|
||||
final action = controller.download();
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(controller.busy, isTrue);
|
||||
expect(controller.canCancel, isTrue);
|
||||
expect(controller.status, 'Preparing file…');
|
||||
|
||||
controller.cancel();
|
||||
expect(controller.busy, isFalse);
|
||||
expect(controller.status, 'Download canceled.');
|
||||
|
||||
pending.complete(ArtifactFetchResult(status: 200, bytes: _bytes));
|
||||
await action;
|
||||
expect(saved, isEmpty);
|
||||
expect(controller.status, 'Download canceled.');
|
||||
},
|
||||
);
|
||||
|
||||
test('one tap starts one transfer even when called twice', () async {
|
||||
var fetches = 0;
|
||||
final pending = Completer<ArtifactFetchResult>();
|
||||
final saved = <ArtifactFile>[];
|
||||
final controller = _controller(
|
||||
fetch: (_) {
|
||||
fetches += 1;
|
||||
return pending.future;
|
||||
},
|
||||
bridge: _bridge(saved: saved),
|
||||
);
|
||||
|
||||
final first = controller.download();
|
||||
final second = controller.download();
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(fetches, 1);
|
||||
|
||||
pending.complete(ArtifactFetchResult(status: 200, bytes: _bytes));
|
||||
await first;
|
||||
await second;
|
||||
expect(saved, hasLength(1));
|
||||
});
|
||||
|
||||
test('a ready share reuses prepared bytes on the fresh tap', () async {
|
||||
var fetches = 0;
|
||||
var shares = 0;
|
||||
final controller = _controller(
|
||||
fetch: (_) async {
|
||||
fetches += 1;
|
||||
return ArtifactFetchResult(status: 200, bytes: _bytes);
|
||||
},
|
||||
bridge: ArtifactFileBridge(
|
||||
shareSupported: true,
|
||||
share: (file) async {
|
||||
shares += 1;
|
||||
return shares == 1
|
||||
? const ArtifactTransferResult(
|
||||
ArtifactTransferState.ready,
|
||||
'File ready. Tap Share again to choose where to send it.',
|
||||
)
|
||||
: const ArtifactTransferResult(
|
||||
ArtifactTransferState.shared,
|
||||
'File shared.',
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
await controller.share();
|
||||
expect(controller.status, contains('Tap Share again'));
|
||||
await controller.share();
|
||||
|
||||
expect(fetches, 1);
|
||||
expect(shares, 2);
|
||||
expect(controller.status, 'File shared.');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/artifacts/actions/artifact_file.dart';
|
||||
import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart';
|
||||
|
||||
ArtifactRecord _record(
|
||||
String name, {
|
||||
String contentType = '',
|
||||
String url = '/artifacts/1/1/report.csv',
|
||||
}) => ArtifactRecord.fromJson(<String, dynamic>{
|
||||
'name': name,
|
||||
'url': url,
|
||||
'content_type': contentType,
|
||||
'size_bytes': 10,
|
||||
});
|
||||
|
||||
void main() {
|
||||
test('a recorded name keeps only its basename', () {
|
||||
final file = artifactFileFor(
|
||||
artifact: _record('public/reports/result.csv', contentType: 'text/csv'),
|
||||
url: Uri.parse('http://127.0.0.1:8081/artifacts/1/1/result.csv'),
|
||||
bytes: Uint8List.fromList(<int>[1, 2, 3]),
|
||||
);
|
||||
|
||||
expect(file.name, 'result.csv');
|
||||
expect(file.mimeType, 'text/csv');
|
||||
expect(file.byteLength, 3);
|
||||
expect(file.bytes, <int>[1, 2, 3]);
|
||||
});
|
||||
|
||||
test(
|
||||
'a recorded name with backslashes and control characters is repaired',
|
||||
() {
|
||||
final name = artifactFileName(
|
||||
_record('C:\\tmp\\odd\u0001name.csv'),
|
||||
Uri.parse('http://127.0.0.1/a.csv'),
|
||||
);
|
||||
|
||||
expect(name, 'odd_name.csv');
|
||||
},
|
||||
);
|
||||
|
||||
test('a missing name falls back to the url basename then artifact', () {
|
||||
expect(
|
||||
artifactFileName(
|
||||
_record(''),
|
||||
Uri.parse('http://127.0.0.1:8081/artifacts/1/1/from-url.csv'),
|
||||
),
|
||||
'from-url.csv',
|
||||
);
|
||||
expect(
|
||||
artifactFileName(_record('public/'), Uri.parse('http://127.0.0.1/')),
|
||||
'artifact',
|
||||
);
|
||||
expect(
|
||||
artifactFileName(_record('..'), Uri.parse('http://127.0.0.1/')),
|
||||
'artifact',
|
||||
);
|
||||
});
|
||||
|
||||
test('the recorded media type loses parameters and is validated', () {
|
||||
expect(
|
||||
artifactFileMimeType(
|
||||
_record('a.txt', contentType: 'Text/Plain; charset=utf-8'),
|
||||
),
|
||||
'text/plain',
|
||||
);
|
||||
expect(
|
||||
artifactFileMimeType(_record('a.bin')),
|
||||
artifactFileFallbackMimeType,
|
||||
);
|
||||
expect(
|
||||
artifactFileMimeType(_record('a.bin', contentType: 'not a mime')),
|
||||
artifactFileFallbackMimeType,
|
||||
);
|
||||
});
|
||||
|
||||
test('a missing name or MIME type gets an explicit fallback notice', () {
|
||||
final url = Uri.parse('http://127.0.0.1/artifacts/1/1/kept.csv');
|
||||
|
||||
expect(
|
||||
artifactFileRecordingNotice(
|
||||
_record('kept.csv', contentType: 'text/csv'),
|
||||
url,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
artifactFileRecordingNotice(_record('', contentType: 'text/csv'), url),
|
||||
'No usable recorded name; the file is saved as kept.csv.',
|
||||
);
|
||||
expect(
|
||||
artifactFileRecordingNotice(_record('kept.csv'), url),
|
||||
'No usable recorded MIME type; the file is saved as '
|
||||
'application/octet-stream.',
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user