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 : `. 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'); }); }