Files
oott/frontend/test/widget/settings_config_dialog_test.dart
T
rzuastiandClaude Opus 4.8 67b94b89a1 Move backend config to a dialog; group settings into cards
Settings screen now splits responsibilities:
- Backend URL + API key move into a Test/Save popup dialog
  (backend_config_dialog.dart), reachable via a "Re-configure" action.
  Per M3 the Test button is left-aligned (neutral) with Cancel/Save
  grouped trailing. On first run the dialog auto-opens non-dismissible.
- The screen shows the connection read-only (URL plain, key masked with
  reveal) in a "Backend connection" card, and groups Theme + Push into an
  "App settings" card. Theme and push apply immediately, no Save button.
- Align all card titles to titleLarge across home/status/settings.

Adds a test seam (dioBuilderForTesting) so reconfigure keeps the mock Dio
in widget tests instead of issuing real requests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 18:15:10 -04:00

130 lines
4.1 KiB
Dart

import 'package:encrypter/encrypter/xor.dart';
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 '../helpers/backend_test_harness.dart';
import '../helpers/pump_app.dart';
void main() {
setUp(() async {
final adapter = await setUpBackendForTest(
prefs: {
'base_url': 'http://my.server/api',
'api_key': XOR().xorEncode('topsecret'),
'theme': 'catppuccin_mocha',
},
);
adapter.onGet(
'/config',
(server) => server.reply(200, {
'notifications': {'method': 'none'},
}),
);
await PrefUtil.setValue('base_url', 'http://my.server/api');
await PrefUtil.setValue('api_key', XOR().xorEncode('topsecret'));
});
// Opens the dialog from the settings screen via the Re-configure button.
Future<void> openDialog(WidgetTester tester) async {
await pumpScreen(tester, const Settings());
await tester.pump(const Duration(milliseconds: 10));
await tester.tap(find.widgetWithText(TextButton, 'Re-configure'));
await tester.pumpAndSettle();
}
testWidgets('prefills the dialog from stored preferences', (tester) async {
await openDialog(tester);
expect(find.text('Backend configuration'), findsOneWidget);
expect(
find.widgetWithText(TextFormField, 'http://my.server/api'),
findsOneWidget,
);
});
testWidgets('validates an empty base URL when testing the connection', (
tester,
) async {
await openDialog(tester);
await tester.enterText(find.byType(TextFormField).first, '');
await tester.tap(find.widgetWithText(FilledButton, 'Test'));
await tester.pump();
expect(find.text('The URL cannot be empty'), findsOneWidget);
});
testWidgets('editing the connection re-gates Save behind a Test', (
tester,
) async {
await openDialog(tester);
final saveButton = find.widgetWithText(FilledButton, 'Save');
expect(
tester.widget<FilledButton>(saveButton).onPressed,
isNotNull,
reason: 'enabled for the unmodified, prefilled form',
);
await tester.enterText(
find.byType(TextFormField).first,
'http://changed/api',
);
await tester.pump();
expect(
tester.widget<FilledButton>(saveButton).onPressed,
isNull,
reason: 'disabled until the new connection is tested',
);
expect(
find.textContaining('Test the connection before saving'),
findsOneWidget,
);
});
testWidgets('saving the prefilled configuration persists and closes', (
tester,
) async {
await openDialog(tester);
await tester.tap(find.widgetWithText(FilledButton, 'Save'));
// Settle the dialog's exit transition so it has fully left the tree.
await tester.pumpAndSettle();
expect(find.text('Settings saved successfully'), findsOneWidget);
expect(find.text('Backend configuration'), findsNothing);
expect(PrefUtil.getValue('base_url', ''), 'http://my.server/api');
// Unmount so the SnackBar's auto-dismiss timer doesn't leak.
await tearDownTree(tester);
});
testWidgets('cancelling discards edits without persisting', (tester) async {
await openDialog(tester);
await tester.enterText(
find.byType(TextFormField).first,
'http://changed/api',
);
await tester.tap(find.widgetWithText(TextButton, 'Cancel'));
await tester.pumpAndSettle();
expect(find.text('Backend configuration'), findsNothing);
expect(PrefUtil.getValue('base_url', ''), 'http://my.server/api');
});
testWidgets('first run auto-opens a non-dismissible dialog', (tester) async {
await PrefUtil.setValue('base_url', '');
await pumpScreen(tester, const Settings());
// Let the post-frame callback open the dialog.
await tester.pumpAndSettle();
expect(find.text('Backend configuration'), findsOneWidget);
// No Cancel on first run; the user must save a working configuration.
expect(find.widgetWithText(TextButton, 'Cancel'), findsNothing);
expect(find.widgetWithText(FilledButton, 'Save'), findsOneWidget);
});
}