wake-alarm/phone_app/test/services/room_proof_test.dart
Krzysztof kuhy Rudnicki af2a9b416b Phase 4/3: the phone's Dart half — ring screen, challenges, room proofs
Dart side of the alarm (items 1, 4, 3). main.dart had no init seam at all;
it now has ensureInitialized(), a navigatorKey and a /ring route.

- challenge.dart: all five kinds ported from _challenges.py so both halves
  behave identically. kDismissCodeLength/kDismissFlashSeconds are duplicated
  from _constants.py, not synced — noted in the file, since Dart cannot read
  Python.
- alarm_scheduler.dart: the Dart side of the single MethodChannel.
- ring_screen.dart: room proof first, then a challenge, then dismiss. Back is
  a no-op (PopScope) so the alarm cannot be escaped.
- room_proof.dart: NFC or QR satisfies on its own; steps NEVER do alone —
  the phone is stationary while you walk to it, so steps only accrue after
  pickup and can be faked by shaking it in bed. Steps count only together
  with elapsed time, as the fallback when both others fail.
- home_screen: arms the app's own alarm instead of asking the user to confirm
  in the Clock app (item 1's "no user interaction"). SET_ALARM stays as
  redundancy with SKIP_UI=true.

Re-arm has three hooks and needs all three: startup, BOOT_COMPLETED, and
dismiss. The plan assumed LogStore.changes would cover this; that stream does
not exist in crdt_sync_dart (verified — the library has no Stream at all), so
the picker's save is only one hook and a restart would otherwise arm nothing.

Fixed a real gap the API 36 AVD exposed: POST_NOTIFICATIONS was never
requested at runtime, so a fresh install sat at importance=NONE and the
full-screen intent was rejected outright — the alarm would ring with no
dismiss screen. MainActivity now requests it, and canUseFullScreenIntent()
also checks areNotificationsEnabled() so the check gates rather than warns.

Verified on the API 36 AVD: screen Asleep -> Awake, AlarmActivity became the
top resumed activity over the lockscreen, service isForeground=true
types=0x2 (mediaPlayback) category=alarm, no crash. flutter test: 54 pass.
dart analyze: clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
2026-08-27 21:22:07 +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;
int 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);
});
});
}