import 'package:cutdex/workspace/workspace_navigation.dart'; import 'package:cutdex/workspace/workspace_mode.dart'; import 'package:cutdex/workspace/view_settings.dart'; import 'package:cutdex/app/editor_theme.dart'; import 'package:cutdex/integrations/platform/device_experience.dart'; import 'support/empty_conversation_repository.dart'; import 'package:cutdex/persistence/app_settings_repositories.dart'; import 'dart:async'; import 'dart:io'; import 'package:cutdex/features/timeline/timeline_panel.dart'; import 'package:cutdex/features/preview/preview_panel.dart'; import 'package:cutdex/features/properties/properties_panel.dart'; import 'package:cutdex/features/media/media_panel.dart'; import 'package:cutdex/features/media/background_tasks_dialog.dart'; import 'package:cutdex/features/assistant/agent_chat_panel.dart'; import 'package:cutdex/features/editor/editor_page.dart'; import 'package:cutdex/features/projects/project_sidebar.dart'; import 'package:cutdex/features/settings/settings_content.dart'; import 'package:cutdex/features/settings/appearance_settings.dart'; import 'support/memory_app_settings.dart'; import 'package:cutdex_ui/icons.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/services.dart'; import 'package:cutdex/workspace/projects.dart'; import 'package:cutdex/workspace/project_state.dart'; import 'package:cutdex_sdk/project.dart'; import 'package:cutdex/app/app.dart'; import 'package:cutdex/app/router/app_router.dart'; import 'package:cutdex/integrations/agent/conversation_repository.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { testWidgets( 'desktop content switches retain editor, settings, draft and split widths', (tester) async { tester.view.physicalSize = const Size(1400, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue( DeviceExperience.desktop, ), appearanceSettingsRepositoryProvider.overrideWithValue( MemoryAppearanceSettingsRepository(), ), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_TestProjects.new), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); final editor = tester.element(find.byType(EditorWorkspace)); final agent = tester.element(find.byType(AgentChatPanel)); final timeline = tester.element(find.byType(EditorTimelinePanel)); final input = find.descendant( of: find.byType(AgentChatPanel), matching: find.byType(TextField), ); await tester.enterText(input, 'Keep settings draft'); await tester.drag( find.byKey(const Key('project-sidebar-resize')), const Offset(80, 0), ); await tester.pumpAndSettle(); final agentWidth = tester.getSize(find.byType(AgentChatPanel)).width; final editorWidth = tester.getSize(find.byType(EditorWorkspace)).width; final container = ProviderScope.containerOf(editor); final header = tester.element( find.byKey(const Key('desktop-window-header')), ); final taskButtonFinder = find.byKey( const ValueKey('background-tasks-button'), ); final taskButton = tester.element(taskButtonFinder); expect( find.descendant( of: find.byKey(const Key('desktop-window-header')), matching: taskButtonFinder, ), findsOneWidget, ); expect( find.descendant( of: find.byKey(const Key('editor-content-header')), matching: taskButtonFinder, ), findsNothing, ); final route = container .read(appRouterProvider) .routeInformationProvider .value .uri; await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-settings-button'))); await tester.pumpAndSettle(); expect( find.byKey(const Key('desktop-settings-workspace')), findsOneWidget, ); expect(find.byType(SettingsContent), findsOneWidget); final navigationCard = find.byKey(const Key('settings-navigation-card')); final contentCard = find.byKey(const Key('settings-content-card')); final navigationRect = tester.getRect(navigationCard); final settingsRect = tester.getRect(contentCard); final settingsBack = find.byKey(const Key('settings-sidebar-back')); expect(settingsBack, findsOneWidget); expect(navigationRect.contains(tester.getCenter(settingsBack)), isTrue); expect( tester.getRect(settingsBack).bottom, lessThan( tester .getRect(find.byKey(const ValueKey('settings-navigation-0'))) .top, ), ); expect(settingsRect.left - navigationRect.right, 6); expect(settingsRect.top, navigationRect.top); expect(settingsRect.bottom, navigationRect.bottom); expect(find.byKey(const Key('settings-sidebar-divider')), findsNothing); await tester.dragFrom( tester.getCenter(find.byKey(const Key('settings-card-gap'))), const Offset(60, 0), ); await tester.pumpAndSettle(); expect(tester.getRect(navigationCard), navigationRect); expect(tester.getRect(contentCard), settingsRect); expect( tester.element(find.byKey(const Key('desktop-window-header'))), same(header), ); expect(tester.element(taskButtonFinder), same(taskButton)); await tester.tap(taskButtonFinder); await tester.pumpAndSettle(); expect(find.byType(BackgroundTasksContent), findsOneWidget); await tester.tap(taskButtonFinder); await tester.pumpAndSettle(); expect(find.byType(BackgroundTasksContent), findsNothing); expect( container.read(appRouterProvider).routeInformationProvider.value.uri, route, ); expect(find.byKey(const Key('app-dialog-frame')), findsNothing); expect(find.byType(EditorWorkspace), findsNothing); expect( tester.element(find.byType(EditorWorkspace, skipOffstage: false)), same(editor), ); await container .read(appearanceSettingsProvider.notifier) .setMode(AppAppearance.penDark); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('settings-navigation-1'))); await tester.pumpAndSettle(); final settings = tester.element(find.byType(SettingsContent)); await tester.tap(find.byKey(const ValueKey('project-tab-project'))); await tester.pumpAndSettle(); expect(find.byType(SettingsContent), findsNothing); expect(find.byKey(const Key('desktop-library-workspace')), findsNothing); await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-settings-button'))); await tester.pumpAndSettle(); expect(tester.element(find.byType(SettingsContent)), same(settings)); expect(find.byKey(const Key('agent-settings-scroll')), findsOneWidget); expect(find.byKey(const ValueKey('project-tab-project')), findsOneWidget); await tester.tap(settingsBack); await tester.pumpAndSettle(); expect(find.byKey(const Key('desktop-settings-workspace')), findsNothing); expect(tester.element(find.byType(EditorWorkspace)), same(editor)); expect(tester.element(find.byType(AgentChatPanel)), same(agent)); expect(tester.element(find.byType(EditorTimelinePanel)), same(timeline)); expect(tester.getSize(find.byType(AgentChatPanel)).width, agentWidth); expect(tester.getSize(find.byType(EditorWorkspace)).width, editorWidth); expect( tester.widget(input).controller!.text, 'Keep settings draft', ); expect(tester.takeException(), isNull); }, ); testWidgets('workspace stays hidden while startup data loads', ( tester, ) async { tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_LoadingProjects.new), ], child: const CutdexApp(), ), ); await tester.pump(); expect(find.byKey(const Key('projects-window-header')), findsNothing); expect(find.byKey(const Key('project-sidebar-separator')), findsNothing); expect(find.byType(EditorWorkspace), findsNothing); expect(find.text('CUTDEX'), findsOneWidget); expect(find.text('Opening workspace…'), findsOneWidget); expect(find.byType(CircularProgressIndicator), findsNothing); expect(find.byType(FadeTransition), findsWidgets); expect(find.text('No projects yet'), findsNothing); }); testWidgets( 'composer is available before selecting a project and keeps the draft', (tester) async { tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue( DeviceExperience.desktop, ), projectsProvider.overrideWith(_UnselectedProjects.new), conversationRepositoryProvider.overrideWith( (ref) async => _GroupedConversationRepository({}), ), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); final input = find.descendant( of: find.byKey(const Key('agent-composer')), matching: find.byType(TextField), ); expect(input, findsOneWidget); expect(find.byKey(const Key('composer-project-bar')), findsNothing); expect(find.byKey(const Key('window-project-tabs')), findsOneWidget); await tester.enterText(input, 'Keep this before selecting'); await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('project-menu-project'))); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('project-tab-project')), findsOneWidget); expect( tester.widget(input).controller!.text, 'Keep this before selecting', ); await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); await tester.tap(find.text('New project')); await tester.pumpAndSettle(); expect(find.byKey(const Key('app-dialog-frame')), findsOneWidget); await tester.tap(find.text('Cancel')); await tester.pumpAndSettle(); expect( tester.widget(input).controller!.text, 'Keep this before selecting', ); }, ); testWidgets('project selection coalesces clicks and ignores stale loads', ( tester, ) async { tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final projects = _DelayedProjects(); final chat = ConversationMetadata( id: 'retained-chat', projectId: 'project', title: 'Retained conversation', createdAt: DateTime.utc(2026), updatedAt: DateTime.utc(2026), providerId: 'provider', modelId: 'model', timelineId: 'project', pending: false, responseDurationsMs: const [], ); final repository = _GroupedConversationRepository({ 'project': [chat], }); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), projectsProvider.overrideWith(() => projects), conversationRepositoryProvider.overrideWith( (ref) async => repository, ), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); final container = ProviderScope.containerOf( tester.element(find.byType(CutdexApp)), ); bool selected(String id) => (container.read(workspaceNavigationProvider).selectedProjectId ?? projects.state.requireValue.activeProject?.id) == id; Future click(String id) async { await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pump(); await tester.tap(find.byKey(ValueKey('project-menu-$id'))); await tester.pump(); await tester.pump(); } await click('other'); expect(selected('other'), isTrue); expect(find.byKey(const ValueKey('project-tab-other')), findsOneWidget); await tester.tap(find.byKey(const Key('project-tabs-add'))); await tester.pump(); expect(find.byKey(const Key('app-dialog-frame')), findsNothing); expect(selected('other'), isTrue); expect(projects.requests, isEmpty); expect(find.byKey(const Key('editor-preview-card')), findsOneWidget); expect(find.byKey(const Key('editor-timeline-card')), findsOneWidget); expect(find.byKey(const Key('preview-default-frame')), findsNothing); expect(find.byKey(const Key('timeline-default-tracks')), findsNothing); expect( tester.widget(find.byType(EditorWorkspace)).projectName, '测试项目', ); final previewCard = tester.element( find.byKey(const Key('editor-preview-card')), ); final timelineCard = tester.element( find.byKey(const Key('editor-timeline-card')), ); expect(find.byKey(const Key('editor-project-loading')), findsNothing); await tester.pump(const Duration(milliseconds: 40)); await click('third'); await click('third'); await tester.pump(const Duration(milliseconds: 100)); expect(projects.requests.map((r) => r.$1), ['third']); expect(selected('third'), isTrue); expect(find.byKey(const Key('editor-project-loading')), findsNothing); await tester.pump(const Duration(seconds: 2)); expect(find.byKey(const Key('preview-default-frame')), findsNothing); expect(find.byKey(const Key('timeline-default-tracks')), findsNothing); expect(find.byKey(const Key('editor-project-loading')), findsNothing); expect( tester.element(find.byKey(const Key('editor-preview-card'))), same(previewCard), ); expect( tester.element(find.byKey(const Key('editor-timeline-card'))), same(timelineCard), ); expect( tester.widget(find.byType(EditorWorkspace)).projectName, '测试项目', ); await click('other'); expect(selected('other'), isTrue); projects.requests.first.$2.complete(true); await tester.pump(); expect(projects.state.requireValue.activeProject?.id, 'project'); await tester.pump(const Duration(milliseconds: 100)); expect(projects.requests.map((r) => r.$1), ['third', 'other']); projects.requests.last.$2.complete(false); await tester.pumpAndSettle(); expect(selected('project'), isTrue); expect(find.byKey(const Key('preview-default-frame')), findsNothing); expect(find.byKey(const Key('timeline-default-tracks')), findsNothing); expect(find.byKey(const Key('editor-project-loading')), findsNothing); await click('other'); await tester.pump(const Duration(milliseconds: 100)); projects.requests.last.$2.complete(true); await tester.pump(); await tester.pumpAndSettle(); expect(selected('other'), isTrue); expect(projects.requests.length, 3); await click('other'); await tester.pumpAndSettle(); expect(projects.requests.length, 3); }); testWidgets('busy operations keep sidebar and timeline controls visible', ( tester, ) async { tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_BusyProjects.new), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); expect(find.byKey(const Key('project-tabs-add')), findsOneWidget); expect(find.byKey(const ValueKey('project-tab-project')), findsOneWidget); expect( tester .widget(find.byKey(const Key('project-tabs-add'))) .onPressed, isNotNull, ); expect(find.byKey(const Key('project-chat-header')), findsOneWidget); final addTimeline = find.byKey(const Key('timeline-add-button')); expect(tester.widget(addTimeline).onPressed, isNotNull); expect( find.descendant(of: addTimeline, matching: find.byIcon(AppIcons.add)), findsOneWidget, ); expect( tester .widget( find.byKey(const ValueKey('timeline-tab-project')), ) .onPressed, isNotNull, ); }); testWidgets('new conversation remains an in-memory draft', (tester) async { tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final root = (await tester.runAsync( () => Directory.systemTemp.createTemp('cutdex-chat-ui-'), ))!; addTearDown(() => root.delete(recursive: true)); final repository = _EmptyConversationRepository( Directory('${root.path}/Conversations'), ); final container = ProviderContainer( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), projectsProvider.overrideWith(_TestProjects.new), conversationRepositoryProvider.overrideWith((ref) async => repository), ], // This project fixture has no persistence session to reopen on retry. retry: (_, _) => null, ); addTearDown(container.dispose); await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const CutdexApp()), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-chat-new'))); await tester.pumpAndSettle(); expect(await tester.runAsync(repository.root.exists), isFalse); }); testWidgets('saved workflow preferences keep the project workspace visible', ( tester, ) async { tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final settings = _MemoryWorkspaceViewSettings(); Future launch() async { await tester.pumpWidget( ProviderScope( key: UniqueKey(), overrides: [ deviceExperienceProvider.overrideWithValue( DeviceExperience.desktop, ), projectsProvider.overrideWith(_TestProjects.new), workspaceViewSettingsRepositoryProvider.overrideWithValue(settings), conversationRepositoryProvider.overrideWith( (ref) async => _GroupedConversationRepository({}), ), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); } Future switchMode(String name) async { ProviderScope.containerOf(tester.element(find.byType(CutdexApp))) .read(workspaceViewSettingsProvider.notifier) .setMode(WorkspaceMode.values.byName(name)); await ProviderScope.containerOf(tester.element(find.byType(CutdexApp))) .read(workspaceViewSettingsProvider.notifier) .save(activeProjectId: null); await tester.pumpAndSettle(); } await launch(); expect(find.byKey(const Key('desktop-chat-card')), findsOneWidget); await switchMode('cut'); expect(settings.value.mode, WorkspaceMode.cut); await tester.pumpWidget(const SizedBox()); await launch(); expect(find.byKey(const Key('desktop-chat-card')), findsOneWidget); await switchMode('agent'); expect(settings.value.mode, WorkspaceMode.agent); await tester.pumpWidget(const SizedBox()); await launch(); expect(find.byKey(const Key('desktop-chat-card')), findsOneWidget); }); testWidgets( 'workflow changes preserve chat and editor in the project workspace', (tester) async { tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue( DeviceExperience.desktop, ), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_TestProjects.new), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); expect(find.byKey(const Key('desktop-chat-card')), findsOneWidget); Color chatSurface() => tester .element( find.byKey(const Key('agent-conversation'), skipOffstage: false), ) .findAncestorWidgetOfExactType()! .color!; ColorScheme chatColors() => Theme.of( tester.element( find.byKey(const Key('agent-conversation'), skipOffstage: false), ), ).colorScheme; expect( tester .widget( find.byKey(const Key('agent-conversation'), skipOffstage: false), ) .child, isNot(isA()), ); expect(chatSurface(), chatColors().surface); final projectTitle = find.byKey(const Key('workspace-project-title')); expect( find.descendant( of: find.byKey(const Key('agent-content-header')), matching: projectTitle, ), findsNothing, ); final chatElement = tester.element( find.byType(AgentChatPanel, skipOffstage: false), ); final editorElement = tester.element(find.byType(EditorWorkspace)); final input = find.descendant( of: find.byType(AgentChatPanel, skipOffstage: false), matching: find.byType(TextField, skipOffstage: false), ); await tester.enterText(input, 'Keep this draft'); expect(find.byKey(const Key('sidebar-mode-trigger')), findsNothing); final container = ProviderScope.containerOf( tester.element(find.byType(CutdexApp)), ); container .read(workspaceViewSettingsProvider.notifier) .setMode(WorkspaceMode.cut); await tester.pumpAndSettle(); expect(find.byKey(const Key('desktop-chat-card')), findsOneWidget); expect(chatSurface(), chatColors().surface); expect( tester.element(find.byType(AgentChatPanel, skipOffstage: false)), same(chatElement), ); expect(tester.element(find.byType(EditorWorkspace)), same(editorElement)); expect( tester.widget(input).controller!.text, 'Keep this draft', ); container .read(workspaceViewSettingsProvider.notifier) .setMode(WorkspaceMode.agent); await tester.pumpAndSettle(); expect(find.byKey(const Key('desktop-chat-card')), findsOneWidget); expect( tester.element(find.byType(AgentChatPanel, skipOffstage: false)), same(chatElement), ); expect(tester.element(find.byType(EditorWorkspace)), same(editorElement)); expect( tester.widget(input).controller!.text, 'Keep this draft', ); }, ); testWidgets( 'chat history contains only current project conversations and can rename', (tester) async { tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark; addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue); tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); ConversationMetadata conversation(String projectId, int index) => ConversationMetadata( id: '$projectId-$index', projectId: projectId, title: 'Conversation $index', createdAt: DateTime.utc(2026, 9, 24), updatedAt: DateTime.utc(2026, 9, 24), providerId: 'provider', modelId: 'model', timelineId: projectId, pending: false, responseDurationsMs: const [], ); final repository = _GroupedConversationRepository({ 'project': [for (var i = 1; i <= 7; i++) conversation('project', i)], 'other': [conversation('other', 1)], }); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue( DeviceExperience.desktop, ), projectsProvider.overrideWith(_GroupedProjects.new), conversationRepositoryProvider.overrideWith( (ref) async => repository, ), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('project-tab-project')), findsOneWidget); expect( find.byKey(const ValueKey('sidebar-project-project')), findsNothing, ); await tester.tap(find.byKey(const Key('project-chat-history'))); await tester.pumpAndSettle(); final chatRow = find.byKey( const ValueKey('sidebar-conversation-project-1'), ); expect(chatRow, findsOneWidget); expect( find.byKey(const ValueKey('sidebar-conversation-project-7')), findsOneWidget, ); expect( find.byKey(const ValueKey('sidebar-conversation-other-1')), findsNothing, ); expect(tester.getSize(chatRow).height, AppMenuMetrics.rowHeight); await tester.tap(chatRow, buttons: kSecondaryMouseButton); await tester.pumpAndSettle(); await tester.tap(find.text('Rename')); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextFormField), 'Updated chat'); await tester.tap(find.widgetWithText(FilledButton, 'Save')); await tester.pumpAndSettle(); expect(repository.groups['project']!.first.title, 'Updated chat'); expect(tester.takeException(), isNull); }, ); testWidgets('empty chat history does not introduce project groups', ( tester, ) async { tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), projectsProvider.overrideWith(_GroupedProjects.new), conversationRepositoryProvider.overrideWith( (ref) async => _GroupedConversationRepository({}), ), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-chat-history'))); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('sidebar-project-other')), findsNothing); expect( find.byKey(const ValueKey('sidebar-conversation-other-1')), findsNothing, ); expect(tester.takeException(), isNull); }); testWidgets('project header opens recent projects and switches tabs', ( tester, ) async { tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_GroupedProjects.new), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-tabs-projects'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('project-menu-other'))); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('project-tab-other')), findsOneWidget); expect(find.byKey(const ValueKey('project-tab-project')), findsOneWidget); await tester.tap(find.byKey(const ValueKey('project-tab-project'))); await tester.pumpAndSettle(); expect( ProviderScope.containerOf(tester.element(find.byType(CutdexApp))) .read(projectsProvider) .requireValue .activeProject ?.id, 'project', ); expect(tester.takeException(), isNull); }); test('dark workspace uses the configured panel palette', () { final theme = EditorTheme.penDark; expect(theme.colorScheme.surface, const Color(0xFF111111)); expect(theme.colorScheme.surfaceContainerLow, const Color(0xFF151515)); expect( theme.colorScheme.outlineVariant, const Color(0xFFFCFCFC).withValues(alpha: 0.084), ); expect( theme.extension()!.composerBackground, const Color(0xFF2F2F2F), ); expect( theme.extension()!.popoverBackground, const Color(0xFF242424), ); }); testWidgets('project context menu renames its item', (tester) async { tester.platformDispatcher.localesTestValue = const [Locale('zh')]; addTearDown(tester.platformDispatcher.clearLocalesTestValue); tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final container = ProviderContainer( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_TestProjects.new), ], ); addTearDown(container.dispose); await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const CutdexApp()), ); await tester.pumpAndSettle(); Future rename(Finder target, String value) async { await tester.tap(target, buttons: kSecondaryMouseButton); await tester.pumpAndSettle(); await tester.tap(find.text('重命名').last); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextFormField), value); await tester.tap(find.widgetWithText(FilledButton, '重命名')); await tester.pumpAndSettle(); } await rename(find.byKey(const ValueKey('project-tab-project')), '重命名项目'); expect( find.descendant( of: find.byKey(const ValueKey('project-tab-project')), matching: find.text('重命名项目'), ), findsOneWidget, ); await tester.pumpWidget(const SizedBox()); container.dispose(); }); testWidgets( 'switching timelines preserves the project workspace and its layout', (tester) async { tester.platformDispatcher.localesTestValue = const [Locale('zh')]; addTearDown(tester.platformDispatcher.clearLocalesTestValue); tester.view.physicalSize = const Size(1440, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final container = ProviderContainer( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_SwitchProjects.new), ], ); await tester.pumpWidget( UncontrolledProviderScope( container: container, child: const CutdexApp(), ), ); await tester.pumpAndSettle(); expect( find.byKey(const ValueKey('timeline-tab-project')), findsOneWidget, ); expect( tester.getSize(find.byKey(const Key('timeline-tabs-bar'))).height, 32, ); expect( find.descendant( of: find.byKey(const Key('timeline-tabs-bar')), matching: find.byType(VerticalDivider), ), findsNothing, ); expect( find.descendant( of: find.byKey(const Key('timeline-tabs-bar')), matching: find.byType(ShaderMask), ), findsOneWidget, ); expect( tester .widget( find.byKey(const ValueKey('timeline-tab-project')), ) .style! .textStyle! .resolve({})! .fontSize, 13, ); final projectTabFinder = find.byKey( const ValueKey('timeline-tab-project'), ); final tabRect = tester.getRect(projectTabFinder); expect(tabRect.height, 26); for (final key in ['timeline-add-button', 'timeline-list-button']) { final buttonRect = tester.getRect(find.byKey(Key(key))); expect(buttonRect.height, tabRect.height); expect(buttonRect.top, tabRect.top); expect(buttonRect.bottom, tabRect.bottom); } expect( tester.getRect(find.byKey(const Key('timeline-tabs-bar'))).right - tester.getRect(find.byKey(const Key('timeline-list-button'))).right, 4, ); expect( tester .widget(projectTabFinder) .style! .backgroundColor! .resolve({}), Theme.of(tester.element(projectTabFinder)).colorScheme.primary .withValues(alpha: .12), ); expect(find.byKey(const ValueKey('timeline-tab-second')), findsOneWidget); final tabs = find.byKey(const Key('timeline-tabs-list')); final tabsScrollable = tester.state( find.descendant(of: tabs, matching: find.byType(Scrollable)), ); expect(tabsScrollable.position.maxScrollExtent, greaterThan(0)); await tester.sendEventToBinding( PointerScrollEvent( position: tester.getCenter(tabs), scrollDelta: const Offset(0, 120), ), ); await tester.pump(); expect(tabsScrollable.position.pixels, greaterThan(0)); tabsScrollable.position.jumpTo(0); await tester.pump(); await tester.tap(find.byKey(const Key('timeline-list-button'))); await tester.pumpAndSettle(); expect( find.byKey(const ValueKey('timeline-menu-project')), findsOneWidget, ); expect( find.byKey(const ValueKey('timeline-menu-timeline-20')), findsOneWidget, ); await tester.tap(find.byKey(const ValueKey('timeline-menu-timeline-3'))); await tester.pumpAndSettle(); expect( tester .widget(find.byType(EditorTimelinePanel)) .timelineId, 'timeline-3', ); final navigation = tester.element(find.byType(ProjectSidebar)); final workspace = tester.element(find.byType(EditorWorkspace)); await tester.tap(find.byKey(const Key('editor-properties-button'))); await tester.pumpAndSettle(); final timelineListButton = find.byKey(const Key('timeline-list-button')); await tester.tap(timelineListButton); await tester.pumpAndSettle(); final timelineMenuPanel = find.byWidgetPredicate( (widget) => widget.runtimeType.toString() == '_MenuPanel', ); expect(timelineMenuPanel, findsOneWidget); expect( tester.getRect(timelineMenuPanel).right, closeTo(tester.getRect(timelineListButton).right, 1), ); final menuRect = tester.getRect(timelineMenuPanel); final buttonRect = tester.getRect(timelineListButton); expect( (menuRect.top - buttonRect.bottom - 4).abs() < 1 || (menuRect.bottom - buttonRect.top + 4).abs() < 1, isTrue, ); await tester.tap(timelineListButton); await tester.pumpAndSettle(); final editorHeader = tester.element( find.byKey(const Key('editor-content-header')), ); final mediaPanel = tester.element( find.byType(MediaPanel, skipOffstage: false), ); final propertiesPanel = tester.element( find.byType(EditorPropertiesPanel), ); await tester.drag( find.byKey(const Key('editor-timeline-resize')), const Offset(0, -60), ); await tester.pumpAndSettle(); final timelineHeight = tester .getSize(find.byType(EditorTimelinePanel)) .height; final taskButtonFinder = find.byKey( const ValueKey('background-tasks-button'), ); final taskButton = tester.element(taskButtonFinder); expect(tester.widget(taskButtonFinder).onPressed, isNotNull); for (final id in ['second', 'project', 'second']) { final previousPreview = tester.element(find.byType(EditorPreviewPanel)); final previousTimeline = tester.element( find.byType(EditorTimelinePanel), ); await container .read(projectsProvider.notifier) .openTimeline('project', id); await tester.pumpAndSettle(); expect( tester.element(find.byKey(const Key('project-chat-header'))), isNotNull, ); expect(tester.element(find.byType(ProjectSidebar)), same(navigation)); expect(tester.element(find.byType(EditorWorkspace)), same(workspace)); expect( tester.element(find.byKey(const Key('editor-content-header'))), same(editorHeader), ); expect( tester.element(find.byType(EditorPreviewPanel)), same(previousPreview), ); expect( tester.element(find.byType(EditorTimelinePanel)), same(previousTimeline), ); expect( tester.element(find.byType(MediaPanel, skipOffstage: false)), same(mediaPanel), ); expect( tester.element(find.byType(EditorPropertiesPanel)), same(propertiesPanel), ); expect(tester.element(taskButtonFinder), same(taskButton)); expect( tester.widget(taskButtonFinder).onPressed, isNotNull, ); expect( find.byKey(const Key('media-panel-surface'), skipOffstage: false), findsOneWidget, ); expect( find.byKey(const Key('editor-properties-region')), findsOneWidget, ); expect( tester.getSize(find.byType(EditorTimelinePanel)).height, timelineHeight, ); expect( tester .widget(find.byType(EditorTimelinePanel)) .timelineId, id, ); expect(find.byKey(const Key('workspace-project-title')), findsNothing); expect( tester .widget(find.byKey(ValueKey('timeline-tab-$id'))) .onPressed, isNotNull, ); expect(tester.takeException(), isNull); } await tester.tap(find.byKey(const Key('timeline-list-button'))); await tester.pumpAndSettle(); await tester.ensureVisible( find.byKey(const ValueKey('timeline-menu-timeline-20')), ); await tester.tap(find.byKey(const ValueKey('timeline-menu-timeline-20'))); await tester.pumpAndSettle(); final selectedTab = tester.getRect( find.byKey(const ValueKey('timeline-tab-timeline-20')), ); final tabsViewport = tester.getRect(tabs); expect(selectedTab.left, greaterThanOrEqualTo(tabsViewport.left)); expect(selectedTab.right, lessThanOrEqualTo(tabsViewport.right)); expect(tabsScrollable.position.pixels, greaterThan(0)); await tester.tap(find.byKey(const Key('timeline-list-button'))); await tester.pumpAndSettle(); final selectedMenuItem = find.byKey( const ValueKey('timeline-menu-timeline-20'), ); final menuScrollView = find .ancestor( of: selectedMenuItem, matching: find.byType(SingleChildScrollView), ) .first; expect( tester.getRect(selectedMenuItem).bottom, lessThanOrEqualTo(tester.getRect(menuScrollView).bottom), ); expect( tester .state( find.descendant( of: menuScrollView, matching: find.byType(Scrollable), ), ) .position .pixels, greaterThan(0), ); await tester.tap(find.byKey(const Key('timeline-list-button'))); await tester.pumpAndSettle(); final previousTabOffset = tabsScrollable.position.pixels; await container .read(projectsProvider.notifier) .openTimeline('project', 'timeline-19'); await tester.pumpAndSettle(); expect(tabsScrollable.position.pixels, previousTabOffset); final nearbyTab = tester.getRect( find.byKey(const ValueKey('timeline-tab-timeline-19')), ); expect(nearbyTab.left, greaterThanOrEqualTo(tabsViewport.left)); expect(nearbyTab.right, lessThanOrEqualTo(tabsViewport.right)); await tester.pumpWidget(const SizedBox()); container.dispose(); }, ); testWidgets('timeline tab context menu pins, renames, and deletes', ( tester, ) async { tester.platformDispatcher.localesTestValue = const [Locale('zh')]; addTearDown(tester.platformDispatcher.clearLocalesTestValue); tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final container = ProviderContainer( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_SwitchProjects.new), ], ); addTearDown(container.dispose); await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const CutdexApp()), ); await tester.pumpAndSettle(); Future openMenu() async { await tester.tap( find.byKey(const ValueKey('timeline-tab-second')), buttons: kSecondaryMouseButton, ); await tester.pumpAndSettle(); } await openMenu(); await tester.tap(find.byKey(const ValueKey('timeline-pin-second'))); await tester.pumpAndSettle(); expect( container.read(projectsProvider).requireValue.timelines.first.id, 'second', ); expect( container.read(projectsProvider).requireValue.timelines.first.pinned, isTrue, ); await openMenu(); await tester.tap(find.byKey(const ValueKey('timeline-rename-second'))); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextFormField), '已重命名时间线'); await tester.tap(find.widgetWithText(FilledButton, '重命名')); await tester.pumpAndSettle(); expect( container.read(projectsProvider).requireValue.timelines.first.name, '已重命名时间线', ); await openMenu(); await tester.tap(find.byKey(const ValueKey('timeline-delete-second'))); await tester.pumpAndSettle(); expect(find.textContaining('已重命名时间线'), findsWidgets); await tester.tap(find.widgetWithText(FilledButton, '删除')); await tester.pumpAndSettle(); expect( container .read(projectsProvider) .requireValue .timelines .any((item) => item.id == 'second'), isFalse, ); await tester.pumpWidget(const SizedBox()); container.dispose(); }); testWidgets('timeline limit is enforced and explained on click', ( tester, ) async { tester.platformDispatcher.localesTestValue = const [Locale('zh')]; addTearDown(tester.platformDispatcher.clearLocalesTestValue); tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final container = ProviderContainer( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_TimelineLimitProjects.new), ], ); await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const CutdexApp()), ); await tester.pumpAndSettle(); expect( container.read(projectsProvider).requireValue.timelines, hasLength(Projects.maxTimelineCount), ); expect( await container.read(projectsProvider.notifier).createTimeline(), isFalse, ); final addButton = find.byKey(const Key('timeline-add-button')); expect(tester.widget(addButton).onPressed, isNotNull); await tester.tap(addButton); await tester.pump(); expect(find.text('每个项目最多可创建 50 条时间线。'), findsOneWidget); await tester.pumpWidget(const SizedBox()); container.dispose(); }); testWidgets('macOS single editing surface integrates native controls', ( tester, ) async { tester.platformDispatcher.localesTestValue = const [Locale('zh')]; addTearDown(tester.platformDispatcher.clearLocalesTestValue); debugDefaultTargetPlatformOverride = TargetPlatform.macOS; addTearDown(() => debugDefaultTargetPlatformOverride = null); tester.view.physicalSize = const Size(1200, 800); tester.view.devicePixelRatio = 1; tester.view.padding = const FakeViewPadding(top: 28); addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.view.resetPadding); const channel = MethodChannel('cutdex_native/window'); final calls = []; final closeModes = []; const controlsChannel = MethodChannel('cutdex_native/window_controls'); final controlRegions = {}; final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; messenger.setMockMethodCallHandler(channel, (call) async { final enabled = (call.arguments as Map)['enabled'] as bool; calls.add(enabled); if (enabled) { closeModes.add((call.arguments as Map)['interceptClose'] as bool); } return enabled; }); addTearDown(() => messenger.setMockMethodCallHandler(channel, null)); messenger.setMockMethodCallHandler(controlsChannel, (call) async { final arguments = call.arguments as Map; final id = arguments['id'] as String; final rect = arguments['rect'] as Map?; if (rect == null) { controlRegions.remove(id); } else { controlRegions[id] = Rect.fromLTWH( (rect['x'] as num).toDouble(), (rect['y'] as num).toDouble(), (rect['width'] as num).toDouble(), (rect['height'] as num).toDouble(), ); } return null; }); addTearDown( () => messenger.setMockMethodCallHandler(controlsChannel, null), ); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_TestProjects.new), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); expect( tester.getSize(find.byKey(const Key('native-window-controls-space'))), const Size(68, 44), ); final tabs = find.byKey(const ValueKey('project-tab-project')); expect( controlRegions.values.any( (region) => region.contains(tester.getCenter(tabs)), ), isTrue, ); final taskButton = find.byKey(const ValueKey('background-tasks-button')); expect(taskButton, findsOneWidget); expect( controlRegions.values.any( (region) => region.contains(tester.getCenter(taskButton)), ), isTrue, reason: 'AppKit must forward task button clicks instead of dragging.', ); expect(tester.getCenter(taskButton).dy, 22); for (final key in ['project-sidebar-toggle', 'agent-minimize-button']) { final control = find.byKey(Key(key)); expect( find.descendant( of: find.byKey(const Key('desktop-window-header')), matching: control, ), findsOneWidget, ); expect(tester.getSize(control), const Size.square(28)); expect(tester.getCenter(control).dy, 22); expect( controlRegions.values.any( (region) => region.contains(tester.getCenter(control)), ), isTrue, reason: 'AppKit must forward $key clicks instead of dragging.', ); } final minimizeRect = tester.getRect( find.byKey(const Key('agent-minimize-button')), ); expect( tester.getRect(taskButton).left - minimizeRect.right, AppProjectTabMetrics.gap, ); final addRect = tester.getRect(find.byKey(const Key('project-tabs-add'))); final listRect = tester.getRect(find.byKey(const Key('project-tabs-list'))); final emptyHeaderPoint = Offset( (addRect.right + listRect.left) / 2, addRect.center.dy, ); expect(listRect.left - addRect.right, greaterThan(100)); expect( controlRegions.values.any((region) => region.contains(emptyHeaderPoint)), isFalse, ); await tester.tap(find.byKey(const Key('project-sidebar-toggle'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('desktop-chat-card')), findsNothing); await tester.tap(find.byKey(const Key('project-sidebar-toggle'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('desktop-chat-card')), findsOneWidget); final reply = Completer(); messenger.handlePlatformMessage( channel.name, const StandardMethodCodec().encodeMethodCall( const MethodCall('requestClose'), ), reply.complete, ); expect( const StandardMethodCodec().decodeEnvelope((await reply.future)!), isFalse, ); await tester.pumpAndSettle(); expect(find.textContaining('测试项目'), findsWidgets); expect(calls, [true]); expect(closeModes, [false]); expect( tester.getTopLeft(find.byKey(const Key('project-chat-header'))).dy, 45.0, ); expect(tester.takeException(), isNull); debugDefaultTargetPlatformOverride = null; }); for (final locale in const [ Locale('en'), Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant'), Locale('ja'), Locale('ko'), Locale('fr'), Locale('de'), Locale('es'), Locale('pt'), Locale('it'), Locale('ru'), ]) { testWidgets('small editor viewport fits in $locale', (tester) async { tester.platformDispatcher.localesTestValue = [locale]; addTearDown(tester.platformDispatcher.clearLocalesTestValue); tester.view.physicalSize = const Size(600, 450); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue( DeviceExperience.desktop, ), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_TestProjects.new), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); ProviderScope.containerOf(tester.element(find.byType(CutdexApp))) .read(workspaceViewSettingsProvider.notifier) .setMode(WorkspaceMode.cut); await tester.pumpAndSettle(); expect( find.ancestor( of: find.byType(EditorTimelinePanel), matching: find.byType(SingleChildScrollView), ), findsNothing, ); expect(find.text('视频预览'), findsNothing); expect(find.text('属性'), findsNothing); final timeline = tester.getRect(find.byType(EditorTimelinePanel)); expect(timeline.right, lessThanOrEqualTo(600)); expect(timeline.bottom, lessThanOrEqualTo(450)); final shellContext = tester.element(find.byType(EditorTimelinePanel)); expect(Directionality.of(shellContext), TextDirection.ltr); expect(tester.takeException(), isNull); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); for (final scale in [1.5, 2.0]) { tester.platformDispatcher.textScaleFactorTestValue = scale; await tester.pumpAndSettle(); final scaledTimeline = tester.getRect(find.byType(EditorTimelinePanel)); expect(scaledTimeline.right, lessThanOrEqualTo(600)); expect(scaledTimeline.bottom, lessThanOrEqualTo(450)); expect(tester.takeException(), isNull); } }); } testWidgets('unknown project can return home without a navigation stack', ( tester, ) async { tester.platformDispatcher.localesTestValue = const [Locale('zh')]; addTearDown(tester.platformDispatcher.clearLocalesTestValue); final container = ProviderContainer( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), conversationRepositoryProvider.overrideWith( (ref) async => EmptyConversationRepository(), ), projectsProvider.overrideWith(_TestProjects.new), ], retry: (_, _) => null, ); addTearDown(container.dispose); container.read(appRouterProvider).go('/projects/missing'); await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const CutdexApp()), ); await tester.pumpAndSettle(); await tester.tap(find.text('返回剪辑')); await tester.pumpAndSettle(); expect(find.textContaining('测试项目'), findsWidgets); }); } class _EmptyConversationRepository extends ConversationRepository { _EmptyConversationRepository(super.root); @override Future> list( String projectId, { bool trash = false, }) async => []; @override Future selected(String projectId) async => null; } class _GroupedConversationRepository extends ConversationRepository { _GroupedConversationRepository(this.groups) : super(Directory('/unused')); final Map> groups; @override Future> list( String projectId, { bool trash = false, }) async => trash ? [] : groups[projectId] ?? []; @override Future selected(String projectId) async => groups[projectId]?.firstOrNull?.id; @override Future update(ConversationMetadata item) async { final conversations = groups[item.projectId]!; final index = conversations.indexWhere((entry) => entry.id == item.id); conversations[index] = item; return item; } } class _GroupedProjects extends _TestProjects { @override Future build() async { final state = await super.build(); return state.copyWith( recent: [ ...state.recent, const RecentProject( id: 'other', name: 'Other Project', path: '/test/Other.cutdex', ), ], ); } @override Future open( String path, { String? name, String? expectedId, String? timelineId, String? initialTimelineName, bool Function()? isCurrent, }) async { final current = state.requireValue; final project = current.recent.firstWhere((item) => item.id == expectedId); state = AsyncData(current.copyWith(activeProject: project)); return true; } } class _SwitchProjects extends _TestProjects { @override Future build() async => (await super.build()).copyWith( timelines: [ const TimelineSummary(id: 'project', name: '测试项目', revision: 1), const TimelineSummary(id: 'second', name: '第二条时间线', revision: 1), for (var i = 3; i <= 20; i++) TimelineSummary(id: 'timeline-$i', name: '时间线 $i', revision: 1), ], assets: [ for (var i = 0; i < 30; i++) AssetDocument( id: 'image-$i', name: 'Image $i.png', storage: AssetStorage.external, location: 'file:///image-$i.png', metadata: const {'kind': 'image'}, ), ], ); @override Future openTimeline(String projectId, String timelineId) async { state = AsyncData( state.requireValue.copyWith( timeline: TimelineDocument( id: timelineId, name: timelineId, sequences: [], ), ), ); return true; } @override Future setTimelinePinned(String timelineId, bool pinned) async { final current = state.requireValue; final updated = [ for (final item in current.timelines) TimelineSummary( id: item.id, name: item.name, revision: item.revision, pinned: item.id == timelineId ? pinned : item.pinned, ), ]; state = AsyncData( current.copyWith( timelines: [ ...updated.where((item) => item.pinned), ...updated.where((item) => !item.pinned), ], ), ); return true; } @override Future renameTimeline( RecentProject project, String timelineId, String name, ) async { final current = state.requireValue; state = AsyncData( current.copyWith( timelines: [ for (final item in current.timelines) TimelineSummary( id: item.id, name: item.id == timelineId ? name : item.name, revision: item.revision, pinned: item.pinned, ), ], ), ); return true; } @override Future deleteTimeline(String timelineId) async { final current = state.requireValue; final timelines = current.timelines .where((item) => item.id != timelineId) .toList(); state = AsyncData( current.copyWith( timelines: timelines, timeline: current.timeline?.id == timelineId ? TimelineDocument( id: timelines.first.id, name: timelines.first.name, sequences: [], ) : current.timeline, ), ); return true; } } class _TimelineLimitProjects extends Projects { @override Future build() async { const project = RecentProject( id: 'project', name: '测试项目', path: '/test/Project.cutdex', ); return ProjectState( activeProject: project, recent: const [project], timeline: TimelineDocument(id: 'project', name: '测试时间线', sequences: []), timelines: [ const TimelineSummary(id: 'project', name: '测试时间线', revision: 1), for (var i = 2; i <= Projects.maxTimelineCount; i++) TimelineSummary(id: 'timeline-$i', name: '时间线 $i', revision: 1), ], ); } } class _LoadingProjects extends Projects { @override Future build() => Completer().future; } class _BusyProjects extends _TestProjects { @override Future build() async => (await super.build()).copyWith(projectBusy: true); } class _TestProjects extends Projects { @override Future build() async { const project = RecentProject( id: 'project', name: '测试项目', path: '/test/Project.cutdex', ); return ProjectState( activeProject: project, recent: [project], timelines: [ const TimelineSummary(id: 'project', name: '测试时间线', revision: 1), ], timeline: TimelineDocument(id: 'project', name: '测试时间线', sequences: []), ); } @override Future open( String path, { String? name, String? expectedId, String? timelineId, String? initialTimelineName, bool Function()? isCurrent, }) async => true; @override Future renameProject(RecentProject project, String name) async { final current = state.requireValue; final renamed = project.copyWith(name: name); state = AsyncData( current.copyWith(activeProject: renamed, recent: [renamed]), ); return true; } @override Future renameTimeline( RecentProject project, String timelineId, String name, ) async { final current = state.requireValue; state = AsyncData( current.copyWith( timelines: [TimelineSummary(id: timelineId, name: name, revision: 1)], timeline: TimelineDocument(id: timelineId, name: name, sequences: []), ), ); return true; } @override Future createTimeline({String? name}) async { final current = state.requireValue; final index = current.timelines.length; final id = 'created-$index'; final created = TimelineSummary( id: id, name: name ?? 'Timeline $index', revision: 1, ); state = AsyncData( current.copyWith( timelines: [...current.timelines, created], timeline: TimelineDocument(id: id, name: created.name, sequences: []), ), ); return true; } } class _MemoryWorkspaceViewSettings implements WorkspaceViewSettingsRepository { WorkspaceViewSettingsData value = const WorkspaceViewSettingsData(); @override Future load() async => value; @override Future save(WorkspaceViewSettingsData settings) async => value = settings; } class _UnselectedProjects extends _TestProjects { @override Future build() async { final initial = await super.build(); return ProjectState(recent: initial.recent); } @override Future open( String path, { String? name, String? expectedId, String? timelineId, String? initialTimelineName, bool Function()? isCurrent, }) async { state = AsyncData(await super.build()); return true; } } class _DelayedProjects extends _GroupedProjects { final requests = <(String, Completer)>[]; @override Future build() async { final initial = await super.build(); return initial.copyWith( recent: [ ...initial.recent, const RecentProject( id: 'third', name: 'Third Project', path: '/test/Third.cutdex', ), ], ); } @override Future open( String path, { String? name, String? expectedId, String? timelineId, String? initialTimelineName, bool Function()? isCurrent, }) async { final completion = Completer(); requests.add((expectedId!, completion)); state = AsyncData(state.requireValue.copyWith(projectBusy: true)); final completed = await completion.future; final success = completed && (isCurrent?.call() ?? true); final current = state.requireValue; state = AsyncData( current.copyWith( projectBusy: false, activeProject: success ? current.recent.firstWhere((project) => project.id == expectedId) : current.activeProject, ), ); return success; } }