Move live background tasks into a composer badge and modal

This commit is contained in:
2026-10-25 21:53:19 +00:00
parent 96178570e5
commit a485ccecb1
15 changed files with 907 additions and 510 deletions
@@ -0,0 +1,100 @@
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';
import 'package:pipelines_app/features/chat/domain/chat_shells.dart';
import 'chat_background_tasks_fixtures.dart';
void main() {
test(
'slow snapshot does not resurrect an exited task or drop a live arrival',
() async {
final gateway = TasksGateway();
final reply = Completer<JsonMap>();
gateway.shells = (_) => reply.future;
final controller = ChatBackgroundTasksController(gateway, 's');
addTearDown(controller.dispose);
addTearDown(gateway.close);
gateway.shell('gone', 'exited');
gateway.shell('new', 'running');
gateway.shell('unrelated', 'running', session: 'other');
await pumpEventQueue();
reply.complete({
'chat_shells': [task('gone')],
});
await pumpEventQueue();
expect(controller.running.map(shellIdOf), ['new']);
expect(controller.loading, isFalse);
},
);
test('replacing sessions ignores a late previous snapshot', () async {
final gateway = TasksGateway();
final old = Completer<JsonMap>();
gateway.shells = (fields) => fields['chat_query']['session_id'] == 's'
? old.future
: Future.value({
'chat_shells': [task('current')],
});
final controller = ChatBackgroundTasksController(gateway, 's');
addTearDown(controller.dispose);
addTearDown(gateway.close);
controller.replace(gateway, 'new-session');
await pumpEventQueue();
old.complete({
'chat_shells': [task('old')],
});
await pumpEventQueue();
expect(controller.running.map(shellIdOf), ['current']);
});
test('an exit invalidates an in-flight output read for that task', () async {
final gateway = TasksGateway()
..shells = (_) async => {
'chat_shells': [task('a')],
};
final page = Completer<JsonMap>();
gateway.output = (_) => page.future;
final controller = ChatBackgroundTasksController(gateway, 's');
addTearDown(controller.dispose);
addTearDown(gateway.close);
await pumpEventQueue();
controller.select('a');
expect(controller.output?.state, ChatShellOutputState.loading);
gateway.shell('a', 'exited');
await pumpEventQueue();
page.complete({
'chat_shell_output': {'shell': task('a'), 'output': 'late'},
});
await pumpEventQueue();
expect(controller.running, isEmpty);
expect(controller.output, isNull);
expect(controller.selectedShell, isNull);
});
test(
'disposal ignores late reads and never attaches another subscription',
() async {
final gateway = TasksGateway();
final reply = Completer<JsonMap>();
gateway.shells = (_) => reply.future;
final controller = ChatBackgroundTasksController(gateway, 's');
var changes = 0;
controller.addListener(() => changes++);
controller.dispose();
controller.replace(gateway, 'other');
reply.complete({
'chat_shells': [task('late')],
});
await pumpEventQueue();
expect(changes, 0);
expect(
gateway.calls.where((call) => call.type == 'chat_shells'),
hasLength(1),
);
gateway.close();
},
);
}
@@ -0,0 +1,36 @@
import 'package:pipelines_app/core/api_gateway.dart';
import 'chat_thread_fixtures.dart';
class TasksGateway extends FakeChatGateway {
TasksGateway() {
history = (_) async => chatPage([]);
}
Future<JsonMap> Function(JsonMap)? shells;
Future<JsonMap> Function(JsonMap)? output;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_shells' || type == 'chat_shell_output') {
calls.add((type: type, fields: fields));
if (type == 'chat_shells') {
return shells?.call(fields) ?? Future.value({'chat_shells': []});
}
return output?.call(fields) ?? Future.value({'chat_shell_output': {}});
}
return super.call(type, fields);
}
void shell(String id, String status, {String session = 's'}) => emitEvent({
'session_id': session,
'kind': 'shell',
'shell': {'id': id, 'status': status, 'command': 'command $id'},
});
}
Map<String, dynamic> task(String id) => {
'id': id,
'status': 'running',
'command': 'command $id',
};
@@ -0,0 +1,143 @@
import 'dart:async';
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_background_tasks.dart';
import 'chat_background_tasks_fixtures.dart';
Widget host(
TasksGateway gateway, {
bool cupertino = false,
String session = 's',
}) {
final button = ChatBackgroundTasks(gateway: gateway, sessionId: session);
return cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: button))
: MaterialApp(home: Scaffold(body: button));
}
Future<void> open(WidgetTester tester) async {
await tester.tap(find.byKey(const Key('chat-background-tasks-button')));
await tester.pumpAndSettle();
}
void main() {
for (final cupertino in [false, true]) {
testWidgets(
'count updates live, no eager output, Back/Done only close modal ($cupertino)',
(tester) async {
final gateway = TasksGateway()
..shells = (_) async => {
'chat_shells': [task('a')],
};
addTearDown(gateway.close);
await tester.pumpWidget(host(gateway, cupertino: cupertino));
await tester.pumpAndSettle();
expect(find.text('1 task'), findsOneWidget);
expect(find.text('command a'), findsNothing);
expect(
gateway.calls.where((call) => call.type == 'chat_shell_output'),
isEmpty,
);
await open(tester);
expect(find.text('command a'), findsOneWidget);
gateway.shell('b', 'running');
await tester.pumpAndSettle();
expect(find.text('2 running'), findsOneWidget);
expect(find.text('command b'), findsOneWidget);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(
find.byKey(const Key('chat-background-tasks-modal')),
findsNothing,
);
expect(find.text('2 tasks'), findsOneWidget);
await open(tester);
await tester.tap(find.text('Done'));
await tester.pumpAndSettle();
expect(find.text('2 tasks'), findsOneWidget);
expect(
gateway.calls.where((call) => call.type == 'chat_shells'),
hasLength(1),
);
},
);
testWidgets(
'session switch closes its own modal and drops late output ($cupertino)',
(tester) async {
final gateway = TasksGateway()
..shells = (_) async => {
'chat_shells': [task('a')],
};
final page = Completer<JsonMap>();
gateway.output = (_) => page.future;
addTearDown(gateway.close);
await tester.pumpWidget(host(gateway, cupertino: cupertino));
await tester.pumpAndSettle();
await open(tester);
await tester.tap(find.byKey(const ValueKey('chat-bg-task-a')));
await tester.pump();
gateway.shells = (_) async => {'chat_shells': []};
await tester.pumpWidget(
host(gateway, cupertino: cupertino, session: 'other'),
);
await tester.pumpAndSettle();
page.complete({
'chat_shell_output': {'output': 'old session output'},
});
await tester.pumpAndSettle();
expect(
find.byKey(const Key('chat-background-tasks-modal')),
findsNothing,
);
expect(find.text('0 tasks'), findsOneWidget);
expect(find.text('old session output'), findsNothing);
expect(tester.takeException(), isNull);
},
);
}
testWidgets(
'owner disposal removes only its modal, not an unrelated top route',
(tester) async {
final gateway = TasksGateway();
addTearDown(gateway.close);
var visible = true;
late StateSetter rebuild;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: StatefulBuilder(
builder: (context, setState) {
rebuild = setState;
return visible
? ChatBackgroundTasks(gateway: gateway, sessionId: 's')
: const SizedBox.shrink();
},
),
),
),
);
await tester.pumpAndSettle();
await open(tester);
final navigator = tester.state<NavigatorState>(find.byType(Navigator));
showDialog<void>(
context: navigator.overlay!.context,
builder: (_) => const AlertDialog(title: Text('Unrelated dialog')),
);
await tester.pumpAndSettle();
rebuild(() => visible = false);
await tester.pumpAndSettle();
expect(find.text('Unrelated dialog'), findsOneWidget);
expect(
find.byKey(const Key('chat-background-tasks-modal')),
findsNothing,
);
expect(tester.takeException(), isNull);
},
);
}
@@ -110,7 +110,18 @@ Widget _thread(_TasksGateway gateway) => MaterialApp(
);
Future<void> _tapTask(WidgetTester tester, String id) async {
await tester.tap(find.byKey(ValueKey<String>('chat-bg-task-$id')));
if (find.byKey(const Key('chat-background-tasks-modal')).evaluate().isEmpty) {
await _openTasks(tester);
}
final row = find.byKey(ValueKey<String>('chat-bg-task-$id'));
await tester.ensureVisible(row);
await tester.pumpAndSettle();
await tester.tap(row);
await tester.pumpAndSettle();
}
Future<void> _openTasks(WidgetTester tester) async {
await tester.tap(find.byKey(const Key('chat-background-tasks-button')));
await tester.pumpAndSettle();
}
@@ -129,6 +140,9 @@ void main() {
await tester.pumpWidget(_host(gateway));
await tester.pumpAndSettle();
expect(find.text('1 task'), findsOneWidget);
expect(find.text('go test ./...'), findsNothing);
await _openTasks(tester);
expect(find.text('Background tasks'), findsOneWidget);
expect(find.text('1 running'), findsOneWidget);
expect(find.text('Running'), findsOneWidget);
@@ -142,16 +156,20 @@ void main() {
]);
});
testWidgets('an honest empty snapshot draws no task surface', (tester) async {
testWidgets('an empty snapshot shows zero tasks and an honest empty modal', (
tester,
) async {
final gateway = _TasksGateway();
addTearDown(gateway.close);
await tester.pumpWidget(_host(gateway));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-background-tasks')), findsNothing);
expect(find.text('0 tasks'), findsOneWidget);
expect(find.text('Background tasks'), findsNothing);
expect(gateway.fieldsFor('chat_shells'), hasLength(1));
await _openTasks(tester);
expect(find.text('No background tasks running.'), findsOneWidget);
});
testWidgets('pushed shell frames add and remove this session tasks', (
@@ -166,20 +184,22 @@ void main() {
gateway.emitShell('sh1', 'running', command: 'tail -f server.log');
await tester.pumpAndSettle();
expect(find.text('1 task'), findsOneWidget);
await _openTasks(tester);
expect(find.text('Background tasks'), findsOneWidget);
expect(find.text('1 running'), findsOneWidget);
expect(find.text('tail -f server.log'), findsOneWidget);
gateway.emitShell('sh1', 'exited');
await tester.pumpAndSettle();
expect(find.text('Background tasks'), findsNothing);
expect(find.text('No background tasks running.'), findsOneWidget);
gateway.emitShell('other', 'running', session: 'other-session');
await tester.pumpAndSettle();
expect(find.text('Background tasks'), findsNothing);
expect(find.text('No background tasks running.'), findsOneWidget);
});
testWidgets('more than three running tasks scroll inside the bar', (
testWidgets('more than three tasks are contained in a scrollable modal', (
tester,
) async {
final gateway = _TasksGateway()
@@ -190,13 +210,11 @@ void main() {
await tester.pumpWidget(_host(gateway));
await tester.pumpAndSettle();
await _openTasks(tester);
expect(find.text('4 running'), findsOneWidget);
expect(find.byKey(const Key('chat-bg-task-list')), findsOneWidget);
expect(
tester.getRect(find.byKey(const Key('chat-bg-task-list'))).height,
lessThanOrEqualTo(kMinInteractiveDimension * backgroundTaskVisibleRows),
);
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.byType(SingleChildScrollView), findsWidgets);
for (var index = 0; index < 4; index++) {
expect(
find.byKey(ValueKey<String>('chat-bg-task-sh$index')),
@@ -315,6 +333,8 @@ void main() {
await tester.pumpWidget(_host(gateway));
await tester.pumpAndSettle();
expect(find.text('0 tasks'), findsNothing);
await _openTasks(tester);
expect(
find.textContaining('Background tasks unavailable. server down'),
@@ -332,7 +352,9 @@ void main() {
expect(gateway.fieldsFor('chat_shells'), hasLength(2));
});
testWidgets('an exit closes the selected pane and the bar', (tester) async {
testWidgets('an exit clears selected output while the modal stays open', (
tester,
) async {
final gateway = _TasksGateway();
gateway.shells = () async => _snapshot([_running('sh1')]);
gateway.output = (_) async => {
@@ -352,7 +374,7 @@ void main() {
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-bg-task-detail')), findsNothing);
expect(find.text('Background tasks'), findsNothing);
expect(find.text('No background tasks running.'), findsOneWidget);
});
testWidgets('a replaced socket drops held tasks and re-learns the snapshot', (
@@ -364,13 +386,14 @@ void main() {
await tester.pumpWidget(_host(gateway));
await tester.pumpAndSettle();
await _openTasks(tester);
expect(find.text('1 running'), findsOneWidget);
gateway.shells = () async => _snapshot(const []);
await tester.pumpWidget(_host(gateway, syncToken: 1));
await tester.pumpAndSettle();
expect(find.text('Background tasks'), findsNothing);
expect(find.text('No background tasks running.'), findsOneWidget);
expect(gateway.fieldsFor('chat_shells'), hasLength(2));
});
@@ -389,6 +412,7 @@ void main() {
await tester.pumpWidget(_host(gateway, cupertino: true));
await tester.pumpAndSettle();
await _openTasks(tester);
expect(find.byType(CupertinoButton), findsWidgets);
expect(find.text('1 running'), findsOneWidget);
@@ -425,20 +449,20 @@ void main() {
await tester.pumpWidget(_thread(gateway));
await tester.pumpAndSettle();
final before = tester.getRect(
find.byKey(const ValueKey('chat-composer-s')),
);
await _tapTask(tester, 'sh1');
// A real shell tail is bounded: the transcript and composer stay in the
// viewport instead of being pushed out by the whole page.
// Output stays bounded and scrollable inside the modal.
expect(tester.takeException(), isNull);
final outputFinder = find.byKey(const Key('chat-bg-task-output'));
final output = tester.getRect(outputFinder);
expect(output.height, lessThanOrEqualTo(210));
final detail = tester.getRect(find.byKey(const Key('chat-bg-task-detail')));
final composer = tester.getRect(
find.byKey(const ValueKey<String>('chat-composer-s')),
final modal = tester.getRect(
find.byKey(const Key('chat-background-tasks-modal')),
);
expect(detail.bottom, lessThanOrEqualTo(composer.top));
expect(composer.bottom, lessThanOrEqualTo(700));
expect(output.height, lessThanOrEqualTo(modal.height));
expect(modal.bottom, lessThanOrEqualTo(700));
// The page scrolls through the host's own text control to its last line.
expect(
@@ -452,6 +476,12 @@ void main() {
await tester.drag(outputFinder, const Offset(0, -300));
await tester.pumpAndSettle();
expect(scrollable.position.pixels, greaterThan(0));
await tester.tap(find.text('Done'));
await tester.pumpAndSettle();
expect(
tester.getRect(find.byKey(const ValueKey('chat-composer-s'))),
before,
);
});
testWidgets('three tasks and an open page fit a 320px phone', (tester) async {
@@ -473,28 +503,23 @@ void main() {
await tester.pumpWidget(_thread(gateway));
await tester.pumpAndSettle();
final before = tester.getRect(
find.byKey(const ValueKey('chat-composer-s')),
);
await _tapTask(tester, 'sh1');
// Three rows plus the whole detail can no longer take the viewport: the bar
// keeps its share, scrolls its own body, and the composer stays reachable.
// Native dialog scrolling contains the task list and its selected output.
expect(tester.takeException(), isNull);
final bar = tester.getRect(find.byKey(const Key('chat-background-tasks')));
final composer = tester.getRect(
find.byKey(const ValueKey<String>('chat-composer-s')),
);
final transcript = tester.getRect(
find.byKey(const ValueKey<String>('chat-thread-s')),
final modal = tester.getRect(
find.byKey(const Key('chat-background-tasks-modal')),
);
expect(modal.height, lessThanOrEqualTo(568));
expect(modal.width, lessThanOrEqualTo(320));
await tester.tap(find.text('Done'));
await tester.pumpAndSettle();
expect(
bar.height,
lessThanOrEqualTo(568 * backgroundTaskViewportShare + 0.5),
);
expect(bar.bottom, lessThanOrEqualTo(composer.top));
expect(composer.bottom, lessThanOrEqualTo(568));
expect(transcript.height, greaterThan(0));
expect(
find.byKey(const Key('chat-background-tasks-scroll')),
findsOneWidget,
tester.getRect(find.byKey(const ValueKey('chat-composer-s'))),
before,
);
});
@@ -523,14 +548,17 @@ void main() {
await _tapTask(tester, 'sh1');
expect(find.byKey(const Key('chat-bg-task-detail')), findsOneWidget);
// The row labels and the detail are laid out at twice the text size without
// overflowing, and the bar's bound still leaves the composer on screen.
// Task details stay in the modal at large text scale, not in the chat body.
expect(tester.takeException(), isNull);
final bar = tester.getRect(find.byKey(const Key('chat-background-tasks')));
final modal = tester.getRect(
find.byKey(const Key('chat-background-tasks-modal')),
);
expect(modal.bottom, lessThanOrEqualTo(568));
await tester.tap(find.text('Done'));
await tester.pumpAndSettle();
final composer = tester.getRect(
find.byKey(const ValueKey<String>('chat-composer-s')),
);
expect(bar.bottom, lessThanOrEqualTo(composer.top));
expect(composer.bottom, lessThanOrEqualTo(568));
});
@@ -562,10 +590,10 @@ void main() {
await _tapTask(tester, 'sh1');
expect(tester.takeException(), isNull);
expect(find.text('Details'), findsOneWidget);
expect(find.text('Task identity'), findsOneWidget);
});
testWidgets('the conversation mounts the session task surface', (
testWidgets('the conversation mounts only the live task badge', (
tester,
) async {
final gateway = _TasksGateway()
@@ -575,12 +603,18 @@ void main() {
await tester.pumpWidget(_thread(gateway));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-background-tasks')), findsOneWidget);
expect(find.text('Background tasks'), findsOneWidget);
expect(find.text('1 running'), findsOneWidget);
expect(
find.byKey(const Key('chat-background-tasks-button')),
findsOneWidget,
);
expect(find.text('1 task'), findsOneWidget);
expect(find.byKey(const Key('chat-background-tasks')), findsNothing);
expect(find.text('go test ./...'), findsNothing);
});
testWidgets('the conversation bar leads, above the task bar', (tester) async {
testWidgets('the task badge sits above Send, outside the transcript', (
tester,
) async {
final gateway = _TasksGateway();
gateway.shells = () async => _snapshot([_running('sh1')]);
addTearDown(gateway.close);
@@ -588,16 +622,19 @@ void main() {
await tester.pumpWidget(_thread(gateway));
await tester.pumpAndSettle();
final header = tester.getRect(find.byKey(const Key('chat-thread-header')));
final tasks = tester.getRect(
find.byKey(const Key('chat-background-tasks')),
find.byKey(const Key('chat-background-tasks-button')),
);
final list = tester.getRect(
find.byKey(const ValueKey<String>('chat-thread-s')),
);
expect(header.top, 0);
expect(tasks.top, greaterThanOrEqualTo(header.bottom));
// The bar sits outside the transcript's scroller, as the web's chat-bar.
expect(list.top, greaterThanOrEqualTo(tasks.bottom));
final send = tester.getRect(find.byKey(const Key('chat-composer-primary')));
final composer = tester.getRect(
find.byKey(const ValueKey('chat-composer-s')),
);
expect(tasks.bottom, lessThanOrEqualTo(send.top));
expect(send.center.dx, inInclusiveRange(tasks.left, tasks.right));
expect(tasks.top, greaterThanOrEqualTo(composer.top));
expect(list.bottom, lessThanOrEqualTo(composer.top));
});
}