Files
pipelines/app/test/application/detached/detached_fixtures.dart
T
alan 03983cfd1f Detached chat: read the chat again once its machine connects, and follow the main window's look
A detached chat window read the chat list once, before any saved machine had
connected, and never again; its pane also did not rebuild when the runtime
adopted a row. Without the row the header showed a bare "Chat" with no folder
or model, the window was named by the session id, and the chat's colour (bar
tint, focus ring, prompt bar) was missing. The window now re-reads the list on
each connection change and rebuilds on the runtime, as the Chat destination
does. It also re-reads the stored design and font on the browser's storage
event and on coming to the front, so a Settings change in the main window
follows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 17:28:47 +00:00

190 lines
5.1 KiB
Dart

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 = <String, String>{};
final sent = <DetachedMessage>[];
bool mainOpen = true;
bool closed = false;
String? becameMain;
final incoming = StreamController<DetachedMessage>.broadcast();
@override
bool get supported => true;
@override
Future<DetachedLaunch> 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<DetachedMessage> get messages => incoming.stream;
@override
Future<void> put(String token, String json) async => stored[token] = json;
@override
Future<String?> take(String token) async => stored[token];
@override
Future<bool> sendToMain(DetachedMessage message) async {
if (!mainOpen) return false;
sent.add(message);
return true;
}
@override
Future<void> closeSelf() async => closed = true;
@override
Future<bool> 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 = <String>[];
final terminals = <String>[];
final diffs = <List<Map<String, Object?>>>[];
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<Map<String, Object?>> 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<Object?> openChats = const []})
: _entries = openChats;
final FakeChannel channel;
final _answered = <Object?>{};
final requests = <Map<String, dynamic>>[];
/// False answers the chat list with no rows, as a pool whose machines
/// have not connected yet does.
bool listsChats = true;
/// Each chat's shared colour, by id.
final colors = <String, String>{};
List<Object?> _entries;
int _revision = 1;
/// The open-chat writes the app sent (`chat_open_workspace_request`).
List<Map<String, dynamic>> 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<String, dynamic>;
final id = m['id'];
if (id == null || !_answered.add(id)) continue;
requests.add(m);
channel.incoming.add(jsonEncode(_reply('${m['type']}', id, m)));
}
}
Map<String, Object?> _reply(String type, Object? id, Map<String, dynamic> m) {
switch (type) {
case 'chat_sessions':
return {
'id': id,
'type': type,
'chat_sessions': [
if (listsChats)
for (final s in const ['a', 'b', 'c'])
{
'id': s,
'title': 'Chat $s',
'directory': '/workspace/demo',
'updated_at': '2026-11-12T10:00:00Z',
'color': ?colors[s],
},
],
};
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<Object?>;
}
return {
'id': id,
'type': type,
'chat_open_workspace': {'revision': _revision, 'entries': _entries},
};
default:
return {'id': id, 'type': type};
}
}
}