mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-21 21:02:20 +01:00
The dismiss gate was an OR — NFC tag *or* QR *or* (25 steps + 2 min) —
followed by one challenge. QR never worked at ring time (no scanner
package, no CAMERA permission, `onQrScanned` had no caller), so the real
bar was "walk 25 steps, wait 2 minutes, answer one question" and the NFC
tag was already fully bypassable.
It is now a strict, latched sequence owned by `DismissGate`:
tag 1 -> 25 steps + 2 min -> tag 2 -> 3 challenges
Every stage is required, in that order, and a stage once passed never
reverts. That last part also fixes a real bug: `_onNfcTag` and `_trySteps`
both assigned the room-proof result straight onto `_inRoom`, so a wrong
tag tapped after the gate had passed sent the user back to the start.
Two *distinct* tags are required; the same id in both slots is refused at
registration, since one tag satisfying both stages removes the walk that
is the whole point. The existing single-tag registration migrates into
slot 1 rather than being stranded.
Answers are cumulative, not consecutive: a wrong one costs the attempt and
nothing else.
The bypass is the only fallback and is deliberately narrow. After 5 minutes
of ringing, or immediately when NFC is off or fewer than two distinct tags
are registered, the two *tag* stages are skipped and the count rises to 6.
The walk is never skipped — jumping straight to the challenges would let
someone lie in bed for five minutes and answer questions without standing
up, which is weaker than the gate this replaces. Without some such hatch a
dead tag at 06:00 leaves an alarm that cannot be switched off at all.
Also deletes the `flash` challenge outright, along with the now-orphaned
`generateCode`/`kDismissCodeLength`/`_dismissChars`. It was the only kind
that removed its prompt from the screen, which is unsolvable for someone
who has just walked in from another room.
The ring screen takes an injectable clock: `Stopwatch` reads the wall
clock, which `tester.pump()` does not advance, so without that seam no
widget test could reach the two-minute floor or the five-minute bypass.
Verified on the Pixel 6a: the existing tag migrated into slot 1 and both
rows render. 102 Dart tests pass; analyzer clean.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PcdngP688mW6rJye3LRkSN
101 lines
3.8 KiB
Dart
101 lines
3.8 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';
|
|
import 'package:wake_alarm_sync/services/room_proof_settings.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);
|
|
});
|
|
|
|
const twoTags = RoomProofSettings(nfcTagIds: <String>['tag-1', 'tag-2']);
|
|
|
|
group('NFC', () {
|
|
test('a matching registered tag satisfies its own slot', () {
|
|
final proof = RoomProof(scheduler: scheduler, settings: twoTags);
|
|
final result = proof.checkNfc('tag-1', slot: 0);
|
|
expect(result.satisfied, isTrue);
|
|
expect(result.kind, RoomProofKind.nfc);
|
|
});
|
|
|
|
test('a different tag does not', () {
|
|
final proof = RoomProof(scheduler: scheduler, settings: twoTags);
|
|
expect(proof.checkNfc('tag-9', slot: 0).satisfied, isFalse);
|
|
});
|
|
|
|
test('the OTHER registered tag does not satisfy this slot', () {
|
|
// Two locations must stay two locations: if tag 2 could pass stage 1
|
|
// the walk between them would be optional.
|
|
final proof = RoomProof(scheduler: scheduler, settings: twoTags);
|
|
final result = proof.checkNfc('tag-2', slot: 0);
|
|
expect(result.satisfied, isFalse);
|
|
expect(result.detail, contains('Not tag 1'));
|
|
});
|
|
|
|
test('each slot matches only its own tag', () {
|
|
final proof = RoomProof(scheduler: scheduler, settings: twoTags);
|
|
expect(proof.checkNfc('tag-2', slot: 1).satisfied, isTrue);
|
|
expect(proof.checkNfc('tag-1', slot: 1).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', slot: 0);
|
|
expect(result.satisfied, isFalse);
|
|
expect(result.detail, contains('No tag 1 registered'));
|
|
});
|
|
|
|
test('an empty registered tag is treated as unregistered', () {
|
|
final proof = RoomProof(scheduler: scheduler);
|
|
expect(proof.checkNfc('', slot: 0).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);
|
|
});
|
|
});
|
|
}
|