Files
cutdex/test/project_workspace_test.dart
T
lingjhf 2e0033ce2d
Gitea CI / Build Android APK (pull_request) Canceled after 0s
Gitea CI / Build Windows x64 app (pull_request) Canceled after 0s
Gitea CI / Audit and package Windows x64 (pull_request) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (pull_request) Canceled after 0s
Gitea CI / Gitea CI passed (pull_request) Canceled after 0s
Gitea CI / Check code and tests (pull_request) Canceled after 6m29s
feat(workspace): refresh desktop editing experience and prepare 0.1.2
2026-10-09 01:56:01 +08:00

523 lines
20 KiB
Dart

import 'support/creation_names.dart';
import 'dart:io';
import 'package:cutdex/errors/app_failure.dart';
import 'package:cutdex_sdk/editor/commands.dart';
import 'package:cutdex/workspace/persistence/project_workspace.dart';
import 'package:cutdex/workspace/background_task_registry.dart';
import 'package:cutdex/workspace/project_state.dart';
import 'package:cutdex/workspace/media_import.dart';
import 'package:cutdex_sdk/project.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test(
'recent project sessions are reused and cache eviction closes old sessions',
() async {
final root = await Directory.systemTemp.createTemp(
'cutdex-warm-projects-',
);
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
final timelines = await workspace.open(
'${root.path}/One.cutdex',
name: 'One',
);
final firstId = workspace.project!.info.id;
final document = await workspace.loadTimeline(
firstId,
timelines.single.id,
);
final session = await workspace.openTimelineSession(firstId, document);
final thumbnails = session.thumbnails;
final waveforms = session.waveforms;
await workspace.open('${root.path}/Two.cutdex', name: 'Two');
await workspace.open('${root.path}/One.cutdex', expectedId: firstId);
final restored = await workspace.openTimelineSession(firstId, document);
expect(restored, same(session));
expect(restored.thumbnails, same(thumbnails));
expect(restored.waveforms, same(waveforms));
expect(restored.acceptsEdits, isTrue);
await workspace.open('${root.path}/Two.cutdex');
await workspace.open('${root.path}/Three.cutdex', name: 'Three');
await workspace.open('${root.path}/Four.cutdex', name: 'Four');
expect(workspace.cachedProjectIds.length, 3);
expect(workspace.cachedProjectIds, isNot(contains(firstId)));
expect(session.acceptsEdits, isFalse);
expect(() => thumbnails.addListener(() {}), throwsFlutterError);
expect(() => waveforms.addListener(() {}), throwsFlutterError);
},
);
test('superseded project loading preserves the current project', () async {
final root = await Directory.systemTemp.createTemp(
'cutdex-cancel-project-',
);
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
await workspace.open('${root.path}/One.cutdex', name: 'One');
final firstId = workspace.project!.info.id;
var checks = 0;
await expectLater(
workspace.open(
'${root.path}/Two.cutdex',
name: 'Two',
isCurrent: () => ++checks < 2,
),
throwsA(isA<ProjectOpenSuperseded>()),
);
expect(workspace.project!.info.id, firstId);
});
test('default timeline is pinned and deleting a prepared timeline clears its session', () async {
final root = await Directory.systemTemp.createTemp('cutdex-timeline-menu-');
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
final path = '${root.path}/Project.cutdex';
final initial = await workspace.open(path, name: 'Project');
expect(initial.single.pinned, isTrue);
final project = RecentProject(
id: workspace.project!.info.id,
name: 'Project',
path: path,
);
final created = await workspace.createPreparedTimeline('Second');
final pinned = await workspace.setTimelinePinned(
project,
created.document.id,
true,
);
expect(pinned.map((item) => item.pinned), [isTrue, isTrue]);
final remaining = await workspace.deleteTimeline(
project,
created.document.id,
);
expect(remaining.map((item) => item.id), [initial.single.id]);
expect(
workspace.cachedTimelineSession(project.id, created.document.id),
isNull,
);
});
test('removed assets retain references and reimport restores the original record', () async {
final root = await Directory.systemTemp.createTemp('cutdex-asset-menu-');
var workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
await workspace.open('${root.path}/Project.cutdex', name: 'Project');
final project = workspace.project!;
final sourceFile = await File('${root.path}/source.mov')
.writeAsString('original');
final source = File(await sourceFile.resolveSymbolicLinks());
await project.saveAsset(
AssetDocument(
id: 'asset',
name: 'Original',
storage: AssetStorage.external,
location: source.uri.toString(),
metadata: {'kind': 'video'},
accessData: {'test': 'retained'},
),
);
final renamed = (await workspace.editAsset(
project.info.id,
'asset',
name: ' Renamed ',
)).single;
expect(renamed.name, 'Renamed');
expect(renamed.location, source.uri.toString());
expect(renamed.accessData['test'], 'retained');
await expectLater(
workspace.editAsset('other', 'asset', name: 'wrong'),
throwsA(isA<CommandRejected>().having((e) => e.code, 'code', 'closed')),
);
await project.saveCollection(
AssetCollection(
id: 'collection',
name: 'Collection',
assetIds: ['asset'],
),
);
final projectId = project.info.id;
expect(await workspace.editAsset(projectId, 'asset'), isEmpty);
expect((await project.listAssets()).single.id, 'asset');
expect(
await project.resolveAsset((await project.listAssets()).single),
source.uri,
);
await workspace.close();
workspace = ProjectWorkspace(creationNames: testCreationNames);
await workspace.open('${root.path}/Project.cutdex');
expect(await workspace.listAssets(projectId), isEmpty);
final restored = await workspace.importExternalMedia(projectId, [
ExternalMediaFile(path: source.path),
ExternalMediaFile(path: source.path),
]);
expect(restored.added, 1);
expect(restored.duplicates, 1);
expect(restored.assets.single.id, 'asset');
expect(restored.assets.single.name, 'Renamed');
expect(restored.assets.single.accessData['test'], 'retained');
expect((await workspace.project!.listAssets()).length, 1);
await workspace.project!.saveAsset(
AssetDocument(
id: 'unused',
name: 'Unused',
storage: AssetStorage.external,
location: source.uri.toString(),
),
);
final remaining = await workspace.editAsset(projectId, 'unused');
expect(remaining.map((asset) => asset.id), ['asset']);
expect(await source.readAsString(), 'original');
});
test(
'project contains multiple durable timelines and releases its lock',
() async {
final root = await Directory.systemTemp.createTemp('cutdex-app-test-');
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
final path = '${root.path}/Project.cutdex';
expect(await workspace.open(path, name: '项目'), hasLength(1));
final id = workspace.project!.info.id;
await workspace.createTimeline('时间线一');
final timelines = await workspace.createTimeline('时间线二');
expect(timelines.map((item) => item.name), [
'Timeline 1',
'时间线一',
'时间线二',
]);
final timeline = await workspace.loadTimeline(id, timelines.first.id);
expect(timeline.revision, greaterThan(0));
expect(timeline.sequences.single.name, 'Main sequence');
expect(timeline.sequences.single.elements, isEmpty);
expect(timeline.defaultSequenceId, timeline.sequences.single.id);
await workspace.close();
final reopened = await ProjectFile.open(Directory(path));
try {
expect(reopened.info.id, id);
expect(await reopened.listTimelines(), hasLength(3));
} finally {
await reopened.close();
}
},
);
test('prepared timeline is cached before it is published', () async {
final root = await Directory.systemTemp.createTemp('cutdex-prepared-');
final registry = BackgroundTaskRegistry();
final workspace = ProjectWorkspace(
creationNames: testCreationNames,
backgroundTasks: registry,
);
addTearDown(() async {
await workspace.close();
registry.dispose();
await root.delete(recursive: true);
});
await workspace.open('${root.path}/Project.cutdex', name: 'Project');
final projectId = workspace.project!.info.id;
final created = await workspace.createPreparedTimeline('Timeline 2');
expect(created.timelines.last.id, created.document.id);
expect(
workspace.cachedTimelineSession(projectId, created.document.id),
same(created.session),
);
expect(registry.entries.map((entry) => entry.timelineId), [
created.document.id,
]);
await workspace.close();
expect(registry.entries, isEmpty);
});
test(
'failed opens preserve current project and successful switch caches it',
() async {
final root = await Directory.systemTemp.createTemp('cutdex-app-test-');
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
final path = '${root.path}/One.cutdex';
await workspace.open(path, name: 'One');
final id = workspace.project!.info.id;
await expectLater(
workspace.open('${root.path}/missing'),
throwsA(isA<FileSystemException>()),
);
expect(workspace.project!.info.id, id);
await expectLater(
workspace.open(path, expectedId: 'wrong'),
throwsA(
isA<AppException>().having(
(error) => error.failure.code,
'code',
AppErrorCode.projectMismatch,
),
),
);
expect(workspace.project!.info.id, id);
await expectLater(
workspace.open(path, name: 'Overwrite'),
throwsA(
isA<AppException>().having(
(error) => error.failure.code,
'code',
AppErrorCode.projectAlreadyExists,
),
),
);
expect(workspace.project!.info.id, id);
await workspace.open('${root.path}/Two.cutdex', name: 'Two');
expect(workspace.cachedProjectIds, contains(id));
await workspace.open(path, expectedId: id);
expect(workspace.project!.info.id, id);
},
);
test(
'timeline inspection and opening the same project are serialized',
() async {
final root = await Directory.systemTemp.createTemp('cutdex-app-race-');
final creator = ProjectWorkspace(creationNames: testCreationNames);
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await creator.close();
await workspace.close();
await root.delete(recursive: true);
});
final path = '${root.path}/Project.cutdex';
await creator.open(path, name: 'Project');
await creator.createTimeline('Timeline');
final id = creator.project!.info.id;
await creator.close();
final inspected = workspace.listProjectTimelines(path, expectedId: id);
final opened = workspace.open(path, expectedId: id);
expect((await inspected).map((item) => item.name), contains('Timeline'));
expect((await opened).map((item) => item.name), contains('Timeline'));
expect(workspace.project!.info.id, id);
},
);
test('close waits for accepted creation and prevents later work', () async {
final root = await Directory.systemTemp.createTemp('cutdex-app-test-');
addTearDown(() => root.delete(recursive: true));
final workspace = ProjectWorkspace(creationNames: testCreationNames);
final path = '${root.path}/Queued.cutdex';
final opening = workspace.open(path, name: 'Queued');
final creating = workspace.createTimeline('Saved before close');
final closing = workspace.close();
expect(workspace.close(), same(closing));
await opening;
await creating;
await closing;
await expectLater(workspace.createTimeline('Late'), throwsStateError);
final file = await ProjectFile.open(Directory(path));
try {
expect(
(await file.listTimelines()).map((item) => item.name),
contains('Saved before close'),
);
} finally {
await file.close();
}
});
test(
'leased timeline survives project switch and is reused on return',
() async {
final root = await Directory.systemTemp.createTemp('cutdex-lease-test-');
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
final firstPath = '${root.path}/First.cutdex';
final timelines = await workspace.open(firstPath, name: 'First');
final projectId = workspace.project!.info.id;
final document = await workspace.loadTimeline(
projectId,
timelines.single.id,
);
final session = await workspace.openTimelineSession(projectId, document);
final lease = await workspace.leaseTimeline(projectId, document.id);
await workspace.open('${root.path}/Second.cutdex', name: 'Second');
expect(session.acceptsEdits, isTrue);
await lease.validate();
expect(
await workspace.listProjectTimelines(firstPath, expectedId: projectId),
hasLength(1),
);
await workspace.open(firstPath, expectedId: projectId);
final reopened = await workspace.loadTimeline(projectId, document.id);
final returned = await workspace.openTimelineSession(projectId, reopened);
expect(identical(returned, session), isTrue);
final manifest = File('$firstPath/manifest.json');
final backup = await manifest.rename('$firstPath/manifest.json.backup');
await expectLater(lease.validate(), throwsA(isA<FileSystemException>()));
await backup.rename(manifest.path);
await lease.release();
expect(returned.acceptsEdits, isTrue);
},
);
test('leased timeline survives switching to another timeline', () async {
final root = await Directory.systemTemp.createTemp(
'cutdex-timeline-lease-',
);
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
final timelines = await workspace.open(
'${root.path}/Project.cutdex',
name: 'Project',
);
final id = workspace.project!.info.id;
final firstDocument = await workspace.loadTimeline(id, timelines.single.id);
final firstSession = await workspace.openTimelineSession(id, firstDocument);
final lease = await workspace.leaseTimeline(id, firstDocument.id);
final added = await workspace.createTimeline('Second');
final secondId = added
.singleWhere((item) => item.id != firstDocument.id)
.id;
final secondDocument = await workspace.loadTimeline(id, secondId);
final secondSession = await workspace.openTimelineSession(
id,
secondDocument,
);
expect(firstSession.acceptsEdits, isTrue);
expect(secondSession.acceptsEdits, isTrue);
await lease.validate();
await lease.release();
expect(firstSession.acceptsEdits, isTrue);
expect(secondSession.acceptsEdits, isTrue);
final returnedDocument = await workspace.loadTimeline(id, firstDocument.id);
final returned = await workspace.openTimelineSession(id, returnedDocument);
expect(identical(returned, firstSession), isTrue);
});
test(
'timeline switch keeps old UI notifiers alive until project close',
() async {
final root = await Directory.systemTemp.createTemp('cutdex-timeline-ui-');
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
final timelines = await workspace.open(
'${root.path}/Project.cutdex',
name: 'Project',
);
final id = workspace.project!.info.id;
final first = await workspace.loadTimeline(id, timelines.single.id);
final session = await workspace.openTimelineSession(id, first);
final releaseViews = session.retainViews();
final tasks = session.audioGainPreview;
final removals = session.view(
first.defaultSequenceId ?? first.sequences.first.id,
);
final added = await workspace.createTimeline('Second');
final secondId = added.singleWhere((item) => item.id != first.id).id;
await workspace.loadTimeline(id, secondId);
expect(session.acceptsEdits, isTrue);
void listener() {}
expect(() => tasks.addListener(listener), returnsNormally);
expect(() => removals.addListener(listener), returnsNormally);
tasks.removeListener(listener);
removals.removeListener(listener);
releaseViews();
expect(() => tasks.addListener(listener), returnsNormally);
tasks.removeListener(listener);
await workspace.open('${root.path}/Other.cutdex', name: 'Other');
expect(session.acceptsEdits, isTrue);
await workspace.close();
expect(session.acceptsEdits, isFalse);
expect(() => tasks.addListener(listener), throwsAssertionError);
expect(() => removals.addListener(listener), throwsAssertionError);
},
);
test('unleased sessions stay warm until workspace close', () async {
final root = await Directory.systemTemp.createTemp('cutdex-session-owner-');
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
final timelines = await workspace.open(
'${root.path}/First.cutdex',
name: 'First',
);
final id = workspace.project!.info.id;
final first = await workspace.loadTimeline(id, timelines.single.id);
final firstSession = await workspace.openTimelineSession(id, first);
final added = await workspace.createTimeline('Second');
final secondId = added.singleWhere((item) => item.id != first.id).id;
final second = await workspace.loadTimeline(id, secondId);
final secondSession = await workspace.openTimelineSession(id, second);
await workspace.open('${root.path}/Other.cutdex', name: 'Other');
expect(firstSession.acceptsEdits, isTrue);
await workspace.close();
expect(firstSession.acceptsEdits, isFalse);
expect(secondSession.acceptsEdits, isFalse);
});
test('failed timeline selection leaves cached sessions usable', () async {
final root = await Directory.systemTemp.createTemp('cutdex-timeline-fail-');
final workspace = ProjectWorkspace(creationNames: testCreationNames);
addTearDown(() async {
await workspace.close();
await root.delete(recursive: true);
});
final timelines = await workspace.open(
'${root.path}/Project.cutdex',
name: 'Project',
);
final id = workspace.project!.info.id;
final document = await workspace.loadTimeline(id, timelines.single.id);
final session = await workspace.openTimelineSession(id, document);
await expectLater(
workspace.loadTimeline(id, 'missing'),
throwsA(isA<ProjectFileException>()),
);
expect(session.acceptsEdits, isTrue);
final selected = await workspace.loadTimeline(id, document.id);
expect(
identical(await workspace.openTimelineSession(id, selected), session),
isTrue,
);
});
}