wake-alarm/phone_app/test/ring_route_test.dart
Krzysztof kuhy Rudnicki cb50e58a56 Phase 4/3: NFC room proof, runtime permissions, non-blocking step reads
Three defects found by reviewing the emulator run, all on the dismiss path:

1. ACTIVITY_RECOGNITION was declared but never requested at runtime (API 29+).
   Denied, TYPE_STEP_COUNTER delivers nothing, so checkSteps() reports "sensor
   unavailable" forever. With steps the only wired proof, that made the alarm
   *undismissable*, not merely degraded. MainActivity now requests it
   alongside POST_NOTIFICATIONS.

2. checkNfc/checkQr were dead code — no reader, no channel method, no
   registration — so steps was the only reachable proof. That inverts the
   decision ("NFC or QR qualifies; steps alone does NOT") and removes exactly
   the redundancy whose purpose is that one failing mechanism at 06:00 must
   never block dismissal. Added NfcReader (hand-written on the existing
   channel, NfcAdapter.enableReaderMode so the tap does not restart the
   activity and reset the challenge) plus RoomProofSettings for the registered
   tag/QR. mobile_scanner stays the droppable extra.

3. StepCounter blocked the platform main thread up to 3s per read, called from
   the MethodChannel handler — jank-to-ANR on the ring screen. Now registers a
   listener and serves the last sample; reads never block.

Also: raiseAlarmVolume() is guarded. setStreamVolume can throw under a zen
rule, and by then startForeground() has already posted — a failed volume bump
must not take the ring down with it.

Verified on the API 36 AVD with the real app widget and route (the earlier
probe rendered its own UI, so it proved the Kotlin path but nothing about
/ring): screen Asleep -> Awake, AlarmActivity top resumed over the lockscreen,
RingScreen showing the room-proof gate. New widget tests cover the /ring route
resolving and the challenge staying gated behind the room proof.

flutter test: 57 pass. dart analyze: clean. Zero Python files touched.

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

71 lines
2.7 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/main.dart';
import 'package:wake_alarm_sync/screens/ring_screen.dart';
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() {
SharedPreferences.setMockInitialValues(<String, Object>{});
const channel = MethodChannel(AlarmScheduler.channelName);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
if (call.method == 'stepCount') return 0;
if (call.method == 'hasNfc') return false;
return null;
});
});
testWidgets('the /ring route resolves to the ring screen', (tester) async {
// AlarmActivity hands "/ring" to the engine as its initial route; if the
// app does not resolve it the full-screen intent opens the home screen and
// there is no dismiss path at all.
await tester.pumpWidget(const WakeAlarmSyncApp());
final state = tester.state<NavigatorState>(find.byType(Navigator));
unawaited(state.pushNamed(kRingRoute));
await tester.pumpAndSettle();
expect(find.byType(RingScreen), findsOneWidget);
});
testWidgets('the ring screen shows the room gate before the challenge', (
tester,
) async {
await tester.pumpWidget(const MaterialApp(home: RingScreen()));
await tester.pumpAndSettle();
expect(find.text('Wake up'), findsOneWidget);
expect(find.text('Check steps'), findsOneWidget);
// The challenge must not be reachable until the room proof is satisfied.
expect(find.text('Dismiss'), findsNothing);
});
testWidgets('satisfying the room proof reveals the challenge', (
tester,
) async {
const channel = MethodChannel(AlarmScheduler.channelName);
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
if (call.method == 'stepCount') return 500;
if (call.method == 'hasNfc') return false;
return null;
});
await tester.pumpWidget(const MaterialApp(home: RingScreen()));
await tester.pumpAndSettle();
// The stopwatch starts at zero, so steps alone must not be enough yet.
await tester.tap(find.text('Check steps'));
await tester.pumpAndSettle();
expect(find.text('Dismiss'), findsNothing);
expect(find.textContaining('Keep moving'), findsOneWidget);
});
}
/// Local helper so the test need not import dart:async for one call.
void unawaited(Future<void> future) {}