import 'dart:async'; import 'dart:convert'; import 'dart:ui'; import 'package:pipelines_app/application/detached/detached_platform.dart'; import 'package:pipelines_app/application/detached/detached_route.dart'; import 'package:pipelines_app/shell/detached/detached_window_scope.dart'; import '../../core/transport/pipelines_client_test.dart' show FakeChannel; /// A platform that records what it was asked to open and send. class FakeDetachedPlatform extends DetachedPlatform { DetachedLaunch result = DetachedLaunch.opened; final launches = <({DetachedRoute route, String title, bool tab})>[]; final stored = {}; final sent = []; bool mainOpen = true; bool closed = false; String? becameMain; final incoming = StreamController.broadcast(); @override bool get supported => true; @override Future launch( DetachedRoute route, { required String title, required Size size, bool tab = false, }) async { launches.add((route: route, title: title, tab: tab)); return tab ? DetachedLaunch.opened : result; } @override Stream get messages => incoming.stream; @override Future put(String token, String json) async => stored[token] = json; @override Future take(String token) async => stored[token]; @override Future sendToMain(DetachedMessage message) async { if (!mainOpen) return false; sent.add(message); return true; } @override Future closeSelf() async => closed = true; @override Future becomeMain(String appPath) async { becameMain = appPath; return true; } } /// An opener that records the items and opens them at once. class FakeOpener implements DetachedWindowOpener { final chats = []; final terminals = []; final diffs = >>[]; bool opens = true; @override void openChat( String sessionId, { required String title, void Function()? onOpened, }) { chats.add(sessionId); if (opens) onOpened?.call(); } @override void openTerminal({ String tabKey = '', required String title, void Function()? onOpened, }) { terminals.add(tabKey); if (opens) onOpened?.call(); } @override void openDiff( List> files, { int initialIndex = 0, required String title, void Function()? onOpened, }) { diffs.add(files); if (opens) onOpened?.call(); } } /// Answers a chat app's requests over [channel] like a server with chats /// a, b and c, recording each request (type and body). class ChatServer { ChatServer(this.channel, {List openChats = const []}) : _entries = openChats; final FakeChannel channel; final _answered = {}; final requests = >[]; List _entries; int _revision = 1; /// The open-chat writes the app sent (`chat_open_workspace_request`). List> get workspaceWrites => [ for (final request in requests) if (request['type'] == 'chat_open_workspace' && request['chat_open_workspace_request'] != null) request, ]; void answer() { for (final raw in channel.outgoing.toList()) { final m = jsonDecode(raw as String) as Map; final id = m['id']; if (id == null || !_answered.add(id)) continue; requests.add(m); channel.incoming.add(jsonEncode(_reply('${m['type']}', id, m))); } } Map _reply(String type, Object? id, Map m) { switch (type) { case 'chat_sessions': return { 'id': id, 'type': type, 'chat_sessions': [ for (final s in const ['a', 'b', 'c']) { 'id': s, 'title': 'Chat $s', 'directory': '/workspace/demo', 'updated_at': '2026-11-12T10:00:00Z', }, ], }; case 'chat_messages': return { 'id': id, 'type': 'chat_history', 'chat_history': { 'messages': [ { 'id': 'm0', 'role': 'user', 'parts': [ {'type': 'text', 'text': 'Hello from chat'}, ], }, ], 'next_cursor': '', 'has_more': false, }, }; case 'chat_open_workspace': final write = m['chat_open_workspace_request'] as Map?; if (write != null) { _revision++; _entries = write['entries'] as List; } return { 'id': id, 'type': type, 'chat_open_workspace': {'revision': _revision, 'entries': _entries}, }; default: return {'id': id, 'type': type}; } } }