Merge branch 'worktree-agent-a717651aa412e3b7c'

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 01:00:02 +00:00
60 changed files with 4265 additions and 222 deletions
@@ -0,0 +1,184 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_find_highlight.dart';
import 'chat_thread_fixtures.dart';
JsonMap _text(String text) => {'type': 'text', 'text': text};
/// Answers scoped searches with [searchHits] for history older than the
/// loaded page.
class _FindGateway extends FakeChatGateway {
final searches = <JsonMap>[];
List<JsonMap> searchHits = const [];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (type == 'search') {
searches.add(Map<String, dynamic>.from(fields['search'] as Map));
return {
'type': 'search_results',
'search_results': {'results': searchHits},
};
}
return super.call(type, fields);
}
}
final _newest = [
for (var i = 0; i < 30; i++)
chatMessage(
'n$i',
i.isEven ? 'user' : 'assistant',
parts: [
_text(i == 2 || i == 27 ? 'Needle at $i, and needle' : 'filler $i'),
],
),
];
final _older = [
chatMessage('o1', 'user', parts: [_text('an old needle')]),
chatMessage('o2', 'assistant', parts: [_text('nothing')]),
];
Future<_FindGateway> _host(WidgetTester tester, {bool older = false}) async {
tester.view.physicalSize = const Size(1000, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final gateway = _FindGateway()
..history = (fields) async {
final query = fields['chat_query'] as Map;
if (query['cursor'] == 'c1') return chatPage(_older);
return chatPage(_newest, cursor: older ? 'c1' : '', hasMore: older);
};
addTearDown(gateway.close);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
drafts: MemoryChatDraftStore(),
newOperationId: () => 'op',
),
),
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
await tester.pump();
return gateway;
}
Future<void> _ctrlF(WidgetTester tester) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyF);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
await tester.pump();
}
String _count(WidgetTester tester) =>
tester.widget<Text>(find.byKey(const ValueKey('chat-find-count'))).data!;
void main() {
testWidgets('Ctrl+F finds in the chat, steps older with Enter and Esc '
'returns to the composer', (tester) async {
await _host(tester);
await tester.tap(find.byType(EditableText).last);
await tester.pump();
await _ctrlF(tester);
expect(find.byKey(const ValueKey('chat-find-field')), findsOneWidget);
await tester.enterText(
find.byKey(const ValueKey('chat-find-field')),
'needle',
);
await tester.pumpAndSettle();
expect(_count(tester), '1 of 4');
// Each built text with a match registers where it draws.
final scope = tester.widget<ChatFindScope>(find.byType(ChatFindScope));
expect(scope.controller, isNotNull);
expect(scope.targets.texts, isNotEmpty);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(_count(tester), '2 of 4');
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(_count(tester), '3 of 4');
// The match in message n2 is near the top of history: it is built now.
expect(
find.textContaining('Needle at 2', findRichText: true),
findsWidgets,
);
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.pumpAndSettle();
expect(_count(tester), '2 of 4');
await tester.tap(find.byKey(const ValueKey('chat-find-case')));
await tester.pumpAndSettle();
expect(_count(tester), '1 of 2');
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('chat-find-field')), findsNothing);
});
testWidgets('matches in unloaded history are counted from the server and '
'reached by loading older pages', (tester) async {
final gateway = await _host(tester, older: true);
gateway.searchHits = [
{
'chat': {'message_id': 'o1', 'part': 0, 'offset': 7},
},
];
await tester.tap(find.byType(EditableText).last);
await tester.pump();
await _ctrlF(tester);
await tester.enterText(
find.byKey(const ValueKey('chat-find-field')),
'needle',
);
await tester.pump(const Duration(milliseconds: 300));
await tester.pumpAndSettle();
expect(gateway.searches.single['scope'], 'chat:s');
expect(gateway.searches.single['cursor'], '0|c1');
expect(_count(tester), '1 of 5');
// Step to the oldest: it is only on the server, so the older page loads.
for (var i = 0; i < 4; i++) {
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
}
expect(_count(tester), '5 of 5');
expect(
gateway.calls.where(
(call) =>
call.type == 'chat_messages' &&
(call.fields['chat_query'] as Map)['cursor'] == 'c1',
),
isNotEmpty,
);
expect(
find.textContaining('an old needle', findRichText: true),
findsWidgets,
);
});
testWidgets('Ctrl+F with no focus in the chat opens nothing', (tester) async {
await _host(tester);
expect(find.byKey(const ValueKey('chat-find-field')), findsNothing);
// No focus in the chat at all: the pane's handler never sees the key.
await _ctrlF(tester);
expect(find.byKey(const ValueKey('chat-find-field')), findsNothing);
});
}
@@ -31,6 +31,8 @@ class _Switcher {
Future<_Switcher> _open(
WidgetTester tester, {
Size size = const Size(900, 800),
List<String> recent = const [],
String? currentId,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
@@ -54,6 +56,8 @@ Future<_Switcher> _open(
onPressed: () => switcher.route = showChatQuickSwitch(
context,
sessions: controller,
recent: () => recent,
currentId: currentId,
onSelected: (row) => switcher.chosen.add('${row['id']}'),
),
child: const Text('Open'),
@@ -162,8 +166,20 @@ void main() {
'build',
);
await tester.pump();
// The closed chat titled "Closed build" is not offered.
expect(_shown(tester), ['one']);
// The closed chat titled "Closed build" follows under Closed.
expect(_shown(tester), ['one', 'gone']);
expect(
find.byKey(const Key('chat-quick-switch-closed-header')),
findsOneWidget,
);
expect(
tester.getTopLeft(find.text('Closed')).dy,
lessThan(
tester
.getTopLeft(find.byKey(const Key('chat-quick-switch-row-gone')))
.dy,
),
);
await tester.enterText(
find.byKey(const Key('chat-quick-switch-field')),
'zzz',
@@ -213,6 +229,38 @@ void main() {
await tester.pump();
});
testWidgets(
'no query: most recent first, the previous chat selected; Enter goes back',
(tester) async {
final switcher = await _open(
tester,
recent: ['three', 'two'],
currentId: 'three',
);
expect(_shown(tester), ['three', 'two', 'one']);
expect(_selected(tester), 'two');
await _key(tester, LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(switcher.chosen, ['two']);
},
);
testWidgets('rows mark working, needs you and unread chats', (tester) async {
await _open(tester);
final two = find.byKey(const Key('chat-quick-switch-row-two'));
expect(
find.descendant(
of: two,
matching: find.byKey(const Key('chat-quick-switch-marker-working')),
),
findsOneWidget,
);
expect(
find.byKey(const Key('chat-quick-switch-marker-needsYou')),
findsNothing,
);
});
testWidgets('a phone shows it as a full-width bottom sheet', (tester) async {
await _open(tester, size: const Size(400, 800));
expect(find.byType(Dialog), findsNothing);
@@ -0,0 +1,44 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/data/chat_recency.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
test('touch moves a chat to the front and persists the order', () async {
final store = MemoryChatRecencyStore(['b', 'a']);
final recency = ChatRecency(store: store);
await recency.load();
expect(recency.ids, ['b', 'a']);
recency.touch('a');
recency.touch('c');
expect(recency.ids, ['c', 'a', 'b']);
await Future<void>.delayed(Duration.zero);
expect(store.ids, ['c', 'a', 'b']);
});
test('a chat touched before the read stays in front', () async {
final recency = ChatRecency(store: MemoryChatRecencyStore(['b', 'a']));
recency.touch('a');
await recency.load();
expect(recency.ids, ['a', 'b']);
});
test('keeps at most the limit', () async {
final recency = ChatRecency(limit: 2);
for (final id in ['a', 'b', 'c']) {
recency.touch(id);
}
expect(recency.ids, ['c', 'b']);
});
test('shared preferences keep the order per server', () async {
SharedPreferences.setMockInitialValues({});
const one = SharedPreferencesChatRecencyStore(serverScope: 'one');
const two = SharedPreferencesChatRecencyStore(serverScope: 'two');
await one.write(['x', 'y']);
expect(await one.read(), ['x', 'y']);
expect(await two.read(), isEmpty);
final reloaded = ChatRecency(store: one);
await reloaded.load();
expect(reloaded.ids, ['x', 'y']);
});
}
@@ -0,0 +1,78 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/domain/chat_find.dart';
void main() {
final rows = [
{
'kind': 'user',
'key': 'u1',
'message': {
'id': 'u1',
'parts': [
{'type': 'text', 'text': 'Find the Needle'},
{'type': 'text', 'text': 'and another needle'},
],
},
},
{'kind': 'activity', 'key': 'x', 'label': 'needle in activity'},
{
'kind': 'assistant',
'key': 'a1',
'messageIds': ['a1', 'a2'],
'parts': [
{'type': 'reasoning', 'text': 'needle thoughts'},
{'type': 'text', 'text': 'A **needle** here, needle there'},
{'type': 'text', 'text': ' '},
],
},
];
test('units are user turns and answer text parts as rendered', () {
final units = chatFindUnits(rows, ['k0', 'k1', 'k2']);
expect(units.map((u) => u.text), [
'Find the Needle\nand another needle',
'A **needle** here, needle there',
]);
expect(units.map((u) => u.rowKey), ['k0', 'k2']);
expect(units.first.messageIds, ['u1']);
expect(units.last.messageIds, ['a1', 'a2']);
});
test('matches fold case unless asked, in display order', () {
final units = chatFindUnits(rows, ['k0', 'k1', 'k2']);
final folded = chatFindMatches(units, 'needle', caseSensitive: false);
expect(folded.length, 4);
expect(folded.first.start, 'Find the '.length);
expect(folded.map((m) => m.ordinal), [0, 1, 0, 1]);
final exact = chatFindMatches(units, 'Needle', caseSensitive: true);
expect(exact.length, 1);
});
test('regex characters in the query are literal', () {
expect(chatFindRanges('a.b axb (c)', '.', caseSensitive: false), [
(start: 1, end: 2),
]);
expect(chatFindRanges('a.b axb (c)', '(c)', caseSensitive: false), [
(start: 8, end: 11),
]);
});
test('server hits parse from a scoped reply', () {
final hits = ChatFindRemoteHit.fromReply({
'search_results': {
'results': [
{
'chat': {'message_id': 'm9', 'part': 1, 'offset': 4},
},
{
'chat': {'message_id': 'm8'},
},
],
},
});
expect(hits.map((h) => (h.messageId, h.part, h.offset)), [
('m9', 1, 4),
('m8', 0, 0),
]);
});
}
@@ -0,0 +1,113 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/domain/chat_attention_cycle.dart';
import 'package:pipelines_app/features/chat/domain/chat_quick_switch_order.dart';
List<String> ids(List<JsonMap> rows) => [
for (final row in rows) '${row['id']}',
];
void main() {
final rows = <JsonMap>[
{'id': 'a', 'title': 'A'},
{'id': 'b', 'title': 'B'},
{'id': 'c', 'title': 'C'},
{'id': 'd', 'title': 'D'},
];
test('recently used chats lead, the rest keep their order', () {
expect(ids(chatQuickSwitchByRecency(rows, ['c', 'x', 'a'])), [
'c',
'a',
'b',
'd',
]);
expect(ids(chatQuickSwitchByRecency(rows, const [])), ['a', 'b', 'c', 'd']);
});
test('no query selects the chat used before the current one', () {
final shown = chatQuickSwitchByRecency(rows, ['c', 'a']);
expect(
chatQuickSwitchInitialSelection(shown, currentId: 'c', emptyQuery: true),
1,
);
// Not showing a chat: the most recent one.
expect(
chatQuickSwitchInitialSelection(shown, currentId: null, emptyQuery: true),
0,
);
// A query selects the best match, even the current chat.
expect(
chatQuickSwitchInitialSelection(shown, currentId: 'c', emptyQuery: false),
0,
);
expect(
chatQuickSwitchInitialSelection(
[rows.first],
currentId: 'a',
emptyQuery: true,
),
0,
);
});
test('closed chats, most recently closed first', () {
expect(
ids(
chatQuickSwitchClosed([
...rows,
{'id': 'old', 'closed_at': 1},
{'id': 'new', 'closed_at': 9},
{'id': 'flag', 'closed': true},
]),
),
['new', 'old', 'flag'],
);
});
test('markers: needs you, working, unread', () {
expect(
chatQuickSwitchMarkers({
'awaiting_operator': 2,
'running': true,
}, unread: true),
[
ChatQuickSwitchMarker.needsYou,
ChatQuickSwitchMarker.working,
ChatQuickSwitchMarker.unread,
],
);
expect(chatQuickSwitchMarkers({'id': 'a'}, unread: false), isEmpty);
});
group('next chat needing attention', () {
final sessions = <JsonMap>[
{'id': 'a', 'title': 'A', 'directory': '/x'},
{'id': 'b', 'title': 'B', 'directory': '/x', 'awaiting_operator': 1},
{'id': 'c', 'title': 'C', 'directory': '/x'},
{'id': 'd', 'title': 'D', 'directory': '/x'},
{'id': 'e', 'title': 'E', 'closed_at': 3, 'awaiting_operator': 1},
];
const unread = {'d'};
const failed = {'a'};
bool needs(JsonMap row) => chatNeedsAttention(
row,
unread: unread.contains(row['id']),
failed: failed.contains(row['id']),
);
test('cycles in list order, wrapping, skipping closed chats', () {
expect(chatNextNeedingAttention(sessions, 'a', needs)?['id'], 'b');
expect(chatNextNeedingAttention(sessions, 'b', needs)?['id'], 'd');
expect(chatNextNeedingAttention(sessions, 'd', needs)?['id'], 'a');
expect(chatNextNeedingAttention(sessions, 'c', needs)?['id'], 'd');
expect(chatNextNeedingAttention(sessions, null, needs)?['id'], 'a');
});
test('null when only the current chat needs the operator', () {
bool onlyB(JsonMap row) => row['id'] == 'b';
expect(chatNextNeedingAttention(sessions, 'b', onlyB), isNull);
expect(chatNextNeedingAttention(sessions, 'a', onlyB)?['id'], 'b');
});
});
}