mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-24 10:26:26 +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
166 lines
5.6 KiB
Dart
166 lines
5.6 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:wake_alarm_sync/services/dismiss_gate.dart';
|
|
|
|
/// Drives a fresh gate through tag 1 -> steps -> tag 2, onto challenges.
|
|
DismissGate _atChallenges() {
|
|
final gate = DismissGate()
|
|
..passTagStage()
|
|
..passStepsStage()
|
|
..passTagStage();
|
|
expect(gate.stage, DismissStage.challenges);
|
|
return gate;
|
|
}
|
|
|
|
void main() {
|
|
group('strict ordering', () {
|
|
test('a fresh gate waits for tag 1', () {
|
|
final gate = DismissGate();
|
|
expect(gate.stage, DismissStage.tag1);
|
|
expect(gate.expectedTagSlot, 0);
|
|
expect(gate.isDone, isFalse);
|
|
});
|
|
|
|
test('steps before tag 1 do not advance anything', () {
|
|
final gate = DismissGate();
|
|
expect(gate.passStepsStage(), isFalse);
|
|
expect(gate.stage, DismissStage.tag1);
|
|
});
|
|
|
|
test('the sequence runs tag 1 -> steps -> tag 2 -> challenges', () {
|
|
final gate = DismissGate();
|
|
expect(gate.passTagStage(), isTrue);
|
|
expect(gate.stage, DismissStage.steps);
|
|
expect(gate.expectedTagSlot, isNull, reason: 'no tag stage is live');
|
|
expect(gate.passStepsStage(), isTrue);
|
|
expect(gate.stage, DismissStage.tag2);
|
|
expect(gate.expectedTagSlot, 1);
|
|
expect(gate.passTagStage(), isTrue);
|
|
expect(gate.stage, DismissStage.challenges);
|
|
});
|
|
|
|
test('a tag tapped during the steps stage does not advance it', () {
|
|
final gate = DismissGate()..passTagStage();
|
|
expect(gate.stage, DismissStage.steps);
|
|
expect(gate.passTagStage(), isFalse);
|
|
expect(gate.stage, DismissStage.steps, reason: 'steps still required');
|
|
});
|
|
});
|
|
|
|
group('latching', () {
|
|
test('a passed stage never reverts', () {
|
|
// The bug this replaces: the old gate assigned the room-proof result
|
|
// straight onto a bool, so a wrong tap after passing hid the challenge.
|
|
final gate = _atChallenges();
|
|
expect(gate.passTagStage(), isFalse);
|
|
expect(gate.passStepsStage(), isFalse);
|
|
expect(gate.stage, DismissStage.challenges);
|
|
});
|
|
|
|
test('finishing does not un-finish', () {
|
|
final gate = _atChallenges();
|
|
for (var i = 0; i < DismissGate.challengesNormal; i++) {
|
|
gate.recordChallenge(correct: true);
|
|
}
|
|
expect(gate.isDone, isTrue);
|
|
expect(gate.recordChallenge(correct: false), isFalse);
|
|
expect(gate.isDone, isTrue);
|
|
});
|
|
});
|
|
|
|
group('challenges', () {
|
|
test('one correct answer is not enough; three are', () {
|
|
final gate = _atChallenges()..recordChallenge(correct: true);
|
|
expect(gate.isDone, isFalse);
|
|
gate.recordChallenge(correct: true);
|
|
expect(gate.isDone, isFalse);
|
|
gate.recordChallenge(correct: true);
|
|
expect(gate.isDone, isTrue);
|
|
});
|
|
|
|
test('a wrong answer does NOT reset the tally', () {
|
|
final gate = _atChallenges()
|
|
..recordChallenge(correct: true)
|
|
..recordChallenge(correct: true)
|
|
..recordChallenge(correct: false);
|
|
expect(gate.challengesPassed, 2, reason: 'cumulative, not consecutive');
|
|
gate.recordChallenge(correct: true);
|
|
expect(gate.isDone, isTrue);
|
|
});
|
|
|
|
test('answers before the challenge stage are ignored', () {
|
|
final gate = DismissGate();
|
|
expect(gate.recordChallenge(correct: true), isFalse);
|
|
expect(gate.challengesPassed, 0);
|
|
});
|
|
});
|
|
|
|
group('bypass', () {
|
|
test('it drops the tags but NOT the walk', () {
|
|
final gate = DismissGate();
|
|
expect(gate.activateBypass(), isTrue);
|
|
expect(gate.bypassActive, isTrue);
|
|
expect(gate.stage, DismissStage.steps, reason: 'steps still required');
|
|
expect(gate.challengesRequired, DismissGate.challengesBypass);
|
|
});
|
|
|
|
test('six answers are needed once bypassed, and five are not', () {
|
|
final gate = DismissGate()
|
|
..activateBypass()
|
|
..passStepsStage();
|
|
expect(gate.stage, DismissStage.challenges, reason: 'tag 2 is skipped');
|
|
for (var i = 0; i < DismissGate.challengesBypass - 1; i++) {
|
|
gate.recordChallenge(correct: true);
|
|
}
|
|
expect(gate.isDone, isFalse);
|
|
gate.recordChallenge(correct: true);
|
|
expect(gate.isDone, isTrue);
|
|
});
|
|
|
|
test('bypassing after tag 1 still requires the walk', () {
|
|
final gate = DismissGate()
|
|
..passTagStage()
|
|
..activateBypass();
|
|
expect(gate.stage, DismissStage.steps);
|
|
gate.passStepsStage();
|
|
expect(gate.stage, DismissStage.challenges);
|
|
});
|
|
|
|
test('bypassing at tag 2 keeps the already-banked steps', () {
|
|
final gate = DismissGate()
|
|
..passTagStage()
|
|
..passStepsStage();
|
|
expect(gate.stage, DismissStage.tag2);
|
|
gate.activateBypass();
|
|
expect(gate.stage, DismissStage.challenges);
|
|
});
|
|
|
|
test('it cannot fire once the challenges are live', () {
|
|
// Raising 3 to 6 mid-answer would punish someone who did the walk.
|
|
final gate = _atChallenges();
|
|
expect(gate.activateBypass(), isFalse);
|
|
expect(gate.challengesRequired, DismissGate.challengesNormal);
|
|
});
|
|
|
|
test('it is one-way and idempotent', () {
|
|
final gate = DismissGate();
|
|
expect(gate.activateBypass(), isTrue);
|
|
expect(gate.activateBypass(), isFalse);
|
|
expect(gate.challengesRequired, DismissGate.challengesBypass);
|
|
});
|
|
|
|
test('tags do nothing once bypassed', () {
|
|
final gate = DismissGate()..activateBypass();
|
|
expect(gate.expectedTagSlot, isNull);
|
|
expect(gate.passTagStage(), isFalse);
|
|
expect(gate.stage, DismissStage.steps);
|
|
});
|
|
});
|
|
|
|
test('the bypass is strictly harder than the normal path', () {
|
|
expect(
|
|
DismissGate.challengesBypass,
|
|
greaterThan(DismissGate.challengesNormal),
|
|
);
|
|
});
|
|
}
|