From 2eda713014570c6c64f62517e7780fb11441e8f1 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 23 Oct 2026 14:32:04 +0100 Subject: [PATCH] fix(app): an in-flight session export repair stays pending across remounts, as the web --- .../actions/session_export_repair_action.dart | 37 +++++++++--- .../session_export_repair_pending_test.dart | 58 +++++++++++++++++++ 2 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 app/test/features/runs/actions/session_export_repair_pending_test.dart diff --git a/app/lib/features/runs/actions/session_export_repair_action.dart b/app/lib/features/runs/actions/session_export_repair_action.dart index 70fc59d6..140c80c4 100644 --- a/app/lib/features/runs/actions/session_export_repair_action.dart +++ b/app/lib/features/runs/actions/session_export_repair_action.dart @@ -63,10 +63,28 @@ class SessionExportRepairAction extends StatefulWidget { _SessionExportRepairActionState(); } +/// The `run:sequence` repairs in flight, shared by every mounted control, as +/// the web's page-level `sessionRepairs` set: leaving the Session section and +/// returning before the reply cannot send the same repair twice. +final ValueNotifier> pendingSessionExportRepairs = ValueNotifier( + const {}, +); + +String _repairKey(num runId, int sequence) => '$runId:$sequence'; + class _SessionExportRepairActionState extends State { - bool _sending = false; String? _refusal; + bool get _sending => pendingSessionExportRepairs.value.contains( + _repairKey(widget.runId, widget.sequence), + ); + + void _setPending(String key, bool pending) { + final next = {...pendingSessionExportRepairs.value}; + pending ? next.add(key) : next.remove(key); + pendingSessionExportRepairs.value = next; + } + @override void didUpdateWidget(covariant SessionExportRepairAction oldWidget) { super.didUpdateWidget(oldWidget); @@ -74,7 +92,6 @@ class _SessionExportRepairActionState extends State { // visit the pane happens to show next. if (oldWidget.runId != widget.runId || oldWidget.sequence != widget.sequence) { - _sending = false; _refusal = null; } } @@ -86,10 +103,9 @@ class _SessionExportRepairActionState extends State { if (_sending || widget.running) return; final runId = widget.runId; final sequence = widget.sequence; - setState(() { - _sending = true; - _refusal = null; - }); + final key = _repairKey(runId, sequence); + _setPending(key, true); + setState(() => _refusal = null); try { final detail = await RunsRepository( widget.gateway, @@ -100,12 +116,17 @@ class _SessionExportRepairActionState extends State { if (!_owns(runId, sequence)) return; setState(() => _refusal = describeSessionExportRepairError(error)); } finally { - if (_owns(runId, sequence)) setState(() => _sending = false); + _setPending(key, false); } } @override - Widget build(BuildContext context) { + Widget build(BuildContext context) => ValueListenableBuilder>( + valueListenable: pendingSessionExportRepairs, + builder: (context, _, _) => _build(context), + ); + + Widget _build(BuildContext context) { final enabled = !_sending && !widget.running; final label = _sending ? sessionExportRepairBusyLabel diff --git a/app/test/features/runs/actions/session_export_repair_pending_test.dart b/app/test/features/runs/actions/session_export_repair_pending_test.dart new file mode 100644 index 00000000..e705c5ed --- /dev/null +++ b/app/test/features/runs/actions/session_export_repair_pending_test.dart @@ -0,0 +1,58 @@ +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/actions/session_export_repair_action.dart'; + +class _HeldGateway implements ApiGateway { + final requests = []; + final reply = Completer(); + + @override + Stream get frames => const Stream.empty(); + + @override + Future call(String type, [JsonMap fields = const {}]) { + requests.add({'type': type, ...fields}); + return reply.future; + } +} + +Widget _host(ApiGateway gateway) => MaterialApp( + home: Scaffold( + body: SessionExportRepairAction( + runId: 7, + sequence: 2, + gateway: gateway, + running: false, + onRepaired: (_) {}, + ), + ), +); + +void main() { + testWidgets('leaving and returning mid-repair cannot send it twice', ( + tester, + ) async { + final gateway = _HeldGateway(); + await tester.pumpWidget(_host(gateway)); + await tester.tap(find.text(sessionExportRepairLabel)); + await tester.pump(); + expect(gateway.requests, hasLength(1)); + + // The Session section unmounts and mounts again before the reply. + await tester.pumpWidget(const SizedBox()); + await tester.pumpWidget(_host(gateway)); + expect(find.text(sessionExportRepairBusyLabel), findsOneWidget); + await tester.tap(find.text(sessionExportRepairBusyLabel)); + await tester.pump(); + expect(gateway.requests, hasLength(1)); + + gateway.reply.complete({ + 'run_detail': {'id': 7}, + }); + await tester.pumpAndSettle(); + expect(find.text(sessionExportRepairLabel), findsOneWidget); + }); +}