Files
cutdex/test/media_import_test.dart
lingjhf 06f1c26681
Gitea CI / Assemble and validate CI artifacts (push) Successful in 1m47s
Gitea CI / Check code and tests (push) Successful in 7m15s
Gitea CI / Build Android APK (push) Successful in 4m6s
Gitea CI / Build Windows x64 app (push) Successful in 3m34s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m29s
Gitea CI / Gitea CI passed (push) Successful in 1s
Gitea Release / Prepare draft Release (push) Failing after 8s
Gitea Release / Upload Android to draft Release (push) Skipped
Gitea Release / Upload Windows to draft Release (push) Skipped
Gitea Release / Verify and publish Release (push) Skipped
Move editor UI into the app and release verified Core dependencies (#14)
2026-10-11 19:59:40 +00:00

331 lines
11 KiB
Dart

import 'support/creation_names.dart';
import 'dart:io';
import 'package:cutdex/workspace/media_import.dart';
import 'package:cutdex/workspace/persistence/project_workspace.dart';
import 'package:cutdex/integrations/platform/media_source_reader.dart';
import 'package:cutdex_core/project.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
late Directory root;
late ProjectWorkspace workspace;
late String projectId;
late String path;
late _ProviderReader reader;
setUp(() async {
root = await Directory.systemTemp.createTemp('cutdex-media-test-');
reader = _ProviderReader();
workspace = ProjectWorkspace(
creationNames: testCreationNames,
mediaSourceReader: reader,
);
path = '${root.path}/Project.cutdex';
await workspace.open(path, name: '媒体库');
projectId = workspace.project!.info.id;
});
test(
'provider originals are managed, deduplicated and readable after reopen',
() async {
final source = ExternalMediaFile.content(
uri: Uri.parse('content://provider/document/42'),
name: '中文 空格 #.MP4',
byteLength: 3,
);
final result = await workspace.importExternalMedia(projectId, [
source,
source,
]);
expect(result.added, 1);
expect(result.duplicates, 1);
expect(result.failures, isEmpty);
expect(reader.opens, 1);
expect(reader.closes, 1);
final asset = result.assets.single;
expect(asset.storage, AssetStorage.managed);
expect(asset.name, source.displayName);
expect(asset.metadata['kind'], 'video');
expect(mediaSourceLocation(asset), source.sourceUri.toString());
expect(
await workspace.withAssetMedia(
projectId,
asset,
(media) => File.fromUri(media.uri).readAsBytes(),
),
[1, 2, 3],
);
await workspace.close();
workspace = ProjectWorkspace(
creationNames: testCreationNames,
mediaSourceReader: reader,
);
await workspace.open(path);
final repeated = await workspace.importExternalMedia(projectId, [source]);
expect(repeated.added, 0);
expect(repeated.duplicates, 1);
expect(repeated.assets.single.id, asset.id);
expect(reader.opens, 1);
expect(
await workspace.withAssetMedia(
projectId,
repeated.assets.single,
(media) => File.fromUri(media.uri).readAsBytes(),
),
[1, 2, 3],
);
},
);
test(
'provider stream failure or size mismatch never registers a partial asset',
() async {
final source = ExternalMediaFile.content(
uri: Uri.parse('content://provider/document/42'),
name: 'a.wav',
byteLength: 4,
);
var result = await workspace.importExternalMedia(projectId, [source]);
expect(result.failures, hasLength(1));
expect(result.assets, isEmpty);
reader.fail = true;
result = await workspace.importExternalMedia(projectId, [source]);
expect(result.failures, hasLength(1));
expect(result.assets, isEmpty);
expect(reader.closes, 2);
expect(await Directory('$path/media').exists(), isFalse);
},
);
test(
'cancelling a provider read closes the stream without registering media',
() async {
var cancelled = false;
reader.afterFirstChunk = () => cancelled = true;
final result = await workspace.importExternalMedia(projectId, [
ExternalMediaFile.content(
uri: Uri.parse('content://provider/document/42'),
name: 'a.mp4',
),
], isCancelled: () => cancelled);
expect(result.failures, hasLength(1));
expect(result.assets, isEmpty);
expect(reader.closes, 1);
expect(await Directory('$path/media').exists(), isFalse);
},
);
tearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
test('video, audio and image drops persist only references across reopen', () async {
final files = <File>[];
for (final name in ['中文 空格 #.MOV', 'sound.wav', 'photo.PNG']) {
files.add(await File('${root.path}/$name').writeAsBytes([1, 2, 3]));
}
final before = await workspace.createTimeline('剪辑');
final result = await workspace.importExternalMedia(
projectId,
files.map((file) => ExternalMediaFile(path: file.path)),
);
expect(result.added, 3);
expect(result.failures, isEmpty);
expect(
result.assets.map((asset) => asset.metadata['kind']),
unorderedEquals(['video', 'audio', 'image']),
);
for (final asset in result.assets) {
expect(asset.storage, AssetStorage.external);
expect(asset.byteLength, 3);
expect(await File.fromUri(Uri.parse(asset.location)).readAsBytes(), [
1,
2,
3,
]);
expect(asset.contentHash, isNull);
}
final after = await workspace.project!.listTimelines();
expect(
after.map((timeline) => timeline.revision),
before.map((timeline) => timeline.revision),
);
expect(await Directory('$path/media').exists(), isFalse);
await workspace.close();
final reopened = await ProjectFile.open(Directory(path));
try {
expect(
(await reopened.listAssets()).map((a) => a.location),
unorderedEquals(result.assets.map((a) => a.location)),
);
// Losing a source does not delete its reference or prevent project open.
await files.first.delete();
expect(await reopened.listAssets(), hasLength(3));
} finally {
await reopened.close();
}
});
test('large sparse file is registered without copying its bytes', () async {
final file = File('${root.path}/large.mp4');
final handle = await file.open(mode: FileMode.write);
await handle.truncate(4 * 1024 * 1024 * 1024);
await handle.close();
final result = await workspace.importExternalMedia(projectId, [
ExternalMediaFile(path: file.path),
]);
expect(result.added, 1);
expect(result.assets.single.byteLength, 4 * 1024 * 1024 * 1024);
expect(await Directory('$path/media').exists(), isFalse);
});
test(
'FLV, RealMedia and TS drops persist video references with original names',
() async {
final files = <File>[];
for (final name in [
'中文 空格 [视频].flv',
'中文 空格 [视频].RMVB',
'legacy.rm',
'transport.ts',
'中文 空格 [视频].TS',
]) {
files.add(await File('${root.path}/$name').writeAsString('fixture'));
}
final result = await workspace.importExternalMedia(
projectId,
files.map((file) => ExternalMediaFile(path: file.path)),
);
expect(result.added, 5);
expect(result.failures, isEmpty);
expect(
result.assets.every((asset) => asset.metadata['kind'] == 'video'),
isTrue,
);
expect(
result.assets.map((asset) => asset.name),
unorderedEquals([
'中文 空格 [视频].flv',
'中文 空格 [视频].RMVB',
'legacy.rm',
'transport.ts',
'中文 空格 [视频].TS',
]),
);
await workspace.close();
workspace = ProjectWorkspace(creationNames: testCreationNames);
await workspace.open(path);
expect(
(await workspace.project!.listAssets()).map((asset) => asset.location),
unorderedEquals(result.assets.map((asset) => asset.location)),
);
expect(await Directory('$path/media').exists(), isFalse);
},
);
test(
'duplicates in a batch, queued drops and reopened projects reuse identity',
() async {
final file = await File('${root.path}/video.mp4')
.writeAsString('fixture');
final source = ExternalMediaFile(path: file.path);
final results = await Future.wait([
workspace.importExternalMedia(projectId, [source, source]),
workspace.importExternalMedia(projectId, [source]),
]);
expect(results.first.added, 1);
expect(results.first.duplicates, 1);
expect(results.last.added, 0);
expect(results.last.duplicates, 1);
final id = results.first.assets.single.id;
await workspace.close();
workspace = ProjectWorkspace(creationNames: testCreationNames);
await workspace.open(path);
expect(
(await workspace.importExternalMedia(projectId, [
source,
])).assets.single.id,
id,
);
},
);
test(
'symlink aliases use the same external identity',
() async {
final file = await File('${root.path}/video.mp4')
.writeAsString('fixture');
final alias = await Link('${root.path}/alias.mp4').create(file.path);
final result = await workspace.importExternalMedia(projectId, [
ExternalMediaFile(path: file.path),
ExternalMediaFile(path: alias.path),
]);
expect(result.added, 1);
expect(result.duplicates, 1);
},
skip: Platform.isWindows
? 'Symlink creation requires Windows privileges'
: false,
);
test('mixed drops report failures and still save valid files', () async {
final file = await File('${root.path}/ok.jpg').writeAsString('fixture');
final unsupported = await File('${root.path}/notes.txt')
.writeAsString('not media');
final directory = await Directory('${root.path}/folder.mp4').create();
final result = await workspace.importExternalMedia(projectId, [
ExternalMediaFile(path: '${root.path}/missing.wav'),
ExternalMediaFile(path: directory.path),
ExternalMediaFile(path: unsupported.path),
const ExternalMediaFile(path: 'relative.mp4'),
const ExternalMediaFile(path: 'https://example.com/video.mp4'),
ExternalMediaFile(path: file.path),
]);
expect(result.added, 1);
expect(result.failures, hasLength(5));
expect(result.assets.single.name, 'ok.jpg');
});
test('queued drop cannot register files into a different project', () async {
final file = await File('${root.path}/video.mp4').writeAsString('fixture');
final switchProject = workspace.open(
'${root.path}/Other.cutdex',
name: 'Other',
);
final drop = workspace.importExternalMedia(projectId, [
ExternalMediaFile(path: file.path),
]);
final imported = await drop;
expect(imported.added, 1);
await switchProject;
expect(await workspace.project!.listAssets(), isEmpty);
await workspace.open(path);
expect(
(await workspace.project!.listAssets()).single.id,
imported.assets.single.id,
);
});
}
class _ProviderReader extends MediaSourceReader {
int opens = 0;
int closes = 0;
bool fail = false;
void Function()? afterFirstChunk;
@override
Stream<List<int>> openRead(Uri uri) async* {
opens++;
try {
yield [1, 2];
afterFirstChunk?.call();
if (fail) throw const FileSystemException('Provider read failed');
yield [3];
} finally {
closes++;
}
}
}