Files
pipelines/app/test/features/runs/ui/runs_screen_test.dart
T

406 lines
13 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../../native_back.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/screen.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_view.dart';
import 'run_detail_finders.dart';
import 'package:pipelines_app/shell/empty_detail_pane.dart';
import 'package:pipelines_app/ui/adaptive/segmented_bar.dart';
class _Gateway implements ApiGateway {
final framesController = StreamController<JsonMap>.broadcast();
final listRequests = <Completer<JsonMap>>[];
final detailRequests = <num, List<Completer<JsonMap>>>{};
@override
Stream<JsonMap> get frames => framesController.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
final request = Completer<JsonMap>();
if (type == 'list_runs') {
listRequests.add(request);
} else if (type == 'get_run') {
final id = fields['run_id'] as num;
detailRequests.putIfAbsent(id, () => []).add(request);
} else {
request.completeError(UnsupportedError(type));
}
return request.future;
}
void replyList(int index, List<JsonMap> runs) =>
listRequests[index].complete({'runs': runs});
void replyDetail(num id, int index, JsonMap detail) =>
detailRequests[id]![index].complete({'run_detail': detail});
void failDetail(num id, int index, Object error) =>
detailRequests[id]![index].completeError(error);
}
const _runs = [
{'id': 1, 'title': 'Alpha run', 'name': 'alpha', 'status': 'running'},
{'id': 2, 'title': 'Beta run', 'name': 'beta', 'status': 'failed'},
];
Widget _materialScreen(
_Gateway gateway, {
int? initialRunId,
int? initialPipelineId,
int syncToken = 0,
int listRefreshToken = 0,
ValueChanged<int>? onRunSelected,
ValueChanged<int?>? onBack,
ValueChanged<int?>? onPipelineFilterChanged,
}) => MaterialApp(
home: Scaffold(
body: RunsScreen(
gateway: gateway,
initialRunId: initialRunId,
initialPipelineId: initialPipelineId,
syncToken: syncToken,
listRefreshToken: listRefreshToken,
onRunSelected: onRunSelected,
onBack: onBack,
onPipelineFilterChanged: onPipelineFilterChanged,
),
),
);
Future<void> _mountList(WidgetTester tester, _Gateway gateway) async {
await tester.pump();
gateway.replyList(0, _runs);
await tester.pump();
await tester.pump();
}
void main() {
late _Gateway gateway;
setUp(() => gateway = _Gateway());
tearDown(() => gateway.framesController.close());
testWidgets('initial run opens a pending detail with Back available', (
tester,
) async {
await tester.pumpWidget(_materialScreen(gateway, initialRunId: 1));
await _mountList(tester, gateway);
expect(gateway.detailRequests[1], hasLength(1));
expect(find.text('Loading run details…'), findsOneWidget);
expect(find.text('Back to list'), findsOneWidget);
expect(
find.text('Alpha run'),
findsOneWidget,
reason: 'the list stays visible',
);
await tester.tap(find.text('Back to list'));
await tester.pump();
expect(find.text('Alpha run'), findsOneWidget);
expect(find.text('Beta run'), findsOneWidget);
// A reply that lands after Back must not reopen the previous detail.
gateway.replyDetail(1, 0, {'id': 1, 'title': 'Alpha run'});
await tester.pump();
expect(find.text('Alpha run'), findsOneWidget);
expect(find.text('Loading run details…'), findsNothing);
});
testWidgets('initial run renders its detail after the reply', (tester) async {
await tester.pumpWidget(_materialScreen(gateway, initialRunId: 1));
await _mountList(tester, gateway);
gateway.replyDetail(1, 0, {
'id': 1,
'title': 'Alpha run',
'status': 'succeeded',
'input': {'prompt': 'initial prompt'},
});
await tester.pump();
await tester.pump();
expect(runDetailText('Alpha run'), findsOneWidget);
expect(find.text('Input'), findsOneWidget);
expect(find.text('Loading run details…'), findsNothing);
});
testWidgets('detail failure keeps Back and returns to the list', (
tester,
) async {
await tester.pumpWidget(_materialScreen(gateway, initialRunId: 2));
await _mountList(tester, gateway);
gateway.failDetail(2, 0, StateError('server unavailable'));
await tester.pump();
expect(
find.descendant(
of: find.byType(RunDetailView),
matching: find.textContaining('server unavailable'),
),
findsOneWidget,
);
expect(find.text('Back to list'), findsOneWidget);
await tester.tap(find.text('Back to list'));
await tester.pump();
expect(find.text('Alpha run'), findsOneWidget);
expect(find.textContaining('server unavailable'), findsNothing);
});
testWidgets('selecting a list row is driven by the controller selection', (
tester,
) async {
await tester.pumpWidget(_materialScreen(gateway));
await _mountList(tester, gateway);
// Beside the list, nothing open yet: the shared empty-pane placeholder.
expect(
find.widgetWithText(EmptyDetailPane, 'Select a run to open it here.'),
findsOneWidget,
);
await tester.tap(find.text('Beta run'));
await tester.pump();
expect(gateway.detailRequests[2], hasLength(1));
gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'});
await tester.pump();
await tester.pump();
expect(runDetailText('Beta run'), findsOneWidget);
expect(find.byKey(const ValueKey('run-row-1')), findsOneWidget);
});
testWidgets('selection and Back report route changes to the host', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
int? selected;
int? backedPipeline;
await tester.pumpWidget(
_materialScreen(
gateway,
onRunSelected: (id) => selected = id,
onBack: (id) => backedPipeline = id,
),
);
await _mountList(tester, gateway);
await tester.tap(find.text('Beta run'));
await tester.pump();
expect(selected, 2);
gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'});
await tester.pumpAndSettle();
await tester.tap(findNativeBack());
await tester.pumpAndSettle();
expect(backedPipeline, isNull);
expect(find.text('Alpha run'), findsOneWidget);
});
testWidgets(
'pipeline route shows its chip and clearing reports the URL change',
(tester) async {
int? changedPipeline = 3;
await tester.pumpWidget(
_materialScreen(
gateway,
initialPipelineId: 3,
onPipelineFilterChanged: (id) => changedPipeline = id,
),
);
await _mountList(tester, gateway);
expect(find.text('Pipeline 3'), findsOneWidget);
expect(changedPipeline, 3);
await tester.tap(find.byTooltip('Clear filter: Pipeline 3'));
await tester.pump();
expect(changedPipeline, isNull);
expect(find.text('Pipeline 3'), findsNothing);
},
);
testWidgets('run detail keeps the pipeline filter for Back routing', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
int? backPipeline;
await tester.pumpWidget(
_materialScreen(
gateway,
initialPipelineId: 3,
onBack: (id) => backPipeline = id,
),
);
await _mountList(tester, gateway);
await tester.pumpWidget(
_materialScreen(
gateway,
initialRunId: 2,
onBack: (id) => backPipeline = id,
),
);
await tester.pump();
gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'});
await tester.pumpAndSettle();
await tester.tap(findNativeBack());
await tester.pumpAndSettle();
expect(backPipeline, 3);
});
testWidgets('a route change opens and closes the named run', (tester) async {
await tester.pumpWidget(_materialScreen(gateway, initialRunId: 2));
await _mountList(tester, gateway);
expect(gateway.detailRequests[2], hasLength(1));
gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'});
await tester.pump();
await tester.pump();
expect(runDetailText('Beta run'), findsOneWidget);
await tester.pumpWidget(_materialScreen(gateway, initialRunId: 1));
await tester.pump();
expect(gateway.detailRequests[1], hasLength(1));
gateway.replyDetail(1, 0, {'id': 1, 'title': 'Alpha run'});
await tester.pump();
await tester.pump();
expect(runDetailText('Alpha run'), findsOneWidget);
await tester.pumpWidget(_materialScreen(gateway));
await tester.pump();
expect(find.text('Alpha run'), findsOneWidget);
expect(find.text('Beta run'), findsOneWidget);
expect(find.text('Back to list'), findsNothing);
});
testWidgets('a reconnect token refreshes the open detail in place', (
tester,
) async {
await tester.pumpWidget(_materialScreen(gateway, initialRunId: 2));
await _mountList(tester, gateway);
gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'});
await tester.pump();
await tester.pump();
expect(runDetailText('Beta run'), findsOneWidget);
await tester.pumpWidget(
_materialScreen(gateway, initialRunId: 2, syncToken: 1),
);
await tester.pump();
expect(gateway.listRequests, hasLength(2));
gateway.replyList(1, _runs);
await tester.pump();
await tester.pump();
expect(gateway.detailRequests[2], hasLength(2));
gateway.replyDetail(2, 1, {
'id': 2,
'title': 'Beta run',
'status': 'succeeded',
});
await tester.pump();
await tester.pump();
expect(runDetailText('Beta run'), findsOneWidget);
expect(find.byKey(const ValueKey('run-row-1')), findsOneWidget);
});
for (final reconnect in [false, true]) {
testWidgets('foreground refresh resyncs detail (reconnect: $reconnect)', (
tester,
) async {
await tester.pumpWidget(_materialScreen(gateway, initialRunId: 2));
await _mountList(tester, gateway);
gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'});
await tester.pump();
await tester.pumpWidget(
_materialScreen(
gateway,
initialRunId: 2,
syncToken: reconnect ? 1 : 0,
listRefreshToken: 1,
),
);
expect(
gateway.listRequests,
hasLength(2),
reason: 'simultaneous reconnect and foreground refresh coalesce',
);
gateway.replyList(1, _runs);
await tester.pump();
expect(gateway.detailRequests[2], hasLength(2));
expect(
runDetailText('Beta run'),
findsOneWidget,
reason: 'the selected detail remains visible during refresh',
);
gateway.replyDetail(2, 1, {'id': 2, 'title': 'Fresh detail'});
await tester.pump();
await tester.pump();
expect(runDetailText('Fresh detail'), findsOneWidget);
});
}
testWidgets(
'wide Cupertino host keeps list and detail bars while pending',
(tester) async {
await tester.pumpWidget(
CupertinoApp(
home: CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(middle: Text('Runs')),
child: SafeArea(
child: RunsScreen(gateway: gateway, initialRunId: 1),
),
),
),
);
await _mountList(tester, gateway);
expect(find.byType(CupertinoPageScaffold), findsNWidgets(2));
// The host's own bar and the detail pane's segmented bar.
expect(find.byType(CupertinoNavigationBar), findsOneWidget);
expect(find.byType(SegmentedBar), findsOneWidget);
expect(find.text('Loading run details…'), findsOneWidget);
expect(find.byType(CupertinoActivityIndicator), findsOneWidget);
expect(find.text('Back to list'), findsOneWidget);
},
variant: TargetPlatformVariant.only(TargetPlatform.iOS),
);
testWidgets('on a phone an open run is its own page and Back returns', (
tester,
) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
int? selected;
var backs = 0;
await tester.pumpWidget(
_materialScreen(
gateway,
onRunSelected: (id) => selected = id,
onBack: (_) => backs++,
),
);
await _mountList(tester, gateway);
await tester.tap(find.text('Beta run'));
await tester.pump();
expect(selected, 2);
gateway.replyDetail(2, 0, {'id': 2, 'title': 'Beta run'});
await tester.pumpAndSettle();
// The run covers the list as a page with its own Back.
expect(find.text('Alpha run'), findsNothing);
expect(findNativeBack(), findsOneWidget);
await tester.tap(findNativeBack());
await tester.pumpAndSettle();
expect(backs, 1);
expect(find.text('Alpha run'), findsOneWidget);
});
}