import 'package:cutdex/features/projects/chat_header.dart'; import 'package:cutdex/l10n/app_localizations.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { const entries = [ (id: 'rough', title: '旅行短片粗剪'), (id: 'opening', title: '调整片头节奏与镜头衔接,让画面更自然'), (id: 'captions', title: '字幕与配乐'), ]; Widget host(Widget child, {double width = 240, ThemeData? theme}) => MaterialApp( theme: theme ?? AppTheme.desktopWorkspaceTheme(AppTheme.penDark), locale: const Locale('zh'), localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, home: Scaffold( body: Align( alignment: Alignment.topLeft, child: SizedBox(width: width, child: child), ), ), ); testWidgets('title, new and more controls fit palettes and narrow widths', ( tester, ) async { for (final palette in AppPalette.values) { for (final brightness in Brightness.values) { for (final width in [120.0, 240.0, 400.0]) { await tester.pumpWidget( host( ChatHeader( title: entries[1].title, onNew: () {}, actions: [AppMenuItem(label: '重命名', onPressed: () {})], historyBuilder: (_, close) => ChatHistoryMenu(entries: entries, onSelect: (_) => close()), ), width: width, theme: AppTheme.desktopWorkspaceTheme( AppTheme.forPalette(palette, brightness), ), ), ); await tester.pumpAndSettle(); final header = tester.getRect( find.byKey(const Key('project-chat-header')), ); final history = tester.getRect( find.byKey(const Key('project-chat-history')), ); final create = tester.getRect( find.byKey(const Key('project-chat-new')), ); final more = tester.getRect( find.byKey(const Key('project-chat-more')), ); expect(header.height, 36); expect(create.size, const Size.square(26)); expect(more.size, const Size.square(26)); expect(create.center.dy, header.center.dy); expect(more.center.dy, header.center.dy); expect(history.left - header.left, 10); expect(header.right - more.right, 10); expect(create.left - history.right, 4); expect(more.left - create.right, 4); final title = tester.widget( find.byKey(const Key('project-chat-title')), ); expect(title.maxLines, 1); expect(title.overflow, TextOverflow.ellipsis); expect( tester.takeException(), isNull, reason: '$palette $brightness $width', ); } } } }); testWidgets('history search, selection, new chat and actions work', ( tester, ) async { String? selected = 'rough'; var actionCount = 0; await tester.pumpWidget( host( StatefulBuilder( builder: (context, setState) => ChatHeader( title: entries .where((entry) => entry.id == selected) .firstOrNull ?.title ?? '新聊天', onNew: () => setState(() => selected = null), actions: selected == null ? const [] : [AppMenuItem(label: '重命名', onPressed: () => actionCount++)], historyBuilder: (_, close) => ChatHistoryMenu( entries: entries, selectedId: selected, onSelect: (id) { close(); setState(() => selected = id); }, ), ), ), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-chat-history'))); await tester.pumpAndSettle(); expect( find.text(lookupAppLocalizations(const Locale('zh')).agentConversations), findsOneWidget, ); await tester.enterText(find.byKey(const Key('project-chat-search')), '字幕'); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('chat-history-rough')), findsNothing); await tester.tap(find.byKey(const ValueKey('chat-history-captions'))); await tester.pumpAndSettle(); expect(selected, 'captions'); expect(find.byKey(const Key('project-chat-history-menu')), findsNothing); expect(find.text('字幕与配乐'), findsOneWidget); await tester.tap(find.byKey(const Key('project-chat-more'))); await tester.pumpAndSettle(); await tester.tap(find.text('重命名')); await tester.pumpAndSettle(); expect(actionCount, 1); await tester.tap(find.byKey(const Key('project-chat-new'))); await tester.pumpAndSettle(); expect(selected, isNull); expect(find.text('新聊天'), findsOneWidget); expect( tester .widget(find.byKey(const Key('project-chat-more'))) .onPressed, isNull, ); await tester.tap(find.byKey(const Key('project-chat-history'))); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('chat-history-rough')), findsOneWidget); await tester.enterText( find.byKey(const Key('project-chat-search')), 'missing', ); await tester.pumpAndSettle(); expect( find.text(lookupAppLocalizations(const Locale('zh')).noMatchingOptions), findsOneWidget, ); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.byKey(const Key('project-chat-history-menu')), findsNothing); expect(tester.takeException(), isNull); }); testWidgets( 'empty history hides search and retains trash without stale filters', (tester) async { var history = []; var showTrash = false; late StateSetter updateHistory; await tester.pumpWidget( host( StatefulBuilder( builder: (_, setState) { updateHistory = setState; return ChatHeader( title: '新聊天', onNew: () {}, actions: const [], historyBuilder: (_, _) => ChatHistoryMenu( entries: history, onSelect: (_) {}, footer: showTrash ? const Text('Trash item') : null, ), ); }, ), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('project-chat-history'))); await tester.pumpAndSettle(); final l10n = lookupAppLocalizations(const Locale('zh')); expect(find.byKey(const Key('project-chat-search')), findsNothing); expect(find.text(l10n.agentNoConversations), findsOneWidget); expect(find.text('Trash item'), findsNothing); expect( tester.getSize(find.byType(ChatHistoryMenu)).height, lessThan(200), ); updateHistory(() => showTrash = true); await tester.pumpAndSettle(); expect(find.text('Trash item'), findsOneWidget); expect(find.byKey(const Key('project-chat-search')), findsNothing); updateHistory(() => history = entries.toList()); await tester.pumpAndSettle(); final search = find.byKey(const Key('project-chat-search')); expect(search, findsOneWidget); await tester.enterText(search, 'unmatched'); await tester.pumpAndSettle(); expect(find.text(l10n.noMatchingOptions), findsOneWidget); expect(search, findsOneWidget); updateHistory(() => history = []); await tester.pumpAndSettle(); expect(search, findsNothing); expect(find.text(l10n.agentNoConversations), findsOneWidget); updateHistory(() => history = entries.toList()); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('chat-history-rough')), findsOneWidget); expect(tester.widget(search).controller?.text ?? '', isEmpty); expect(tester.takeException(), isNull); }, ); testWidgets('panel commands stay available while a conversation is busy', ( tester, ) async { var collapsed = false; await tester.pumpWidget( host( ChatHeader( title: 'Working', busy: true, onNew: () {}, actions: [AppMenuItem(label: '重命名', onPressed: () {})], panelActions: [ AppMenuItem(label: '折叠对话', onPressed: () => collapsed = true), ], historyBuilder: (_, close) => ChatHistoryMenu(entries: entries, onSelect: (_) => close()), ), ), ); await tester.pumpAndSettle(); expect( tester .widget(find.byKey(const Key('project-chat-new'))) .onPressed, isNull, ); await tester.tap(find.byKey(const Key('project-chat-more'))); await tester.pumpAndSettle(); expect(find.text('重命名'), findsNothing); await tester.tap(find.text('折叠对话')); await tester.pumpAndSettle(); expect(collapsed, isTrue); expect(tester.takeException(), isNull); }); testWidgets('busy header disables navigation', (tester) async { await tester.pumpWidget( host( ChatHeader( title: 'Chat', busy: true, onNew: () {}, actions: [AppMenuItem(label: 'Rename', onPressed: () {})], historyBuilder: (_, _) => const Text('History'), ), ), ); await tester.pumpAndSettle(); expect( tester .widget(find.byKey(const Key('project-chat-history'))) .onPressed, isNull, ); for (final key in ['project-chat-new', 'project-chat-more']) { expect(tester.widget(find.byKey(Key(key))).onPressed, isNull); } }); }