Merge: running durations tick live in runs list, summary and timeline

This commit is contained in:
2026-11-04 17:48:34 +00:00
15 changed files with 735 additions and 44 deletions
@@ -0,0 +1,215 @@
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));
});
}
@@ -163,7 +163,10 @@ void main() {
'totalRebuildsPerMinute=${rebuilds.total} '
'timersScheduledPerMinute=$perMinute',
);
expect(active, lessThanOrEqualTo(1));
// One shared minute clock for the relative times and one shared clock
// for the 20 running rows' ticking durations; never a timer per row.
expect(active, lessThanOrEqualTo(2));
expect(perMinute, lessThanOrEqualTo(61));
await tester.pumpWidget(const SizedBox());
expect(timers.active, 0);
});
+111
View File
@@ -0,0 +1,111 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/text/elapsed_clock.dart';
import 'package:pipelines_app/ui/text/live_elapsed_builder.dart';
/// A fake wall clock the test advances together with the fake timers.
class _Time {
_Time(this.value);
DateTime value;
Future<void> advance(WidgetTester tester, Duration by) async {
value = value.add(by);
await tester.pump(by);
}
}
void main() {
test('wakes when the displayed precision next changes', () {
expect(elapsedTextChangesIn(Duration.zero), const Duration(seconds: 1));
expect(
elapsedTextChangesIn(const Duration(seconds: 59, milliseconds: 400)),
const Duration(milliseconds: 600),
);
// `1h 2m` shows minutes, `2d 3h` hours.
expect(
elapsedTextChangesIn(const Duration(hours: 1, minutes: 2, seconds: 15)),
const Duration(seconds: 45),
);
expect(
elapsedTextChangesIn(const Duration(days: 2, minutes: 30)),
const Duration(minutes: 30),
);
// Clock skew reads 0s until a second has passed.
expect(
elapsedTextChangesIn(const Duration(seconds: -2)),
const Duration(seconds: 3),
);
});
testWidgets('a live label ticks, stops when no longer running and on '
'dispose', (tester) async {
final start = DateTime.utc(2026, 11, 4, 12);
final time = _Time(start.add(const Duration(seconds: 5)));
final clock = ElapsedClock(now: () => time.value);
var builds = 0;
Widget app(DateTime? since) => Directionality(
textDirection: TextDirection.ltr,
child: ElapsedClockScope(
clock: clock,
child: LiveElapsedBuilder(
since: since,
builder: (context, now) {
builds += 1;
return Text('${now.difference(start).inSeconds}s');
},
),
),
);
await tester.pumpWidget(app(start));
expect(find.text('5s'), findsOneWidget);
expect(clock.ticking, isTrue);
await time.advance(tester, const Duration(seconds: 1));
expect(find.text('6s'), findsOneWidget);
await time.advance(tester, const Duration(seconds: 1));
expect(find.text('7s'), findsOneWidget);
// Finished: no longer watched, so the timer stops.
await tester.pumpWidget(app(null));
expect(clock.ticking, isFalse);
final settled = builds;
await time.advance(tester, const Duration(seconds: 5));
expect(builds, settled);
await tester.pumpWidget(app(start));
expect(clock.ticking, isTrue);
await tester.pumpWidget(const SizedBox());
expect(clock.ticking, isFalse);
});
testWidgets('a muted label stops ticking and catches up when shown', (
tester,
) async {
final start = DateTime.utc(2026, 11, 4, 12);
final time = _Time(start);
final clock = ElapsedClock(now: () => time.value);
Widget app({required bool shown}) => Directionality(
textDirection: TextDirection.ltr,
child: ElapsedClockScope(
clock: clock,
child: TickerMode(
enabled: shown,
child: LiveElapsedBuilder(
since: start,
builder: (context, now) =>
Text('${now.difference(start).inSeconds}s'),
),
),
),
);
await tester.pumpWidget(app(shown: true));
expect(clock.ticking, isTrue);
await tester.pumpWidget(app(shown: false));
expect(clock.ticking, isFalse);
await time.advance(tester, const Duration(seconds: 30));
await tester.pumpWidget(app(shown: true));
expect(find.text('30s'), findsOneWidget);
expect(clock.ticking, isTrue);
await tester.pumpWidget(const SizedBox());
});
}