import 'dart:convert'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/artifacts/actions/artifact_file.dart'; import 'package:pipelines_app/features/artifacts/actions/artifact_file_bridge.dart'; import 'package:pipelines_app/features/artifacts/actions/artifact_transfer.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/domain/chat_session_handoff.dart'; import 'chat_thread_fixtures.dart'; const _id = '0b6f1c2e-1111-4222-8333-444455556666'; const _resume = 'cd /work/app && claude --resume $_id'; JsonMap _row({String backend = 'claude', bool native = true}) => { 'id': _id, 'title': 'Fix tests', 'backend': backend, 'directory': '/work/app', 'resume_command': _resume, 'native_export': native, }; /// Answers chat_export with a fixed file; everything else as the thread fake. class _ExportGateway extends FakeChatGateway { @override Future call(String type, [JsonMap fields = const {}]) async { if (type != 'chat_export') return super.call(type, fields); calls.add((type: type, fields: fields)); final format = fields['chat_export']['format']; return { 'type': 'chat_export', 'chat_export_file': { 'session_id': _id, 'format': format, 'name': format == 'json' ? 'Fix-tests.json' : 'Fix-tests.md', 'mime': format == 'json' ? 'application/json' : 'text/markdown', 'content': '# Fix tests\n', 'messages': 1, }, }; } } Widget _host(Widget child, bool cupertino) => cupertino ? CupertinoApp( theme: const CupertinoThemeData(brightness: Brightness.light), home: CupertinoPageScaffold(child: child), ) : MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: Scaffold(body: child), ); Future< ({_ExportGateway gateway, List shared, List clipboard}) > _open(WidgetTester tester, {required bool cupertino, JsonMap? row}) async { tester.view.physicalSize = const Size(800, 2400); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final clipboard = []; final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; messenger.setMockMethodCallHandler(SystemChannels.platform, (call) async { if (call.method == 'Clipboard.setData') { clipboard.add('${(call.arguments as Map)['text']}'); } return null; }); addTearDown( () => messenger.setMockMethodCallHandler(SystemChannels.platform, null), ); final gateway = _ExportGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage([]); final shared = []; final bridge = ArtifactFileBridge( shareSupported: true, downloadSupported: true, share: (file) async { shared.add(file); return const ArtifactTransferResult(ArtifactTransferState.shared, ''); }, download: (file) async { shared.add(file); return const ArtifactTransferResult(ArtifactTransferState.saved, ''); }, ); await tester.pumpWidget( _host( ChatThreadView( gateway: gateway, sessionId: _id, session: row ?? _row(), onBack: () {}, fileBridge: bridge, ), cupertino, ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('chat-info'))); await tester.pumpAndSettle(); return (gateway: gateway, shared: shared, clipboard: clipboard); } Future _tap(WidgetTester tester, Key key) async { await tester.ensureVisible(find.byKey(key)); await tester.tap(find.byKey(key)); await tester.pump(); } void main() { for (final cupertino in [false, true]) { final host = cupertino ? 'cupertino' : 'material'; testWidgets('$host: shows the native id, harness and directory', ( tester, ) async { await _open(tester, cupertino: cupertino); expect(find.text('Session'), findsOneWidget); expect(find.text('Session ID'), findsOneWidget); expect(find.text(_id), findsOneWidget); expect(find.text('Claude'), findsOneWidget); expect(find.text('/work/app'), findsOneWidget); expect(find.text(_resume), findsOneWidget); expect(find.text('Export native session'), findsOneWidget); expect(find.bySemanticsLabel('Copy session ID'), findsOneWidget); }); testWidgets('$host: copies the id and the resume command', (tester) async { final opened = await _open(tester, cupertino: cupertino); await _tap(tester, const Key('chat-session-copy-id')); expect(opened.clipboard, [_id]); expect(find.text('Copied'), findsOneWidget); await _tap(tester, const Key('chat-session-copy-resume')); expect(opened.clipboard, [_id, _resume]); await tester.pump(const Duration(seconds: 3)); expect(find.text('Copied'), findsNothing); }); testWidgets('$host: export hands the server file to the file action', ( tester, ) async { final opened = await _open(tester, cupertino: cupertino); await _tap(tester, const Key('chat-session-export-markdown')); await tester.pumpAndSettle(); final request = opened.gateway.calls.lastWhere( (call) => call.type == 'chat_export', ); expect(request.fields['chat_export'], { 'session_id': _id, 'format': 'markdown', }); expect(opened.shared, hasLength(1)); final file = opened.shared.single; expect(file.name, 'Fix-tests.md'); expect(file.mimeType, 'text/markdown'); expect(utf8.decode(file.bytes), '# Fix tests\n'); expect(find.text('Shared Fix-tests.md.'), findsOneWidget); await _tap(tester, const Key('chat-session-export-json')); await tester.pumpAndSettle(); expect(opened.shared.last.name, 'Fix-tests.json'); }); } testWidgets('no native export or resume command when the row has none', ( tester, ) async { await _open( tester, cupertino: false, row: {'id': _id, 'directory': '/work/app'}, ); expect(find.text('OpenCode'), findsOneWidget); expect(find.text('Export native session'), findsNothing); expect(find.text('Copy resume command'), findsNothing); expect(find.text('Export conversation'), findsOneWidget); }); test('a conversation opened by id alone keeps the id', () { final handoff = ChatSessionHandoff.from('ses_abc', null); expect(handoff.id, 'ses_abc'); expect(handoff.backend, ''); expect(handoff.resumeCommand, ''); }); }