Merge: background-task notices grouped in the chat
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
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/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_injected_rows.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_bubble.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_task_notices_group.dart';
|
||||
|
||||
import 'chat_thread_fixtures.dart';
|
||||
|
||||
/// Consecutive background-task notices are one collapsed chip on the agent's
|
||||
/// side; it opens a sheet with one compact row per task. None of them is ever
|
||||
/// the operator's bubble.
|
||||
|
||||
JsonMap _notice(String id, String text) => {
|
||||
'id': id,
|
||||
'role': 'assistant',
|
||||
'kind': 'notice',
|
||||
'parts': [
|
||||
{'type': 'text', 'id': '$id#text#0', 'text': text},
|
||||
],
|
||||
};
|
||||
|
||||
final _messages = [
|
||||
chatMessage(
|
||||
'u1',
|
||||
'user',
|
||||
parts: [
|
||||
{'type': 'text', 'text': 'Build everything'},
|
||||
],
|
||||
),
|
||||
_notice(
|
||||
'task-notice-t1-completed',
|
||||
'Background task finished: Background command "Build the app" '
|
||||
'completed (exit code 0)',
|
||||
),
|
||||
_notice(
|
||||
'task-notice-t2-failed',
|
||||
'Background task failed: Background command "Run the tests" failed '
|
||||
'(exit code 1)',
|
||||
),
|
||||
_notice(
|
||||
'task-notice-t3-completed',
|
||||
'Background task finished: Agent "Review the diff" finished',
|
||||
),
|
||||
];
|
||||
|
||||
Future<void> _pump(WidgetTester tester) async {
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
Future<FakeChatGateway> _host(
|
||||
WidgetTester tester,
|
||||
List<JsonMap> messages, {
|
||||
bool cupertino = false,
|
||||
}) async {
|
||||
tester.view.physicalSize = const Size(1000, 1600);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final gateway = FakeChatGateway()..history = (_) async => chatPage(messages);
|
||||
addTearDown(gateway.close);
|
||||
final thread = ChatThreadView(
|
||||
gateway: gateway,
|
||||
sessionId: 's',
|
||||
onBack: () {},
|
||||
drafts: MemoryChatDraftStore(),
|
||||
newOperationId: () => 'op',
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(home: CupertinoPageScaffold(child: thread))
|
||||
: MaterialApp(home: Scaffold(body: thread)),
|
||||
);
|
||||
await _pump(tester);
|
||||
return gateway;
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
testWidgets('consecutive task notices are one chip that opens to each '
|
||||
'task ($cupertino)', (tester) async {
|
||||
await _host(tester, _messages, cupertino: cupertino);
|
||||
|
||||
expect(find.byType(ChatTaskNoticesGroup), findsOneWidget);
|
||||
expect(find.byType(ChatSystemNoticeLine), findsNothing);
|
||||
final chip = find.byKey(const Key('chat-task-notices-chip'));
|
||||
expect(chip, findsOneWidget);
|
||||
expect(find.text('3 background tasks · 1 failed'), findsOneWidget);
|
||||
expect(
|
||||
find.ancestor(of: chip, matching: find.byType(ChatUserBubble)),
|
||||
findsNothing,
|
||||
);
|
||||
// Only the operator's prompt is a bubble, on the trailing side; the
|
||||
// group starts on the agent's side.
|
||||
expect(find.byType(ChatUserBubble), findsOneWidget);
|
||||
final width = tester.getSize(find.byType(ChatThreadView)).width;
|
||||
expect(tester.getTopLeft(chip).dx, lessThan(width * 0.2));
|
||||
// Collapsed: no summary is laid out yet.
|
||||
expect(find.textContaining('Build the app'), findsNothing);
|
||||
|
||||
await tester.tap(chip);
|
||||
await tester.pumpAndSettle();
|
||||
final sheet = find.byKey(const Key('chat-task-notices-sheet'));
|
||||
expect(sheet, findsOneWidget);
|
||||
for (final summary in [
|
||||
'Background command "Build the app" completed (exit code 0)',
|
||||
'Background command "Run the tests" failed (exit code 1)',
|
||||
'Agent "Review the diff" finished',
|
||||
]) {
|
||||
expect(
|
||||
find.descendant(of: sheet, matching: find.text(summary)),
|
||||
findsOneWidget,
|
||||
);
|
||||
}
|
||||
expect(
|
||||
find.descendant(of: sheet, matching: find.text('Finished')),
|
||||
findsNWidgets(2),
|
||||
);
|
||||
expect(
|
||||
find.descendant(of: sheet, matching: find.text('Failed')),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('a lone notice stays one line and a live second notice joins '
|
||||
'it into the group', (tester) async {
|
||||
final gateway = await _host(tester, _messages.take(2).toList());
|
||||
expect(find.byType(ChatSystemNoticeLine), findsOneWidget);
|
||||
expect(find.byType(ChatTaskNoticesGroup), findsNothing);
|
||||
|
||||
gateway.emitEvent({
|
||||
'session_id': 's',
|
||||
'message_id': 'task-notice-t2-failed',
|
||||
'message_kind': 'notice',
|
||||
'kind': 'text',
|
||||
'part': {'id': 'task-notice-t2-failed#text#0'},
|
||||
'delta':
|
||||
'Background task failed: Background command "Run the tests" '
|
||||
'failed (exit code 1)',
|
||||
});
|
||||
await _pump(tester);
|
||||
expect(find.byType(ChatSystemNoticeLine), findsNothing);
|
||||
expect(find.text('2 background tasks · 1 failed'), findsOneWidget);
|
||||
expect(find.byType(ChatUserBubble), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_task_notices.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
|
||||
|
||||
/// The server records each Claude Code task notification as a `notice`
|
||||
/// message `Background task <outcome>: <summary>`.
|
||||
JsonMap _notice(String id, String text, {String kind = 'notice'}) => {
|
||||
'id': id,
|
||||
'role': 'assistant',
|
||||
'kind': kind,
|
||||
'parts': [
|
||||
{'type': 'text', 'id': '$id#text#0', 'text': text},
|
||||
],
|
||||
};
|
||||
|
||||
JsonMap _turn(String id, String role) => {
|
||||
'id': id,
|
||||
'role': role,
|
||||
'parts': [
|
||||
{'type': 'text', 'id': '$id#text#0', 'text': 'text of $id'},
|
||||
],
|
||||
};
|
||||
|
||||
void main() {
|
||||
test('a task notice reads its outcome and summary', () {
|
||||
final done = chatTaskNotice(
|
||||
_notice(
|
||||
'n1',
|
||||
'Background task finished: Background command "Build" completed '
|
||||
'(exit code 0)',
|
||||
),
|
||||
)!;
|
||||
expect(done.id, 'n1');
|
||||
expect(done.outcome, ChatTaskOutcome.finished);
|
||||
expect(done.summary, 'Background command "Build" completed (exit code 0)');
|
||||
expect(
|
||||
chatTaskNotice(_notice('n2', 'Background task failed: x'))!.outcome,
|
||||
ChatTaskOutcome.failed,
|
||||
);
|
||||
final stopped = chatTaskNotice(_notice('n3', 'Background task stopped'))!;
|
||||
expect(stopped.outcome, ChatTaskOutcome.stopped);
|
||||
expect(stopped.summary, isEmpty);
|
||||
// Other notices, and the same words in an ordinary message, are not tasks.
|
||||
expect(chatTaskNotice(_notice('n4', 'Context compacted')), isNull);
|
||||
expect(
|
||||
chatTaskNotice(_notice('n5', 'Background task finished: x', kind: '')),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('consecutive task notices fold into one group in order', () {
|
||||
final rows = transcriptRows([
|
||||
_turn('u1', 'user'),
|
||||
_turn('a1', 'assistant'),
|
||||
_notice('n1', 'Background task finished: one'),
|
||||
_notice('n2', 'Background task failed: two'),
|
||||
_notice('n3', 'Background task stopped: three'),
|
||||
_turn('a2', 'assistant'),
|
||||
_notice('n4', 'Background task finished: lone'),
|
||||
_turn('u2', 'user'),
|
||||
_notice('n5', 'Background task finished: five'),
|
||||
_notice('n6', 'Background task finished: six'),
|
||||
]);
|
||||
expect(rows.map((row) => row['kind']), [
|
||||
'user',
|
||||
'assistant',
|
||||
'task_notices',
|
||||
'assistant',
|
||||
'system_notice',
|
||||
'user',
|
||||
'task_notices',
|
||||
]);
|
||||
final group = rows[2];
|
||||
expect(group['key'], 'n1');
|
||||
expect(group['messageIds'], ['n1', 'n2', 'n3']);
|
||||
final notices = chatTaskNoticesOf(group);
|
||||
expect(notices.map((n) => n.summary), ['one', 'two', 'three']);
|
||||
expect(notices.map((n) => n.outcome), [
|
||||
ChatTaskOutcome.finished,
|
||||
ChatTaskOutcome.failed,
|
||||
ChatTaskOutcome.stopped,
|
||||
]);
|
||||
expect(rows[4]['key'], 'n4');
|
||||
expect(chatTaskNoticesOf(rows[6]).map((n) => n.id), ['n5', 'n6']);
|
||||
});
|
||||
|
||||
test('a non-task notice breaks a run and stays its own line', () {
|
||||
final rows = groupTaskNotices([
|
||||
{
|
||||
'kind': 'system_notice',
|
||||
'key': 'n1',
|
||||
'message': _notice('n1', 'Background task finished: one'),
|
||||
},
|
||||
{
|
||||
'kind': 'system_notice',
|
||||
'key': 'x',
|
||||
'message': _notice('x', 'Something else'),
|
||||
},
|
||||
{
|
||||
'kind': 'system_notice',
|
||||
'key': 'n2',
|
||||
'message': _notice('n2', 'Background task finished: two'),
|
||||
},
|
||||
]);
|
||||
expect(rows.map((row) => row['kind']), [
|
||||
'system_notice',
|
||||
'system_notice',
|
||||
'system_notice',
|
||||
]);
|
||||
});
|
||||
|
||||
test('the label counts tasks', () {
|
||||
expect(chatTaskNoticesLabel(1), '1 background task');
|
||||
expect(chatTaskNoticesLabel(3), '3 background tasks');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user