wake-alarm/phone_app/test/room_proof_screen_test.dart
Krzysztof kuhy Rudnicki dc2d54668b Phase 4/3: close the ring screen on dismiss, and make NFC registerable
Two defects that each defeated the alarm in a different way:

1. Nothing finished AlarmActivity after a correct answer. Sound stopped and
   the alarm re-armed, but the dismiss screen stayed up over the lockscreen
   forever — back is disabled in both halves by design — and the live instance
   kept _inRoom=true with the solved answer still typed, so the next ring
   could reuse an already-passed gate. AlarmActivity now handles
   closeRingScreen; the shared AlarmChannel deliberately holds
   applicationContext and cannot finish an activity, hence the split handler.

2. Nothing could write roomProof.nfcTagId — there was no registration UI — so
   checkNfc always answered "no tag registered" and checkQr was never called.
   Functionally that left steps as the only satisfiable proof, which is
   exactly the state the decision rules out ("NFC or QR qualifies; steps alone
   does NOT") and removes the redundancy whose purpose is that one failing
   mechanism at 06:00 must never block dismissal. Added RoomProofScreen
   (reader mode from MainActivity, since registration needs an Activity) and
   linked it from Settings.

Also: StepCounter kept one listener instead of registering a fresh one per
ring and never unregistering, and the ring screen drops its NFC handler on
dispose.

New tests cover what had been dead code: a registered tag satisfies the proof
and reveals the challenge, a foreign tag does not, and a correct answer
invokes BOTH dismiss and closeRingScreen. The emulator cannot reach the
dismiss path at all (no step sensor, no NFC), which is why it is covered by
widget tests rather than claimed from the AVD run.

Documented ringNow/main_ring_probe.dart in AGENTS.md as a deliberate manual
test handle, the phone-side equivalent of wake-alarm.service.

flutter test: 69 pass. dart analyze lib/ test/: clean. pre-commit: passed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
2026-08-27 21:39:01 +02:00

81 lines
2.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wake_alarm_sync/screens/room_proof_screen.dart';
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
import 'package:wake_alarm_sync/services/room_proof_settings.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late List<String> calls;
late bool nfcStarts;
late MethodChannel channel;
setUp(() {
SharedPreferences.setMockInitialValues(<String, Object>{});
calls = <String>[];
nfcStarts = true;
channel = const MethodChannel(AlarmScheduler.channelName);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
calls.add(call.method);
if (call.method == 'startTagRegistration') return nfcStarts;
return null;
});
});
testWidgets('shows that no tag is registered on a fresh install', (
tester,
) async {
await tester.pumpWidget(const MaterialApp(home: RoomProofScreen()));
await tester.pumpAndSettle();
expect(find.textContaining('none registered'), findsOneWidget);
});
testWidgets('starting a scan puts the reader into waiting state', (
tester,
) async {
await tester.pumpWidget(const MaterialApp(home: RoomProofScreen()));
await tester.pumpAndSettle();
await tester.tap(find.text('Register NFC tag'));
await tester.pumpAndSettle();
expect(calls, contains('startTagRegistration'));
expect(find.textContaining('Tap the tag'), findsOneWidget);
});
testWidgets('says so when the device cannot scan, rather than waiting', (
tester,
) async {
nfcStarts = false;
await tester.pumpWidget(const MaterialApp(home: RoomProofScreen()));
await tester.pumpAndSettle();
await tester.tap(find.text('Register NFC tag'));
await tester.pumpAndSettle();
expect(find.textContaining('unavailable'), findsOneWidget);
});
testWidgets('saving a QR payload persists it', (tester) async {
await tester.pumpWidget(const MaterialApp(home: RoomProofScreen()));
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextField), 'kitchen');
await tester.tap(find.text('Save QR code'));
await tester.pumpAndSettle();
expect((await RoomProofSettings.load()).qrPayload, 'kitchen');
expect(find.text('QR code saved.'), findsOneWidget);
});
testWidgets('a registered tag is shown when reopening the screen', (
tester,
) async {
await RoomProofSettings.saveNfcTag('04a2b3c4');
await tester.pumpWidget(const MaterialApp(home: RoomProofScreen()));
await tester.pumpAndSettle();
expect(find.textContaining('04a2b3c4'), findsOneWidget);
});
}