Add permanent device deletion and refine frontend UI/snackbars

Backend:
- Add db::devices::delete to erase a device and its events atomically
- Expose DELETE /api/devices/{mac}/permanently, wired to OpenAPI
- Cover the new db method and endpoint with tests

Frontend:
- Delete action for not-registered devices (detail screen + list row)
  and an opt-in "permanently delete" checkbox in the Forget dialog
- Navigate to the devices list after deleting from the detail screen
- Refine button emphasis to M3: single filled primary, error-colored
  text buttons for destructive actions, Test demoted to filled-tonal
- Flash the backend-config Test button red on a failed connection test
- Render snackbars through a top-level ScaffoldMessenger host so they
  show above dialogs; keep the built-in SnackBar (with an Overlay host)

Docs:
- CLAUDE.md: rustfmt edition 2024, don't revert formatter-only changes,
  prefer built-in Flutter components, follow existing patterns + M3

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-06-12 09:49:11 -04:00
co-authored by Claude Opus 4.8
parent 2541d0989c
commit 587e097291
20 changed files with 1021 additions and 177 deletions
@@ -0,0 +1,170 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:frontend/devices/device_list.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import '../helpers/backend_test_harness.dart';
import '../helpers/fixtures.dart';
import '../helpers/pump_app.dart';
void main() {
late DioAdapter adapter;
setUp(() async {
adapter = await setUpBackendForTest();
});
Future<void> openRowMenu(WidgetTester tester) async {
await tester.tap(find.byIcon(Icons.more_vert).first);
await tester.pumpAndSettle();
}
testWidgets(
'deleting a not-registered device from the list calls the delete endpoint',
(tester) async {
adapter.onGet(
'/devices',
(server) => server.reply(
200,
pagedListJson([
deviceJson(macAddress: 'aa:bb:cc:dd:ee:01', isRegistered: false),
]),
),
);
var deleteCalled = false;
adapter.onDelete('/devices/aa:bb:cc:dd:ee:01/permanently', (server) {
deleteCalled = true;
server.reply(200, null);
});
await pumpScreen(tester, const DeviceList());
await pumpUntilFound(tester, find.byIcon(Icons.more_vert));
await openRowMenu(tester);
expect(find.text('Delete'), findsOneWidget);
await tester.tap(find.text('Delete'));
await tester.pumpAndSettle();
// The confirmation dialog warns the deletion is permanent.
expect(find.textContaining('cannot be undone'), findsOneWidget);
await tester.tap(find.widgetWithText(TextButton, 'Delete'));
await tester.pumpAndSettle();
expect(deleteCalled, isTrue);
expect(find.text('Device deleted'), findsOneWidget);
await tearDownTree(tester);
},
);
testWidgets('not-registered devices do not expose Forget', (tester) async {
adapter.onGet(
'/devices',
(server) => server.reply(
200,
pagedListJson([
deviceJson(macAddress: 'aa:bb:cc:dd:ee:02', isRegistered: false),
]),
),
);
await pumpScreen(tester, const DeviceList());
await pumpUntilFound(tester, find.byIcon(Icons.more_vert));
await openRowMenu(tester);
expect(find.text('Forget'), findsNothing);
expect(find.text('Delete'), findsOneWidget);
await tearDownTree(tester);
});
testWidgets(
'forget dialog deletes the device when the also-delete box is ticked',
(tester) async {
adapter.onGet(
'/devices',
(server) => server.reply(
200,
pagedListJson([
deviceJson(
macAddress: 'aa:bb:cc:dd:ee:03',
isRegistered: true,
owner: 'alice',
),
]),
),
);
var deleteCalled = false;
// Only the permanent-delete route is mocked: had the dialog taken the
// plain "forget" path instead, the unmocked DELETE would surface an error
// snackbar rather than the success one asserted below.
adapter.onDelete('/devices/aa:bb:cc:dd:ee:03/permanently', (server) {
deleteCalled = true;
server.reply(200, null);
});
await pumpScreen(tester, const DeviceList());
await pumpUntilFound(tester, find.byIcon(Icons.more_vert));
await openRowMenu(tester);
await tester.tap(find.text('Forget'));
await tester.pumpAndSettle();
// Checkbox is unchecked by default, so no warning is shown yet.
expect(find.textContaining('cannot be undone'), findsNothing);
await tester.tap(find.byType(CheckboxListTile));
await tester.pumpAndSettle();
// Ticking it reveals the permanent-deletion warning.
expect(find.textContaining('cannot be undone'), findsOneWidget);
await tester.tap(find.widgetWithText(TextButton, 'Delete'));
await tester.pumpAndSettle();
expect(deleteCalled, isTrue);
expect(find.text('Device deleted'), findsOneWidget);
await tearDownTree(tester);
},
);
testWidgets('forget dialog unregisters when the box is left unticked', (
tester,
) async {
adapter.onGet(
'/devices',
(server) => server.reply(
200,
pagedListJson([
deviceJson(
macAddress: 'aa:bb:cc:dd:ee:04',
isRegistered: true,
owner: 'bob',
),
]),
),
);
var forgetCalled = false;
adapter.onDelete('/devices/aa:bb:cc:dd:ee:04', (server) {
forgetCalled = true;
server.reply(200, null);
});
await pumpScreen(tester, const DeviceList());
await pumpUntilFound(tester, find.byIcon(Icons.more_vert));
await openRowMenu(tester);
await tester.tap(find.text('Forget'));
await tester.pumpAndSettle();
await tester.tap(find.widgetWithText(TextButton, 'Forget'));
await tester.pumpAndSettle();
expect(forgetCalled, isTrue);
expect(find.text('Device forgotten'), findsOneWidget);
await tearDownTree(tester);
});
}
@@ -0,0 +1,142 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:frontend/devices/device_detail.dart';
import 'package:frontend/main.dart';
import 'package:frontend/theme/catppuccin_mocha_theme.dart';
import 'package:go_router/go_router.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import 'package:provider/provider.dart';
import '../helpers/backend_test_harness.dart';
import '../helpers/fixtures.dart';
import '../helpers/pump_app.dart';
void main() {
late DioAdapter adapter;
setUp(() async {
adapter = await setUpBackendForTest();
});
// Pumps the device-detail page inside a minimal router so `context.go` has a
// real GoRouter to navigate with. The `/devices` destination renders a marker
// we can assert against once a deletion navigates away from the detail page.
Future<void> pumpDetail(WidgetTester tester, String mac) async {
tester.view.physicalSize = const Size(900, 1600);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final router = GoRouter(
initialLocation: '/devices/$mac',
routes: [
GoRoute(
path: '/devices',
builder: (_, _) =>
const Scaffold(body: Center(child: Text('DEVICES LIST'))),
),
GoRoute(
path: '/devices/:mac',
builder: (_, state) => Scaffold(
body: DeviceDetail(macAddress: state.pathParameters['mac']!),
),
),
],
);
await tester.pumpWidget(
ChangeNotifierProvider(
create: (_) => AppState(),
child: MaterialApp.router(
theme: catppuccinMochaDarkTheme,
routerConfig: router,
),
),
);
}
testWidgets('deleting a not-registered device navigates to the list', (
tester,
) async {
const mac = 'aa:bb:cc:dd:ee:01';
adapter.onGet(
'/devices/$mac',
(server) =>
server.reply(200, deviceJson(macAddress: mac, isRegistered: false)),
);
adapter.onGet(
'/devices/$mac/events',
(server) => server.reply(200, const []),
);
var deleteCalled = false;
adapter.onDelete('/devices/$mac/permanently', (server) {
deleteCalled = true;
server.reply(200, null);
});
await pumpDetail(tester, mac);
await pumpUntilFound(tester, find.widgetWithText(TextButton, 'Delete'));
await tester.tap(find.widgetWithText(TextButton, 'Delete'));
await tester.pumpAndSettle();
await tester.tap(
find.descendant(
of: find.byType(AlertDialog),
matching: find.widgetWithText(TextButton, 'Delete'),
),
);
await pumpUntilFound(tester, find.text('DEVICES LIST'));
expect(deleteCalled, isTrue);
expect(find.text('DEVICES LIST'), findsOneWidget);
await tearDownTree(tester);
});
testWidgets('forgetting with the delete box ticked navigates to the list', (
tester,
) async {
const mac = 'aa:bb:cc:dd:ee:02';
adapter.onGet(
'/devices/$mac',
(server) => server.reply(
200,
deviceJson(macAddress: mac, isRegistered: true, owner: 'alice'),
),
);
adapter.onGet(
'/devices/$mac/events',
(server) => server.reply(200, const []),
);
var deleteCalled = false;
adapter.onDelete('/devices/$mac/permanently', (server) {
deleteCalled = true;
server.reply(200, null);
});
await pumpDetail(tester, mac);
await pumpUntilFound(
tester,
find.widgetWithText(TextButton, 'Forget Device'),
);
await tester.tap(find.widgetWithText(TextButton, 'Forget Device'));
await tester.pumpAndSettle();
await tester.tap(find.byType(CheckboxListTile));
await tester.pumpAndSettle();
await tester.tap(
find.descendant(
of: find.byType(AlertDialog),
matching: find.widgetWithText(TextButton, 'Delete'),
),
);
await pumpUntilFound(tester, find.text('DEVICES LIST'));
expect(deleteCalled, isTrue);
expect(find.text('DEVICES LIST'), findsOneWidget);
await tearDownTree(tester);
});
}
@@ -3,13 +3,16 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:frontend/settings/settings.dart';
import 'package:frontend/utils/pref_utils.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import '../helpers/backend_test_harness.dart';
import '../helpers/pump_app.dart';
void main() {
late DioAdapter adapter;
setUp(() async {
final adapter = await setUpBackendForTest(
adapter = await setUpBackendForTest(
prefs: {
'base_url': 'http://my.server/api',
'api_key': XOR().xorEncode('topsecret'),
@@ -56,6 +59,47 @@ void main() {
expect(find.text('The URL cannot be empty'), findsOneWidget);
});
// Locates the error icon inside the Test button (its failure-flash state).
Finder testButtonErrorIcon() => find.descendant(
of: find.widgetWithText(FilledButton, 'Test'),
matching: find.byIcon(Icons.error_outline),
);
testWidgets('a failed test flashes the Test button red, then reverts', (
tester,
) async {
// The test binding fails outbound HTTP, so the connection test fails
// without any explicit stubbing.
await openDialog(tester);
await tester.tap(find.widgetWithText(FilledButton, 'Test'));
// Let the request resolve and the snackbar's entrance animation run.
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
// The button flashes its failure state...
expect(testButtonErrorIcon(), findsOneWidget);
// ...and the error snackbar is shown *while the dialog is still open*. (In
// the real app it renders above the dialog via the top-level messenger; the
// bare test harness falls back to the page messenger, but co-presence still
// proves the snackbar fires on failure.)
expect(find.text('Backend configuration'), findsOneWidget);
expect(find.byType(SnackBar), findsOneWidget);
// After the flash duration the button reverts to its idle look.
await tester.pump(const Duration(milliseconds: 1600));
expect(testButtonErrorIcon(), findsNothing);
expect(
find.descendant(
of: find.widgetWithText(FilledButton, 'Test'),
matching: find.byIcon(Icons.play_arrow),
),
findsOneWidget,
);
await tearDownTree(tester);
});
testWidgets('editing the connection re-gates Save behind a Test', (
tester,
) async {
@@ -0,0 +1,66 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:frontend/theme/catppuccin_mocha_theme.dart';
import 'package:frontend/utils/ui_snackbars.dart';
import '../helpers/pump_app.dart';
void main() {
// Builds an app wired exactly like main.dart: the router/Navigator sits under
// the snackbar host so snackbars render above dialogs.
Future<BuildContext> pumpHostedApp(WidgetTester tester) async {
late BuildContext pageContext;
await tester.pumpWidget(
MaterialApp(
theme: catppuccinMochaDarkTheme,
builder: buildSnackbarHost,
home: Builder(
builder: (context) {
pageContext = context;
return const Scaffold(body: SizedBox.expand());
},
),
),
);
return pageContext;
}
testWidgets('snackbar shows through the host above an open dialog', (
tester,
) async {
final context = await pumpHostedApp(tester);
// Open a dialog so a modal barrier is on screen.
showDialog<void>(
context: context,
builder: (_) => const AlertDialog(title: Text('A dialog')),
);
await tester.pumpAndSettle();
expect(find.text('A dialog'), findsOneWidget);
UISnackbars.showSuccess(context, 'Hello there');
await tester.pumpAndSettle();
expect(find.byType(SnackBar), findsOneWidget);
expect(find.text('Hello there'), findsOneWidget);
await tearDownTree(tester);
});
testWidgets('the snackbar close-icon tooltip does not crash for lack of an '
'overlay', (tester) async {
final context = await pumpHostedApp(tester);
UISnackbars.showError(context, 'Boom');
await tester.pumpAndSettle();
// Long-pressing the close icon shows its tooltip, which needs an Overlay
// ancestor — the previous host had none and threw "No Overlay widget found".
await tester.longPress(find.byIcon(Icons.close));
await tester.pump(const Duration(seconds: 1));
expect(tester.takeException(), isNull);
await tearDownTree(tester);
});
}