Show session automation counts in chat composer

This commit is contained in:
2026-11-08 16:40:53 +00:00
parent 3fbfb85ace
commit b7f439a94f
13 changed files with 716 additions and 89 deletions
@@ -0,0 +1,134 @@
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_background_tasks_controller.dart';
JsonMap _report(String id, int count) => {
'chat_automations': {
'session_id': id,
'status': 'supported',
'active_count': count,
},
};
class _Gateway implements ApiGateway {
final events = StreamController<JsonMap>.broadcast();
final reads = <String>[];
late Future<JsonMap> Function(String id) automation;
@override
Stream<JsonMap> get frames => events.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_shells') {
return Future.value({'chat_shells': <JsonMap>[]});
}
if (type == 'chat_automations') {
final id = (fields['chat_query'] as Map)['session_id'] as String;
reads.add(id);
return automation(id);
}
throw UnsupportedError(type);
}
void ended(String id) => events.add({
'type': 'chat_event',
'chat_event': {
'session_id': id,
'kind': 'step',
'step': {'status': 'ended', 'intermediate': false},
},
});
}
void main() {
testWidgets('replacement loads while old server reply is still pending', (
tester,
) async {
final oldReply = Completer<JsonMap>();
final nextReply = Completer<JsonMap>();
final old = _Gateway()..automation = (_) => oldReply.future;
final next = _Gateway()..automation = (_) => nextReply.future;
final controller = ChatBackgroundTasksController(old, 'same-id');
final notifications = <int?>[];
controller.addListener(
() => notifications.add(controller.automationStatus.count),
);
addTearDown(() async {
controller.dispose();
await old.events.close();
await next.events.close();
});
await tester.pump();
controller.replace(next, 'same-id', notify: false);
await tester.pump();
expect(next.reads, ['same-id']);
expect(controller.loading, isFalse);
notifications.clear();
// The shell list already settled. This slower response must still notify.
nextReply.complete(_report('same-id', 2));
await tester.pump();
expect(controller.automationStatus.count, 2);
expect(notifications, contains(2));
oldReply.complete(_report('same-id', 99));
await tester.pump();
expect(controller.automationStatus.count, 2);
expect(notifications, isNot(contains(99)));
});
testWidgets('a real completion during a read receives one trailing refresh', (
tester,
) async {
final first = Completer<JsonMap>();
final gateway = _Gateway();
gateway.automation = (id) =>
gateway.reads.length == 1 ? first.future : Future.value(_report(id, 1));
final controller = ChatBackgroundTasksController(gateway, 'chat');
addTearDown(() async {
controller.dispose();
await gateway.events.close();
});
await tester.pump();
gateway.ended('chat');
await tester.pump();
await tester.pump(const Duration(milliseconds: 251));
expect(gateway.reads.length, 1);
first.complete(_report('chat', 0));
await tester.pump();
await tester.pump(const Duration(milliseconds: 251));
expect(gateway.reads.length, 2);
expect(controller.automationStatus.count, 1);
gateway.ended('another-chat');
gateway.events.add({
'type': 'chat_event',
'chat_event': {'session_id': 'chat', 'kind': 'text', 'text': 'token'},
});
await tester.pump();
await tester.pump(const Duration(milliseconds: 251));
expect(gateway.reads.length, 2);
});
testWidgets('losing metadata notifies and clears the old positive count', (
tester,
) async {
final gateway = _Gateway()..automation = (id) async => _report(id, 3);
final controller = ChatBackgroundTasksController(gateway, 'chat');
addTearDown(() async {
controller.dispose();
await gateway.events.close();
});
await tester.pump();
expect(controller.automationStatus.count, 3);
var notifications = 0;
controller.addListener(() => notifications++);
gateway.automation = (_) => throw UnsupportedError('older server');
unawaited(controller.refreshAutomations());
await tester.pump();
expect(controller.automationStatus.count, isNull);
expect(notifications, greaterThan(0));
expect(controller.error, isNull);
});
}
@@ -15,6 +15,7 @@ class _TasksGateway implements ApiGateway {
final calls = <({String type, JsonMap fields})>[];
final _frames = StreamController<JsonMap>.broadcast();
Future<JsonMap> Function()? shells;
Future<JsonMap> Function()? automations;
Future<JsonMap> Function(JsonMap fields)? output;
@override
@@ -28,6 +29,12 @@ class _TasksGateway implements ApiGateway {
return shells == null
? <String, dynamic>{'chat_shells': const []}
: shells!();
case 'chat_automations':
return automations == null
? <String, dynamic>{
'chat_automations': {'status': 'unavailable'},
}
: automations!();
case 'chat_shell_output':
return output == null
? <String, dynamic>{'chat_shell_output': const {}}
@@ -156,6 +163,39 @@ void main() {
]);
});
testWidgets(
'shows an automation-only count and its native report in inspector',
(tester) async {
final gateway = _TasksGateway()
..automations = () async => {
'chat_automations': {
'session_id': 's',
'provider': 'claude',
'status': 'supported',
'active_count': 1,
'observed_at_ms': 1700000000000,
},
};
addTearDown(gateway.close);
await tester.pumpWidget(_host(gateway));
await tester.pumpAndSettle();
expect(find.text('1 automation'), findsOneWidget);
await _openTasks(tester);
expect(find.text('1 automation'), findsNWidgets(2));
expect(find.textContaining('Last reported:'), findsOneWidget);
expect(gateway.fieldsFor('chat_automations'), [
{
'chat_query': {'session_id': 's'},
},
{
'chat_query': {'session_id': 's'},
},
]);
},
);
testWidgets('an empty snapshot shows no task control, never "0 tasks"', (
tester,
) async {
@@ -315,12 +355,7 @@ void main() {
await tester.tap(find.text('Retry'));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-bg-task-output-error')), findsNothing);
expect(
tester
.widget<SelectableText>(find.byKey(const Key('chat-bg-task-output')))
.data,
'fresh output',
);
expect(find.text('Retry'), findsNothing);
});
testWidgets('a refused snapshot is reported and Retry rereads it', (
@@ -658,4 +693,70 @@ void main() {
expect(find.byKey(const Key('chat-background-tasks-button')), findsNothing);
expect(find.byKey(const Key('chat-composer-primary')), findsOneWidget);
});
testWidgets(
'combined caption leaves a usable input on a narrow running chat',
(tester) async {
tester.view.physicalSize = const Size(360, 800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final gateway = _TasksGateway();
gateway.shells = () async =>
_snapshot([_running('sh1'), _running('sh2')]);
gateway.automations = () async => {
'chat_automations': {
'session_id': 's',
'status': 'supported',
'active_count': 12,
},
};
addTearDown(gateway.close);
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
builder: (context, child) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(2)),
child: child!,
),
home: Scaffold(
body: ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
),
),
),
);
await tester.pumpAndSettle();
gateway._frames.add({
'type': 'chat_event',
'chat_event': {
'session_id': 's',
'kind': 'step',
'step': {
'status': 'started',
'turn_id': 'turn',
'intermediate': false,
},
},
});
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
expect(find.byTooltip('2 tasks, 12 automations'), findsOneWidget);
final counter = find.byKey(const Key('chat-background-tasks-button'));
expect(
find.descendant(of: counter, matching: find.text('2')),
findsOneWidget,
);
expect(
find.descendant(of: counter, matching: find.text('12')),
findsOneWidget,
);
expect(tester.getSize(find.byType(EditableText)).width, greaterThan(64));
expect(tester.takeException(), isNull);
},
);
}
@@ -0,0 +1,42 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/domain/chat_automations.dart';
void main() {
test(
'only supported integer counts from the selected chat are displayed',
() {
ChatAutomationStatus read(Map<String, dynamic> fields) =>
ChatAutomationStatus.fromReply({
'chat_automations': {'session_id': 'chat', ...fields},
}, expectedSessionId: 'chat');
expect(read({'status': 'supported', 'active_count': 0}).count, 0);
expect(read({'status': 'supported', 'active_count': 2}).count, 2);
for (final fields in <Map<String, dynamic>>[
{'status': 'unavailable', 'active_count': 2},
{'status': 'unknown', 'active_count': 2},
{'status': 'supported', 'active_count': 1.5},
{'status': 'supported', 'active_count': -1},
{'status': 'supported'},
{'session_id': 'foreign', 'status': 'supported', 'active_count': 2},
]) {
expect(read(fields).count, isNull, reason: '$fields');
}
},
);
test('invalid report times remain unknown without losing a valid count', () {
for (final stamp in [null, 'yesterday', -1, 1.5, 9223372036854775807]) {
final result = ChatAutomationStatus.fromReply({
'chat_automations': {
'session_id': 'chat',
'status': 'supported',
'active_count': 1,
'observed_at_ms': stamp,
},
}, expectedSessionId: 'chat');
expect(result.count, 1);
expect(result.observedAt, isNull);
}
});
}