wake-alarm/phone_app/test/services/room_proof_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

102 lines
3.7 KiB
Dart

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
import 'package:wake_alarm_sync/services/room_proof.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late AlarmScheduler scheduler;
var steps = 0;
setUp(() {
const channel = MethodChannel(AlarmScheduler.channelName);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
if (call.method == 'stepCount') return steps;
return null;
});
scheduler = AlarmScheduler(channel: channel);
});
group('NFC', () {
test('a matching registered tag satisfies the proof', () {
final proof = RoomProof(scheduler: scheduler, registeredTagId: 'tag-1');
final result = proof.checkNfc('tag-1');
expect(result.satisfied, isTrue);
expect(result.kind, RoomProofKind.nfc);
});
test('a different tag does not', () {
final proof = RoomProof(scheduler: scheduler, registeredTagId: 'tag-1');
expect(proof.checkNfc('tag-2').satisfied, isFalse);
});
test('no registered tag means NFC cannot satisfy, not that all pass', () {
final proof = RoomProof(scheduler: scheduler);
final result = proof.checkNfc('tag-1');
expect(result.satisfied, isFalse);
expect(result.detail, contains('No NFC tag registered'));
});
test('an empty registered tag is treated as unregistered', () {
final proof = RoomProof(scheduler: scheduler, registeredTagId: '');
expect(proof.checkNfc('').satisfied, isFalse);
});
});
group('QR', () {
test('the expected payload satisfies the proof', () {
final proof = RoomProof(scheduler: scheduler);
final result = proof.checkQr('kitchen', expected: 'kitchen');
expect(result.satisfied, isTrue);
expect(result.kind, RoomProofKind.qr);
});
test('a different payload does not', () {
final proof = RoomProof(scheduler: scheduler);
expect(proof.checkQr('bedroom', expected: 'kitchen').satisfied, isFalse);
});
test('an empty expectation never matches', () {
final proof = RoomProof(scheduler: scheduler);
expect(proof.checkQr('', expected: '').satisfied, isFalse);
});
});
group('steps fallback', () {
test('reports the sensor being unavailable', () async {
steps = -1;
final proof = RoomProof(scheduler: scheduler);
final result = await proof.checkSteps(const Duration(minutes: 5));
expect(result.satisfied, isFalse);
expect(result.detail, contains('unavailable'));
});
test('too few steps does not satisfy', () async {
steps = 5;
final proof = RoomProof(scheduler: scheduler);
final result = await proof.checkSteps(const Duration(minutes: 5));
expect(result.satisfied, isFalse);
expect(result.detail, contains('5/${RoomProof.requiredSteps}'));
});
test('enough steps but too little time does not satisfy', () async {
// This is the shake-it-in-bed case the elapsed requirement exists for.
steps = RoomProof.requiredSteps + 10;
final proof = RoomProof(scheduler: scheduler);
final result = await proof.checkSteps(const Duration(seconds: 10));
expect(result.satisfied, isFalse);
expect(result.detail, contains('Keep moving'));
});
test('enough steps and enough time satisfies', () async {
steps = RoomProof.requiredSteps;
final proof = RoomProof(scheduler: scheduler);
final result = await proof.checkSteps(RoomProof.requiredElapsed);
expect(result.satisfied, isTrue);
expect(result.kind, RoomProofKind.steps);
});
});
}