Files
cutdex/test/workspace/projects_creation_test.dart
lingjhf abc1868550
Gitea CI / Check code and tests (push) Successful in 6m37s
Gitea CI / Build Windows x64 app (push) Successful in 3m18s
Gitea CI / Build Android APK (push) Successful in 3m47s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m7s
Gitea CI / Assemble and validate CI artifacts (push) Successful in 50s
Gitea CI / Gitea CI passed (push) Successful in 2s
Gitea Release / Prepare draft Release (push) Successful in 9s
Gitea Release / Upload Android to draft Release (push) Successful in 16s
Gitea Release / Upload Windows to draft Release (push) Successful in 22s
Gitea Release / Verify and publish Release (push) Successful in 14s
feat(workspace): 桌面工作区、主题与交互更新,发布 0.1.2
2026-10-08 18:17:12 +00:00

180 lines
6.1 KiB
Dart

import 'dart:io';
import 'package:cutdex/errors/app_failure.dart';
import 'package:cutdex/integrations/platform/device_experience.dart';
import 'package:cutdex/persistence/app_settings_repositories.dart';
import 'package:cutdex/workspace/persistence/recent_projects.dart';
import 'package:cutdex/workspace/projects.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as p;
import '../support/memory_app_settings.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('project creation collisions', () {
late Directory root;
late ProviderContainer container;
late Projects projects;
setUp(() async {
root = await Directory.systemTemp.createTemp('cutdex-name-conflict-');
container = ProviderContainer(
overrides: [
deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop),
recentProjectsProvider.overrideWithValue(
MemoryRecentProjectsRepository(),
),
projectLocationSettingsRepositoryProvider.overrideWithValue(
DefaultingProjectLocationSettingsRepository(
MemoryProjectLocationSettingsRepository(root.path),
),
),
resourceLocationSettingsRepositoryProvider.overrideWithValue(
MemoryResourceLocationSettingsRepository(root.path),
),
],
);
projects = container.read(projectsProvider.notifier);
});
tearDown(() async {
await projects.prepareToQuit();
container.dispose();
await root.delete(recursive: true);
});
test(
'duplicate creation preserves the existing project and session',
() async {
expect(
await projects.createProject('One', initialTimelineName: 'Timeline'),
isTrue,
);
final before = container.read(projectsProvider).requireValue;
final project = before.activeProject!;
final session = await projects.openTimelineSession(
project.id,
before.timeline!.id,
);
final manifest = File(p.join(project.path, 'manifest.json'));
final contents = await manifest.readAsString();
expect(
(await projects.validateNewProjectName(' One '))?.code,
AppErrorCode.projectAlreadyExists,
);
expect(container.read(projectsProvider).requireValue.error, isNull);
expect(
await projects.createProject(
' One ',
initialTimelineName: 'Replacement',
),
isFalse,
);
final after = container.read(projectsProvider).requireValue;
expect(after.error?.code, AppErrorCode.projectAlreadyExists);
expect(after.activeProject?.id, project.id);
expect(after.timeline?.id, before.timeline?.id);
expect(after.recent, before.recent);
expect(await manifest.readAsString(), contents);
expect(
await projects.openTimelineSession(project.id, before.timeline!.id),
same(session),
);
expect(await projects.validateNewProjectName('Two'), isNull);
expect(
await projects.createProject('Two', initialTimelineName: 'Timeline'),
isTrue,
);
expect(container.read(projectsProvider).requireValue.error, isNull);
expect(
container.read(projectsProvider).requireValue.activeProject?.name,
'Two',
);
expect(await manifest.readAsString(), contents);
},
);
test(
'creation detects a destination occupied after name validation',
() async {
expect(await projects.validateNewProjectName('Racing'), isNull);
final destination = File(p.join(root.path, 'Racing.cutdex'));
await destination.writeAsString('created by another operation');
expect(
await projects.createProject(
'Racing',
initialTimelineName: 'Timeline',
),
isFalse,
);
expect(
container.read(projectsProvider).requireValue.error?.code,
AppErrorCode.projectAlreadyExists,
);
expect(
await destination.readAsString(),
'created by another operation',
);
},
);
for (final type in ['file', 'directory', 'broken link']) {
test('occupied $type is reported without replacing it', () async {
final destination = p.join(root.path, 'Occupied.cutdex');
switch (type) {
case 'file':
await File(destination).writeAsString('keep this file');
case 'directory':
await Directory(destination).create();
await File(p.join(destination, 'keep.txt'))
.writeAsString('keep this directory');
case 'broken link':
await Link(destination).create(p.join(root.path, 'missing-target'));
}
final before = await FileSystemEntity.type(
destination,
followLinks: false,
);
expect(
(await projects.validateNewProjectName('Occupied'))?.code,
AppErrorCode.projectAlreadyExists,
);
expect(
await projects.createProject(
'Occupied',
initialTimelineName: 'Timeline',
),
isFalse,
);
final state = container.read(projectsProvider).requireValue;
expect(state.error?.code, AppErrorCode.projectAlreadyExists);
expect(state.activeProject, isNull);
expect(state.recent, isEmpty);
expect(
await FileSystemEntity.type(destination, followLinks: false),
before,
);
switch (type) {
case 'file':
expect(await File(destination).readAsString(), 'keep this file');
case 'directory':
expect(
await File(p.join(destination, 'keep.txt')).readAsString(),
'keep this directory',
);
case 'broken link':
expect(
await Link(destination).target(),
p.join(root.path, 'missing-target'),
);
}
});
}
});
}