Show recorded timing and progress in background task lists

This commit is contained in:
2026-11-06 15:08:11 +00:00
parent c9a4cd0bf6
commit 6dfb5d7a4b
9 changed files with 513 additions and 9 deletions
@@ -0,0 +1,170 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_background_tasks_controller.dart';
import 'package:pipelines_app/features/chat/conversation/chat_background_tasks_panel.dart';
import 'package:pipelines_app/features/chat/conversation/chat_task_metadata_view.dart';
import 'chat_background_tasks_fixtures.dart';
void main() {
for (final cupertino in [false, true]) {
testWidgets(
'recorded facts wrap at 2x, tick without requests, dispose clock ($cupertino)',
(tester) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
var now = DateTime.utc(2026, 11, 6, 10);
var clockReads = 0;
final gateway = TasksGateway()
..shells = (_) async => {
'chat_shells': [
{
'id': 'a',
'status': 'running',
'command': 'Review integration changes',
'started_at': now
.subtract(const Duration(minutes: 2))
.millisecondsSinceEpoch,
'progress':
'Checking the targeted tests and reviewing the remaining findings',
'tool_count': 18,
'token_count': 12450,
},
],
};
final controller = ChatBackgroundTasksController(gateway, 's');
addTearDown(controller.dispose);
addTearDown(gateway.close);
final panel = ChatBackgroundTasksPanel(
controller: controller,
clock: () {
clockReads++;
return now;
},
);
Widget builder(BuildContext context, Widget? child) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: const TextScaler.linear(2)),
child: child!,
);
await tester.pumpWidget(
cupertino
? CupertinoApp(
builder: builder,
home: CupertinoPageScaffold(
child: SingleChildScrollView(child: panel),
),
)
: MaterialApp(
builder: builder,
home: Scaffold(body: SingleChildScrollView(child: panel)),
),
);
await tester.pumpAndSettle();
expect(find.text('Running · 2m 0s'), findsOneWidget);
expect(find.text('18 tool calls · 12450 tokens'), findsOneWidget);
expect(find.textContaining('Started 6 November 2026'), findsOneWidget);
final requests = gateway.calls.length;
now = now.add(const Duration(seconds: 11));
await tester.pump(const Duration(seconds: 11));
expect(find.text('Running · 2m 11s'), findsOneWidget);
expect(gateway.calls.length, requests);
expect(
gateway.calls.where((call) => call.type == 'chat_shell_output'),
isEmpty,
);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox.shrink());
final stoppedAt = clockReads;
await tester.pump(const Duration(seconds: 11));
expect(clockReads, stoppedAt);
expect(tester.takeException(), isNull);
},
);
}
testWidgets(
'selected detail keeps full metadata and requests only its output',
(tester) async {
final gateway = TasksGateway()
..shells = (_) async => {
'chat_shells': [
{
'id': 'a',
'status': 'running',
'command': 'Agent: Review integration',
'description': 'Review integration',
'last_tool': 'Read',
'started_at': DateTime.utc(2026, 11, 6, 9).millisecondsSinceEpoch,
'updated_at': DateTime.utc(
2026,
11,
6,
9,
20,
).millisecondsSinceEpoch,
},
],
};
final controller = ChatBackgroundTasksController(gateway, 's');
addTearDown(controller.dispose);
addTearDown(gateway.close);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: ChatBackgroundTasksPanel(
controller: controller,
clock: () => DateTime.utc(2026, 11, 6, 10),
),
),
),
),
);
await tester.pumpAndSettle();
expect(find.text('Review integration'), findsOneWidget);
expect(find.text('Last tool: Read'), findsNothing);
expect(
gateway.calls.where((call) => call.type == 'chat_shell_output'),
isEmpty,
);
await tester.tap(find.byKey(const ValueKey('chat-bg-task-a')));
await tester.pumpAndSettle();
expect(find.text('Agent: Review integration'), findsOneWidget);
expect(find.text('Last tool: Read'), findsOneWidget);
expect(
find.textContaining('Last update 6 November 2026'),
findsOneWidget,
);
expect(
gateway.calls.where((call) => call.type == 'chat_shell_output'),
hasLength(1),
);
await tester.pumpWidget(const SizedBox.shrink());
},
);
testWidgets('unknown start and reported runtime stay distinct from elapsed', (
tester,
) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChatTaskMetadataView(
shell: const {'duration_ms': 74000, 'tool_count': 0},
now: DateTime.utc(2026, 11, 6),
),
),
),
);
expect(find.text('Start time unavailable'), findsOneWidget);
expect(find.text('Reported runtime: 1m 14s'), findsOneWidget);
expect(find.text('Running'), findsOneWidget);
expect(find.text('0 tool calls'), findsOneWidget);
expect(find.textContaining('tokens'), findsNothing);
});
}
@@ -0,0 +1,74 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/domain/chat_shells.dart';
import 'package:pipelines_app/features/chat/domain/chat_task_metadata.dart';
void main() {
test('missing, malformed and future start never invent an elapsed time', () {
final now = DateTime.utc(2026, 11, 6, 10);
for (final value in [
null,
0,
-1,
'2026-11-06',
double.nan,
double.infinity,
8640000000000001,
]) {
expect(taskElapsed({'started_at': value}, now), isNull);
}
expect(
taskElapsed({
'started_at': now
.add(const Duration(minutes: 1))
.millisecondsSinceEpoch,
}, now),
isNull,
);
expect(taskElapsed({'duration_ms': 120000}, now), isNull);
expect(taskDuration(const Duration(days: 2, hours: 4)), '2d 4h');
expect(taskDuration(const Duration(hours: 2, minutes: 14)), '2h 14m');
expect(taskDuration(const Duration(seconds: 74)), '1m 14s');
expect(taskNumber({'tool_count': 0}, 'tool_count'), 0);
expect(taskNumber({'tool_count': 1.5}, 'tool_count'), isNull);
});
test('partial stream frames and snapshots retain known task facts', () {
final initial = <String, dynamic>{
'id': 'a',
'status': 'running',
'command': 'check',
'started_at': 123000,
'updated_at': 124000,
'description': 'Verify changes',
'progress': 'Checking',
'task_type': 'agent',
'tool_count': 2,
'token_count': 50,
};
final progress = mergeShellFrame(
[initial],
{
'id': 'a',
'status': 'running',
'progress': 'Reviewing',
'tool_count': 0,
},
);
final loaded = shellsLoaded(progress, [
{'id': 'a', 'status': 'running', 'command': ''},
]).single;
expect(loaded['started_at'], 123000);
expect(loaded['description'], 'Verify changes');
expect(loaded['progress'], 'Reviewing');
expect(loaded['tool_count'], 0);
expect(loaded['token_count'], 50);
expect(loaded['command'], 'check');
final exited = mergeShellFrame(
[loaded],
{'id': 'a', 'status': 'exited'},
).single;
expect(exited['started_at'], 123000);
expect(exited['status'], 'exited');
expect(runningShells([exited]), isEmpty);
});
}