100 lines
2.7 KiB
Dart
100 lines
2.7 KiB
Dart
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.',
|
|
);
|
|
});
|
|
}
|