Files
pipelines/app/test/features/artifacts/domain/artifact_preview_test.dart
T

116 lines
3.8 KiB
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_record.dart';
import 'artifact_fixtures.dart';
const _endpoint = 'ws://127.0.0.1:8081/connect';
ArtifactRecord _record(String name, {String? url, String contentType = ''}) =>
ArtifactRecord.fromJson(artifact(name, url: url, contentType: contentType));
ArtifactPreviewPlan _plan(
ArtifactRecord artifact, {
String endpoint = _endpoint,
}) => planArtifactPreview(artifact: artifact, configuredEndpoint: endpoint);
void main() {
test('an image on the configured origin is fetched locally', () {
final plan = _plan(_record('shot.png', contentType: 'image/png'));
expect(plan.kind, ArtifactPreviewKind.image);
expect(plan.canFetch, isTrue);
expect(plan.resolved?.url, 'http://127.0.0.1:8081/artifacts/1/1/shot.png');
expect(plan.message, isEmpty);
});
test('text/plain and markdown are selectable text plans', () {
expect(
_plan(_record('build.log', contentType: 'text/plain')).kind,
ArtifactPreviewKind.text,
);
expect(
_plan(_record('notes.txt', contentType: 'text/plain')).kind,
ArtifactPreviewKind.text,
);
expect(
_plan(_record('notes.md', contentType: 'text/markdown')).kind,
ArtifactPreviewKind.markdown,
);
expect(
_plan(
_record('notes.markdown', contentType: 'application/octet-stream'),
).kind,
ArtifactPreviewKind.markdown,
);
});
test('an external origin is marked and never fetchable', () {
final plan = _plan(
_record(
'shot.png',
url: 'https://elsewhere.test/shot.png',
contentType: 'image/png',
),
);
expect(plan.kind, ArtifactPreviewKind.unavailable);
expect(plan.canFetch, isFalse);
expect(plan.message, artifactPreviewExternalUrl);
expect(plan.resolved?.url, 'https://elsewhere.test/shot.png');
});
test('unsafe and absent urls are explicit and never fetchable', () {
final unsafe = _plan(
_record(
'shot.png',
url: '//evil.test/shot.png',
contentType: 'image/png',
),
);
expect(unsafe.kind, ArtifactPreviewKind.unavailable);
expect(unsafe.canFetch, isFalse);
expect(unsafe.message, artifactPreviewUnsafeUrl);
final absent = _plan(
ArtifactRecord.fromJson(<String, dynamic>{
'name': 'shot.png',
'url': '',
'content_type': 'image/png',
'size_bytes': 1,
}),
);
expect(absent.kind, ArtifactPreviewKind.unavailable);
expect(absent.canFetch, isFalse);
expect(absent.message, artifactPreviewNoUrl);
});
test('unsupported types are explicit instead of a fake preview', () {
for (final record in <ArtifactRecord>[
_record('bundle.zip', contentType: 'application/zip'),
]) {
final plan = _plan(record);
expect(plan.kind, ArtifactPreviewKind.unsupported);
expect(plan.canFetch, isFalse);
expect(plan.message, 'This file type cannot be previewed here.');
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);
});
test('CSV and TSV are tables, with their own delimiter', () {
final csv = _plan(_record('result.csv', contentType: 'text/csv'));
expect(csv.kind, ArtifactPreviewKind.table);
expect(csv.canFetch, isTrue);
expect(csv.delimiter, ',');
final tsv = _plan(_record('rows.tsv', contentType: 'text/plain'));
expect(tsv.kind, ArtifactPreviewKind.table);
expect(tsv.delimiter, '\t');
});
}