Skip to content

Commit 1524fc0

Browse files
committed
fix: creating a report under a taken name silently destroyed the old one
mergeReport replaces same-name blocks — that is what makes editing work — so "Create" with the name of an existing custom report quietly overwrote a report the user had built, with no warning that anything was lost. The builder now refuses: "A report named X already exists — pick another name, or edit that report instead." Shadowing a *default* name stays allowed, because that is how Taskwarrior overrides built-ins, and the sheet already shows a hint for it. Also, if a rename ever reaches _save, the block stored under the old name is deleted after the new one is written — otherwise the report comes back duplicated under both names. Today this is defensive only: the name field is disabled in edit mode (enabled: !_isEditing), so the UI cannot produce a rename. If that restriction is ever lifted, the cleanup is already in place — without it, lifting the restriction would have quietly introduced the duplication bug. Tested: two new widget tests (430 Dart tests pass) — a taken custom name is refused with the explanation, and re-saving an edited report under its own name is not treated as a clobber. Verified on device: creating "e2e-report" a second time shows the refusal inline and leaves the stored report untouched.
1 parent 4d9c5af commit 1524fc0

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎lib/app/modules/report_engine/views/report_builder_sheet.dart‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,42 @@ class _ReportBuilderSheetState extends State<ReportBuilderSheet> {
144144
isCustom: true,
145145
);
146146

147+
final String newName = draft.name;
148+
final String? oldName = widget.existing?.name;
149+
150+
// Refuse to silently clobber a different custom report. mergeReport
151+
// replaces same-name blocks, so without this check "Create" with a taken
152+
// name quietly destroyed the report already stored under it. Shadowing a
153+
// *default* name stays allowed — that is how Taskwarrior overrides work,
154+
// and the sheet already shows a hint for it.
155+
final bool clobbersOtherCustom = newName != oldName &&
156+
widget.controller.reports
157+
.any((r) => r.isCustom && r.name == newName);
158+
if (clobbersOtherCustom) {
159+
setState(() => _error =
160+
'A report named "$newName" already exists — pick another name, '
161+
'or edit that report instead.');
162+
return;
163+
}
164+
147165
final String? error = await widget.controller.saveReport(draft);
148166
if (error != null) {
149167
setState(() => _error = error);
150168
return;
151169
}
170+
171+
// A rename writes a block under the new name; the old block must go too,
172+
// or the report comes back duplicated under both names.
173+
if (oldName != null && oldName != newName) {
174+
final String? deleteError =
175+
await widget.controller.deleteReport(oldName);
176+
if (deleteError != null) {
177+
setState(() => _error =
178+
'Saved as "$newName", but the old "$oldName" could not be '
179+
'removed: $deleteError');
180+
return;
181+
}
182+
}
152183
if (mounted) Navigator.of(context).pop(true);
153184
}
154185

‎test/modules/report_engine/report_builder_sheet_test.dart‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,55 @@ void main() {
107107
expect(chip(tester, '+READY').onPressed, isNotNull);
108108
});
109109

110+
testWidgets('creating a report under a taken custom name is refused',
111+
(tester) async {
112+
final ReportEngineController controller =
113+
Get.find<ReportEngineController>();
114+
controller.reports.assignAll([
115+
ReportDefinition(
116+
name: 'mine',
117+
description: 'mine',
118+
filterExpression: '+OVERDUE',
119+
sortCriteria: SortCriterion.parseList('due+'),
120+
columns: ColumnSpec.parseList('id,description'),
121+
isCustom: true,
122+
),
123+
]);
124+
await pump(tester);
125+
126+
await tester.enterText(find.byType(TextField).at(0), 'mine');
127+
await tester.pumpAndSettle();
128+
await tester.tap(find.text('Create'));
129+
await tester.pumpAndSettle();
130+
131+
// Refused with an explanation instead of silently replacing the stored
132+
// report (mergeReport clobbers same-name blocks).
133+
expect(find.textContaining('already exists'), findsOneWidget);
134+
});
135+
136+
testWidgets('re-saving an edited report under its own name is not refused',
137+
(tester) async {
138+
final ReportDefinition mine = ReportDefinition(
139+
name: 'mine',
140+
description: 'mine',
141+
filterExpression: '+OVERDUE',
142+
sortCriteria: SortCriterion.parseList('due+'),
143+
columns: ColumnSpec.parseList('id,description'),
144+
isCustom: true,
145+
);
146+
final ReportEngineController controller =
147+
Get.find<ReportEngineController>();
148+
controller.reports.assignAll([mine]);
149+
await pump(tester, existing: mine);
150+
151+
await tester.tap(find.text('Save'));
152+
await tester.pumpAndSettle();
153+
154+
// Its own name is the normal update path, not a clobber. (The actual file
155+
// write fails in the test environment; the point is which error we get.)
156+
expect(find.textContaining('already exists'), findsNothing);
157+
});
158+
110159
testWidgets('editing an existing report opens with its tokens disabled',
111160
(tester) async {
112161
await pump(

0 commit comments

Comments
 (0)