import 'package:cutdex/errors/app_failure.dart'; import 'dart:async'; import 'package:cutdex/features/assistant/agent_composer_store.dart'; import 'package:cutdex/features/projects/conversation_sidebar.dart'; import 'package:cutdex/integrations/platform/file_picker.dart'; import 'package:cutdex/workspace/external_media_import.dart'; import 'package:cutdex/workspace/media_import.dart'; import 'package:cutdex/app/adaptive/workspace_layout_policy.dart'; import 'package:cutdex/app/app.dart'; import 'package:cutdex/features/assistant/agent_chat_panel.dart'; import 'package:cutdex/features/editor/editor_page.dart'; import 'package:cutdex/features/media/media_browser_controller.dart'; import 'package:cutdex/features/media/media_browser_page.dart'; import 'package:cutdex/features/media/media_panel.dart'; import 'package:cutdex/features/properties/properties_editor_page.dart'; import 'package:cutdex/features/properties/properties_panel.dart'; import 'package:cutdex/features/preview/preview_panel.dart'; import 'package:cutdex/features/timeline/timeline_panel.dart'; import 'package:cutdex/integrations/agent/conversation_repository.dart'; import 'package:cutdex/integrations/platform/device_experience.dart'; import 'package:cutdex/persistence/app_database.dart'; import 'package:cutdex/persistence/app_settings_repositories.dart'; import 'package:cutdex/shared/layout/workspace_layout_metrics.dart'; import 'package:cutdex/workspace/media_availability.dart'; import 'package:cutdex/workspace/project_state.dart'; import 'package:cutdex/workspace/projects.dart'; import 'package:cutdex/workspace/timeline_provider.dart'; import 'package:cutdex/workspace/timeline_session.dart'; import 'package:cutdex/workspace/view_settings.dart'; import 'package:cutdex/workspace/workspace_mode.dart'; import 'package:cutdex_sdk/project.dart'; import 'package:cutdex_sdk/timeline/model.dart'; import 'package:drift/native.dart'; import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'support/empty_conversation_repository.dart'; import 'support/preview_fixture.dart'; void main() { test('space classes and two-pane decisions do not depend on workflow', () { expect(WorkspaceLayoutPolicy.desktopNavigationFits(560), isFalse); expect(WorkspaceLayoutPolicy.desktopNavigationFits(561), isTrue); expect(WorkspaceLayoutPolicy.showAgentBesideEditor(839), isFalse); expect(WorkspaceLayoutPolicy.showAgentBesideEditor(840), isTrue); }); Future< ({ ProviderContainer container, EditorTimelineSession session, String projectId, Future Function() close, }) > mount( WidgetTester tester, Size size, { DeviceExperience experience = DeviceExperience.desktop, MediaFileResolver? delayedResolver, AppFilePicker? picker, ConversationRepository? conversations, MediaImportResult? importResult, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final fixture = (await tester.runAsync( () => createPreviewFixture(Uri.file('/test/photo.png'), mediaType: 'image'), ))!; final database = AppDatabase(NativeDatabase.memory()); final projectId = fixture.workspace.project!.info.id; final container = ProviderContainer( overrides: [ deviceExperienceProvider.overrideWithValue(experience), if (picker != null) filePickerProvider.overrideWithValue(picker), if (delayedResolver != null) mediaFileResolverProvider.overrideWithValue(delayedResolver), appDatabaseProvider.overrideWithValue(database), timelineViewSettingsRepositoryProvider.overrideWithValue( _TimelineViewRepository(), ), projectsProvider.overrideWith( () => _FixtureProjects(projectId, fixture.timeline, importResult), ), conversationRepositoryProvider.overrideWith( (ref) async => conversations ?? EmptyConversationRepository(), ), editorPreviewProvider.overrideWith((ref, ids) => null), mediaAvailabilityProvider.overrideWith( (ref, ids) async => MediaAvailability.missing, ), ], ); var closed = false; Future close() async { if (closed) return; closed = true; await tester.pump(); await tester.pumpWidget(const SizedBox()); container.dispose(); await tester.pump(); await tester.runAsync(() async { await fixture.workspace.close(); await fixture.directory.delete(recursive: true); await database.close(); }); } addTearDown(close); // The app owns the scope; this harness deliberately provides its container. // ignore: riverpod_lint/provider_scope_placement await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const CutdexApp()), ); await tester.pumpAndSettle(); return ( container: container, session: fixture.timeline, projectId: projectId, close: close, ); } Future setMode( WidgetTester tester, ProviderContainer container, WorkspaceMode mode, ) async { container.read(workspaceViewSettingsProvider.notifier).setMode(mode); await tester.pumpAndSettle(); } testWidgets('Windows caption space follows real workspace pane changes', ( tester, ) async { debugDefaultTargetPlatformOverride = TargetPlatform.windows; const channel = MethodChannel('cutdex_native/window'); const dropChannel = MethodChannel('cutdex_native/file_drop/timeline'); final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; messenger.setMockMethodCallHandler( channel, (call) async => call.method == 'configureChrome' ? (call.arguments as Map)['enabled'] : null, ); messenger.setMockMethodCallHandler(dropChannel, (call) async => null); addTearDown(() { debugDefaultTargetPlatformOverride = null; messenger.setMockMethodCallHandler(channel, null); messenger.setMockMethodCallHandler(dropChannel, null); }); final mounted = await mount(tester, const Size(1200, 800)); for (final mode in [WorkspaceMode.agent, WorkspaceMode.cut]) { await setMode(tester, mounted.container, mode); for (final width in [1200.0, 700.0, 400.0, 1200.0]) { tester.view.physicalSize = Size(width, 800); await tester.pumpAndSettle(); final controlsLeft = tester .getTopLeft(find.byKey(const Key('window-minimize'))) .dx; for (final key in [ 'editor-media-button', 'editor-properties-button', 'project-settings-button', 'expand-editor-button', ]) { final finder = find.byKey(Key(key)); for (final element in finder.evaluate()) { var rect = tester.getRect(find.byWidget(element.widget)); // Narrow headers can scroll. Assert the visible control bounds, // respecting each scroll viewport's clip. element.visitAncestorElements((ancestor) { if (ancestor.widget case SingleChildScrollView( scrollDirection: Axis.horizontal, )) { rect = rect.intersect( tester.getRect(find.byWidget(ancestor.widget)), ); } return true; }); if (rect.isEmpty) continue; expect( rect.right, lessThanOrEqualTo(controlsLeft + 0.1), reason: '$key at $width in $mode', ); } } expect(tester.takeException(), isNull, reason: '$width in $mode'); } } await mounted.close(); debugDefaultTargetPlatformOverride = null; }); testWidgets( 'desktop resizing retains desktop interactions, session, undo and composer', (tester) async { final mounted = await mount(tester, const Size(1200, 800)); final session = mounted.session; final sequence = session.activeSequenceId!; final view = session.view(sequence); final input = find.descendant( of: find.byKey(const Key('agent-composer')), matching: find.byType(TextField), ); await tester.enterText(input, 'Keep my draft'); await tester.drag( find.byKey(const Key('agent-panel-resize')), const Offset(40, 0), ); await tester.pumpAndSettle(); final agentWidth = tester.getSize(find.byType(AgentChatPanel)).width; view.setSelection([ view.version.kernelVersion.query().elementById(ElementId('clip'))!, ]); view.seekTo(TimelineClock.ticksPerSecond); await tester.runAsync(() async { view.cutSelectionAtPlayhead(); await session.flush(); }); final selected = view.selectedIdentities.toList(); final version = session.version; expect(view.canUndo, isTrue); for (final size in [ const Size(390, 844), const Size(780, 1024), const Size(900, 600), const Size(1200, 800), ]) { tester.view.physicalSize = size; await tester.pumpAndSettle(); expect( find.byKey(const Key('desktop-workspace-shell')), findsOneWidget, ); expect(find.byType(NavigationBar), findsNothing); expect(find.byType(NavigationRail), findsNothing); await setMode(tester, mounted.container, WorkspaceMode.cut); expect(find.byType(EditorTimelinePanel), findsOneWidget); expect( await mounted.container.read( editorTimelineProvider(mounted.projectId, 'project').future, ), same(session), ); expect(session.view(sequence), same(view)); expect(view.selectedIdentities, selected); expect(view.playheadTick, TimelineClock.ticksPerSecond); expect(session.version, version); expect(view.canUndo, isTrue); final rect = tester.getRect(find.byType(EditorTimelinePanel)); expect(rect.right, lessThanOrEqualTo(size.width)); expect(rect.bottom, lessThanOrEqualTo(size.height)); await setMode(tester, mounted.container, WorkspaceMode.agent); expect( tester.widget(input).controller!.text, 'Keep my draft', ); expect(tester.takeException(), isNull); } expect(tester.getSize(find.byType(AgentChatPanel)).width, agentWidth); await tester.runAsync(() async { view.undo(); await session.flush(); }); expect( view.version.kernelVersion.query().elementById(ElementId('clip')), isNotNull, ); expect(view.canRedo, isTrue); await mounted.close(); }, ); testWidgets( 'desktop tools overlay in limited editor space and restore fixed sidebars', (tester) async { final mounted = await mount(tester, const Size(1200, 800)); await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.tap(find.byKey(const Key('editor-properties-button'))); await tester.pumpAndSettle(); expect( tester.getSize(find.byType(MediaPanel)).width, WorkspaceLayoutMetrics.mediaPanelWidth, ); expect( tester.getSize(find.byType(EditorPropertiesPanel)).width, WorkspaceLayoutMetrics.propertiesPanelWidth, ); expect(find.byKey(const Key('editor-tool-surface')), findsNothing); await setMode(tester, mounted.container, WorkspaceMode.agent); expect(find.byKey(const Key('editor-tool-surface')), findsOneWidget); expect(find.byType(EditorPropertiesPanel), findsOneWidget); expect(find.byType(PropertiesEditorPage), findsNothing); expect(find.byType(EditorTimelinePanel), findsOneWidget); expect( mounted.container .read(projectViewSettingsProvider(mounted.projectId)) .requireValue .propertiesOpen, isTrue, ); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); expect(find.byType(MediaPanel), findsOneWidget); expect(find.byType(MediaBrowserDrawer), findsNothing); expect(find.byType(PropertiesToolPanel), findsNothing); expect( tester .widget(find.byKey(const Key('editor-media-button'))) .isSelected, isTrue, ); await setMode(tester, mounted.container, WorkspaceMode.cut); expect(find.byKey(const Key('editor-tool-surface')), findsNothing); expect( tester.getSize(find.byType(MediaPanel)).width, WorkspaceLayoutMetrics.mediaPanelWidth, ); expect( tester.getSize(find.byType(EditorPropertiesPanel)).width, WorkspaceLayoutMetrics.propertiesPanelWidth, ); expect( await mounted.container.read( editorTimelineProvider(mounted.projectId, 'project').future, ), same(mounted.session), ); expect(tester.takeException(), isNull); await mounted.close(); }, ); testWidgets( 'phone tools use independent pages without changing desktop preferences', (tester) async { final mounted = await mount( tester, const Size(390, 844), experience: DeviceExperience.phone, ); final container = mounted.container; await tester.tap(find.byKey(const Key('workspace-cut-destination'))); await tester.pumpAndSettle(); expect(find.byType(EditorWorkspace), findsOneWidget); final session = mounted.session; final view = session.view(session.activeSequenceId!); view.setSelection([ view.version.kernelVersion.query().elementById(ElementId('clip'))!, ]); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('editor-properties-button'))); await tester.pumpAndSettle(); expect(find.byType(PropertiesEditorPage), findsOneWidget); expect(find.byKey(const Key('property-category-picker')), findsOneWidget); expect( container .read(projectViewSettingsProvider(mounted.projectId)) .requireValue .propertiesOpen, isFalse, ); final category = container.read( propertyCategoryProvider(( mounted.projectId, 'project', '${session.activeSequenceId!.value}/clip', )), ); category.value = PropertyCategory.animation; await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('editor-tool-close'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); expect(find.byType(MediaBrowserPage), findsOneWidget); expect(find.byKey(const Key('media-add-selected')), findsOneWidget); container.read(mediaBrowserProvider(mounted.projectId)).filter = MediaFilter.image; await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('editor-tool-close'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('workspace-agent-destination'))); await tester.pumpAndSettle(); expect(find.byType(EditorWorkspace), findsNothing); await tester.tap(find.byKey(const Key('workspace-cut-destination'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('editor-properties-button'))); await tester.pumpAndSettle(); expect( tester .widget>( find.byKey(const Key('property-category-picker')), ) .value, PropertyCategory.animation, ); tester.view.physicalSize = const Size(780, 1024); await tester.pumpAndSettle(); expect(find.byType(PropertiesEditorPage), findsOneWidget); expect(find.byType(PropertiesToolPanel), findsNothing); expect( tester .widget>( find.byKey(const Key('property-category-picker')), ) .value, PropertyCategory.animation, ); tester.view.physicalSize = const Size(1200, 800); await tester.pumpAndSettle(); expect(find.byType(PropertiesEditorPage), findsOneWidget); expect(find.byKey(const Key('phone-workspace-shell')), findsOneWidget); expect(find.byType(NavigationBar), findsOneWidget); expect(find.byType(NavigationRail), findsNothing); expect( container.read(mediaBrowserProvider(mounted.projectId)).filter, MediaFilter.image, ); expect(tester.takeException(), isNull); await mounted.close(); }, ); for (final experience in DeviceExperience.values) { testWidgets( '${experience.name} keeps its navigation and media interaction across widths', (tester) async { final mounted = await mount( tester, const Size(1200, 800), experience: experience, ); await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); final browser = mounted.container.read( mediaBrowserProvider(mounted.projectId), ); browser.filter = MediaFilter.image; browser.selectedAssetId = 'media'; for (final size in [ const Size(390, 844), const Size(600, 800), const Size(1200, 800), ]) { tester.view.physicalSize = size; await tester.pumpAndSettle(); expect( find.byKey(Key('${experience.name}-workspace-shell')), findsOneWidget, ); expect( find.byType(NavigationBar), experience == DeviceExperience.phone ? findsOneWidget : findsNothing, ); expect( find.byType(NavigationRail), experience == DeviceExperience.tablet ? findsOneWidget : findsNothing, ); final panel = find.byKey(const Key('media-panel-surface')); expect( find.byType(MediaPanel), experience == DeviceExperience.desktop ? findsOneWidget : findsNothing, ); expect( find.byType(MediaBrowserPage), experience == DeviceExperience.phone ? findsOneWidget : findsNothing, ); expect( find.byType(MediaBrowserDrawer), experience == DeviceExperience.tablet ? findsOneWidget : findsNothing, ); expect( find.byKey(const Key('media-add-selected')), experience == DeviceExperience.desktop ? findsNothing : findsOneWidget, ); if (experience == DeviceExperience.desktop) { expect( find.byKey(const Key('editor-timeline-resize')), findsOneWidget, ); } final rect = tester.getRect(panel); expect(rect.left, greaterThanOrEqualTo(0)); expect(rect.right, lessThanOrEqualTo(size.width)); expect(rect.bottom, lessThanOrEqualTo(size.height)); expect(browser.filter, MediaFilter.image); expect(browser.selectedAssetId, 'media'); expect(tester.takeException(), isNull); } await mounted.close(); }, ); } testWidgets( 'tablet split screen keeps rail and tool panel with mouse and keyboard', (tester) async { final mounted = await mount( tester, const Size(1200, 800), experience: DeviceExperience.tablet, ); final input = find.descendant( of: find.byKey(const Key('agent-composer')), matching: find.byType(TextField), ); await tester.enterText(input, 'Tablet draft'); expect(find.byType(EditorTimelinePanel), findsOneWidget); tester.view.physicalSize = const Size(390, 844); tester.view.viewInsets = const FakeViewPadding(bottom: 300); addTearDown(tester.view.resetViewInsets); await tester.pumpAndSettle(); expect(find.byType(NavigationRail), findsOneWidget); expect(find.byType(NavigationBar), findsNothing); expect( tester.getRect(find.byKey(const Key('agent-composer'))).bottom, lessThanOrEqualTo(544), ); expect(tester.widget(input).controller!.text, 'Tablet draft'); tester.view.resetViewInsets(); await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-properties-button'))); await tester.pumpAndSettle(); expect(find.byType(PropertiesToolPanel), findsOneWidget); expect(find.byType(PropertiesEditorPage), findsNothing); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: const Offset(200, 24)); await mouse.moveTo(const Offset(250, 24)); await tester.pumpAndSettle(); expect(find.byKey(const Key('tablet-workspace-shell')), findsOneWidget); expect(find.byType(PropertiesToolPanel), findsOneWidget); await mouse.removePointer(); final prefs = mounted.container .read(projectViewSettingsProvider(mounted.projectId)) .requireValue; expect(prefs.propertiesOpen, isFalse); expect(tester.takeException(), isNull); await mounted.close(); }, ); testWidgets( 'phone navigation and landscape remain usable with keyboard and scaled text', (tester) async { final mounted = await mount( tester, const Size(390, 844), experience: DeviceExperience.phone, ); await tester.tap(find.byKey(const Key('workspace-navigation-button'))); await tester.pumpAndSettle(); expect( find.byKey(ValueKey('sidebar-project-${mounted.projectId}')), findsOneWidget, ); await tester.tap( find.byKey(ValueKey('sidebar-project-${mounted.projectId}')), ); await tester.pumpAndSettle(); expect( find.byKey(const Key('sidebar-projects-conversations')), findsNothing, ); expect(find.byType(AgentChatPanel), findsOneWidget); tester.view.viewInsets = const FakeViewPadding(bottom: 300); await tester.pumpAndSettle(); expect(find.byKey(const Key('phone-workspace-shell')), findsOneWidget); expect( tester.getRect(find.byKey(const Key('agent-composer'))).bottom, lessThanOrEqualTo(544), ); tester.view.resetViewInsets(); addTearDown(tester.view.resetViewInsets); await setMode(tester, mounted.container, WorkspaceMode.cut); tester.view.physicalSize = const Size(844, 390); tester.platformDispatcher.textScaleFactorTestValue = 2; addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); await tester.pumpAndSettle(); final timeline = tester.getRect(find.byType(EditorTimelinePanel)); expect(timeline.right, lessThanOrEqualTo(844)); expect(timeline.bottom, lessThanOrEqualTo(390)); expect(tester.takeException(), isNull); await mounted.close(); }, ); for (final experience in [DeviceExperience.phone, DeviceExperience.tablet]) { testWidgets( '$experience navigation exposes touch actions and closes after new conversation', (tester) async { final mounted = await mount( tester, const Size(390, 844), experience: experience, ); final selection = conversationSelectionProvider(mounted.projectId); final before = mounted.container.read(selection).draftKey; await tester.tap(find.byKey(const Key('workspace-navigation-button'))); await tester.pumpAndSettle(); final create = find.byKey(const Key('create-project')); expect(tester.getSize(create).height, greaterThanOrEqualTo(48)); final action = find.byKey( ValueKey('new-agent-conversation-${mounted.projectId}'), ); expect(tester.getSize(action).height, greaterThanOrEqualTo(48)); await tester.tap(action); await tester.pumpAndSettle(); expect(mounted.container.read(selection).draftKey, isNot(before)); expect( find.byKey(const Key('sidebar-projects-conversations')), findsNothing, ); expect(tester.takeException(), isNull); }, ); } for (final experience in [DeviceExperience.phone, DeviceExperience.tablet]) { testWidgets( '$experience system back closes both tool pages while keeping the editing session', (tester) async { final mounted = await mount( tester, const Size(390, 844), experience: experience, ); await setMode(tester, mounted.container, WorkspaceMode.cut); for (final button in [ 'editor-media-button', 'editor-properties-button', ]) { await tester.tap(find.byKey(Key(button))); await tester.pumpAndSettle(); expect(find.byKey(const Key('editor-tool-surface')), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(find.byKey(const Key('editor-tool-surface')), findsNothing); expect(find.byType(EditorTimelinePanel), findsOneWidget); expect( await mounted.container.read( editorTimelineProvider(mounted.projectId, 'project').future, ), same(mounted.session), ); } }, ); testWidgets( '$experience back closes the navigation drawer before the tool page', (tester) async { final mounted = await mount( tester, const Size(390, 844), experience: experience, ); await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('workspace-navigation-button'))); await tester.pumpAndSettle(); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect( find.byKey(const Key('sidebar-projects-conversations')), findsNothing, ); expect(find.byKey(const Key('editor-tool-surface')), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(find.byKey(const Key('editor-tool-surface')), findsNothing); }, ); } testWidgets('phone new conversation from a tool drawer returns to Agent', ( tester, ) async { final mounted = await mount( tester, const Size(390, 844), experience: DeviceExperience.phone, ); await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('workspace-navigation-button'))); await tester.pumpAndSettle(); await tester.tap( find.byKey(ValueKey('new-agent-conversation-${mounted.projectId}')), ); await tester.pumpAndSettle(); expect( find.byKey(const Key('sidebar-projects-conversations')), findsNothing, ); expect(find.byType(AgentChatPanel), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets( 'phone property category survives closing and remounting the inspector', (tester) async { final mounted = await mount( tester, const Size(390, 844), experience: DeviceExperience.phone, ); final sequence = mounted.session.activeSequenceId!; final view = mounted.session.view(sequence); view.setSelection([ view.version.kernelVersion.query().elementById(ElementId('clip'))!, ]); await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-properties-button'))); await tester.pumpAndSettle(); final picker = find.byKey(const Key('property-category-picker')); final dropdown = tester.widget>(picker); dropdown.onChanged!(PropertyCategory.animation); await tester.pumpAndSettle(); expect( tester.widget>(picker).value, PropertyCategory.animation, ); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await setMode(tester, mounted.container, WorkspaceMode.agent); await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-properties-button'))); await tester.pumpAndSettle(); expect( tester.widget>(picker).value, PropertyCategory.animation, ); expect(tester.takeException(), isNull); }, ); testWidgets( 'phone attachment import completes while the composer is hidden', (tester) async { final resolver = _DelayedResolver(); final mounted = await mount( tester, const Size(390, 844), experience: DeviceExperience.phone, delayedResolver: resolver, picker: const _PhotoPicker(), ); final selection = mounted.container.read( conversationSelectionProvider(mounted.projectId), ); final composer = mounted.container .read(agentComposerStoreProvider) .composer(mounted.projectId, selection.draftKey); final add = find.descendant( of: find.byKey(const Key('agent-composer')), matching: find.byIcon(Icons.add), ); await tester.tap(add); await tester.pump(); expect(resolver.started, isTrue); await setMode(tester, mounted.container, WorkspaceMode.cut); expect(find.byType(AgentChatPanel), findsNothing); resolver.gate.complete(); await tester.pumpAndSettle(); expect(composer.attachments.single.id, 'media'); await setMode(tester, mounted.container, WorkspaceMode.agent); expect(find.byKey(const Key('agent-attachment-media')), findsOneWidget); expect(tester.takeException(), isNull); }, ); testWidgets( 'phone media import updates browser selection after the tool is closed', (tester) async { final resolver = _DelayedResolver(); final mounted = await mount( tester, const Size(390, 844), experience: DeviceExperience.phone, delayedResolver: resolver, picker: const _PhotoPicker(), ); await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('media-import-button'))); await tester.pump(); expect(resolver.started, isTrue); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); resolver.gate.complete(); await tester.pumpAndSettle(); expect( mounted.container .read(mediaBrowserProvider(mounted.projectId)) .selectedAssetId, 'media', ); expect(tester.takeException(), isNull); }, ); testWidgets( 'created conversation preserves follow-up text and pending imports', (tester) async { final resolver = _DelayedResolver(); final mounted = await mount( tester, const Size(390, 844), experience: DeviceExperience.phone, delayedResolver: resolver, picker: const _PhotoPicker(), conversations: _WorkspaceConversationRepository(), ); final selectionProvider = conversationSelectionProvider( mounted.projectId, ); final draftKey = mounted.container.read(selectionProvider).draftKey; final store = mounted.container.read(agentComposerStoreProvider); final input = find.descendant( of: find.byKey(const Key('agent-composer')), matching: find.byType(TextField), ); await tester.enterText( input, 'Follow-up while the first message is saved', ); final draftInput = store.input(mounted.projectId, null); final composer = store.composer(mounted.projectId, draftKey); await tester.tap( find.descendant( of: find.byKey(const Key('agent-composer')), matching: find.byIcon(Icons.add), ), ); await tester.pump(); expect(resolver.started, isTrue); final now = DateTime.utc(2026); final created = ConversationMetadata( id: 'created-chat', pending: false, responseDurationsMs: const [], projectId: mounted.projectId, title: 'Created', createdAt: now, updatedAt: now, providerId: 'provider', modelId: 'model', timelineId: 'project', ); await mounted.container .read(conversationSidebarProvider.notifier) .updateListing(created, selectIfDraftKey: draftKey); await tester.pumpAndSettle(); expect(mounted.container.read(selectionProvider).selectedId, created.id); expect(store.input(mounted.projectId, created.id), same(draftInput)); expect(store.composer(mounted.projectId, created.id), same(composer)); expect( tester.widget(input).controller!.text, 'Follow-up while the first message is saved', ); resolver.gate.complete(); await tester.pumpAndSettle(); expect(find.byKey(const Key('agent-attachment-media')), findsOneWidget); expect(composer.busy, isFalse); mounted.container .read(conversationSidebarProvider.notifier) .newConversation(); await tester.pumpAndSettle(); expect(tester.widget(input).controller!.text, isEmpty); expect(find.byKey(const Key('agent-attachment-media')), findsNothing); expect( store.input(mounted.projectId, created.id).text, 'Follow-up while the first message is saved', ); expect(tester.takeException(), isNull); }, ); for (final target in ['composer', 'media']) { testWidgets('hidden $target import failure is shown once on return', ( tester, ) async { final resolver = _DelayedResolver(); final mounted = await mount( tester, const Size(390, 844), experience: DeviceExperience.phone, delayedResolver: resolver, picker: const _BadPicker(), importResult: const MediaImportResult( assets: [], added: 0, duplicates: 0, failures: [ MediaImportFailure( name: 'bad.mov', failure: AppFailure( AppErrorCode.diagnostic, diagnostic: 'File cannot be read', ), ), ], ), ); if (target == 'media') { await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('media-import-button'))); } else { await tester.tap( find.descendant( of: find.byKey(const Key('agent-composer')), matching: find.byIcon(Icons.add), ), ); } await tester.pump(); expect(resolver.started, isTrue); if (target == 'media') { await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); } else { await setMode(tester, mounted.container, WorkspaceMode.cut); } resolver.gate.complete(); await tester.pumpAndSettle(); expect(find.byType(SnackBar), findsNothing); if (target == 'media') { await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); } else { await setMode(tester, mounted.container, WorkspaceMode.agent); } expect(find.byType(SnackBar), findsOneWidget); expect(find.textContaining('bad.mov:'), findsOneWidget); final feedback = target == 'media' ? mounted.container .read(mediaBrowserProvider(mounted.projectId)) .feedback : mounted.container .read(agentComposerStoreProvider) .composer( mounted.projectId, mounted.container .read(conversationSelectionProvider(mounted.projectId)) .draftKey, ) .feedback; expect(feedback.hasPending, isFalse); ScaffoldMessenger.of(tester.element(find.byType(SnackBar))) .removeCurrentSnackBar(); await tester.pumpAndSettle(); await setMode(tester, mounted.container, WorkspaceMode.cut); await setMode(tester, mounted.container, WorkspaceMode.agent); if (target == 'media') { await setMode(tester, mounted.container, WorkspaceMode.cut); await tester.tap(find.byKey(const Key('editor-media-button'))); await tester.pumpAndSettle(); } expect(find.byType(SnackBar), findsNothing); expect(tester.takeException(), isNull); }); } testWidgets( 'extremely narrow desktop keeps safe layout and splitter bounds', (tester) async { final mounted = await mount(tester, const Size(200, 800)); expect(find.byKey(const Key('desktop-workspace-shell')), findsOneWidget); expect(find.byKey(const Key('agent-panel-resize')), findsNothing); expect(tester.takeException(), isNull); tester.view.physicalSize = const Size(240, 800); await tester.pumpAndSettle(); expect(find.byKey(const Key('agent-panel-resize')), findsOneWidget); await tester.drag( find.byKey(const Key('agent-panel-resize')), const Offset(-20, 0), ); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); tester.view.physicalSize = const Size(200, 800); await tester.pumpAndSettle(); expect(find.byKey(const Key('agent-panel-resize')), findsNothing); expect(tester.takeException(), isNull); tester.view.physicalSize = const Size(1200, 800); await tester.pumpAndSettle(); expect(find.byKey(const Key('agent-panel-resize')), findsOneWidget); await tester.drag( find.byKey(const Key('agent-panel-resize')), const Offset(-20, 0), ); await tester.pumpAndSettle(); expect(find.byKey(const Key('desktop-workspace-shell')), findsOneWidget); expect(tester.takeException(), isNull); await mounted.close(); }, ); } class _FixtureProjects extends Projects { _FixtureProjects(this.projectId, this.session, this.importResult); final MediaImportResult? importResult; final String projectId; final EditorTimelineSession session; @override Future build() async { final project = RecentProject( id: projectId, name: 'Adaptive test', path: '/test/Adaptive.cutdex', ); return ProjectState( activeProject: project, recent: [project], timeline: session.document, timelines: const [ TimelineSummary(id: 'project', name: 'Timeline', revision: 1), ], assets: [ AssetDocument( id: 'media', name: 'Photo', storage: AssetStorage.external, location: const ExternalMediaFile(path: '/test/photo.png').sourceUri .toString(), metadata: {'kind': 'image'}, ), ], ); } @override Future importExternalMedia( String projectId, List files, { bool Function()? isCancelled, }) async => importResult ?? await super.importExternalMedia( projectId, files, isCancelled: isCancelled, ); @override EditorTimelineSession? cachedTimelineSession( String projectId, String timelineId, ) => session; } class _TimelineViewRepository implements TimelineViewSettingsRepository { @override Future load(String projectId, String timelineId) async => const TimelineViewState(); @override Future save( String projectId, String timelineId, SequenceId sequenceId, TimelineSequenceViewState state, ) async {} } class _PhotoPicker extends AppFilePicker { const _PhotoPicker(); @override Future> pickFiles({ required List allowedExtensions, }) async => [const ExternalMediaFile(path: '/test/photo.png')]; } class _DelayedResolver extends MediaFileResolver { final gate = Completer(); bool started = false; @override Future>> resolve( List files, List assets, ) async { started = true; await gate.future; return super.resolve(files, assets); } } class _BadPicker extends AppFilePicker { const _BadPicker(); @override Future> pickFiles({ required List allowedExtensions, }) async => [const ExternalMediaFile(path: '/test/bad.mov')]; } class _WorkspaceConversationRepository extends EmptyConversationRepository { @override Future select(String projectId, String? conversationId) async {} }