feat(app): add native rename-run action

Native Material/Cupertino prompt seeded from the run's stored title. Only
Save sends set_run_title {run_id, title} with the text untrimmed; typing and
Cancel send nothing, refusals stay verbatim with the draft kept, and only the
accepted run_detail is delivered once.
This commit is contained in:
2026-10-21 15:14:44 +01:00
parent 0a00526692
commit f2643a0ff4
7 changed files with 1006 additions and 0 deletions
@@ -0,0 +1,244 @@
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/core/transport/connection_state.dart';
import 'package:pipelines_app/features/runs/actions/title_action.dart';
import 'package:pipelines_app/features/runs/actions/title_wording.dart';
import 'title_test_support.dart';
String _materialFieldText(WidgetTester tester) =>
tester.widget<TextField>(find.byKey(runTitleFieldKey)).controller!.text;
String _cupertinoFieldText(WidgetTester tester) => tester
.widget<CupertinoTextField>(find.byKey(runTitleFieldKey))
.controller!
.text;
void main() {
testWidgets(
'material rename starts from the stored title, typing sends nothing, '
'Cancel sends nothing and Save sends the exact text',
(tester) async {
final accepted = renamedRun(title: 'Release build');
final gateway = FakeTitleGateway(
(type, fields) async => {'run_detail': accepted},
);
final changed = <JsonMap>[];
await tester.pumpWidget(
titleApp(
TitleHost(gateway: gateway, initial: titledRun(), changed: changed),
cupertino: false,
),
);
expect(find.byKey(runTitleActionKey), findsOneWidget);
expect(find.byType(OutlinedButton), findsOneWidget);
expect(gateway.calls, isEmpty);
await tester.tap(find.byKey(runTitleActionKey));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text(runTitleDialogTitle), findsWidgets);
expect(find.text(runTitleFieldLabel), findsOneWidget);
expect(_materialFieldText(tester), 'Nightly build');
await tester.enterText(
find.byKey(runTitleFieldKey),
' 发布 🚀 — nightly ',
);
await tester.pump();
expect(gateway.calls, isEmpty);
await tester.tap(find.byKey(runTitleCancelKey));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(gateway.calls, isEmpty);
expect(changed, isEmpty);
expect(
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
'Nightly build',
);
await tester.tap(find.byKey(runTitleActionKey));
await tester.pumpAndSettle();
expect(_materialFieldText(tester), 'Nightly build');
await tester.enterText(
find.byKey(runTitleFieldKey),
' 发布 🚀 — nightly ',
);
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': ' 发布 🚀 — nightly ',
});
expect(changed.single, equals(accepted));
expect(find.byType(AlertDialog), findsNothing);
expect(
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
'Release build',
);
},
);
testWidgets('cupertino rename is native and sends only on Save', (
tester,
) async {
final accepted = renamedRun(title: 'Release build');
final gateway = FakeTitleGateway(
(type, fields) async => {'run_detail': accepted},
);
final changed = <JsonMap>[];
await tester.pumpWidget(
titleApp(
TitleHost(gateway: gateway, initial: titledRun(), changed: changed),
cupertino: true,
),
);
expect(find.byKey(runTitleActionKey), findsOneWidget);
expect(find.byType(CupertinoButton), findsOneWidget);
expect(find.byType(OutlinedButton), findsNothing);
expect(gateway.calls, isEmpty);
await tester.tap(find.byKey(runTitleActionKey));
await tester.pumpAndSettle();
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
expect(_cupertinoFieldText(tester), 'Nightly build');
await tester.enterText(find.byKey(runTitleFieldKey), 'Typed only');
await tester.pump();
expect(gateway.calls, isEmpty);
await tester.tap(find.byKey(runTitleCancelKey));
await tester.pumpAndSettle();
expect(find.byType(CupertinoAlertDialog), findsNothing);
expect(gateway.calls, isEmpty);
await tester.tap(find.byKey(runTitleActionKey));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(runTitleFieldKey), 'Release build');
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': 'Release build',
});
expect(changed.single, equals(accepted));
expect(find.byType(CupertinoAlertDialog), findsNothing);
});
testWidgets('a blank title is sent exactly and the server refusal is shown', (
tester,
) async {
const refusal = 'title must not be blank';
final gateway = FakeTitleGateway(
(type, fields) async => throw const ProtocolError(refusal),
);
final changed = <JsonMap>[];
await tester.pumpWidget(
titleApp(
TitleHost(gateway: gateway, initial: titledRun(), changed: changed),
cupertino: false,
),
);
await tester.tap(find.byKey(runTitleActionKey));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(runTitleFieldKey), '');
await tester.tap(find.byKey(runTitleSaveKey));
await tester.pumpAndSettle();
expect(gateway.calls.single.fields, {'run_id': 7, 'title': ''});
expect(find.text(refusal), findsOneWidget);
expect(find.byKey(runTitleErrorKey), findsOneWidget);
expect(_materialFieldText(tester), '');
expect(find.byType(AlertDialog), findsOneWidget);
expect(changed, isEmpty);
expect(
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
'Nightly build',
);
expect(
tester.widget<FilledButton>(find.byKey(runTitleSaveKey)).onPressed,
isNotNull,
);
});
testWidgets('the prompt is seeded from the stored title, never the prompt', (
tester,
) async {
final gateway = FakeTitleGateway(
(type, fields) async => {'run_detail': titledRun()},
);
final changed = <JsonMap>[];
await tester.pumpWidget(
titleApp(
TitleHost(
gateway: gateway,
initial: titledRun(
title: 'Stored title',
prompt: 'prompt fallback must not appear',
),
changed: changed,
),
cupertino: false,
),
);
await tester.tap(find.byKey(runTitleActionKey));
await tester.pumpAndSettle();
expect(_materialFieldText(tester), 'Stored title');
expect(find.text('prompt fallback must not appear'), findsNothing);
await tester.tap(find.byKey(runTitleCancelKey));
await tester.pumpAndSettle();
await tester.pumpWidget(
titleApp(
TitleHost(
gateway: gateway,
initial: titledRun(title: null, prompt: 'prompt is not a title'),
changed: changed,
),
cupertino: false,
),
);
await tester.tap(find.byKey(runTitleActionKey));
await tester.pumpAndSettle();
expect(_materialFieldText(tester), '');
expect(find.text('prompt is not a title'), findsNothing);
await tester.tap(find.byKey(runTitleCancelKey));
await tester.pumpAndSettle();
expect(gateway.calls, isEmpty);
expect(changed, isEmpty);
});
testWidgets('a malformed run has no rename entry point', (tester) async {
final gateway = FakeTitleGateway(
(type, fields) async => {'run_detail': titledRun()},
);
await tester.pumpWidget(
titleApp(
TitleHost(
gateway: gateway,
initial: {'title': 'No id'},
changed: <JsonMap>[],
),
cupertino: false,
),
);
expect(find.byKey(runTitleActionKey), findsNothing);
expect(gateway.calls, isEmpty);
expect(runTitleId({'title': 'No id'}), isNull);
expect(storedRunTitle({'title': 'Kept'}), 'Kept');
expect(storedRunTitle({}), '');
});
}
@@ -0,0 +1,243 @@
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/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, {
bool cupertino = false,
double textScale = 1,
JsonMap? run,
}) async {
await tester.pumpWidget(
titleApp(
TitleHost(
gateway: gateway,
initial: run ?? titledRun(),
changed: changed,
),
cupertino: cupertino,
textScale: textScale,
),
);
}
Future<void> _open(WidgetTester tester) async {
await tester.tap(find.byKey(runTitleActionKey));
await tester.pumpAndSettle();
}
String _fieldText(WidgetTester tester, {required bool cupertino}) {
if (cupertino) {
return tester
.widget<CupertinoTextField>(find.byKey(runTitleFieldKey))
.controller!
.text;
}
return 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, cupertino: false), ' 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, cupertino: true);
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, cupertino: true), 'Unsent title');
expect(find.byType(CupertinoAlertDialog), 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,
),
cupertino: false,
),
);
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';
for (final cupertino in [false, true]) {
final gateway = FakeTitleGateway(
(type, fields) async => throw const ProtocolError(refusal),
);
await _pump(
tester,
gateway,
<JsonMap>[],
cupertino: cupertino,
textScale: 2,
);
await _open(tester);
expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino');
expect(
cupertino
? find.byType(CupertinoAlertDialog)
: 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, reason: 'cupertino=$cupertino');
expect(find.text(refusal), findsOneWidget);
expect(
_fieldText(tester, cupertino: cupertino),
'A deliberately long title that must scroll rather than overflow',
);
await tester.tap(find.byKey(runTitleCancelKey));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(find.byType(CupertinoAlertDialog), findsNothing);
}
});
}
@@ -0,0 +1,110 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/actions/title_action.dart';
typedef TitleHandler = Future<JsonMap> Function(String type, JsonMap fields);
/// A fake gateway that records every request and never owns a socket.
class FakeTitleGateway implements ApiGateway {
FakeTitleGateway(this.handler);
final TitleHandler handler;
final List<({String type, JsonMap fields})> calls = [];
@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);
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
JsonMap titledRun({
num id = 7,
String? title = 'Nightly build',
String status = 'succeeded',
String prompt = 'the submitted prompt',
}) => {
'id': id,
'title': title,
'status': status,
'input': {'prompt': prompt},
};
JsonMap renamedRun({num id = 7, String title = 'Release build'}) =>
titledRun(id: id, title: title, status: 'succeeded');
/// Owns the run like a run-detail screen would, so the callback's returned
/// server detail is what the action renders next. Re-pumping with a different
/// initial run updates the action in place, as a refreshed detail does.
class TitleHost extends StatefulWidget {
const TitleHost({
super.key,
required this.gateway,
required this.initial,
required this.changed,
});
final FakeTitleGateway gateway;
final JsonMap initial;
final List<JsonMap> changed;
@override
State<TitleHost> createState() => _TitleHostState();
}
class _TitleHostState extends State<TitleHost> {
late JsonMap _run = widget.initial;
@override
void didUpdateWidget(covariant TitleHost oldWidget) {
super.didUpdateWidget(oldWidget);
if (!identical(oldWidget.initial, widget.initial)) {
_run = widget.initial;
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('${_run['title'] ?? ''}', key: titleHostTitleKey),
RunTitleAction(
run: _run,
gateway: widget.gateway,
onRunChanged: (run) {
widget.changed.add(run);
setState(() => _run = run);
},
),
],
);
}
}
const titleHostTitleKey = ValueKey<String>('rename-host-title');
Widget titleApp(Widget child, {required bool cupertino, double textScale = 1}) {
Widget media(BuildContext context, Widget? app) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: app!,
);
final body = Center(child: child);
if (cupertino) {
return CupertinoApp(
builder: media,
home: CupertinoPageScaffold(child: body),
);
}
return MaterialApp(
builder: media,
home: Scaffold(body: body),
);
}