import 'dart:async'; 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/chat/domain/chat_commands.dart'; import 'package:pipelines_app/features/chat/screen.dart'; /// A chat destination server whose `/new` re-keys chat `s` to `n1`, as the /// registry does, and which can push another device's move. class _NewSessionGateway implements ApiGateway { final calls = <({String type, JsonMap fields})>[]; final pushed = StreamController.broadcast(); var current = _row('s'); @override Stream get frames => pushed.stream; @override Future call(String type, [JsonMap fields = const {}]) async { calls.add((type: type, fields: fields)); switch (type) { case 'chat_sessions': return { 'chat_sessions': [current, _other], }; case 'chat_new_session': current = _row('n1'); return { 'chat_session': {...current, 'previous_id': 's'}, }; case 'chat_messages': final query = fields['chat_query'] as JsonMap; final old = query['session_id'] == 's'; return { 'chat_history': { 'messages': [ if (old) { 'id': 'm1', 'role': 'user', 'parts': [ {'type': 'text', 'text': 'Earlier question'}, ], }, ], 'next_cursor': '', 'has_more': false, }, }; } return {}; } Iterable subscribed() => [ for (final call in calls) if (call.type == 'chat_subscribe') '${(call.fields['chat_subscribe'] as Map?)?['session_id']}', ]; } JsonMap _row(String id) => { 'id': id, 'title': 'Wire chat', 'backend': 'claude', 'directory': '/w', 'color': 'teal', 'starred': true, 'resume_command': 'cd /w && claude --resume $id', }; const JsonMap _other = {'id': 't', 'title': 'Other chat', 'directory': '/w'}; final _desktop = TargetPlatformVariant.only(TargetPlatform.linux); Future _pump(WidgetTester tester, _NewSessionGateway gateway) async { tester.view.physicalSize = const Size(1400, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( MaterialApp( theme: ThemeData(platform: TargetPlatform.linux), home: Scaffold(body: ChatScreen(gateway: gateway)), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('chat-row-s'))); await tester.pumpAndSettle(); expect(find.text('Earlier question'), findsOneWidget); } void main() { testWidgets( '/new keeps the chat in its row and opens the fresh, empty session', (tester) async { final gateway = _NewSessionGateway(); await _pump(tester, gateway); await tester.enterText( find.byKey(const Key('chat-composer-field')), '/new', ); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pump(); final field = tester.widget( find.descendant( of: find.byKey(const Key('chat-composer-field')), matching: find.byType(EditableText), ), ); if (field.controller.text.isNotEmpty) { await tester.sendKeyEvent(LogicalKeyboardKey.enter); } await tester.pumpAndSettle(); expect(gateway.calls.where((c) => c.type == 'chat_create'), isEmpty); expect(find.byKey(const Key('chat-row-s')), findsNothing); expect(find.byKey(const Key('chat-row-n1')), findsOneWidget); expect(find.byKey(const Key('chat-row-t')), findsOneWidget); expect(find.text(chatNewSessionNotice), findsOneWidget); expect(find.text('Earlier question'), findsNothing); expect(gateway.subscribed().last, 'n1'); }, variant: _desktop, ); testWidgets( 'an open chat follows a new session started on another device', (tester) async { final gateway = _NewSessionGateway(); await _pump(tester, gateway); gateway.current = _row('n1'); gateway.pushed.add({ 'type': 'chat_session_changed', 'chat_session': {...gateway.current, 'previous_id': 's'}, }); await tester.pumpAndSettle(); expect(find.byKey(const Key('chat-row-s')), findsNothing); expect(find.byKey(const Key('chat-row-n1')), findsOneWidget); expect(find.text(chatNewSessionElsewhereNotice), findsOneWidget); expect(find.text('Earlier question'), findsNothing); expect(gateway.subscribed().last, 'n1'); }, variant: _desktop, ); }