diet-guard/app/test/screens/settings_kuchnia_test.dart
Krzysztof kuhy Rudnicki 5c9c1ff160 feat: add the catering login to settings, with an Android-only fetch
The credential **fields** appear on every platform; the **fetch button** only
where it can work. That split is the point, not an oversight: the panel sends
no CORS headers so a browser cannot call it, but the desktop app is where a
password is most likely to be typed, and the credential syncs — so hiding the
fields on web would mean the phone could only ever be set up by typing on the
phone. On web the button is replaced by a line saying why, and noting the
login still reaches the phone from there.

The section says the password is stored and synced in plain text rather than
implying otherwise.

The button disables itself while a fetch is in flight, so an impatient
double-tap cannot pay a second login plus three requests against a third
party. The result line says dishes were added to the **food bank** and that
the user still logs them when eaten — the wording carries the
delivered-is-not-eaten rule to the one place a user actually reads.

`settings_screen.dart` was already at 247 of its 250-line budget, so the
section owns its own leading divider and the fetch handler lives beside it;
the screen spends one line on it.

678 Dart tests.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QQpCiPaSbyRpwsLkH3dgsX
2026-08-23 08:33:52 +02:00

131 lines
4.7 KiB
Dart

/// The catering credential section of the settings screen.
///
/// The behaviour that needs pinning is the platform split, because it is the
/// one thing a reader would plausibly "simplify" the wrong way: the credential
/// **fields** show everywhere (the desktop app is where a password is most
/// likely to be typed, and it syncs from there to the phone), while the
/// **fetch button** is Android-only, because the panel sends no CORS headers
/// and a browser cannot call it at all.
library;
import 'dart:async';
import 'package:diet_guard_app/screens/settings_kuchnia.dart';
import 'package:diet_guard_app/services/document_store.dart';
import 'package:diet_guard_app/services/kuchnia_credential_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
/// In-memory store, so these tests need no plugin channel.
class _MemoryStore implements DocumentStore {
final Map<String, String> documents = {};
@override
Future<String?> read(String name) async => documents[name];
@override
Future<void> write(String name, String contents) async {
documents[name] = contents;
}
}
Widget _host({required bool canFetch, Future<String> Function()? onFetch}) =>
MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: KuchniaSettingsSection(canFetch: canFetch, onFetch: onFetch),
),
),
);
void main() {
setUp(() => KuchniaCredentialService.resetForTesting(store: _MemoryStore()));
tearDown(KuchniaCredentialService.resetForTesting);
testWidgets('shows both credential fields on every platform', (tester) async {
await tester.pumpWidget(_host(canFetch: false));
expect(find.text('Panel e-mail'), findsOneWidget);
expect(find.text('Panel password'), findsOneWidget);
});
testWidgets('offers the fetch button only where it can work', (tester) async {
await tester.pumpWidget(_host(canFetch: true));
expect(find.text("Fetch today's delivery"), findsOneWidget);
await tester.pumpWidget(_host(canFetch: false));
await tester.pumpAndSettle();
expect(find.text("Fetch today's delivery"), findsNothing);
});
testWidgets('explains the missing button rather than hiding it silently', (
tester,
) async {
await tester.pumpWidget(_host(canFetch: false));
expect(
find.textContaining('Android only'),
findsOneWidget,
reason: 'a browser user needs to know why there is no fetch button, and '
'that entering the login here still helps their phone',
);
});
testWidgets('saving persists the credential', (tester) async {
await tester.pumpWidget(_host(canFetch: true));
await tester.enterText(find.byType(TextField).first, 'me@example.com');
await tester.enterText(find.byType(TextField).last, 'hunter2');
await tester.tap(find.text('Save login'));
await tester.pumpAndSettle();
expect(KuchniaCredentialService.username, 'me@example.com');
expect(KuchniaCredentialService.password, 'hunter2');
expect(find.textContaining('Saved'), findsOneWidget);
});
testWidgets('the password is obscured until revealed', (tester) async {
await tester.pumpWidget(_host(canFetch: true));
TextField passwordField() =>
tester.widget<TextField>(find.byType(TextField).last);
expect(passwordField().obscureText, isTrue);
await tester.tap(find.byTooltip('Show password'));
await tester.pumpAndSettle();
expect(passwordField().obscureText, isFalse);
});
testWidgets('a fetch shows its result', (tester) async {
await tester.pumpWidget(
_host(canFetch: true, onFetch: () async => '3 dishes added'),
);
await tester.tap(find.text("Fetch today's delivery"));
await tester.pumpAndSettle();
expect(find.text('3 dishes added'), findsOneWidget);
});
testWidgets('a failed fetch shows the reason, not a crash', (tester) async {
await tester.pumpWidget(
_host(canFetch: true, onFetch: () async => 'catering login rejected'),
);
await tester.tap(find.text("Fetch today's delivery"));
await tester.pumpAndSettle();
expect(find.text('catering login rejected'), findsOneWidget);
});
testWidgets('the fetch button is disabled while a fetch is in flight', (
tester,
) async {
// Otherwise an impatient double-tap pays a second login plus three
// requests against a third party.
final gate = Completer<String>();
await tester.pumpWidget(_host(canFetch: true, onFetch: () => gate.future));
await tester.tap(find.text("Fetch today's delivery"));
await tester.pump();
expect(find.text('Fetching…'), findsOneWidget);
final button = tester.widget<OutlinedButton>(find.byType(OutlinedButton));
expect(button.onPressed, isNull);
gate.complete('done');
await tester.pumpAndSettle();
});
}