import 'package:cutdex/app/editor_theme.dart'; import 'package:cutdex/features/projects/conversation_sidebar.dart'; import 'package:cutdex/features/projects/project_sidebar.dart'; import 'package:cutdex/integrations/agent/conversation_repository.dart'; import 'package:cutdex/l10n/app_localizations.dart'; import 'package:cutdex/workspace/project_state.dart'; import 'package:cutdex/workspace/projects.dart'; import 'package:cutdex/workspace/workspace_mode.dart'; import 'package:cutdex_ui/icons.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import '../../support/empty_conversation_repository.dart'; void main() { testWidgets( 'desktop header follows selection, rename, trash and project scope', (tester) async { const projects = [ RecentProject(id: 'a', name: 'Project A', path: '/a'), RecentProject(id: 'b', name: 'Project B', path: '/b'), ]; final repository = _Conversations([ _item('first', 'a', 'First chat'), _item('second', 'a', 'Second chat'), _item('other', 'b', 'Other project chat'), ]); final workspace = _Projects( ProjectState(activeProject: projects.first, recent: projects), ); final container = ProviderContainer( overrides: [ projectsProvider.overrideWith(() => workspace), conversationRepositoryProvider.overrideWith((_) => repository), ], ); addTearDown(container.dispose); await container.read(projectsProvider.future); await container.read(conversationSidebarProvider.future); await tester.pumpWidget( UncontrolledProviderScope( container: container, child: MaterialApp( theme: EditorTheme.desktop(EditorTheme.penDark), locale: const Locale('zh'), localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, home: Scaffold( body: SizedBox( width: 240, child: Consumer( builder: (context, ref, _) { final state = ref.watch(projectsProvider).requireValue; return ProjectSidebar( chatOnly: true, agent: const Text('Conversation content'), mode: WorkspaceMode.agent, onModeChanged: (_) {}, navigationWidth: 240, navigationCollapsed: false, agentHeaderLeading: const SizedBox(), editorHidden: false, onExpandEditor: null, state: state, selectedProjectId: state.activeProject?.id, integratedTitlebar: false, onCreateProject: null, onSelectProject: null, onRenameProject: (_) async => false, headerControls: const SizedBox(), ); }, ), ), ), ), ), ); await tester.pumpAndSettle(); expect(find.text('First chat'), findsOneWidget); await tester.tap(find.byKey(const Key('project-chat-history'))); await tester.pumpAndSettle(); final menu = find.byKey(const Key('project-chat-history-menu')); Finder menuText(String text) => find.descendant(of: menu, matching: find.text(text)); final group = tester.getRect(find.byKey(const Key('project-chat-group'))); expect(tester.getRect(menuText('First chat')).left, group.left); expect(tester.getRect(menuText('Second chat')).left, group.left); final searchIcon = find.descendant( of: find.byKey(const Key('project-chat-search')), matching: find.byIcon(AppIcons.search), ); expect(tester.getCenter(searchIcon).dx, group.left + 7); final search = find.byKey(const Key('project-chat-search')); expect(tester.getSize(search).height, 28); expect(tester.widget(search).style?.fontSize, 13); expect(tester.widget(search).cursorHeight, 13); expect( find.byKey(const ValueKey('sidebar-conversation-other')), findsNothing, ); await tester.enterText( find.byKey(const Key('project-chat-search')), 'Second', ); await tester.pumpAndSettle(); await tester.tap( find.byKey(const ValueKey('sidebar-conversation-second')), ); await tester.pumpAndSettle(); expect(find.byKey(const Key('project-chat-history-menu')), findsNothing); expect(find.text('Second chat'), findsOneWidget); expect(repository.selection['a'], 'second'); await tester.tap(find.byKey(const Key('project-chat-more'))); await tester.pumpAndSettle(); await tester.tap( find.text(lookupAppLocalizations(const Locale('zh')).rename), ); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextFormField), 'Renamed second'); await tester.tap( find.text(lookupAppLocalizations(const Locale('zh')).save), ); await tester.pumpAndSettle(); expect(find.text('Renamed second'), findsOneWidget); expect( repository.items.firstWhere((item) => item.id == 'second').title, 'Renamed second', ); await tester.tap(find.byKey(const Key('project-chat-more'))); await tester.pumpAndSettle(); await tester.tap( find.text(lookupAppLocalizations(const Locale('zh')).agentMoveToTrash), ); await tester.pumpAndSettle(); expect(find.text('新聊天'), findsOneWidget); expect( container.read(conversationSelectionProvider('a')).selectedId, isNull, ); await tester.tap(find.byKey(const Key('project-chat-history'))); await tester.pumpAndSettle(); expect(find.text('Renamed second'), findsOneWidget); final trashTitle = tester.getRect( find.byKey(const Key('project-chat-trash-title')), ); expect( trashTitle.left, tester.getRect(find.byKey(const Key('project-chat-group'))).left, ); expect(tester.getRect(menuText('Renamed second')).left, trashTitle.left); expect( find.byKey(const ValueKey('sidebar-conversation-second')), findsNothing, ); await tester.tap( find.byTooltip(lookupAppLocalizations(const Locale('zh')).agentRestore), ); await tester.pumpAndSettle(); expect( find.byKey(const ValueKey('sidebar-conversation-second')), findsOneWidget, ); workspace.switchTo(projects.last); await tester.pumpAndSettle(); expect(find.byKey(const Key('project-chat-history-menu')), findsNothing); expect(find.text('Other project chat'), findsOneWidget); await tester.tap(find.byKey(const Key('project-chat-history'))); await tester.pumpAndSettle(); expect( find.byKey(const ValueKey('sidebar-conversation-other')), findsOneWidget, ); expect( find.byKey(const ValueKey('sidebar-conversation-first')), findsNothing, ); expect( find.byKey(const ValueKey('sidebar-conversation-second')), findsNothing, ); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); }, ); } ConversationMetadata _item(String id, String projectId, String title) => ConversationMetadata( id: id, projectId: projectId, title: title, createdAt: DateTime.utc(2026), updatedAt: DateTime.utc(2026), providerId: 'provider', modelId: 'model', timelineId: 'timeline', pending: false, responseDurationsMs: const [], ); class _Projects extends Projects { _Projects(this.initial); final ProjectState initial; @override Future build() async => initial; void switchTo(RecentProject project) => state = AsyncData(state.requireValue.copyWith(activeProject: project)); } class _Conversations extends EmptyConversationRepository { _Conversations(this.items); final List items; final trashed = []; final selection = {'a': 'first', 'b': 'other'}; @override Future> list( String projectId, { bool trash = false, }) async => (trash ? trashed : items) .where((item) => item.projectId == projectId) .toList(); @override Future selected(String projectId) async => selection[projectId]; @override Future select(String projectId, String? conversationId) async => selection[projectId] = conversationId; @override Future update(ConversationMetadata item) async { items[items.indexWhere((entry) => entry.id == item.id)] = item; return item; } @override Future trash(String projectId, String id) async { final item = items.firstWhere( (entry) => entry.projectId == projectId && entry.id == id, ); items.remove(item); trashed.add(item); if (selection[projectId] == id) selection[projectId] = null; } @override Future restore(String projectId, String id) async { final item = trashed.firstWhere( (entry) => entry.projectId == projectId && entry.id == id, ); trashed.remove(item); items.add(item); } }