Files
pipelines/app/test/features/chat/conversation/chat_session_section_test.dart
alan 8f52c7ec55 Narrow chat tests to Material hosts
Remove Cupertino-only fixtures, test variants, and the obsolete readable-text test now that the app is Material-only. Preserve the Material coverage while avoiding unreachable platform branches.
2026-11-11 12:35:38 +00:00

178 lines
6.1 KiB
Dart

import 'dart:convert';
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<JsonMap> 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) => MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(body: child),
);
Future<
({_ExportGateway gateway, List<ArtifactFile> shared, List<String> clipboard})
>
_open(WidgetTester tester, {JsonMap? row}) async {
tester.view.physicalSize = const Size(800, 2400);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final clipboard = <String>[];
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 = <ArtifactFile>[];
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,
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-info')));
await tester.pumpAndSettle();
return (gateway: gateway, shared: shared, clipboard: clipboard);
}
Future<void> _tap(WidgetTester tester, Key key) async {
await tester.ensureVisible(find.byKey(key));
await tester.tap(find.byKey(key));
await tester.pump();
}
void main() {
// Chat details is Material only (a dialog, or a sheet on a phone).
testWidgets('shows the native id, harness and directory', (tester) async {
await _open(tester);
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('copies the id and the resume command', (tester) async {
final opened = await _open(tester);
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('export hands the server file to the file action', (
tester,
) async {
final opened = await _open(tester);
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, 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, '');
});
}