import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/application/open_in_chat.dart'; import 'package:pipelines_app/application/open_in_chat_preferences.dart'; import 'package:pipelines_app/features/chat/chat_runtime.dart'; import 'package:pipelines_app/features/chat/ui/sessions/chat_create_fields.dart'; import 'package:pipelines_app/features/pipelines/pipeline_models.dart'; import 'package:pipelines_app/features/pipelines/pipeline_repository.dart'; import 'package:pipelines_app/features/pipelines/pipelines_controller.dart'; import 'package:pipelines_app/features/pipelines/widgets/pipeline_archive_action.dart'; import 'package:pipelines_app/features/pipelines/widgets/pipeline_list_panel.dart'; import 'package:pipelines_app/features/runs/ui/run_list_row.dart'; import 'package:pipelines_app/features/runs/ui/run_more_menu.dart'; import '../features/chat/ui/sessions/fake_chat_gateway.dart'; import '../features/pipelines/fixtures.dart'; import '../features/pipelines/test_helpers.dart'; void main() { setUp(() => SharedPreferences.setMockInitialValues({})); testWidgets('the harness and model chosen in Settings start the chat', ( tester, ) async { SharedPreferences.setMockInitialValues({}); final gateway = FakeChatGateway() ..backendsHandler = () async => const { 'chat_backends': ['opencode', 'claude'], }; await const OpenInChatPreferences( harness: 'claude', provider: 'anthropic', model: 'claude-opus-5-5', ).save(gateway); final runtime = ChatRuntime(gateway: gateway); await tester.pumpWidget(const MaterialApp(home: Scaffold())); await openReferenceInChat( context: tester.element(find.byType(Scaffold)), runtime: runtime, selectChat: (_) {}, directory: '/work/run', title: 'Run #42: Build', draft: 'About Pipelines run #42.', ); final create = gateway.lastFields('chat_create')!['chat_create'] as Map; expect(create['backend'], 'claude'); final model = gateway.lastFields('chat_model')!['chat_model'] as Map; expect(model['session_id'], 'created'); expect(model['provider'], 'anthropic'); expect(model['model'], 'claude-opus-5-5'); // Opened for work: beside the active chat, with the keyboard in it. expect(runtime.openedForWork, contains('created')); runtime.dispose(); }); testWidgets('a harness the server no longer offers falls back to the ' 'default, without the saved model', (tester) async { SharedPreferences.setMockInitialValues({}); final gateway = FakeChatGateway() ..backendsHandler = () async => const { 'chat_backends': ['opencode', 'codex'], }; await const OpenInChatPreferences( harness: 'claude', provider: 'anthropic', model: 'claude-opus-5-5', ).save(gateway); final runtime = ChatRuntime(gateway: gateway); await tester.pumpWidget(const MaterialApp(home: Scaffold())); await openReferenceInChat( context: tester.element(find.byType(Scaffold)), runtime: runtime, selectChat: (_) {}, directory: '/work/run', title: 'Run #42: Build', draft: 'About Pipelines run #42.', ); final create = gateway.lastFields('chat_create')!['chat_create'] as Map; // The default harness (OpenCode) is the request's default: not claude. expect(create['backend'], anyOf(isNull, 'opencode')); expect(gateway.lastFields('chat_model'), isNull); runtime.dispose(); }); test('a run summary names its pipeline in name', () { final draft = runChatReference({ 'id': 7, 'name': 'release', 'title': 'Ship notes', 'version': 3, 'status': 'failed', }); expect( draft, 'About Pipelines run #7 "Ship notes" (pipeline "release" v3, ' 'status failed). Inspect it with `pipelines inspect 7`.', ); final untitled = runChatReference({ 'id': 8, 'name': 'release', 'version': 3, 'status': 'succeeded', }); expect(untitled, contains('run #8 "Run 8" (pipeline "release" v3')); final unversioned = runChatReference({ 'id': 9, 'name': 'release', 'status': 'running', }); expect(unversioned, contains('(pipeline "release", status running)')); }); test('run references contain the exact inspect command', () { expect( runChatReference({ 'id': 42, 'title': 'Nightly build', 'pipeline_name': 'Deploy', 'pipeline_version': 3, 'status': 'failed', }), 'About Pipelines run #42 "Nightly build" (pipeline "Deploy" v3, ' 'status failed). Inspect it with `pipelines inspect 42`.', ); }); test('pipeline references contain show and edit guidance', () { expect( pipelineChatReference(7, 'Deploy', 4), 'About pipeline "Deploy" (id 7, version 4). Show it with ' '`pipelines show "Deploy"`; to change it, draft with `pipelines edit` ' 'and publish with `pipelines submit`.', ); }); test('open in chat keeps the more menu available', () { expect(showRunMoreMenu({'id': 42}, canOpenInChat: true), isTrue); }); test('pipeline latest run retains its workspace', () { final run = PipelineRun.fromJson({ 'id': 9, 'pipeline_id': 7, 'workspace': '/work/deploy', }); expect(run.workspace, '/work/deploy'); }); testWidgets( 'open in chat uses the catalog backend, draft, route, and focus', (tester) async { final gateway = FakeChatGateway() ..backendsHandler = () async => const { 'chat_backends': ['codex', 'claude'], }; final runtime = ChatRuntime(gateway: gateway); String? selected; await tester.pumpWidget(const MaterialApp(home: Scaffold())); await openReferenceInChat( context: tester.element(find.byType(Scaffold)), runtime: runtime, selectChat: (id) => selected = id, directory: '/work/run', title: 'Run #42: Build', draft: 'About Pipelines run #42.', ); final create = gateway.lastFields('chat_create')!['chat_create'] as Map; expect(create['backend'], 'codex'); expect(create['directory'], '/work/run'); expect(selected, 'created'); expect(runtime.pendingDrafts['created'], 'About Pipelines run #42.'); expect(runtime.composerFocus.take('created'), isTrue); runtime.dispose(); }, ); testWidgets('open in chat falls back to the default folder', (tester) async { final gateway = FakeChatGateway(); final runtime = ChatRuntime(gateway: gateway); await tester.pumpWidget(const MaterialApp(home: Scaffold())); await openReferenceInChat( context: tester.element(find.byType(Scaffold)), runtime: runtime, selectChat: (_) {}, directory: '', title: 'Pipeline: Deploy', draft: 'About pipeline "Deploy".', ); final create = gateway.lastFields('chat_create')!['chat_create'] as Map; expect(create['directory'], chatHomeDirectory); runtime.dispose(); }); testWidgets('create failure leaves navigation and chat state unchanged', ( tester, ) async { final gateway = FakeChatGateway()..failures['chat_create'] = 'refused'; final runtime = ChatRuntime(gateway: gateway); String? selected; await tester.pumpWidget(const MaterialApp(home: Scaffold())); await openReferenceInChat( context: tester.element(find.byType(Scaffold)), runtime: runtime, selectChat: (id) => selected = id, directory: '/work/run', title: 'Run #42: Build', draft: 'About Pipelines run #42.', ); await tester.pump(); expect(selected, isNull); expect(runtime.opened, isEmpty); expect(find.textContaining('Could not open in chat'), findsOneWidget); runtime.dispose(); }); testWidgets('run list More selects Open in chat on desktop', (tester) async { final run = { 'id': 42, 'title': 'Build', 'status': 'failed', 'created_at': '2026-10-17T19:00:00', }; JsonMap? selected; final gateway = FakeChatGateway(); tester.view.physicalSize = const Size(900, 700); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( MaterialApp( home: Scaffold( body: RunListRow( run: run, now: DateTime(2026, 10, 18), gateway: gateway, onRunChanged: (_) {}, onOpenInChat: (context, value) async => selected = value, ), ), ), ); await tester.tap(find.byKey(const Key('run-list-more-menu-42'))); await tester.pumpAndSettle(); expect(find.text('Open in chat'), findsOneWidget); await tester.tap(find.text('Open in chat')); expect(selected, same(run)); }); testWidgets('run detail More selects Open in chat from the phone sheet', ( tester, ) async { final run = {'id': 42, 'title': 'Build'}; JsonMap? selected; final gateway = FakeChatGateway(); tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( MaterialApp( home: Scaffold( appBar: AppBar( actions: [ RunMoreMenu( run: run, gateway: gateway, onRunChanged: (_) {}, onOpenInChat: (context, value) async => selected = value, ), ], ), ), ), ); await tester.tap(find.byTooltip('More actions')); await tester.pumpAndSettle(); expect(find.text('Open in chat'), findsOneWidget); await tester.tap(find.text('Open in chat')); expect(selected, same(run)); }); testWidgets('pipeline list row More selects Open in chat with latest run', ( tester, ) async { final fixture = PipelineFixture(); final gateway = gatewayFor(fixture); final controller = PipelinesController(PipelinesRepository(gateway)); addTearDown(controller.dispose); await controller.load(); PipelineSummary? selected; PipelineRun? selectedRun; tester.view.physicalSize = const Size(900, 700); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( MaterialApp( home: Scaffold( body: PipelineListPanel( controller: controller, onOpenInChat: (context, pipeline, run) async { selected = pipeline; selectedRun = run; }, ), ), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('pipeline-row-more-menu-1'))); await tester.pumpAndSettle(); expect(find.text('Open in chat'), findsOneWidget); await tester.tap(find.text('Open in chat')); expect(selected?.id, 1); expect(selectedRun?.id, 42); }); testWidgets( 'pipeline detail actions select Open in chat from phone overflow', (tester) async { final fixture = PipelineFixture(); final gateway = gatewayFor(fixture); final controller = PipelinesController(PipelinesRepository(gateway)); addTearDown(controller.dispose); final detail = PipelineDetail.fromJson(fixture.details.first); PipelineDetail? selected; tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( MaterialApp( home: Scaffold( body: Builder( builder: (context) => pipelineActionsMenu( context, controller, detail, onOpenInChat: (context, value, run) async => selected = value, ), ), ), ), ); await tester.tap(find.byTooltip('Pipeline actions')); await tester.pumpAndSettle(); expect(find.text('Open in chat'), findsOneWidget); await tester.tap(find.text('Open in chat')); expect(selected?.id, detail.id); }, ); }