merge(app): finish retained Runs integration after deadline

This commit is contained in:
2026-10-21 17:26:41 +01:00
40 changed files with 5025 additions and 50 deletions
@@ -0,0 +1,264 @@
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.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_view.dart';
import 'package:pipelines_app/features/runs/ui/runs_controller.dart';
enum AppPlatform { material, cupertino }
class FakeGateway implements ApiGateway {
FakeGateway(this.detail);
JsonMap detail;
Object? failure;
Completer<JsonMap>? hold;
@override
Stream<JsonMap> get frames => const Stream<JsonMap>.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type != 'get_run') throw UnsupportedError(type);
if (hold != null) return hold!.future;
if (failure != null) return Future.error(failure!);
return Future.value({'run_detail': detail});
}
}
const prompt = ' keep\n\nspacing ';
JsonMap runFixture({JsonMap? usage, int steps = 2}) {
final run = <String, dynamic>{
'id': 7,
'title': 'Nightly build',
'name': 'release',
'status': 'succeeded',
'input': {
'prompt': prompt,
'nested': {
'alpha': 1,
'items': [1, 2],
},
},
'output': {
'result': {'ok': true},
},
'steps': [
for (var i = 0; i < steps; i++)
{
'stage': i == 0 ? 'build' : 'test',
'sequence': i + 3,
'status': 'completed',
'outcome': 'success',
'target': '#success',
},
],
};
if (usage != null) run['usage'] = usage;
return run;
}
JsonMap usageFixture({int known = 1}) => {
'stages': 1,
'tokens_total': 0,
'tokens_known_stages': known,
'tokens_in': 0,
'tokens_out': 0,
'split_known': true,
'cost_known_stages': known,
};
Future<void> pumpDetail(
WidgetTester tester,
AppPlatform platform,
RunsController controller, {
VoidCallback? onBack,
bool settle = true,
}) async {
tester.view.physicalSize = const Size(390, 1600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final view = RunDetailView(controller: controller, onBack: onBack ?? () {});
final page = MediaQuery(
data: MediaQueryData.fromView(
tester.view,
).copyWith(textScaler: const TextScaler.linear(2)),
child: view,
);
await tester.pumpWidget(
platform == AppPlatform.material
? MaterialApp(home: Scaffold(body: page))
: CupertinoApp(
home: CupertinoPageScaffold(child: SafeArea(child: page)),
),
);
if (settle) {
await tester.pumpAndSettle();
} else {
await tester.pump();
}
}
void mockClipboard(
WidgetTester tester,
List<String> copied, {
bool Function()? denied,
}) {
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(call) async {
if (call.method != 'Clipboard.setData') return null;
if (denied?.call() ?? false) {
throw PlatformException(code: 'clipboard-denied');
}
copied.add((call.arguments as Map)['text'] as String);
return null;
},
);
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
null,
),
);
}
void main() {
late FakeGateway gateway;
late RunsController controller;
setUp(() {
gateway = FakeGateway(runFixture());
controller = RunsController(RunsRepository(gateway));
});
tearDown(() => controller.dispose());
for (final platform in AppPlatform.values) {
testWidgets('${platform.name} detail at 390 with 2x text', (tester) async {
gateway.detail = runFixture(steps: 12);
await controller.select(1);
await pumpDetail(tester, platform, controller);
expect(tester.takeException(), isNull);
expect(find.text('Nightly build'), findsOneWidget);
expect(find.textContaining('Original prompt'), findsOneWidget);
if (platform == AppPlatform.material) {
expect(find.byType(Chip), findsOneWidget);
expect(find.byType(ExpansionTile), findsNWidgets(2));
} else {
expect(find.byType(Material), findsNothing);
expect(find.byType(Chip), findsNothing);
expect(find.byType(CupertinoExpansionTile), findsNWidgets(2));
}
await tester.tap(find.text('Input'));
await tester.pumpAndSettle();
expect(find.textContaining('"alpha": 1'), findsOneWidget);
await tester.scrollUntilVisible(
find.text('Output'),
200,
scrollable: find.byType(Scrollable).first,
);
await tester.tap(find.text('Output'));
await tester.pumpAndSettle();
expect(find.textContaining('"ok": true'), findsOneWidget);
await tester.scrollUntilVisible(
find.textContaining('build · visit 3'),
200,
scrollable: find.byType(Scrollable).first,
);
expect(find.textContaining('build · visit 3'), findsOneWidget);
expect(find.textContaining('test · visit 4'), findsOneWidget);
if (platform == AppPlatform.material) {
final scrollable = tester.state<ScrollableState>(
find.byType(Scrollable).first,
);
final before = scrollable.position.pixels;
expect(before, greaterThan(0));
gateway.detail = runFixture(steps: 13);
await controller.select(1);
await tester.pumpAndSettle();
expect(scrollable.position.pixels, closeTo(before, 0.1));
}
});
testWidgets('${platform.name} loading and error stay honest', (
tester,
) async {
gateway.hold = Completer<JsonMap>();
final pending = controller.select(1);
await pumpDetail(tester, platform, controller, settle: false);
expect(find.text('Loading run details…'), findsOneWidget);
expect(
find.byType(
platform == AppPlatform.material
? CircularProgressIndicator
: CupertinoActivityIndicator,
),
findsOneWidget,
);
gateway.hold!.complete({'run_detail': runFixture()});
await pending;
await tester.pumpAndSettle();
expect(find.text('Nightly build'), findsOneWidget);
controller.dispose();
gateway = FakeGateway(runFixture())
..failure = StateError('server unavailable');
controller = RunsController(RunsRepository(gateway));
await controller.select(1);
var backs = 0;
await pumpDetail(tester, platform, controller, onBack: () => backs++);
expect(find.textContaining('server unavailable'), findsOneWidget);
await tester.tap(find.text('Back to list'));
expect(backs, 1);
});
testWidgets('${platform.name} copies the exact prompt and reports denial', (
tester,
) async {
final copied = <String>[];
var denied = false;
mockClipboard(tester, copied, denied: () => denied);
final copy = platform == AppPlatform.material
? find.byTooltip('Copy prompt')
: find.text('Copy prompt');
await controller.select(1);
await pumpDetail(tester, platform, controller);
await tester.tap(copy);
await tester.pumpAndSettle();
expect(copied.single, prompt);
expect(find.text('Prompt copied to clipboard.'), findsOneWidget);
if (platform == AppPlatform.material) {
await tester.pump(const Duration(seconds: 5));
await tester.pumpAndSettle();
}
denied = true;
await tester.tap(copy);
await tester.pumpAndSettle();
expect(find.text('Could not copy prompt to clipboard.'), findsOneWidget);
});
}
testWidgets('reported zero and missing usage stay distinct', (tester) async {
gateway.detail = runFixture(usage: usageFixture());
await controller.select(1);
await pumpDetail(tester, AppPlatform.material, controller);
await tester.tap(find.text('Metrics'));
await tester.pumpAndSettle();
expect(find.text('Unavailable'), findsNothing);
expect(find.text('0'), findsNWidgets(3));
expect(find.text('\$0.000000'), findsOneWidget);
gateway.detail = runFixture(usage: usageFixture(known: 0));
await controller.select(1);
await tester.pumpAndSettle();
expect(find.text('Unavailable'), findsNWidgets(4));
expect(find.text('0'), findsNothing);
});
}
@@ -0,0 +1,344 @@
import 'dart:async';
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/runs_controller.dart';
class FakeGateway implements ApiGateway {
final framesController = StreamController<JsonMap>.broadcast();
final lists = <Completer<JsonMap>>[];
final details = <num, List<Completer<JsonMap>>>{};
int listCalls = 0;
@override
Stream<JsonMap> get frames => framesController.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'list_runs') {
listCalls++;
final request = Completer<JsonMap>();
lists.add(request);
return request.future;
}
final id = fields['run_id'] as num;
final request = Completer<JsonMap>();
details.putIfAbsent(id, () => []).add(request);
return request.future;
}
void listReply(int index, List<JsonMap> runs) =>
lists[index].complete({'runs': runs});
void detailReply(num id, int index, JsonMap value) =>
details[id]![index].complete({'run_detail': value});
}
void main() {
late FakeGateway gateway;
late RunsController controller;
var disposed = false;
setUp(() {
gateway = FakeGateway();
controller = RunsController(RunsRepository(gateway));
disposed = false;
});
tearDown(() {
if (!disposed) controller.dispose();
gateway.framesController.close();
});
test('late selection response cannot replace latest selection', () async {
final first = controller.select(1);
final second = controller.select(2);
gateway.detailReply(2, 0, {'id': 2});
await second;
gateway.detailReply(1, 0, {'id': 1});
await first;
expect(controller.detail?['id'], 2);
});
test('pending load and selection futures are safe after dispose', () async {
final load = controller.load();
final select = controller.select(1);
controller.dispose();
disposed = true;
gateway.listReply(0, []);
gateway.detailReply(1, 0, {'id': 1});
await Future.wait([load, select]);
});
test('Back cancels scheduled detail refresh', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
controller.back();
await Future<void>.delayed(const Duration(milliseconds: 220));
expect(gateway.details[1], hasLength(1));
expect(controller.detail, isNull);
});
test('unrelated run event does not refresh selected detail', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 2,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 220));
expect(gateway.details[1], hasLength(1));
});
test('identity-less event reloads list', () async {
controller.listen(gateway.frames);
gateway.framesController.add({'type': 'run_event', 'status': 'running'});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
gateway.listReply(0, [
{'id': 3},
]);
await Future<void>.delayed(Duration.zero);
expect(controller.runs.single['id'], 3);
expect(controller.loading, isFalse);
});
test(
'full refresh is coalesced and keeps current detail until reply',
() async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(controller.detail?['id'], 1);
expect(gateway.details[1], hasLength(2));
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['status'], 'succeeded');
},
);
test('load with initial id clears list and detail loading', () async {
final load = controller.load(7);
expect(controller.loading, isTrue);
gateway.listReply(0, [
{'id': 7},
{'id': 8},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.details[7], hasLength(1));
expect(controller.loading, isFalse);
expect(controller.detailLoading, isTrue);
gateway.detailReply(7, 0, {'id': 7});
await load;
expect(controller.loading, isFalse);
expect(controller.detailLoading, isFalse);
expect(controller.detail?['id'], 7);
expect(controller.selectedRunId, 7);
});
test('newer list load wins when replies complete out of order', () async {
final first = controller.load();
final second = controller.load();
gateway.listReply(1, [
{'id': 2},
]);
await second;
gateway.listReply(0, [
{'id': 1},
]);
await first;
expect(controller.runs.single['id'], 2);
expect(controller.loading, isFalse);
});
test('list resync leaves the selected detail request valid', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({'type': 'run_event', 'status': 'running'});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
gateway.listReply(0, [
{'id': 1, 'status': 'running'},
]);
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['id'], 1);
expect(controller.selectedRunId, 1);
expect(gateway.details[1], hasLength(1));
});
test('manual list load keeps an in-flight detail refresh valid', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
final load = controller.load();
gateway.listReply(1, [
{'id': 1},
]);
await load;
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['status'], 'succeeded');
expect(controller.detailLoading, isFalse);
gateway.listReply(0, [
{'id': 1},
]);
});
test('event during a superseded resync still runs one followup', () async {
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 9,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
final load = controller.load();
expect(gateway.listCalls, 2);
gateway.listReply(1, [
{'id': 1},
]);
await load;
gateway.framesController.add({
'type': 'run_event',
'run_id': 10,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 2);
gateway.listReply(0, [
{'id': 1},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 3);
gateway.listReply(2, [
{'id': 1},
{'id': 2},
]);
await Future<void>.delayed(Duration.zero);
expect(controller.runs, hasLength(2));
expect(controller.loading, isFalse);
});
test('unknown run events coalesce into one followup resync', () async {
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 9,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
expect(controller.loading, isTrue);
gateway.framesController.add({
'type': 'run_event',
'run_id': 10,
'status': 'running',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 1);
gateway.listReply(0, [
{'id': 3},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 2);
expect(controller.loading, isTrue);
gateway.listReply(1, [
{'id': 3},
{'id': 4},
]);
await Future<void>.delayed(Duration.zero);
expect(gateway.listCalls, 2);
expect(controller.loading, isFalse);
expect(controller.runs, hasLength(2));
});
test(
'selected refresh coalesces and Back ignores the pending reply',
() async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'failed',
});
await Future<void>.delayed(Duration.zero);
expect(gateway.details[1], hasLength(2));
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(gateway.details[1], hasLength(3));
controller.back();
gateway.detailReply(1, 2, {'id': 1, 'status': 'failed'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail, isNull);
expect(controller.detailLoading, isFalse);
expect(controller.error, isNull);
},
);
test('choosing another run does not display the previous detail', () async {
final first = controller.select(1);
gateway.detailReply(1, 0, {'id': 1});
await first;
final second = controller.select(2);
expect(controller.detail, isNull);
expect(controller.selectedRunId, 2);
expect(controller.detailLoading, isTrue);
gateway.detailReply(2, 0, {'id': 2});
await second;
expect(controller.detail?['id'], 2);
expect(controller.detailLoading, isFalse);
});
test('pending detail refresh is safe after dispose', () async {
final select = controller.select(1);
gateway.detailReply(1, 0, {'id': 1, 'status': 'running'});
await select;
controller.listen(gateway.frames);
gateway.framesController.add({
'type': 'run_event',
'run_id': 1,
'status': 'succeeded',
});
await Future<void>.delayed(const Duration(milliseconds: 180));
expect(gateway.details[1], hasLength(2));
controller.dispose();
disposed = true;
gateway.detailReply(1, 1, {'id': 1, 'status': 'succeeded'});
await Future<void>.delayed(Duration.zero);
expect(controller.detail?['id'], 1);
});
}
@@ -0,0 +1,180 @@
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/runs/data/runs_repository.dart';
import 'package:pipelines_app/features/runs/ui/runs_controller.dart';
import 'package:pipelines_app/features/runs/ui/runs_list.dart';
class _Gateway implements ApiGateway {
final framesController = StreamController<JsonMap>.broadcast();
JsonMap? reply;
Object? failure;
Completer<void>? gate;
@override
Stream<JsonMap> get frames => framesController.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
await gate?.future;
if (failure != null) throw failure!;
return reply ?? {'runs': []};
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late _Gateway gateway;
late RunsController controller;
setUp(() {
gateway = _Gateway();
controller = RunsController(RunsRepository(gateway));
});
tearDown(() async {
controller.dispose();
await gateway.framesController.close();
});
Future<void> mount(
WidgetTester tester, {
required bool cupertino,
ValueChanged<num>? onSelected,
}) async {
gateway.reply = {
'runs': [
{'id': 1, 'name': 'Alpha', 'status': 'running', 'scheduled': true},
{'id': 2, 'name': 'Beta', 'status': 'failed', 'scheduled': false},
],
};
await controller.load();
final list = AnimatedBuilder(
animation: controller,
builder: (context, _) =>
RunsList(controller: controller, onSelected: onSelected ?? (_) {}),
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: list))
: MaterialApp(home: Scaffold(body: list)),
);
}
testWidgets(
'uses native search and filters runs in CupertinoApp',
(tester) async {
num? selectedRun;
await mount(
tester,
cupertino: true,
onSelected: (id) => selectedRun = id,
);
expect(find.byType(CupertinoSearchTextField), findsOneWidget);
expect(find.text('Alpha'), findsOneWidget);
await tester.enterText(find.byType(CupertinoSearchTextField), 'Beta');
await tester.pumpAndSettle();
expect(find.text('Beta'), findsNWidgets(2));
expect(find.text('Alpha'), findsNothing);
await tester.tap(find.byType(CupertinoListTile));
expect(selectedRun, 2);
await tester.enterText(find.byType(CupertinoSearchTextField), 'missing');
await tester.pumpAndSettle();
expect(find.text('No runs found.'), findsOneWidget);
await tester.enterText(find.byType(CupertinoSearchTextField), '');
await tester.pumpAndSettle();
await tester.tap(find.textContaining('Schedule:'));
await tester.pumpAndSettle();
expect(find.text('Scheduled'), findsWidgets);
await tester.tap(find.text('Scheduled').last);
await tester.pumpAndSettle();
expect(controller.scheduled, 'scheduled');
},
variant: TargetPlatformVariant.only(TargetPlatform.iOS),
);
testWidgets(
'retains Material controls and handles large text on narrow layout',
(tester) async {
tester.view.devicePixelRatio = 2;
tester.view.physicalSize = const Size(780, 1560);
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
MaterialApp(
builder: (context, child) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: const TextScaler.linear(2)),
child: child!,
),
home: Scaffold(
body: AnimatedBuilder(
animation: controller,
builder: (_, _) =>
RunsList(controller: controller, onSelected: (_) {}),
),
),
),
);
gateway.reply = {
'runs': [
{'id': 1, 'name': 'Alpha', 'status': 'running', 'scheduled': true},
{'id': 2, 'name': 'Beta', 'status': 'failed', 'scheduled': false},
],
};
await controller.load();
await tester.pumpAndSettle();
expect(find.byType(TextField), findsOneWidget);
final layoutError = tester.takeException();
expect(
layoutError,
isNull,
reason: layoutError is FlutterError
? layoutError.toStringDeep()
: '$layoutError',
);
},
variant: TargetPlatformVariant.only(TargetPlatform.android),
);
testWidgets(
'shows loading, error and server-empty states distinctly',
(tester) async {
gateway.failure = StateError('offline');
gateway.gate = Completer<void>();
final loading = controller.load();
await tester.pumpWidget(
CupertinoApp(
home: CupertinoPageScaffold(
child: AnimatedBuilder(
animation: controller,
builder: (_, child) =>
RunsList(controller: controller, onSelected: (_) {}),
),
),
),
);
await tester.pump();
expect(find.byType(CupertinoActivityIndicator), findsOneWidget);
gateway.gate!.complete();
await loading;
await tester.pumpAndSettle();
expect(find.textContaining('offline'), findsOneWidget);
expect(tester.takeException(), isNull);
gateway.failure = null;
gateway.gate = null;
gateway.reply = {'runs': []};
await controller.load();
await tester.pumpAndSettle();
expect(find.text('No runs found.'), findsOneWidget);
expect(find.textContaining('offline'), findsNothing);
expect(tester.takeException(), isNull);
},
variant: TargetPlatformVariant.only(TargetPlatform.iOS),
);
}
@@ -0,0 +1,247 @@
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/runs/screen.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 syncToken = 0,
ValueChanged<int>? onRunSelected,
VoidCallback? onBack,
}) => MaterialApp(
home: Scaffold(
body: RunsScreen(
gateway: gateway,
initialRunId: initialRunId,
syncToken: syncToken,
onRunSelected: onRunSelected,
onBack: onBack,
),
),
);
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'), findsNothing);
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();
expect(find.text('Alpha run'), findsOneWidget);
expect(find.text('initial prompt'), 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.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);
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();
expect(find.text('Beta run'), findsOneWidget);
expect(find.text('Back to list'), findsOneWidget);
});
testWidgets('selection and Back report route changes to the host', (
tester,
) async {
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.pump();
await tester.tap(find.text('Back to list'));
await tester.pump();
expect(backs, 1);
expect(find.text('Alpha run'), findsOneWidget);
});
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();
expect(find.text('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();
expect(find.text('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();
expect(find.text('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();
expect(find.text('Beta run'), findsOneWidget);
expect(find.text('Back to list'), findsOneWidget);
});
testWidgets(
'Cupertino host keeps exactly one navigation bar 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), findsOneWidget);
expect(find.byType(CupertinoNavigationBar), 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),
);
}