import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.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, {required bool cupertino}) => cupertino ? CupertinoApp( theme: const CupertinoThemeData(brightness: Brightness.dark), home: CupertinoPageScaffold(child: 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() { for (final cupertino in [false, true]) { final host = cupertino ? 'Cupertino' : 'Material'; testWidgets('$host header keeps back, title, model and actions on one row', ( 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++, ), cupertino: cupertino, ), ); await tester.pumpAndSettle(); final title = find.text(_title); final model = find.byKey(const Key('chat-model')); final info = find.byKey(const Key('chat-info')); final back = find.byKey(const Key('chat-back')); expect(title, findsOneWidget); expect(model, findsOneWidget); expect(info, findsOneWidget); expect(back, findsOneWidget); expect(tester.takeException(), isNull); // The icon-only control still states its destination for assistive use. if (cupertino) { expect(find.bySemanticsLabel('Back to chats'), findsOneWidget); } else { expect(find.byTooltip('Back to chats'), findsOneWidget); } // One row, in the web's order: back, title, model, information. A wrapped // header would put the controls on a different line from the title. double centerY(Finder finder) => tester.getCenter(finder).dy; expect((centerY(back) - centerY(title)).abs(), lessThan(16)); expect((centerY(model) - centerY(title)).abs(), lessThan(16)); expect((centerY(info) - centerY(title)).abs(), lessThan(16)); double right(Finder finder) => tester.getTopRight(finder).dx; expect(right(back), lessThanOrEqualTo(tester.getTopLeft(title).dx)); expect(right(title), lessThanOrEqualTo(tester.getTopLeft(model).dx)); expect(right(model), lessThanOrEqualTo(tester.getTopLeft(info).dx)); await tester.tap(back); await tester.pump(); expect(backed, 1); semantics.dispose(); }); } 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: () {}, ), cupertino: false, ), ); 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); }); }