Files
pipelines/app/test/features/runs/actions/title_resilience_test.dart
T
alan 364fd5b538 Remove Cupertino run action branches
Keep the Material-only run action behavior while preserving public Cupertino parameters for callers that will be cleaned up separately. Narrow the related tests to the supported Material path.
2026-11-11 04:18:26 +00:00

218 lines
6.7 KiB
Dart

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/core/transport/connection_state.dart';
import 'package:pipelines_app/features/runs/actions/title_wording.dart';
import 'title_test_support.dart';
Future<void> _pump(
WidgetTester tester,
FakeTitleGateway gateway,
List<JsonMap> changed, {
double textScale = 1,
JsonMap? run,
}) async {
await tester.pumpWidget(
titleApp(
TitleHost(
gateway: gateway,
initial: run ?? titledRun(),
changed: changed,
),
textScale: textScale,
),
);
}
Future<void> _open(WidgetTester tester) async {
await tester.tap(find.byKey(runTitleActionKey));
await tester.pumpAndSettle();
}
String _fieldText(WidgetTester tester) => tester
.widget<TextField>(find.byKey(runTitleFieldKey))
.controller!
.text;
void main() {
testWidgets('a failed save keeps the prompt open with the draft intact', (
tester,
) async {
const refusal = 'rename refused: the run is locked';
final gateway = FakeTitleGateway(
(type, fields) async => throw const ProtocolError(refusal),
);
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _open(tester);
await tester.enterText(find.byKey(runTitleFieldKey), ' Renamed draft ');
await tester.tap(find.byKey(runTitleSaveKey));
await tester.pumpAndSettle();
expect(gateway.calls.map((call) => call.type), ['set_run_title']);
expect(gateway.calls.single.fields, {
'run_id': 7,
'title': ' Renamed draft ',
});
expect(find.text(refusal), findsOneWidget);
expect(find.byKey(runTitleErrorKey), findsOneWidget);
expect(_fieldText(tester), ' Renamed draft ');
expect(find.byType(AlertDialog), findsOneWidget);
expect(changed, isEmpty);
expect(
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
'Nightly build',
);
});
testWidgets('a transport failure keeps the server words and the draft', (
tester,
) async {
final gateway = FakeTitleGateway(
(type, fields) async => throw const NotConnectedError(),
);
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _open(tester);
await tester.enterText(find.byKey(runTitleFieldKey), 'Unsent title');
await tester.tap(find.byKey(runTitleSaveKey));
await tester.pumpAndSettle();
expect(find.byKey(runTitleErrorKey), findsOneWidget);
expect(_fieldText(tester), 'Unsent title');
expect(find.byType(AlertDialog), findsOneWidget);
expect(changed, isEmpty);
});
testWidgets('Save cannot send twice while the rename is in flight', (
tester,
) async {
final reply = Completer<JsonMap>();
final gateway = FakeTitleGateway((type, fields) => reply.future);
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _open(tester);
await tester.enterText(find.byKey(runTitleFieldKey), 'One title');
final save = find.byKey(runTitleSaveKey);
await tester.tap(save);
await tester.tap(save);
await tester.pump();
expect(
gateway.calls.where((call) => call.type == 'set_run_title'),
hasLength(1),
);
expect(tester.widget<FilledButton>(save).onPressed, isNull);
expect(
tester.widget<TextButton>(find.byKey(runTitleCancelKey)).onPressed,
isNull,
);
expect(find.byType(AlertDialog), findsOneWidget);
reply.complete({'run_detail': renamedRun(title: 'One title')});
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(changed, hasLength(1));
expect(changed.single['title'], 'One title');
});
testWidgets('a late reply after disposal is ignored safely', (tester) async {
final reply = Completer<JsonMap>();
final gateway = FakeTitleGateway((type, fields) => reply.future);
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _open(tester);
await tester.tap(find.byKey(runTitleSaveKey));
await tester.pump();
expect(gateway.calls, hasLength(1));
await tester.pumpWidget(const SizedBox());
reply.complete({'run_detail': renamedRun()});
await tester.pump();
expect(tester.takeException(), isNull);
expect(changed, isEmpty);
});
testWidgets('a reply for a run the widget no longer owns is not delivered', (
tester,
) async {
final reply = Completer<JsonMap>();
final gateway = FakeTitleGateway((type, fields) => reply.future);
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _open(tester);
await tester.enterText(find.byKey(runTitleFieldKey), 'Run seven title');
await tester.tap(find.byKey(runTitleSaveKey));
await tester.pump();
await tester.pumpWidget(
titleApp(
TitleHost(
gateway: gateway,
initial: titledRun(id: 8, title: 'Another run'),
changed: changed,
),
),
);
await tester.pump();
reply.complete({'run_detail': renamedRun(title: 'Run seven title')});
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(gateway.calls.single.fields, {
'run_id': 7,
'title': 'Run seven title',
});
expect(changed, isEmpty);
expect(
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
'Another run',
);
});
testWidgets('both prompts fit 2x text at a narrow width', (tester) async {
tester.view.physicalSize = const Size(320, 480);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
const refusal = 'the title is too long for this run; shorten it';
final gateway = FakeTitleGateway(
(type, fields) async => throw const ProtocolError(refusal),
);
await _pump(tester, gateway, <JsonMap>[], textScale: 2);
await _open(tester);
expect(tester.takeException(), isNull);
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text(runTitleFieldLabel), findsOneWidget);
expect(gateway.calls, isEmpty);
await tester.enterText(
find.byKey(runTitleFieldKey),
'A deliberately long title that must scroll rather than overflow',
);
await tester.tap(find.byKey(runTitleSaveKey));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.text(refusal), findsOneWidget);
expect(
_fieldText(tester),
'A deliberately long title that must scroll rather than overflow',
);
await tester.tap(find.byKey(runTitleCancelKey));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
});
}