Merge: Next chat button cycles through the list

This commit is contained in:
2026-11-04 18:22:45 +00:00
9 changed files with 425 additions and 8 deletions
@@ -0,0 +1,191 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/screen.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/ui/layout/app_list_row.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'conversation/chat_thread_fixtures.dart';
/// Server recency order: the list shows Starred by name (apple, Zed), then
/// Chats by directory (Recent, Second in /w; Other in /v), then Closed folded.
const _rows = <JsonMap>[
{'id': 'r', 'title': 'Recent', 'directory': '/w'},
{'id': 'd', 'title': 'Other', 'directory': '/v'},
{'id': 'c', 'title': 'Done', 'directory': '/w', 'closed_at': 1},
{'id': 's', 'title': 'Second', 'directory': '/w'},
{'id': 'z', 'title': 'Zed', 'directory': '/w', 'starred': true},
{'id': 'a', 'title': 'apple', 'directory': '/v', 'starred': true},
];
class _Gateway extends FakeChatGateway {
_Gateway(this.rows);
final List<JsonMap> rows;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_sessions') {
return Future.value({
'chat_sessions': [
for (final row in rows) {...row},
],
});
}
if (type == 'chat_shells') return Future.value({'chat_shells': []});
return super.call(type, fields);
}
}
/// The app's routing: the route names the open chat; selecting one replaces
/// the route, Back returns to the list.
Future<ValueNotifier<String?>> _pump(
WidgetTester tester,
Size size, {
List<JsonMap> rows = _rows,
}) async {
SharedPreferences.setMockInitialValues({});
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final gateway = _Gateway(rows)..history = (_) async => chatPage(const []);
addTearDown(gateway.close);
final route = ValueNotifier<String?>(null);
addTearDown(route.dispose);
await tester.pumpWidget(
MaterialApp(
home: AdaptiveShell(
destination: AppDestination.chat,
destinationBodies: {
AppDestination.chat: (_) => ValueListenableBuilder<String?>(
valueListenable: route,
builder: (context, id, _) => ChatScreen(
gateway: gateway,
initialSessionId: id,
onSessionSelected: (next) => route.value = next,
onBack: () => route.value = null,
),
),
},
),
),
);
await tester.pumpAndSettle();
return route;
}
Finder _thread(String id) => find.byKey(ValueKey('chat-thread-$id'));
final _next = find.byKey(const Key('chat-next'));
bool _rowSelected(WidgetTester tester, String id) =>
tester.widget<AppListRow>(find.byKey(Key('chat-row-$id'))).selected;
Future<void> _pressNext(WidgetTester tester) async {
await tester.tap(_next);
await tester.pumpAndSettle();
}
void main() {
testWidgets('wide: Next chat walks the list order, wraps, syncs the row', (
tester,
) async {
final route = await _pump(tester, const Size(1280, 800));
await tester.tap(find.byKey(const Key('chat-row-a')));
await tester.pumpAndSettle();
expect(_thread('a'), findsOneWidget);
expect(
tester
.widget<IconButton>(
find.descendant(of: _next, matching: find.byType(IconButton)),
)
.tooltip,
'Next chat',
);
final visited = <String>[];
for (var i = 0; i < 5; i++) {
await _pressNext(tester);
visited.add(route.value!);
expect(_thread(route.value!), findsOneWidget);
expect(_rowSelected(tester, route.value!), isTrue);
}
// Closed is folded, so Done is skipped; after the last row comes the
// first again.
expect(visited, ['z', 'r', 's', 'd', 'a']);
expect(_rowSelected(tester, 'd'), isFalse);
final navigator = tester.state<NavigatorState>(
find.byType(Navigator).first,
);
expect(navigator.canPop(), isFalse);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('phone: Next chat replaces the open page, never stacks one', (
tester,
) async {
final route = await _pump(tester, const Size(390, 844));
await tester.tap(find.text('Second'));
await tester.pumpAndSettle();
expect(_thread('s'), findsOneWidget);
await _pressNext(tester);
expect(route.value, 'd');
expect(_thread('d'), findsOneWidget);
expect(_thread('s'), findsNothing);
await _pressNext(tester);
expect(route.value, 'a', reason: 'wraps past the last row');
// One Back leaves the chat: no earlier chat page waits beneath it.
await tester.pageBack();
await tester.pumpAndSettle();
expect(route.value, isNull);
expect(_thread('a'), findsNothing);
final navigator = tester.state<NavigatorState>(
find.byType(Navigator).first,
);
expect(navigator.canPop(), isFalse);
expect(_rowSelected(tester, 'a'), isFalse);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('only one chat to show hides Next chat', (tester) async {
await _pump(
tester,
const Size(390, 844),
rows: const [
{'id': 'r', 'title': 'Recent', 'directory': '/w'},
{'id': 'c', 'title': 'Done', 'directory': '/w', 'closed_at': 1},
],
);
await tester.tap(find.text('Recent'));
await tester.pumpAndSettle();
expect(_thread('r'), findsOneWidget);
expect(_next, findsNothing);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('unfolding Closed offers Next chat and includes its rows', (
tester,
) async {
final route = await _pump(
tester,
const Size(1280, 800),
rows: const [
{'id': 'r', 'title': 'Recent', 'directory': '/w'},
{'id': 'c', 'title': 'Done', 'directory': '/w', 'closed_at': 1},
],
);
await tester.tap(find.text('Recent'));
await tester.pumpAndSettle();
expect(_next, findsNothing);
await tester.tap(find.text('Closed'));
await tester.pumpAndSettle();
expect(_next, findsOneWidget);
await _pressNext(tester);
expect(route.value, 'c');
await tester.pumpWidget(const SizedBox.shrink());
});
}
@@ -0,0 +1,66 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/domain/chat_cycle.dart';
List<String> _ids(List<JsonMap> rows) => [
for (final row in rows) '${row['id']}',
];
String? _after(List<JsonMap> rows, String id) =>
chatSessionAfter(rows, id)?['id'] as String?;
bool _closedFolded(String title) => title != 'Closed';
void main() {
// Server recency order; the list regroups it.
final sessions = <JsonMap>[
{'id': 'r', 'title': 'Recent', 'directory': '/w'},
{'id': 'd', 'title': 'Other', 'directory': '/v'},
{'id': 'c', 'title': 'Done', 'directory': '/w', 'closed_at': 1},
{'id': 's', 'title': 'Second', 'directory': '/w'},
{'id': 'z', 'title': 'Zed', 'directory': '/w', 'starred': true},
{'id': 'a', 'title': 'apple', 'directory': '/v', 'starred': true},
];
test('shown rows: Starred by name, Chats by directory, folded skipped', () {
final folded = chatSessionsShown(
sessions,
query: '',
isOpen: _closedFolded,
);
expect(_ids(folded), ['a', 'z', 'r', 's', 'd']);
final open = chatSessionsShown(sessions, query: '', isOpen: (_) => true);
expect(_ids(open), ['a', 'z', 'r', 's', 'd', 'c']);
final searched = chatSessionsShown(
sessions,
query: 'o',
isOpen: (_) => true,
);
expect(_ids(searched), ['d', 's', 'c']);
});
test('next wraps from the last row to the first', () {
final rows = chatSessionsShown(sessions, query: '', isOpen: _closedFolded);
var id = 'a';
final visited = <String>[];
for (var i = 0; i < rows.length; i++) {
id = _after(rows, id)!;
visited.add(id);
}
expect(visited, ['z', 'r', 's', 'd', 'a']);
});
test('a chat in a folded section moves to the first shown row', () {
final rows = chatSessionsShown(sessions, query: '', isOpen: _closedFolded);
expect(_after(rows, 'c'), 'a');
});
test('nothing to move to without another shown row', () {
final one = <JsonMap>[
{'id': 'only'},
];
expect(_after(one, 'only'), isNull);
expect(_after(const [], 'only'), isNull);
expect(_after(one, 'elsewhere'), 'only');
});
}