import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/conversation/chat_bar_control_style.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_header.dart'; import 'package:pipelines_app/features/chat/conversation/chat_sidebar_controls.dart'; import 'package:pipelines_app/features/chat/ui/sessions/chat_adaptive.dart'; import 'package:pipelines_app/features/chat/ui/sessions/session_row_content.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; import 'package:pipelines_app/ui/adaptive/window_margins.dart'; import '../../../native_back.dart'; import 'chat_thread_fixtures.dart'; const _title = 'A conversation title long enough to need an ellipsis at 320px'; const _session = { 'id': 's', 'backend': 'opencode', 'model': 'provider/an-extremely-long-recorded-model-id', 'model_provider': 'p1', }; Widget _host(Widget child) => MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: Scaffold(body: child), ); void _useNarrowText(WidgetTester tester) { tester.view.physicalSize = const Size(320, 640); tester.view.devicePixelRatio = 1; tester.platformDispatcher.textScaleFactorTestValue = 2; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); } void main() { testWidgets('wide header uses one explicit control gap', (tester) async { tester.view.physicalSize = const Size(1440, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); Widget control(String key) => SizedBox(key: Key(key), width: 48, height: 48); await tester.pumpWidget( _host( ChatThreadHeader( title: 'Chat', folder: 'demo', onInfo: () {}, controls: [control('header-control-1'), control('header-control-2')], actions: [control('header-action-1'), control('header-action-2')], moreActions: [ ChatBarAction( label: 'More action', onPressed: () {}, materialIcon: Icons.more_horiz, ), ], ), ), ); await tester.pumpAndSettle(); double gap(String left, String right) => tester.getTopLeft(find.byKey(Key(right))).dx - tester.getTopRight(find.byKey(Key(left))).dx; expect(gap('header-control-1', 'header-control-2'), 8); expect(gap('header-control-2', 'header-action-1'), 8); expect(gap('header-action-1', 'header-action-2'), 8); expect( tester.getTopLeft(find.byKey(segmentedMoreKey).first).dx - tester.getTopRight(find.byKey(const Key('header-action-2'))).dx, 8, ); }); testWidgets('short large-text header keeps controls in its single row', ( tester, ) async { _useNarrowText(tester); await tester.pumpWidget( _host( Align( alignment: Alignment.topLeft, child: SizedBox( width: 320, height: 100, child: ChatThreadHeader( title: _title, folder: 'demo', onInfo: () {}, showBack: true, onBack: () {}, controls: const [ IconButton(onPressed: null, icon: Icon(Icons.memory_outlined)), IconButton(onPressed: null, icon: Icon(Icons.shield_outlined)), ], ), ), ), ), ); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); // Narrow: the two selectors wait in the More menu's settings entry. expect(find.byKey(const Key('chat-model')), findsNothing); expect(find.byKey(const Key('chat-model-permission-island')), findsNothing); expect(tester.takeException(), isNull); }); testWidgets( 'Material narrow header keeps native back/title and compact model icon', (tester) async { _useNarrowText(tester); final semantics = tester.ensureSemantics(); final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage(const []); gateway.subscribe = () async => const {}; var backed = 0; await tester.pumpWidget( _host( ChatThreadView( gateway: gateway, sessionId: 's', title: _title, session: _session, onBack: () => backed++, ), ), ); await tester.pumpAndSettle(); final title = find.text(_title); final more = find.byKey(segmentedMoreKey); final info = find.byKey(const Key('chat-info')); final back = find.byKey(const Key('chat-back')); expect(title, findsOneWidget); // A narrow bar keeps its selectors in the More menu's settings entry. expect(more, findsWidgets); expect(find.byKey(const Key('chat-model')), findsNothing); expect(info, findsOneWidget); expect(back, findsOneWidget); expect(tester.takeException(), isNull); // Back is the platform's own control, as on every other page. expect( find.descendant(of: back, matching: findNativeBack()), findsOneWidget, ); // One floating row of separate pieces: the round Back, the title pill // (which is the information control and holds the title), then the // selector island in the trailing row. double centerY(Finder finder) => tester.getCenter(finder).dy; expect((centerY(back) - centerY(info)).abs(), lessThan(16)); expect((centerY(more.first) - centerY(info)).abs(), lessThan(16)); expect(find.descendant(of: info, matching: title), findsOneWidget); double right(Finder finder) => tester.getTopRight(finder).dx; expect(right(back), lessThanOrEqualTo(tester.getTopLeft(info).dx)); expect(find.byIcon(Icons.tune), findsNothing); expect( find.textContaining('an-extremely-long-recorded-model-id'), findsNothing, ); await tester.tap(back); await tester.pump(); expect(backed, 1); semantics.dispose(); }, ); // The mode control is Material only. for (final width in [320.0, 390.0, 1280.0]) { testWidgets('grouped actions at $width and text scale 2', (tester) async { _useNarrowText(tester); tester.view.physicalSize = Size(width, 800); final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage(const []); gateway.subscribe = () async => const {}; var sidebar = 0; var pane = 0; var next = 0; await tester.pumpWidget( _host( ChatThreadView( gateway: gateway, sessionId: 's', title: _title, session: { ..._session, 'mode': 'safe', 'mode_name': 'Ask before making changes', }, onBack: () {}, onNextChat: () => next++, sidebarControl: ChatBarAction( label: 'Sidebar action', onPressed: () => sidebar++, materialIcon: Icons.menu, ), paneControls: [ ChatBarAction( label: 'Pane action', onPressed: () => pane++, materialIcon: Icons.add_box, ), ], ), ), ); await tester.pumpAndSettle(); final actions = find.byKey(const Key('chat-actions-island')); expect(actions, findsOneWidget); expect(find.byKey(const Key('chat-next')), findsNothing); expect(find.text('Next chat'), findsNothing); if (width < chatHeaderNarrowWidth) { // The selectors wait in the More menu's settings entry. expect(find.byKey(const Key('chat-model')), findsNothing); expect(find.byKey(const Key('chat-mode')), findsNothing); expect(find.text('model-a'), findsNothing); expect(find.text('Ask before making changes'), findsNothing); } else { final selectors = find.byKey(const Key('chat-model-permission-island')); expect(selectors, findsOneWidget); expect( (tester.getCenter(selectors).dy - tester.getCenter(actions).dy).abs(), lessThan(16), ); expect(find.byKey(const Key('chat-model')), findsOneWidget); expect(find.byKey(const Key('chat-mode')), findsOneWidget); for (final key in ['chat-model', 'chat-mode']) { expect( tester.getSize(find.byKey(Key(key))).width, greaterThanOrEqualTo(48), ); } } expect(find.text('Sidebar action'), findsNothing); expect(find.text('Pane action'), findsNothing); expect(tester.takeException(), isNull); // Both More entries retain their original callbacks in native menus. await tester.tap(find.byKey(segmentedMoreKey).hitTestable()); await tester.pumpAndSettle(); await tester.tap(find.text('Sidebar action')); await tester.pumpAndSettle(); expect(sidebar, 1); await tester.tap(find.byKey(segmentedMoreKey).hitTestable()); await tester.pumpAndSettle(); await tester.tap(find.text('Pane action')); await tester.pumpAndSettle(); expect(pane, 1); expect(next, 0); expect(tester.takeException(), isNull); }); } testWidgets('sidebar layout submenu retains callback on Material', ( tester, ) async { ChatSidebarMode? picked; await tester.pumpWidget( _host( ChatThreadHeader( title: 'Chat', folder: 'demo', onInfo: () {}, actions: const [Icon(Icons.folder_outlined)], moreActions: [ ChatSidebarButton( mode: ChatSidebarMode.expanded, onChanged: (value) => picked = value, ), ], ), ), ); await tester.pumpAndSettle(); expect(find.byType(ChatSidebarButton), findsNothing); await tester.tap(find.byKey(segmentedMoreKey).hitTestable()); await tester.pumpAndSettle(); await tester.tap(find.text('Chat list layout')); await tester.pumpAndSettle(); await tester.tap(find.text('Hide chat list')); await tester.pumpAndSettle(); expect(picked, ChatSidebarMode.hidden); expect(tester.takeException(), isNull); }); testWidgets('a conversation without a model keeps the same compact row', ( tester, ) async { _useNarrowText(tester); final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage(const []); gateway.subscribe = () async => const {}; await tester.pumpWidget( _host( ChatThreadView( gateway: gateway, sessionId: 's', title: _title, session: {..._session, 'backend': 'other'}, onBack: () {}, ), ), ); await tester.pumpAndSettle(); expect(find.text(_title), findsOneWidget); expect(find.byKey(const Key('chat-model')), findsNothing); expect( (tester.getCenter(find.byKey(const Key('chat-info'))).dy - tester.getCenter(find.text(_title)).dy) .abs(), lessThan(16), ); expect(tester.takeException(), isNull); }); testWidgets('wide bar text buttons keep their text off the shape ends', ( tester, ) async { late ButtonStyle wide; await tester.pumpWidget( MaterialApp( home: Builder( builder: (context) { wide = chatBarTextButtonStyle(context); return const SizedBox(); }, ), ), ); expect(wide.padding?.resolve({}), chatBarButtonPadding); expect(chatBarButtonPadding.left, 12); expect(chatBarButtonPadding.right, 12); }); group('narrow title priority at 430px', () { const width = 430.0; const longTitle = 'A conversation title long enough to need an ellipsis at 430px'; void useWidth(WidgetTester tester) { tester.view.physicalSize = const Size(width, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); } Widget bar({ required String title, bool showTitle = true, Widget? identity, }) => _host( Align( alignment: Alignment.topLeft, child: SizedBox( width: width, child: ChatThreadHeader( title: title, folder: 'demo', onInfo: () {}, showTitle: showTitle, showBack: true, onBack: () {}, identity: identity, controls: const [ SizedBox(key: Key('header-control-1'), width: 48, height: 48), SizedBox(key: Key('header-control-2'), width: 48, height: 48), ], actions: [ ChatBarAction( label: 'Files', onPressed: () {}, materialIcon: Icons.folder_outlined, ), ], ), ), ), ); // The bar's own width: the window less its phone side margins. double barWidth() => width - 2 * WindowMargins.narrowSide; bool truncated(WidgetTester tester, String text) { final render = tester.renderObject(find.text(text)); final painter = TextPainter( text: render.text, textDirection: TextDirection.ltr, textScaler: render.textScaler, maxLines: 1, )..layout(); final full = painter.width; painter.dispose(); return full > render.size.width + 1; } testWidgets('a short title reads whole and the selectors collapse', ( tester, ) async { useWidth(tester); await tester.pumpWidget(bar(title: 'Fixture chat 1')); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(truncated(tester, 'Fixture chat 1'), isFalse); expect(find.byKey(const Key('header-control-1')), findsNothing); }); testWidgets('a long title ellipsizes and keeps 60% of the bar', ( tester, ) async { useWidth(tester); await tester.pumpWidget(bar(title: longTitle)); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(truncated(tester, longTitle), isTrue); final info = tester.getSize(find.byKey(const Key('chat-info'))); expect(info.width, greaterThanOrEqualTo(barWidth() * 0.6 - 1)); }); testWidgets('the harness mark keeps its size beside the title', ( tester, ) async { useWidth(tester); await tester.pumpWidget( bar( title: 'Fixture chat 1', identity: ChatSessionAvatar( session: {'id': 's', 'backend': 'opencode'}, selected: true, ), ), ); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); final mark = tester.getSize(find.byType(ImageIcon)); expect(mark.width, greaterThanOrEqualTo(20)); expect(mark.height, greaterThanOrEqualTo(20)); }); testWidgets('the More menu opens the controls in a sheet', (tester) async { useWidth(tester); await tester.pumpWidget(bar(title: 'Fixture chat 1')); await tester.pumpAndSettle(); expect(find.byKey(const Key('header-control-1')), findsNothing); await tester.tap(find.byKey(segmentedMoreKey).hitTestable()); await tester.pumpAndSettle(); await tester.tap(find.text('Chat settings')); await tester.pumpAndSettle(); expect(find.byKey(const Key('header-control-1')), findsOneWidget); expect(find.byKey(const Key('header-control-2')), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('a narrow bar moves Files into its More menu', (tester) async { useWidth(tester); await tester.pumpWidget(bar(title: 'Fixture chat 1')); await tester.pumpAndSettle(); expect(find.byTooltip('Files'), findsNothing); await tester.tap(find.byKey(segmentedMoreKey).hitTestable()); await tester.pumpAndSettle(); expect(find.text('Files'), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('a floating window bar shows no title pill', (tester) async { useWidth(tester); await tester.pumpWidget(bar(title: 'Fixture chat 1', showTitle: false)); await tester.pumpAndSettle(); expect(find.text('Fixture chat 1'), findsNothing); expect(find.byKey(const Key('chat-info')), findsNothing); expect(find.byKey(const Key('chat-back')), findsOneWidget); expect(find.byKey(const Key('chat-actions-island')), findsOneWidget); expect(tester.takeException(), isNull); }); }); }