import 'package:cutdex/workspace/workspace_mode.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/l10n/app_localizations.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/assistant/agent_chat_panel.dart'; import 'package:cutdex/features/editor/editor_page.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'; Future _hoverProjectHeader(WidgetTester tester) async { final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: Offset.zero); await mouse.moveTo( tester.getCenter(find.byKey(const Key('projects-heading'))), ); await tester.pumpAndSettle(); addTearDown(mouse.removePointer); } void main() { 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); final projectBar = find.byKey(const Key('composer-project-bar')); final composer = find.byKey(const Key('agent-composer')); expect(tester.getSize(projectBar).height, 60); expect( tester.getRect(projectBar).left, tester.getRect(composer).left + 13, ); expect( tester.getRect(projectBar).right, tester.getRect(composer).right - 13, ); expect(tester.getRect(projectBar).top + 38, tester.getRect(composer).top); expect(find.text('Select project'), findsOneWidget); await tester.enterText(input, 'Keep this before selecting'); await tester.tap(find.byKey(const Key('composer-project-trigger'))); await tester.pumpAndSettle(); final search = find.byKey(const Key('composer-project-search')); await tester.enterText(search, 'unmatched'); await tester.pumpAndSettle(); expect(find.text('No matching projects'), findsOneWidget); expect(find.byKey(const Key('composer-new-project')), findsOneWidget); await tester.enterText(search, '测试'); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('composer-project-project'))); await tester.pumpAndSettle(); expect(search, findsNothing); expect( tester.widget(input).controller!.text, 'Keep this before selecting', ); expect(find.text('Select project'), findsNothing); await tester.tap(find.byKey(const Key('composer-project-trigger'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('composer-new-project'))); await tester.pumpAndSettle(); expect(search, findsNothing); 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(); await tester.pumpWidget( ProviderScope( overrides: [ deviceExperienceProvider.overrideWithValue(DeviceExperience.desktop), projectsProvider.overrideWith(() => projects), conversationRepositoryProvider.overrideWith( (ref) async => _GroupedConversationRepository({}), ), ], child: const CutdexApp(), ), ); await tester.pumpAndSettle(); bool selected(String id) => tester .widget(find.byKey(ValueKey('sidebar-project-$id'))) .selected; Future click(String id) async { await tester.tap(find.byKey(ValueKey('sidebar-project-$id'))); await tester.pump(); } await click('other'); expect(selected('other'), isTrue); expect(projects.requests, isEmpty); 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(milliseconds: 70)); expect(find.byKey(const Key('editor-project-loading')), findsOneWidget); 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('editor-project-loading')), findsNothing); await click('other'); await tester.pump(const Duration(milliseconds: 100)); projects.requests.last.$2.complete(true); 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( tester .widget(find.byKey(const Key('create-project'))) .onPressed, isNotNull, ); expect( tester .widget( find.byKey(const ValueKey('sidebar-project-project')), ) .enabled, isTrue, ); expect(find.byKey(const Key('projects-heading')), findsOneWidget); final addTimeline = find.byKey(const Key('timeline-add-button')); expect(tester.widget(addTimeline).onPressed, isNotNull); expect( find.descendant(of: addTimeline, matching: find.byIcon(Icons.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('projects-heading'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('project-popover')), findsNothing); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: Offset.zero); await mouse.moveTo( tester.getCenter(find.byKey(const ValueKey('sidebar-project-project'))), ); await tester.pumpAndSettle(); await tester.tap( find.byKey(const ValueKey('new-agent-conversation-project')), ); await mouse.removePointer(); await tester.pumpAndSettle(); expect(await tester.runAsync(repository.root.exists), isFalse); }); testWidgets('sidebar mode is saved and restored after rebuilding the app', ( 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 { await tester.tap(find.byKey(const Key('sidebar-mode-trigger'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(ValueKey('sidebar-mode-$name'))); await tester.pumpAndSettle(); } await launch(); expect(find.byKey(const Key('agent-mode-layout')), findsOneWidget); await switchMode('cut'); expect(settings.value.mode, WorkspaceMode.cut); await tester.pumpWidget(const SizedBox()); await launch(); expect(find.byKey(const Key('cut-mode-layout')), findsOneWidget); await switchMode('agent'); expect(settings.value.mode, WorkspaceMode.agent); await tester.pumpWidget(const SizedBox()); await launch(); expect(find.byKey(const Key('agent-mode-layout')), findsOneWidget); }); testWidgets('sidebar mode defaults to Agent and can select Cut', ( 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('agent-mode-layout')), findsOneWidget); Color chatSurface() => tester .element(find.byKey(const Key('agent-conversation'))) .findAncestorWidgetOfExactType()! .color!; ColorScheme chatColors() => Theme.of(tester.element(find.byKey(const Key('agent-conversation')))) .colorScheme; expect( tester .widget(find.byKey(const Key('agent-conversation'))) .child, isNot(isA()), ); expect(chatSurface(), chatColors().surface); final projectTitle = find.byKey(const Key('workspace-project-title')); final titlePosition = tester.getTopLeft(projectTitle); expect( find.descendant( of: find.byKey(const Key('agent-content-header')), matching: projectTitle, ), findsOneWidget, ); final chatElement = tester.element(find.byType(AgentChatPanel)); final editorElement = tester.element(find.byType(EditorWorkspace)); final input = find.descendant( of: find.byType(AgentChatPanel), matching: find.byType(TextField), ); await tester.enterText(input, 'Keep this draft'); final trigger = find.byKey(const Key('sidebar-mode-trigger')); final modeButton = tester.widget(trigger); expect(modeButton.style!.backgroundColor!.resolve({}), Colors.transparent); expect( modeButton.style!.backgroundColor!.resolve({WidgetState.hovered}), isNot(Colors.transparent), ); expect(tester.getSize(trigger).width, lessThan(160)); expect( tester.getSize(find.byKey(const Key('projects-window-header'))).height, AppControlSize.headerButton, ); expect( find.descendant(of: trigger, matching: find.text('Agent')), findsOneWidget, ); await tester.tap(trigger); await tester.pumpAndSettle(); final modeStrings = AppLocalizations.of(tester.element(trigger))!; expect(find.text(modeStrings.agentLayoutDescription), findsOneWidget); expect(find.text(modeStrings.editingLayoutDescription), findsOneWidget); expect( tester.getSize(find.byKey(const ValueKey('sidebar-mode-cut'))).height, 56, ); await tester.tap(find.byKey(const ValueKey('sidebar-mode-cut'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('cut-mode-layout')), findsOneWidget); expect(chatSurface(), chatColors().surfaceContainerLow); expect(tester.getTopLeft(projectTitle), titlePosition); expect( find.descendant( of: find.byKey(const Key('editor-content-header')), matching: projectTitle, ), findsOneWidget, ); expect( find.byKey(const Key('sidebar-projects-conversations')), findsNothing, ); expect(tester.element(find.byType(AgentChatPanel)), same(chatElement)); expect(tester.element(find.byType(EditorWorkspace)), same(editorElement)); expect(tester.widget(input).controller!.text, 'Keep this draft'); await tester.tap(find.byKey(const Key('cut-projects-trigger'))); await tester.pumpAndSettle(); expect( find.byKey(const Key('sidebar-projects-conversations')), findsOneWidget, ); await tester.tap(find.byKey(const ValueKey('sidebar-project-project'))); await tester.pumpAndSettle(); expect( find.byKey(const Key('sidebar-projects-conversations')), findsNothing, ); expect( find.descendant(of: trigger, matching: find.text('Cut')), findsOneWidget, ); await tester.tap(trigger); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('sidebar-mode-agent'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('agent-mode-layout')), findsOneWidget); expect(tester.element(find.byType(AgentChatPanel)), same(chatElement)); expect(tester.element(find.byType(EditorWorkspace)), same(editorElement)); expect(tester.widget(input).controller!.text, 'Keep this draft'); }); testWidgets('Agent right edge resizes independently and respects limits', ( 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(); final navigation = find.byKey(const Key('sidebar-projects-conversations')); final resize = find.byKey(const Key('agent-panel-resize')); final navigationWidth = tester.getSize(navigation).width; final chatWidth = tester.getSize(find.byType(AgentChatPanel)).width; final chatElement = tester.element(find.byType(AgentChatPanel)); await tester.drag(resize, const Offset(80, 0)); await tester.pumpAndSettle(); expect(tester.getSize(find.byType(AgentChatPanel)).width, chatWidth + 80); expect(tester.getSize(navigation).width, navigationWidth); expect(tester.element(find.byType(AgentChatPanel)), same(chatElement)); await tester.drag(resize, const Offset(-2000, 0)); await tester.pumpAndSettle(); expect( tester.getSize(find.byType(AgentChatPanel)).width, greaterThanOrEqualTo(219), ); await tester.drag(resize, const Offset(2000, 0)); await tester.pumpAndSettle(); expect(find.byType(EditorWorkspace), findsNothing); expect(tester.getSize(navigation).width, navigationWidth); expect(tester.takeException(), isNull); }); testWidgets('Agent buffers collapse by one third and restores at 560', ( 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(); final editor = tester.element(find.byType(EditorWorkspace)); final resize = find.byKey(const Key('agent-panel-resize')); await tester.drag(resize, const Offset(159, 0)); await tester.pumpAndSettle(); expect(tester.getSize(find.byType(EditorWorkspace)).width, 480); final drag = await tester.startGesture(tester.getCenter(resize)); await drag.moveBy(const Offset(159, 0)); await tester.pumpAndSettle(); expect(tester.getSize(find.byType(EditorWorkspace)).width, 480); await drag.moveBy(const Offset(1, 0)); await drag.up(); await tester.pumpAndSettle(); expect(find.byType(EditorWorkspace), findsNothing); expect( tester.element(find.byType(EditorWorkspace, skipOffstage: false)), same(editor), ); await tester.drag(resize, const Offset(-560, 0)); await tester.pumpAndSettle(); expect(find.byType(EditorWorkspace), findsNothing); await tester.drag(resize, const Offset(-561, 0)); await tester.pumpAndSettle(); expect(tester.getSize(find.byType(EditorWorkspace)).width, 560); expect(tester.element(find.byType(EditorWorkspace)), same(editor)); await tester.drag(resize, const Offset(2000, 0)); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('expand-editor-button'))); await tester.pumpAndSettle(); expect(tester.getSize(find.byType(EditorWorkspace)).width, 560); expect(tester.element(find.byType(EditorWorkspace)), same(editor)); expect(find.byKey(const Key('expand-editor-button')), findsNothing); expect(tester.takeException(), isNull); }); testWidgets('expand editor collapses project sidebar when space is limited', ( tester, ) async { tester.view.physicalSize = const Size(1000, 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(); final chat = tester.element(find.byType(AgentChatPanel)); await tester.drag( find.byKey(const Key('agent-panel-resize')), const Offset(2000, 0), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('expand-editor-button'))); await tester.pumpAndSettle(); expect( find.byKey(const Key('sidebar-projects-conversations')), findsNothing, ); expect(tester.getSize(find.byType(EditorWorkspace)).width, 561); expect(tester.element(find.byType(AgentChatPanel)), same(chat)); expect(tester.takeException(), isNull); }); testWidgets('sidebar groups conversations under projects', (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('sidebar-project-project')), findsOneWidget, ); expect(find.byKey(const ValueKey('sidebar-project-other')), findsOneWidget); expect( find.byKey(const ValueKey('sidebar-conversation-project-1')), findsOneWidget, ); expect( find.byKey(const ValueKey('sidebar-conversation-project-6')), findsNothing, ); expect( find.byKey(const ValueKey('sidebar-conversation-other-1')), findsOneWidget, ); final projectRow = find.byKey(const ValueKey('sidebar-project-project')); final chatRow = find.byKey( const ValueKey('sidebar-conversation-project-1'), ); expect(tester.getSize(projectRow).height, 30); expect(tester.getSize(chatRow).height, 30); final selectedProjectTile = tester.widget(projectRow); expect(selectedProjectTile.selected, isTrue); expect( selectedProjectTile.hoverColor, AppSidebarColors.of(tester.element(projectRow)).hover, ); expect( selectedProjectTile.focusColor, AppSidebarColors.of(tester.element(projectRow)).hover, ); final modeButton = find.byKey(const Key('sidebar-mode-trigger')); expect( tester.getTopLeft(modeButton).dy, tester.getBottomLeft(find.byKey(const Key('projects-window-header'))).dy, ); expect( tester.getTopLeft(find.byKey(const Key('projects-heading'))).dy - tester.getBottomLeft(modeButton).dy, 4, ); final conversationText = tester.widget( find.descendant(of: chatRow, matching: find.text('Conversation 1')), ); expect(conversationText.style?.fontSize, 14); expect(conversationText.style?.height, closeTo(20 / 14, 0.001)); expect(conversationText.style?.fontVariations, const [ FontVariation('wght', 430), ]); expect( conversationText.style?.color, const Color(0xFFFCFCFC).withValues(alpha: 0.85), ); expect( tester .getTopLeft( find.byKey(const ValueKey('sidebar-conversation-project-2')), ) .dy - tester.getTopLeft(chatRow).dy, 31, ); expect(tester.getRect(chatRow).left, tester.getRect(projectRow).left); expect(tester.getRect(chatRow).right, tester.getRect(projectRow).right); expect( tester .getTopLeft( find.descendant(of: chatRow, matching: find.text('Conversation 1')), ) .dx, tester .getTopLeft( find.descendant(of: projectRow, matching: find.byType(Text)).first, ) .dx, ); await tester.tap(chatRow, buttons: kSecondaryMouseButton); await tester.pumpAndSettle(); await tester.tap(find.text('Rename')); await tester.pumpAndSettle(); expect(find.byKey(const Key('app-dialog-frame')), findsOneWidget); expect(find.text('Rename chat'), findsOneWidget); expect(find.text('Keep it short and easy to recognize.'), findsOneWidget); 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(find.text('Updated chat'), findsOneWidget); await tester.tap(chatRow, buttons: kSecondaryMouseButton); await tester.pumpAndSettle(); await tester.tap(find.text('Delete permanently…')); await tester.pumpAndSettle(); expect(find.byKey(const Key('app-dialog-frame')), findsOneWidget); expect(find.textContaining('This cannot be undone.'), findsOneWidget); await tester.tap(find.text('Cancel')); await tester.pumpAndSettle(); expect(chatRow, findsOneWidget); await tester.tap( find.byKey(const ValueKey('sidebar-project-toggle-project')), ); await tester.pumpAndSettle(); expect(chatRow, findsNothing); expect( tester .getTopLeft(find.byKey(const ValueKey('sidebar-project-other'))) .dy - tester.getBottomLeft(projectRow).dy, 1, ); await tester.tap( find.byKey(const ValueKey('sidebar-project-toggle-project')), ); await tester.pumpAndSettle(); await tester.tap(find.text('Show more')); await tester.pumpAndSettle(); expect( find.byKey(const ValueKey('sidebar-conversation-project-7')), findsOneWidget, ); }); testWidgets( 'empty project hint follows expansion and project menu offers removal', (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(); final hint = find.byKey(const ValueKey('sidebar-project-empty-other')); expect(hint, findsOneWidget); await tester.tap( find.byKey(const ValueKey('sidebar-project-toggle-other')), ); await tester.pumpAndSettle(); expect(hint, findsNothing); await tester.tap( find.byKey(const ValueKey('sidebar-project-other')), buttons: kSecondaryMouseButton, ); await tester.pumpAndSettle(); expect(find.text('Remove from sidebar'), findsOneWidget); }, ); testWidgets('project header more is inert and project rows switch projects', ( 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(); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: Offset.zero); await mouse.moveTo( tester.getCenter(find.byKey(const Key('projects-heading'))), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('projects-more'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('project-popover')), findsNothing); await tester.tap(find.byKey(const ValueKey('sidebar-project-other'))); await tester.pumpAndSettle(); expect( tester .widget(find.byKey(const ValueKey('sidebar-project-other'))) .selected, isTrue, ); await tester.tap(find.byKey(const ValueKey('sidebar-project-project'))); await tester.pumpAndSettle(); expect( tester .widget( find.byKey(const ValueKey('sidebar-project-project')), ) .selected, isTrue, ); await mouse.removePointer(); }); 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('sidebar-project-project')), '重命名项目', ); expect( tester .widget(find.byKey(const Key('workspace-project-title'))) .data, '重命名项目', ); }); 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, 26, ); 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, 12, ); final projectTabFinder = find.byKey( const ValueKey('timeline-tab-project'), ); final selectedInk = Theme.of(tester.element(projectTabFinder)) .colorScheme .onSurface; expect( tester .widget(projectTabFinder) .style! .backgroundColor! .resolve({}), selectedInk.withValues(alpha: .05), ); 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.byKey(const Key('projects-heading')), ); final workspace = tester.element(find.byType(EditorWorkspace)); await tester.tap(find.byKey(const Key('editor-media-button'))); 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)); 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('projects-window-header'))), isNotNull, ); expect( tester.element(find.byKey(const Key('projects-heading'))), 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)), 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('editor-media-region')), 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( tester .widget(find.byKey(const Key('workspace-project-title'))) .data, '测试项目', ); 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, ); }); 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.getTopLeft(find.byKey(const Key('projects-window-header'))).dy, 0, ); expect( tester.getTopLeft(find.byKey(const Key('sidebar-mode-trigger'))).dy, AppControlSize.panelHeader, ); expect( tester.getSize(find.byKey(const Key('native-window-controls-space'))), const Size(88, AppControlSize.panelHeader), ); expect( tester.getSize(find.byKey(const Key('project-sidebar-separator'))).width, 1, ); final sidebarSeparator = tester.widget( find.byKey(const Key('project-sidebar-separator')), ); expect( sidebarSeparator.color, Theme.of( tester.element(find.byKey(const Key('project-sidebar-separator'))), ).colorScheme.outlineVariant, ); expect( tester.getSize(find.byKey(const Key('project-sidebar-separator'))).height, tester.getSize(find.byType(EditorWorkspace)).height, ); final sidebarToggle = find.byKey(const Key('project-sidebar-toggle')); expect( tester.getRect(sidebarToggle).center.dy, AppControlSize.panelHeader / 2, ); final projectSettings = find.byKey(const Key('project-settings-button')); final backgroundTasks = find.byKey( const ValueKey('background-tasks-button'), ); expect(projectSettings, findsOneWidget); expect(backgroundTasks, findsOneWidget); expect( tester.getRect(sidebarToggle).right + AppSpacing.tiny, tester.getRect(projectSettings).left, ); expect( tester.getRect(projectSettings).center.dy, AppControlSize.panelHeader / 2, ); expect(tester.getTopLeft(sidebarToggle).dx, 88); await tester.tap(sidebarToggle); await tester.pump(); expect(find.byKey(const ValueKey('open-project-project')), findsNothing); expect(find.byTooltip('展开侧边栏'), findsOneWidget); expect(projectSettings, findsOneWidget); expect(backgroundTasks, findsOneWidget); final timelineTitle = find.byKey(const Key('workspace-project-title')); expect( tester.getRect(backgroundTasks).right + AppSpacing.compact, lessThanOrEqualTo(tester.getRect(timelineTitle).left), ); expect( tester.getTopLeft(find.byType(EditorPreviewPanel)).dx, tester.getRect(find.byType(AgentChatPanel)).right, ); expect(sidebarToggle, findsOneWidget); await tester.tap(sidebarToggle); await tester.pump(); expect(find.byKey(const Key('projects-heading')), findsOneWidget); expect(find.byTooltip('收起侧边栏'), findsOneWidget); await _hoverProjectHeader(tester); await tester.tap(find.byKey(const Key('projects-more'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('project-popover')), findsNothing); expect( find.byKey(const ValueKey('sidebar-project-project')), findsOneWidget, ); expect(find.byKey(const ValueKey('open-timeline-project')), findsNothing); expect( tester .getSize(find.byKey(const ValueKey('sidebar-project-project'))) .height, 30, ); final projectTitle = tester.widget( find.descendant( of: find.byKey(const ValueKey('sidebar-project-project')), matching: find.text('测试项目'), ), ); expect(projectTitle.style?.fontSize, 14); final projectRow = find.byKey(const ValueKey('sidebar-project-project')); expect( find.descendant(of: projectRow, matching: find.byIcon(Icons.add)), findsNothing, ); expect(find.byKey(const Key('projects-section-header')), findsNothing); expect(find.byKey(const Key('projects-more')), findsOneWidget); expect( tester.getSize(find.byKey(const Key('create-project'))), const Size.square(24), ); expect( tester.getRect(find.byKey(const Key('projects-heading'))).right + AppSpacing.tiny, tester.getRect(find.byKey(const Key('projects-more'))).left, ); expect( find.descendant(of: projectRow, matching: find.byIcon(Icons.add)), findsNothing, ); expect( tester.getTopLeft(find.byKey(const Key('project-sidebar-resize'))).dx, 256, ); expect( tester.getTopLeft(find.byType(EditorPreviewPanel)).dy, AppControlSize.panelHeader, ); expect(find.byKey(const Key('editor-content-header')), findsOneWidget); final editingTopDivider = find.byKey(const Key('editing-top-divider')); expect(editingTopDivider, findsOneWidget); expect( tester.getRect(editingTopDivider).top, tester.getRect(find.byType(EditorPreviewPanel)).top, ); expect( tester.getRect(editingTopDivider).left, tester.getRect(find.byType(EditorPreviewPanel)).left, ); tester.view.physicalSize = const Size(500, 450); await tester.pumpAndSettle(); final navigationButton = find.byKey(const Key('project-sidebar-toggle')); expect( controlRegions.values.any( (region) => region.contains(tester.getCenter(navigationButton)), ), isTrue, reason: 'AppKit must forward navigation clicks instead of dragging.', ); await tester.tap(navigationButton); await tester.pumpAndSettle(); expect( find.byKey(const ValueKey('sidebar-project-project')), findsOneWidget, ); final closeNavigation = find.byTooltip('关闭'); expect(tester.getRect(closeNavigation).left, greaterThanOrEqualTo(88)); expect( controlRegions.values.any( (region) => region.contains(tester.getCenter(closeNavigation)), ), isTrue, ); await tester.tap(closeNavigation); await tester.pumpAndSettle(); expect( tester.getRect(find.byKey(const Key('agent-content-header'))).left, 0, ); 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('agent-content-header'))).dy, 0, ); expect(tester.takeException(), isNull); debugDefaultTargetPlatformOverride = null; }); testWidgets('editing page keeps sidebar and editing workspace', ( tester, ) async { tester.platformDispatcher.localesTestValue = const [Locale('zh')]; addTearDown(tester.platformDispatcher.clearLocalesTestValue); tester.view.physicalSize = const Size(1440, 900); 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(); final projectHeading = find.byKey(const Key('projects-heading')); final createProjectButton = find.byKey(const Key('create-project')); expect(projectHeading, findsOneWidget); expect(createProjectButton, findsOneWidget); expect( tester.getRect(projectHeading).right + AppSpacing.tiny, tester.getRect(find.byKey(const Key('projects-more'))).left, ); expect(find.byKey(const ValueKey('open-project-project')), findsNothing); expect(find.byKey(const ValueKey('open-timeline-project')), findsNothing); expect(find.byType(AgentChatPanel), findsOneWidget); final upperSidebar = find.byKey( const Key('sidebar-projects-conversations'), ); final chatDivider = find.byKey(const Key('sidebar-chat-divider')); expect(upperSidebar, findsOneWidget); expect(chatDivider, findsOneWidget); expect( tester.getRect(upperSidebar).right, tester.getRect(chatDivider).left, ); expect( tester.getRect(find.byType(AgentChatPanel)).left, tester.getRect(chatDivider).right, ); expect( tester.getRect(find.byKey(const Key('agent-composer'))).bottom, lessThanOrEqualTo(tester.getRect(find.byType(AgentChatPanel)).bottom), ); expect( tester.getRect(find.byType(AgentChatPanel)).right, lessThanOrEqualTo(tester.getRect(find.byType(EditorWorkspace)).left), ); await _hoverProjectHeader(tester); await tester.tap(find.byKey(const Key('projects-more'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('projects-section-header')), findsNothing); expect(find.byKey(const Key('project-popover')), findsNothing); expect(find.byKey(const ValueKey('open-timeline-project')), findsNothing); expect(find.byKey(const Key('editor-content-header')), findsOneWidget); expect(find.byKey(const Key('editor-media-button')), findsOneWidget); expect(find.byKey(const Key('timeline-tabs-bar')), findsOneWidget); expect(find.byKey(const Key('timeline-add-button')), findsOneWidget); expect(find.byKey(const ValueKey('timeline-tab-project')), findsOneWidget); await tester.tap(find.byKey(const Key('timeline-add-button'))); await tester.pumpAndSettle(); expect(find.text('时间线 1'), findsWidgets); expect( find.byKey(const ValueKey('timeline-tab-created-1')), findsOneWidget, ); final timelineTitleOffset = tester.getRect(find.byKey(const Key('workspace-project-title'))).left - tester.getRect(find.byType(EditorWorkspace)).left; expect( timelineTitleOffset, lessThan( tester.getRect(find.byKey(const Key('editor-media-button'))).left - tester.getRect(find.byType(EditorWorkspace)).left, ), ); final sidebarDivider = find.byKey(const Key('project-sidebar-resize')); final sidebarLeftBefore = tester.getTopLeft(sidebarDivider).dx; await tester.drag(sidebarDivider, const Offset(80, 0)); await tester.pumpAndSettle(); expect( tester.getTopLeft(sidebarDivider).dx, greaterThan(sidebarLeftBefore), ); final agentWidth = tester.getSize(find.byType(AgentChatPanel)).width; await tester.drag(sidebarDivider, const Offset(2000, 0)); await tester.pumpAndSettle(); expect( tester .getSize(find.byKey(const Key('sidebar-projects-conversations'))) .width, 360, ); expect(tester.getSize(find.byType(AgentChatPanel)).width, agentWidth); final preview = tester.getTopLeft(find.byType(EditorPreviewPanel)); final timeline = tester.getTopLeft(find.byType(EditorTimelinePanel)); final editingTopDivider = find.byKey(const Key('editing-top-divider')); expect(editingTopDivider, findsOneWidget); expect(timeline.dx, tester.getTopLeft(find.byType(EditorPreviewPanel)).dx); expect(timeline.dy, greaterThan(preview.dy)); expect(find.byType(AgentChatPanel), findsOneWidget); expect(find.byKey(const Key('agent-top-divider')), findsNothing); expect(find.byType(EditorPropertiesPanel), findsNothing); final editingPropertiesButton = find.byKey( const Key('editor-properties-button'), ); expect(editingPropertiesButton, findsOneWidget); await tester.tap(editingPropertiesButton); await tester.pumpAndSettle(); expect(find.byType(EditorPropertiesPanel), findsOneWidget); await tester.tap(editingPropertiesButton); await tester.pumpAndSettle(); expect(find.byType(EditorPropertiesPanel), findsNothing); expect(tester.takeException(), isNull); final timelineResize = find.byKey(const Key('editor-timeline-resize')); final timelineHeightBefore = tester .getSize(find.byType(EditorTimelinePanel)) .height; await tester.drag(timelineResize, const Offset(0, -80)); await tester.pumpAndSettle(); expect( tester.getSize(find.byType(EditorTimelinePanel)).height, greaterThan(timelineHeightBefore), ); final mediaButton = find.byKey(const Key('editor-media-button')); final propertiesButton = find.byKey(const Key('editor-properties-button')); expect( tester.getRect(mediaButton).right + AppSpacing.tiny, tester.getRect(propertiesButton).left, ); expect( find.descendant( of: find.byKey(const Key('editor-media-button')), matching: find.byIcon(Icons.perm_media_outlined), ), findsOneWidget, ); expect(find.byKey(const Key('workspace-layout-menu')), findsNothing); expect(find.byKey(const Key('editor-editing-toggle')), findsNothing); expect(find.byKey(const Key('agent-editing-divider')), findsNothing); expect( tester.getTopLeft(find.byType(EditorTimelinePanel)).dy, greaterThan(tester.getTopLeft(find.byType(EditorPreviewPanel)).dy), ); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); expect(find.byType(MediaPanel), findsOneWidget); expect(tester.widget(mediaButton).isSelected, isTrue); expect(find.byType(Dialog), findsNothing); expect(find.byKey(const Key('media-panel-header')), findsOneWidget); expect(find.byKey(const Key('media-panel-body')), findsOneWidget); final mediaRegion = find.byKey(const Key('editor-media-region')); expect(mediaRegion, findsOneWidget); expect(find.byKey(const Key('editor-properties-region')), findsNothing); expect( tester.getRect(find.byType(MediaPanel)), tester.getRect(mediaRegion), ); expect( tester.getRect(mediaRegion).left, lessThan(tester.getRect(find.byType(EditorPreviewPanel)).left), ); expect( tester.getRect(mediaRegion).right, lessThanOrEqualTo(tester.getRect(find.byType(EditorPreviewPanel)).left), ); await tester.tap(find.byKey(const Key('media-layout-toggle'))); await tester.pumpAndSettle(); expect(find.byTooltip('宫格布局'), findsOneWidget); await tester.tap(find.byKey(const Key('editor-properties-button'))); await tester.pumpAndSettle(); final propertiesRegion = find.byKey(const Key('editor-properties-region')); expect(mediaRegion, findsOneWidget); expect(find.byType(MediaPanel), findsOneWidget); expect(propertiesRegion, findsOneWidget); expect(find.byType(EditorPropertiesPanel), findsOneWidget); expect( tester.getRect(mediaRegion).right, lessThanOrEqualTo(tester.getRect(find.byType(EditorPreviewPanel)).left), ); expect( tester.getRect(propertiesRegion).left, greaterThanOrEqualTo( tester.getRect(find.byType(EditorPreviewPanel)).right, ), ); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); expect(mediaRegion, findsNothing); expect(propertiesRegion, findsOneWidget); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); expect(mediaRegion, findsOneWidget); expect(propertiesRegion, findsOneWidget); expect(find.byTooltip('宫格布局'), findsOneWidget); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); expect(mediaRegion, findsNothing); expect(propertiesRegion, findsOneWidget); expect(tester.widget(mediaButton).isSelected, isFalse); expect(tester.takeException(), isNull); }); 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(); await tester.tap(find.byKey(const Key('sidebar-mode-trigger'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const ValueKey('sidebar-mode-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), ], ); 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; } }