merge(app): land artifact-session-repair micro-slice

This commit is contained in:
2026-10-22 11:09:47 +01:00
18 changed files with 670 additions and 14 deletions
@@ -4,6 +4,7 @@ import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_visit_session.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_visit_session_state.dart';
import 'run_detail_graph_support.dart';
import 'run_detail_visit_support.dart';
Future<void> pumpSessionExport(WidgetTester tester, JsonMap step) =>
@@ -11,7 +12,10 @@ Future<void> pumpSessionExport(WidgetTester tester, JsonMap step) =>
MaterialApp(
home: Scaffold(
body: RunVisitSession(
run: visitRunFor(step),
step: step,
gateway: RunDetailFakeGateway(visitRunFor(step)),
onRunChanged: (_) {},
configuredEndpoint: 'ws://example.test/connect',
),
),
@@ -19,6 +19,8 @@ void main() {
expect(find.text('sess-1'), findsOneWidget);
expect(find.text('Open session.html'), findsOneWidget);
// No recorded export evidence, so the web offers no export-only repair.
expect(find.text('Retry export only'), findsNothing);
expect(
find.textContaining('not available in the artifact inventory'),
findsNothing,
@@ -69,7 +71,13 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RunVisitSession(step: step, configuredEndpoint: ''),
body: RunVisitSession(
run: visitRunFor(step),
step: step,
gateway: RunDetailFakeGateway(visitRunFor(step)),
onRunChanged: (_) {},
configuredEndpoint: '',
),
),
),
);
@@ -79,6 +87,8 @@ void main() {
findsOneWidget,
);
expect(find.textContaining('Open '), findsNothing);
// No openable document at all is the web's other repair branch.
expect(find.text('Retry export only'), findsOneWidget);
step['session_export'] = <String, dynamic>{
'html': 'session.html',
@@ -88,7 +98,13 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RunVisitSession(step: step, configuredEndpoint: ''),
body: RunVisitSession(
run: visitRunFor(step),
step: step,
gateway: RunDetailFakeGateway(visitRunFor(step)),
onRunChanged: (_) {},
configuredEndpoint: '',
),
),
),
);
@@ -108,7 +124,10 @@ void main() {
MaterialApp(
home: Scaffold(
body: RunVisitSession(
run: visitRunFixture(),
step: firstVisitStep(visitRunFixture()),
gateway: RunDetailFakeGateway(visitRunFixture()),
onRunChanged: (_) {},
configuredEndpoint: 'ws://example.test/connect',
openExternal: (uri) async {
opened = uri;
@@ -131,7 +150,10 @@ void main() {
MaterialApp(
home: Scaffold(
body: RunVisitSession(
run: visitRunFixture(),
step: firstVisitStep(visitRunFixture()),
gateway: RunDetailFakeGateway(visitRunFixture()),
onRunChanged: (_) {},
configuredEndpoint: 'ws://example.test/connect',
openExternal: (uri) async => false,
),
@@ -82,6 +82,14 @@ JsonMap visitRunFixture({
JsonMap firstVisitStep(JsonMap run) =>
(run['steps'] as List).first as Map<String, dynamic>;
/// The run that owns [step], for direct session-pane pumps outside the detail.
JsonMap visitRunFor(JsonMap step, {String status = 'succeeded'}) =>
<String, dynamic>{
'id': 77,
'status': status,
'steps': <dynamic>[step],
};
Future<void> openVisit(
WidgetTester tester,
RunDetailTestPlatform platform, {
@@ -95,7 +95,13 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RunVisitSession(step: step, configuredEndpoint: ''),
body: RunVisitSession(
run: visitRunFor(step),
step: step,
gateway: RunDetailFakeGateway(visitRunFor(step)),
onRunChanged: (_) {},
configuredEndpoint: '',
),
),
),
);
@@ -0,0 +1,271 @@
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/actions/session_export_repair_action.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_visit_session.dart';
import 'run_detail_visit_support.dart';
typedef _RepairHandler = Future<JsonMap> Function(String type, JsonMap fields);
class _RepairGateway implements ApiGateway {
_RepairGateway(this.handler);
final _RepairHandler handler;
final List<({String type, JsonMap fields})> calls = [];
@override
Stream<JsonMap> get frames => const Stream<JsonMap>.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
final copy = Map<String, dynamic>.from(fields);
calls.add((type: type, fields: copy));
return handler(type, copy);
}
Iterable<({String type, JsonMap fields})> get repairs =>
calls.where((call) => call.type == 'repair_session_export');
}
/// A recorded visit whose export failed, so the repair option is offered.
JsonMap partialRun() {
final run = visitRunFixture();
final step = firstVisitStep(run);
step['session_export'] = <String, dynamic>{
'html': 'session.html',
'status': 'failed',
'messages': 0,
'diagnostic': 'The first export failed.',
};
return run;
}
/// A reply whose recorded repair is verified complete and openable.
JsonMap repairedRun() {
final run = visitRunFixture();
final step = firstVisitStep(run);
step['session_export_repairs'] = <dynamic>[
<String, dynamic>{
'html': 'session-repair-1.html',
'status': 'complete',
'messages': 3,
'html_bytes': 900,
},
];
(step['artifacts'] as List).add(<String, dynamic>{
'name': 'session-repair-1.html',
'url': 'https://example.test/artifacts/session-repair-1.html',
'content_type': 'text/html',
'size_bytes': 900,
});
return run;
}
class _SessionHost extends StatefulWidget {
const _SessionHost({required this.run, required this.gateway});
final JsonMap run;
final _RepairGateway gateway;
@override
State<_SessionHost> createState() => _SessionHostState();
}
class _SessionHostState extends State<_SessionHost> {
late JsonMap _run = widget.run;
@override
void didUpdateWidget(covariant _SessionHost oldWidget) {
super.didUpdateWidget(oldWidget);
// A test that swaps the recorded run must show the new visit, exactly as
// the detail does when the operator selects another one.
if (!identical(widget.run, oldWidget.run)) _run = widget.run;
}
@override
Widget build(BuildContext context) => RunVisitSession(
run: _run,
step: firstVisitStep(_run),
gateway: widget.gateway,
onRunChanged: (detail) => setState(() => _run = detail),
configuredEndpoint: 'ws://example.test/connect',
);
}
Future<void> _pumpSession(
WidgetTester tester,
_RepairGateway gateway, {
JsonMap? run,
bool cupertino = false,
}) {
final host = _SessionHost(run: run ?? partialRun(), gateway: gateway);
return tester.pumpWidget(
cupertino
? CupertinoApp(
home: CupertinoPageScaffold(child: SafeArea(child: host)),
)
: MaterialApp(home: Scaffold(body: host)),
);
}
TextButton _materialButton(WidgetTester tester) => tester.widget<TextButton>(
find.widgetWithText(TextButton, 'Retry export only'),
);
void main() {
testWidgets('sends the recorded run and visit exactly once', (tester) async {
final reply = Completer<JsonMap>();
final gateway = _RepairGateway((_, _) => reply.future);
await _pumpSession(tester, gateway);
expect(find.text('Retry export only'), findsOneWidget);
await tester.tap(find.text('Retry export only'));
await tester.pump();
expect(gateway.repairs, hasLength(1));
expect(gateway.repairs.single.fields, {'run_id': 77, 'export_sequence': 1});
// Nothing but the one repair request is sent.
expect(gateway.calls.map((call) => call.type), ['repair_session_export']);
// The recorded export state is kept until the server replies.
expect(find.text('partial'), findsOneWidget);
expect(find.text('complete'), findsNothing);
expect(find.text('Exporting…'), findsOneWidget);
await tester.tap(find.text('Exporting…'), warnIfMissed: false);
await tester.pump();
expect(gateway.repairs, hasLength(1));
reply.complete({'run_detail': repairedRun()});
await tester.pumpAndSettle();
// The accepted reply names the verified repair the pane must now show.
expect(find.text('repair'), findsOneWidget);
expect(find.text('complete · 3 messages.'), findsOneWidget);
expect(find.text('Open session-repair-1.html'), findsOneWidget);
expect(find.text('Retry export only'), findsNothing);
});
testWidgets('a refusal is shown and the recorded state is unchanged', (
tester,
) async {
final gateway = _RepairGateway(
(_, _) => Future<JsonMap>.error(StateError('The session is gone.')),
);
await _pumpSession(tester, gateway);
await tester.tap(find.text('Retry export only'));
await tester.pumpAndSettle();
expect(find.byKey(sessionExportRepairErrorKey), findsOneWidget);
expect(find.text('The session is gone.'), findsOneWidget);
expect(find.text('partial'), findsOneWidget);
expect(find.text('complete'), findsNothing);
// The option is still available: a refusal is not a success.
expect(find.text('Retry export only'), findsOneWidget);
});
testWidgets('a legacy session.html with no recorded evidence keeps no '
'control', (tester) async {
final gateway = _RepairGateway(
(_, _) => Future<JsonMap>.error(StateError('unexpected')),
);
final run = partialRun();
firstVisitStep(run).remove('session_export');
await _pumpSession(tester, gateway, run: run);
// The document still opens, but there is no evidence to repair.
expect(find.text('Open session.html'), findsOneWidget);
expect(find.text('Retry export only'), findsNothing);
expect(gateway.calls, isEmpty);
});
testWidgets('a verified complete export offers no repair', (tester) async {
final gateway = _RepairGateway(
(_, _) => Future<JsonMap>.error(StateError('unexpected')),
);
await _pumpSession(tester, gateway, run: visitRunFixture());
expect(find.text('complete'), findsOneWidget);
expect(find.text('Retry export only'), findsNothing);
});
testWidgets('the control is disabled while the visit or run is active', (
tester,
) async {
final gateway = _RepairGateway(
(_, _) => Future<JsonMap>.error(StateError('unexpected')),
);
final active = partialRun();
firstVisitStep(active)['agent_active'] = true;
await _pumpSession(tester, gateway, run: active);
expect(find.text('Retry export only'), findsOneWidget);
expect(_materialButton(tester).onPressed, isNull);
final waiting = partialRun();
waiting['status'] = 'waiting';
waiting['export_pending_sequence'] = 2;
await _pumpSession(tester, gateway, run: waiting);
expect(_materialButton(tester).onPressed, isNull);
// The same wait allows the repair when this visit's export is the pending
// one, exactly as the web client's availability check does.
waiting['export_pending_sequence'] = 1;
await _pumpSession(tester, gateway, run: waiting);
expect(_materialButton(tester).onPressed, isNotNull);
});
testWidgets('changing visits clears a pending repair and its reply', (
tester,
) async {
final first = Completer<JsonMap>();
final second = Completer<JsonMap>();
final gateway = _RepairGateway(
(_, fields) =>
fields['export_sequence'] == 1 ? first.future : second.future,
);
await _pumpSession(tester, gateway);
await tester.tap(find.text('Retry export only'));
await tester.pump();
expect(find.text('Exporting…'), findsOneWidget);
final next = partialRun();
firstVisitStep(next)['sequence'] = 2;
await _pumpSession(tester, gateway, run: next);
expect(find.text('Retry export only'), findsOneWidget);
expect(find.text('Exporting…'), findsNothing);
await tester.tap(find.text('Retry export only'));
await tester.pump();
expect(gateway.repairs.map((call) => call.fields['export_sequence']), [
1,
2,
]);
// The first visit's late reply is not folded into the second visit.
first.complete({'run_detail': repairedRun()});
await tester.pump();
expect(find.text('repair'), findsNothing);
expect(find.text('partial'), findsOneWidget);
});
testWidgets('the Cupertino host uses a native Cupertino button', (
tester,
) async {
final gateway = _RepairGateway(
(_, _) => Future<JsonMap>.error(StateError('unexpected')),
);
await _pumpSession(tester, gateway, cupertino: true);
expect(
find.widgetWithText(CupertinoButton, 'Retry export only'),
findsOneWidget,
);
expect(find.widgetWithText(TextButton, 'Retry export only'), findsNothing);
});
}