Files
pipelines/app/test/features/runs/ui/run_live_duration_test.dart
T
alan e7d00b85ff Runs: running run and stage durations tick live
One shared ElapsedClock (single timer, only while a visible running item
is watched and the app is visible; cadence follows formatDuration
precision) and a LiveElapsedBuilder that repaints only its own text.
Running list rows, the Summary Duration and running timeline visits
tick. A stage start re-reads the open detail, since step deltas carry
no started_at. Fixture ?running=1 for captures.
2026-11-04 17:43:29 +00:00

216 lines
6.6 KiB
Dart

import 'dart:async';
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/runs/data/runs_repository.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_content.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_tab_bodies.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_timeline.dart';
import 'package:pipelines_app/features/runs/ui/run_list_row.dart';
import 'package:pipelines_app/features/runs/ui/runs_controller.dart';
import 'package:pipelines_app/ui/text/elapsed_clock.dart';
final _start = DateTime.utc(2026, 11, 4, 12);
final _running = <String, dynamic>{
'id': 9,
'title': 'Nightly build',
'name': 'release',
'status': 'running',
'executing': true,
'created_at': '2026-11-04T12:00:00Z',
'finished_at': '',
'current_stage': 'build',
};
final _succeeded = <String, dynamic>{
..._running,
'status': 'succeeded',
'executing': false,
'finished_at': '2026-11-04T12:02:05Z',
};
/// A fake wall clock advanced together with the test's fake timers.
class _Time {
_Time(this.value);
DateTime value;
late final clock = ElapsedClock(now: () => value);
Future<void> advance(WidgetTester tester, Duration by) async {
value = value.add(by);
await tester.pump(by);
}
}
Widget _host(_Time time, Widget child) => MaterialApp(
home: ElapsedClockScope(
clock: time.clock,
child: Scaffold(body: child),
),
);
Finder _textContaining(String text) => find.textContaining(text);
class _Gateway implements ApiGateway {
final framesController = StreamController<JsonMap>.broadcast();
final detailRequests = <Completer<JsonMap>>[];
@override
Stream<JsonMap> get frames => framesController.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
final request = Completer<JsonMap>();
if (type == 'get_run') detailRequests.add(request);
return request.future;
}
}
void main() {
testWidgets('a running row ticks and stops once the run completes', (
tester,
) async {
final time = _Time(_start.add(const Duration(seconds: 45)));
Widget row(JsonMap run) =>
_host(time, RunListRow(run: run, now: time.value));
await tester.pumpWidget(row(_running));
expect(_textContaining('45s'), findsOneWidget);
await time.advance(tester, const Duration(seconds: 1));
expect(_textContaining('46s'), findsOneWidget);
await time.advance(tester, const Duration(seconds: 14));
expect(_textContaining('1m 0s'), findsOneWidget);
expect(time.clock.ticking, isTrue);
// The terminal frame's row: the recorded duration, no timer.
await tester.pumpWidget(row(_succeeded));
expect(_textContaining('2m 5s'), findsOneWidget);
expect(time.clock.ticking, isFalse);
});
testWidgets('no timer runs when no row is running', (tester) async {
final time = _Time(_start.add(const Duration(minutes: 5)));
await tester.pumpWidget(
_host(
time,
Column(
children: [
RunListRow(run: _succeeded, now: time.value),
RunListRow(
run: {..._running, 'status': 'waiting', 'executing': false},
now: time.value,
),
],
),
),
);
expect(time.clock.ticking, isFalse);
});
testWidgets('the Summary Duration of a running run ticks', (tester) async {
final time = _Time(_start.add(const Duration(seconds: 59)));
await tester.pumpWidget(
_host(
time,
Builder(
builder: (context) => runDetailSummaryTab(
context: context,
run: _running,
cupertino: false,
copying: false,
copyStatus: null,
onCopy: () {},
),
),
),
);
expect(find.text('59s'), findsOneWidget);
await time.advance(tester, const Duration(seconds: 1));
expect(find.text('1m 0s'), findsOneWidget);
await tester.pumpWidget(const SizedBox());
expect(time.clock.ticking, isFalse);
});
testWidgets('a running stage row ticks; finished rows keep their time', (
tester,
) async {
final time = _Time(_start.add(const Duration(minutes: 3, seconds: 4)));
final steps = [
{
'stage': 'plan',
'sequence': 1,
'status': 'completed',
'outcome': 'success',
'started_at': '2026-11-04T11:58:00Z',
'finished_at': '2026-11-04T12:00:00Z',
},
{
'stage': 'build',
'sequence': 2,
'status': 'running',
'started_at': '2026-11-04T12:00:00Z',
},
];
await tester.pumpWidget(
_host(
time,
RunTimelineList(
visits: detailTimeline(steps),
cupertino: false,
onSelected: (_) {},
),
),
);
expect(find.text('2m 0s'), findsOneWidget);
expect(find.text('3m 4s'), findsOneWidget);
await time.advance(tester, const Duration(seconds: 1));
expect(find.text('3m 5s'), findsOneWidget);
expect(find.text('2m 0s'), findsOneWidget);
await tester.pumpWidget(const SizedBox());
expect(time.clock.ticking, isFalse);
});
test('a terminal frame finishes the row without a list refresh', () async {
final gateway = _Gateway();
final controller = RunsController(RunsRepository(gateway))
..runs = [_running]
..listen(gateway.frames);
addTearDown(() {
controller.dispose();
gateway.framesController.close();
});
gateway.framesController.add({
'type': 'run_event',
'run_id': 9,
'status': 'succeeded',
'finished_at': '2026-11-04T12:02:05Z',
});
await Future<void>.delayed(Duration.zero);
final row = controller.runs.single;
expect(row['status'], 'succeeded');
expect(row['executing'], isFalse);
expect(row['finished_at'], '2026-11-04T12:02:05Z');
});
test('a stage start re-reads the open detail for its start time', () async {
final gateway = _Gateway();
final controller = RunsController(RunsRepository(gateway));
addTearDown(() {
controller.dispose();
gateway.framesController.close();
});
final select = controller.select(9);
gateway.detailRequests.single.complete({
'run_detail': {..._running, 'steps': <Object>[]},
});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 9,
'status': 'running',
'step': {'sequence': 1, 'stage': 'build', 'status': 'running'},
});
await Future<void>.delayed(const Duration(milliseconds: 200));
expect(gateway.detailRequests, hasLength(2));
});
}