Files
pipelines/app/test/features/artifacts/actions/artifact_file_controller_test.dart
T

230 lines
7.1 KiB
Dart

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.');
});
}