Chat stack: groups of open chats, synced with the open-chat set

A chat dropped onto the middle of another makes a named group (asked at
once, "Group N" by default); onto a group it joins, a group dropped onto
a chat takes it in, groups merge. The top level shows each group as a
small pile with its name and count; a tap opens it in place under a
header that leads back (also the scrim, Back and Escape). In a group,
dropping a chat on the header leaves the group; a group of one
dissolves. The close button ungroups a dropped group with Undo. Rename,
Ungroup, Close all (with Undo), Move to group and Remove from group are
in the group menu and as semantic actions.

Each open-chat entry carries its group name (protocol field "group",
omitted when ungrouped; older clients ignore it), stored and pushed by
the server like the order; names are trimmed, printable, at most 80
characters. Needs a server restart to keep groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 22:18:44 +00:00
parent 130c1e6708
commit efaa0d9fca
30 changed files with 2399 additions and 308 deletions
@@ -159,8 +159,8 @@ void main() {
// Lifted: the remove target comes in.
await tester.pumpAndSettle();
expect(_shown(tester), 1);
// Up two slots: b and c slide down to open the gap.
await gesture.moveBy(const Offset(0, -kChatStackPitch * 2));
// Up past the middle of c (its middle would group): b and c slide down.
await gesture.moveBy(const Offset(0, -kChatStackPitch * 2.4));
await tester.pumpAndSettle();
expect(tester.getCenter(_button(tester, 'b')).dy, b.dy + kChatStackPitch);
await gesture.up();
@@ -305,7 +305,7 @@ void main() {
final b = tester.getCenter(_button(tester, 'b'));
final gesture = await _lift(tester, _button(tester, 'a'));
expect(_shown(tester), 1);
await gesture.moveBy(const Offset(0, -kChatStackPitch));
await gesture.moveBy(const Offset(0, -kChatStackPitch * 1.4));
await tester.pump();
expect(tester.getCenter(_button(tester, 'b')).dy, b.dy + kChatStackPitch);
// No turn or growth: the button only fades.
@@ -0,0 +1,366 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/servers/server_scope.dart';
import 'package:pipelines_app/features/chat/screen.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_close_target.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_column.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_disc.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_drag.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_group_dialogs.dart';
import 'package:pipelines_app/features/chat/stack/mobile_chat_navigation.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../conversation/chat_thread_fixtures.dart';
/// A chat server keeping the open-chat set with each entry's group.
class _Server extends FakeChatGateway {
List<String> open = ['a', 'b', 'c'];
Map<String, String> groups = {};
int revision = 1;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_sessions') {
return Future.value({
'chat_sessions': [
for (final id in ['a', 'b', 'c', 'd'])
{'id': id, 'title': 'Chat $id', 'directory': '/w'},
],
});
}
if (type == 'chat_shells') return Future.value({'chat_shells': []});
if (type == 'chat_open_workspace') {
final request = fields['chat_open_workspace_request'] as Map?;
var rejected = false;
if (request != null && request['seed'] != true) {
if (request['base_revision'] != revision) {
rejected = true;
} else {
final entries = (request['entries'] as List).cast<Map>();
open = [for (final e in entries) '${e['session_id']}'];
groups = {
for (final e in entries)
if (e['group'] != null) '${e['session_id']}': '${e['group']}',
};
revision++;
}
}
return Future.value({
'type': 'chat_open_workspace',
'chat_open_workspace': _body(rejected),
});
}
return super.call(type, fields);
}
JsonMap _body([bool rejected = false]) => {
'revision': revision,
'entries': [
for (final id in open)
{'session_id': id, if (groups[id] != null) 'group': groups[id]},
],
if (rejected) 'rejected': true,
};
/// Another device changed the set.
void change(List<String> ids, Map<String, String> byGroup) {
open = ids;
groups = byGroup;
revision++;
emitRaw({
'type': 'chat_open_workspace_changed',
'chat_open_workspace': _body(),
});
}
}
Future<_Server> _pump(WidgetTester tester) async {
SharedPreferences.setMockInitialValues({});
MobileChatNavigationController.instance.debugUse(
MobileChatNavigation.chatStack,
);
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final server = _Server()..history = (_) async => chatPage(const []);
bindServerIdentity(server, 'server-groups');
addTearDown(server.close);
await tester.pumpWidget(
MaterialApp(
home: AdaptiveShell(
destination: AppDestination.chat,
destinationBodies: {
AppDestination.chat: (_) =>
ChatScreen(gateway: server, persistWorkspace: true),
},
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-row-a')));
await tester.pumpAndSettle();
await _expand(tester);
return server;
}
Future<void> _expand(WidgetTester tester) async {
await tester.tap(find.byKey(const Key('chat-stack')));
await tester.pumpAndSettle();
}
ChatWorkspaceController _workspace(WidgetTester tester) =>
tester.widget<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
Finder _chat(WidgetTester tester, String id) {
final pane = _workspace(
tester,
).panes.firstWhere((pane) => pane.sessionId == id);
return find.byKey(ValueKey('chat-stack-button-${pane.id}'));
}
Finder _group(String name) =>
find.byKey(ValueKey('chat-stack-button-group-$name'));
final _header = find.byKey(const ValueKey('chat-stack-button-header'));
/// Holds [item] until it lifts, moves it by [by], lets go.
Future<void> _drag(WidgetTester tester, Finder item, Offset by) async {
final gesture = await tester.startGesture(tester.getCenter(item));
await tester.pump(chatStackLiftDelay + const Duration(milliseconds: 20));
await gesture.moveBy(by);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
}
const _up = Offset(0, -kChatStackPitch);
/// Makes group "Work" of b and a (a dropped onto b), naming it.
Future<void> _makeWork(WidgetTester tester) async {
await _drag(tester, _chat(tester, 'a'), _up);
expect(find.text('Name the group'), findsOneWidget);
await tester.enterText(
find.byKey(const Key('chat-stack-group-name')),
'Work',
);
await tester.tap(find.text('Save'));
await tester.pumpAndSettle();
}
void _act(WidgetTester tester, Finder target, String label) {
final node = tester.getSemantics(target);
final action = node
.getSemanticsData()
.customSemanticsActionIds!
.map(CustomSemanticsAction.getAction)
.firstWhere((action) => action!.label == label)!;
tester.binding.renderViews.first.owner!.semanticsOwner!.performAction(
node.id,
SemanticsAction.customAction,
CustomSemanticsAction.getIdentifier(action),
);
}
void main() {
tearDown(
() => MobileChatNavigationController.instance.debugUse(
MobileChatNavigation.quickSwitch,
),
);
testWidgets('a chat dropped onto a chat makes a named group that syncs', (
tester,
) async {
final server = await _pump(tester);
// Mid-drag over b's middle: b grows (the drop target).
final gesture = await tester.startGesture(
tester.getCenter(_chat(tester, 'a')),
);
await tester.pump(chatStackLiftDelay + const Duration(milliseconds: 20));
final b = tester.getSize(_chat(tester, 'b'));
await gesture.moveBy(_up);
await tester.pumpAndSettle();
expect(
tester.getRect(_chat(tester, 'b')).width,
closeTo(b.width * chatStackOntoScale, .5),
);
await gesture.up();
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('chat-stack-group-name')),
'Work',
);
await tester.tap(find.text('Save'));
await tester.pumpAndSettle();
expect(server.open, ['b', 'a', 'c']);
expect(server.groups, {'b': 'Work', 'a': 'Work'});
expect(_group('Work'), findsOneWidget);
expect(find.text('Work'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('a group opens in place; header, Back and outside return', (
tester,
) async {
await _pump(tester);
await _makeWork(tester);
await tester.tap(_group('Work'));
await tester.pumpAndSettle();
expect(_chat(tester, 'a').hitTestable(), findsOneWidget);
expect(_chat(tester, 'b').hitTestable(), findsOneWidget);
expect(_header.hitTestable(), findsOneWidget);
// The top level's chat c is behind: no input.
expect(_chat(tester, 'c').hitTestable(), findsNothing);
await tester.tap(_header);
await tester.pumpAndSettle();
expect(_group('Work').hitTestable(), findsOneWidget);
// Outside: out of the group first, then folded.
await tester.tap(_group('Work'));
await tester.pumpAndSettle();
await tester.tapAt(const Offset(40, 120));
await tester.pumpAndSettle();
expect(_group('Work').hitTestable(), findsOneWidget);
await tester.tapAt(const Offset(40, 120));
await tester.pumpAndSettle();
expect(find.byType(ChatStackColumn), findsNothing);
// Back leaves the group, then folds.
await _expand(tester);
await tester.tap(_group('Work'));
await tester.pumpAndSettle();
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(_group('Work').hitTestable(), findsOneWidget);
// A chat in a group opens as any other.
await tester.tap(_group('Work'));
await tester.pumpAndSettle();
await tester.tap(_chat(tester, 'b'));
await tester.pumpAndSettle();
expect(_workspace(tester).activePane?.sessionId, 'b');
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('a chat joins a group; a group takes a chat in', (tester) async {
final server = await _pump(tester);
await _makeWork(tester);
// Top level: Work (b, a) at the bottom, c above it. c down onto it.
await _drag(tester, _chat(tester, 'c'), -_up);
expect(server.groups, {'b': 'Work', 'a': 'Work', 'c': 'Work'});
expect(server.open, ['b', 'a', 'c']);
// Leaving from inside the group: drop on its header.
await tester.tap(_group('Work'));
await tester.pumpAndSettle();
await _drag(tester, _chat(tester, 'c'), _up);
expect(server.groups, {'b': 'Work', 'a': 'Work'});
expect(server.open, ['b', 'a', 'c']);
// Back on the top level: drag the group onto c.
await tester.tap(_header);
await tester.pumpAndSettle();
await _drag(tester, _group('Work'), _up);
expect(server.groups, {'b': 'Work', 'a': 'Work', 'c': 'Work'});
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('the last chat left alone dissolves the group', (tester) async {
final server = await _pump(tester);
await _makeWork(tester);
await tester.tap(_group('Work'));
await tester.pumpAndSettle();
// a is second in the group: one slot up is the header.
await _drag(tester, _chat(tester, 'a'), _up);
expect(server.groups, isEmpty);
// The group is gone: back on the top level.
expect(_header, findsNothing);
expect(_chat(tester, 'a').hitTestable(), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('Rename, Ungroup and Close all; each syncs, Undo restores', (
tester,
) async {
final semantics = tester.ensureSemantics();
final server = await _pump(tester);
await _makeWork(tester);
_act(tester, _group('Work'), ChatStackGroupAction.rename.label);
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('chat-stack-group-name')),
'Personal',
);
await tester.tap(find.text('Save'));
await tester.pumpAndSettle();
expect(server.groups, {'b': 'Personal', 'a': 'Personal'});
_act(tester, _group('Personal'), ChatStackGroupAction.ungroup.label);
await tester.pumpAndSettle();
expect(server.groups, isEmpty);
expect(find.text('Ungrouped Personal'), findsOneWidget);
await tester.tap(find.text('Undo'));
await tester.pumpAndSettle();
expect(server.groups, {'b': 'Personal', 'a': 'Personal'});
await _expand(tester);
_act(tester, _group('Personal'), ChatStackGroupAction.closeAll.label);
await tester.pumpAndSettle();
expect(server.open, ['c']);
await tester.tap(find.text('Undo'));
await tester.pumpAndSettle();
expect(server.open, ['b', 'a', 'c']);
expect(server.groups, {'b': 'Personal', 'a': 'Personal'});
semantics.dispose();
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('Move to group and Remove from group as semantic actions', (
tester,
) async {
final semantics = tester.ensureSemantics();
final server = await _pump(tester);
await _makeWork(tester);
_act(tester, _chat(tester, 'c'), ChatStackColumn.moveToGroupLabel);
await tester.pumpAndSettle();
await tester.tap(find.text('Work').last);
await tester.pumpAndSettle();
expect(server.groups['c'], 'Work');
await tester.tap(_group('Work'));
await tester.pumpAndSettle();
_act(tester, _chat(tester, 'b'), ChatStackColumn.leaveGroupLabel);
await tester.pumpAndSettle();
expect(server.groups, {'a': 'Work', 'c': 'Work'});
expect(server.open, ['a', 'c', 'b']);
semantics.dispose();
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('a group dropped on the close button is ungrouped', (
tester,
) async {
final server = await _pump(tester);
await _makeWork(tester);
final group = tester.getCenter(_group('Work'));
final close = tester.getCenter(
find.bySemanticsLabel(ChatStackCloseTarget.label),
);
await _drag(tester, _group('Work'), close - group);
expect(server.groups, isEmpty);
expect(server.open, ['b', 'a', 'c']);
expect(find.text('Ungrouped Work'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('groups from another device appear', (tester) async {
final server = await _pump(tester);
server.change(['a', 'b', 'c'], {'a': 'Home', 'c': 'Home'});
await tester.pumpAndSettle();
expect(_group('Home'), findsOneWidget);
expect(find.text('Home'), findsOneWidget);
// The group's pill counts its chats.
expect(find.text('2'), findsWidgets);
await tester.pumpWidget(const SizedBox.shrink());
});
}
@@ -0,0 +1,168 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_drop_target.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_entry.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_group_ops.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_groups.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_node.dart';
import 'package:pipelines_app/features/chat/workspace/chat_open_workspace_gateway.dart';
ChatStackEntry _entry(String id, [String? group]) =>
ChatStackEntry(paneId: 'p$id', session: {'id': id}, group: group);
void main() {
group('ChatStackGroups', () {
test('assigns, merges on rename, dissolves a group of one', () {
final groups = ChatStackGroups();
var changes = 0;
groups.addListener(() => changes++);
// One chat is no group.
groups.assign(['a'], 'Work');
expect(groups.assignments, isEmpty);
expect(changes, 0);
groups.assign(['a', 'b'], 'Work');
groups.assign(['c', 'd'], 'Home');
expect(groups.membersOf('Work'), ['a', 'b']);
expect(groups.nextName(), 'Group 1');
groups.rename('Home', 'Work');
expect(groups.membersOf('Work'), ['a', 'b', 'c', 'd']);
groups.unassign(['a', 'b', 'c']);
expect(groups.assignments, isEmpty);
groups.assign(['a', 'b', 'c'], 'Group 1');
expect(groups.nextName(), 'Group 2');
groups.retain({'a', 'x'});
expect(groups.assignments, isEmpty);
expect(changes, greaterThan(0));
groups.dispose();
});
test('names are trimmed, bounded and never empty', () {
expect(chatStackGroupName(' Work \n'), 'Work');
expect(chatStackGroupName(' '), isNull);
expect(chatStackGroupName('a\tb'), 'a b');
expect(chatStackGroupName('x' * 100)!.length, chatStackGroupNameLimit);
});
});
group('nodes and drops', () {
final entries = [
_entry('a'),
_entry('b', 'Work'),
_entry('c'),
_entry('d', 'Work'),
];
final nodes = chatStackNodes(entries);
test('a group stands where its first chat is', () {
expect(nodes.map((node) => node.key), [
'chat:pa',
'group:Work',
'chat:pc',
]);
expect(chatStackOrder(nodes), ['a', 'b', 'd', 'c']);
// A lone member shows as a chat.
expect(
chatStackNodes([_entry('x', 'Solo')]).single,
isA<ChatStackChatNode>(),
);
});
test('dropping onto a chat or a group joins as expected', () {
final made = chatStackJoined(nodes, 2, 0, () => 'Group 1');
expect(made.created, isTrue);
expect(made.join, ['a', 'c']);
expect(made.order, ['a', 'c', 'b', 'd']);
final joined = chatStackJoined(nodes, 0, 1, () => 'unused');
expect((joined.group, joined.created), ('Work', false));
expect(joined.join, ['a']);
expect(joined.order, ['b', 'd', 'a', 'c']);
final took = chatStackJoined(nodes, 1, 2, () => 'unused');
expect(took.join, ['c']);
expect(took.order, ['a', 'b', 'd', 'c']);
expect(chatStackLeft(nodes, 'Work', 'b'), ['a', 'd', 'b', 'c']);
expect(chatStackRegrouped(nodes, 'Work', ['d', 'b']), [
'a',
'd',
'b',
'c',
]);
expect(chatStackMoved(nodes, 0, 2), ['b', 'd', 'c', 'a']);
});
test('the middle of an item groups, its edges reorder, stably', () {
bool all(int _) => true;
// From slot 0, one slot up: the middle of item 1.
expect(
chatStackDropTarget(
position: 1,
from: 0,
count: 3,
slot: 0,
canDropOnto: all,
),
(slot: 0, onto: 1),
);
// Past its middle: the gap moves above it...
expect(
chatStackDropTarget(
position: 1.4,
from: 0,
count: 3,
slot: 0,
canDropOnto: all,
),
(slot: 1, onto: null),
);
// ...and holds there while item 1 slides below it.
expect(
chatStackDropTarget(
position: 1.3,
from: 0,
count: 3,
slot: 1,
canDropOnto: all,
),
(slot: 1, onto: null),
);
// Below its middle the gap stays below it.
expect(
chatStackDropTarget(
position: .7,
from: 0,
count: 3,
slot: 0,
canDropOnto: all,
),
(slot: 0, onto: null),
);
// Without grouping the middle reorders too; the gap never passes
// lastSlot.
expect(
chatStackDropTarget(
position: 2.3,
from: 0,
count: 3,
slot: 1,
lastSlot: 1,
),
(slot: 1, onto: null),
);
});
});
test('an entry carries its group over the wire', () {
final entry = ChatOpenWorkspaceEntry.fromJson({
'session_id': 'a',
'group': ' Work ',
})!;
expect(entry.group, 'Work');
expect(entry.toJson(), {'session_id': 'a', 'group': 'Work'});
expect(ChatOpenWorkspaceEntry.fromJson({'session_id': 'b'})!.group, isNull);
const snapshot = ChatOpenWorkspaceSnapshot(1, [
ChatOpenWorkspaceEntry('a'),
]);
expect(
snapshot.sameEntries([const ChatOpenWorkspaceEntry('a', group: 'W')]),
isFalse,
);
});
}