Files
pipelines/app/test/features/chat/chat_starred_tab_cycle_test.dart
T
alan 23d8658e80 Chat: Ctrl+Tab cycles the list's chats, starred only when any are starred
Ctrl+Tab / Ctrl+Shift+Tab on the Chat destination now steps through the
chat list's open chats in list order (wrapping) instead of the workspace
panes: only the starred chats when any open chat is starred, otherwise all
open chats. From a chat outside the cycle it goes to the first (Shift: last).
The target opens as a list click does, focusing the pane already showing it.
The terminal's claim on Ctrl+Tab and floating-window cycling are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 14:28:18 +00:00

218 lines
7.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/screen.dart';
import 'package:pipelines_app/features/chat/ui/sessions/session_actions.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
import 'package:pipelines_app/features/terminal/terminal_scope.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'conversation/chat_thread_fixtures.dart';
/// Ctrl+Tab steps through the list's chats: the starred ones when any is
/// starred, otherwise every open chat, opening each as a click would.
class _Gateway extends FakeChatGateway {
_Gateway(this.rows, this.starred);
final List<String> rows;
final Set<String> starred;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_sessions') {
return Future.value({
'chat_sessions': [
for (final id in rows)
{
'id': id,
'title': 'Chat $id',
if (starred.contains(id)) 'starred': true,
if (id == 'closed') 'closed_at': 1,
},
],
});
}
if (type == 'chat_shells') return Future.value({'chat_shells': []});
return super.call(type, fields);
}
}
class _ClaimingTerminal implements TerminalActions {
const _ClaimingTerminal();
@override
void toggle() {}
@override
bool get claimsTabKeys => true;
@override
Future<String?> openAt(ApiGateway gateway, String cwd) async => null;
}
Finder _input(String id) => find.descendant(
of: find.byKey(ValueKey('chat-composer-$id')),
matching: find.byType(EditableText),
);
ChatWorkspaceController _workspace(WidgetTester tester) =>
tester.widget<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
String? _active(WidgetTester tester) =>
_workspace(tester).activePane?.sessionId;
List<String> _paneSessions(WidgetTester tester) => [
for (final pane in _workspace(tester).panes) pane.sessionId,
];
Future<void> _tab(WidgetTester tester, {bool reverse = false}) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
if (reverse) await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
if (reverse) await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pumpAndSettle();
}
Future<void> _setup(
WidgetTester tester, {
required List<String> rows,
Set<String> starred = const {},
required String open,
Size size = const Size(1800, 1000),
bool terminal = false,
}) async {
SharedPreferences.setMockInitialValues({});
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final gateway = _Gateway(rows, starred)..history = (_) async => chatPage([]);
addTearDown(gateway.close);
await tester.pumpWidget(
MaterialApp(
builder: terminal
? (context, child) =>
TerminalScope(actions: const _ClaimingTerminal(), child: child!)
: null,
home: AdaptiveShell(
destination: AppDestination.chat,
destinationBodies: {
AppDestination.chat: (_) => ChatScreen(gateway: gateway),
},
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(Key('chat-row-$open')));
await tester.pumpAndSettle();
}
void main() {
testWidgets('starred chats only, in list order, wrapping both ways', (
tester,
) async {
// Starred sorts by title: Chat b, Chat d.
await _setup(
tester,
rows: ['a', 'd', 'c', 'b', 'closed'],
starred: {'b', 'd'},
open: 'b',
);
await _tab(tester);
expect(_active(tester), 'd');
expect(
tester.widget<EditableText>(_input('d')).focusNode.hasPrimaryFocus,
isTrue,
);
await _tab(tester);
expect(_active(tester), 'b');
await _tab(tester, reverse: true);
expect(_active(tester), 'd');
await _tab(tester, reverse: true);
expect(_active(tester), 'b');
expect(_workspace(tester).paneCount, 1);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('from an unstarred chat: first starred, or last backwards', (
tester,
) async {
await _setup(
tester,
rows: ['a', 'd', 'c', 'b'],
starred: {'b', 'd'},
open: 'a',
);
await _tab(tester);
expect(_active(tester), 'b');
await tester.tap(find.byKey(const Key('chat-row-c')));
await tester.pumpAndSettle();
await _tab(tester, reverse: true);
expect(_active(tester), 'd');
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('no starred chat: every open chat, closed left out', (
tester,
) async {
await _setup(tester, rows: ['a', 'b', 'c', 'closed'], open: 'a');
for (final id in ['b', 'c', 'a']) {
await _tab(tester);
expect(_active(tester), id);
}
await _tab(tester, reverse: true);
expect(_active(tester), 'c');
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('a chat shown in another pane is focused, never duplicated', (
tester,
) async {
await _setup(tester, rows: ['a', 'b', 'c'], open: 'a');
await tester.tap(
find.byWidgetPredicate(
(widget) =>
widget is SessionActionsButton && widget.session['id'] == 'c',
),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Open to the right'));
await tester.pumpAndSettle();
expect(_paneSessions(tester), ['a', 'c']);
expect(_active(tester), 'c');
// c wraps to a, which already has a pane: that pane takes focus.
await _tab(tester);
expect(_paneSessions(tester), ['a', 'c']);
expect(_active(tester), 'a');
// b has no pane: it opens in the active pane, as a click would.
await _tab(tester);
expect(_paneSessions(tester), ['b', 'c']);
expect(_active(tester), 'b');
await _tab(tester);
expect(_paneSessions(tester), ['b', 'c']);
expect(_active(tester), 'c');
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('narrow single-chat view follows the same cycle', (tester) async {
await _setup(
tester,
rows: ['a', 'b', 'c'],
starred: {'a', 'c'},
open: 'a',
size: const Size(400, 800),
);
await _tab(tester);
expect(_active(tester), 'c');
expect(find.byKey(const Key('chat-row-a')).hitTestable(), findsNothing);
await _tab(tester);
expect(_active(tester), 'a');
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('a shown terminal keeps Ctrl+Tab', (tester) async {
await _setup(tester, rows: ['a', 'b', 'c'], open: 'a', terminal: true);
await _tab(tester);
await _tab(tester, reverse: true);
expect(_active(tester), 'a');
await tester.pumpWidget(const SizedBox.shrink());
});
}