mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-21 16:22:18 +01:00
Two things, one of them an undo of my own change.
The flash challenge is gone from the phone's rotation. It hides the code after
kDismissFlashSeconds, and on the phone that is unsolvable in practice: the ring
screen is reached by walking to another room and tapping a tag, so the code has
already vanished by the time the user is looking at it. Reported from the phone
as "invisible - impossible to actually do". Every challenge the alarm shows now
stays on screen until it is answered, and a test asserts next() never returns
one that hides itself. The constructor stays for the PC half's parity tests;
nothing routes to it.
The two-channel split is reverted. I introduced it to fix a tag reaching Kotlin
but not Dart, and pointed the push channel at the call channel — which broke
NFC *and* the step counter, both of which had been confirmed working on the
phone one build earlier. The user caught it: "they worked previously so your
changes regressed it". The split was a theory that was never shown to be
necessary; the single channel from 08b44b4 is restored unchanged.
Lesson worth keeping: when hardware has just confirmed a path works, the next
change should be the smallest possible delta on top of that exact state, not a
restructure bundled with a fix.
flutter test: 70 pass. dart analyze lib/ test/: clean.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
170 lines
5.3 KiB
Dart
170 lines
5.3 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:wake_alarm_sync/services/challenge.dart';
|
|
|
|
/// Deterministic source so every branch can be reached on purpose.
|
|
class _FixedRandom implements Random {
|
|
_FixedRandom(this._values);
|
|
|
|
final List<int> _values;
|
|
int _at = 0;
|
|
|
|
@override
|
|
int nextInt(int max) {
|
|
final v = _values[_at % _values.length];
|
|
_at++;
|
|
return v % max;
|
|
}
|
|
|
|
@override
|
|
bool nextBool() => nextInt(2) == 0;
|
|
|
|
@override
|
|
double nextDouble() => 0;
|
|
}
|
|
|
|
void main() {
|
|
group('Challenge.accepts', () {
|
|
const challenge = Challenge(
|
|
kind: ChallengeKind.math,
|
|
display: '1 + 1 = ?',
|
|
answer: '2',
|
|
hint: 'h',
|
|
);
|
|
|
|
test('accepts the exact answer', () {
|
|
expect(challenge.accepts('2'), isTrue);
|
|
});
|
|
|
|
test('ignores case and surrounding whitespace', () {
|
|
const word = Challenge(
|
|
kind: ChallengeKind.reverse,
|
|
display: 'd',
|
|
answer: 'MRALA',
|
|
hint: 'h',
|
|
);
|
|
expect(word.accepts(' mrala '), isTrue);
|
|
});
|
|
|
|
test('rejects a wrong answer', () {
|
|
expect(challenge.accepts('3'), isFalse);
|
|
});
|
|
});
|
|
|
|
group('ChallengeGenerator', () {
|
|
test('generateCode has the mirrored length and no ambiguous chars', () {
|
|
final code = ChallengeGenerator().generateCode();
|
|
expect(code, hasLength(kDismissCodeLength));
|
|
expect(RegExp(r'^[A-HJ-NP-Z2-9]+$').hasMatch(code), isTrue);
|
|
});
|
|
|
|
test('math covers addition', () {
|
|
final c = ChallengeGenerator(random: _FixedRandom(<int>[0, 5, 7])).math();
|
|
expect(c.kind, ChallengeKind.math);
|
|
expect(c.display, contains('+'));
|
|
final parts = RegExp(r'(\d+) \+ (\d+)').firstMatch(c.display)!;
|
|
final sum = int.parse(parts.group(1)!) + int.parse(parts.group(2)!);
|
|
expect(c.answer, '$sum');
|
|
});
|
|
|
|
test('math covers subtraction with a non-negative result', () {
|
|
final c = ChallengeGenerator(random: _FixedRandom(<int>[1, 5, 7])).math();
|
|
expect(c.display, contains('-'));
|
|
expect(int.parse(c.answer), greaterThanOrEqualTo(0));
|
|
});
|
|
|
|
test('math covers multiplication', () {
|
|
final c = ChallengeGenerator(random: _FixedRandom(<int>[2, 3, 4])).math();
|
|
expect(c.display, contains('*'));
|
|
final parts = RegExp(r'(\d+) \* (\d+)').firstMatch(c.display)!;
|
|
final product =
|
|
int.parse(parts.group(1)!) * int.parse(parts.group(2)!);
|
|
expect(c.answer, '$product');
|
|
});
|
|
|
|
test('math division always has an integer answer', () {
|
|
final c = ChallengeGenerator(random: _FixedRandom(<int>[3, 2, 6])).math();
|
|
expect(c.display, contains('÷'));
|
|
final parts = RegExp(r'(\d+) ÷ (\d+)').firstMatch(c.display)!;
|
|
final a = int.parse(parts.group(1)!);
|
|
final b = int.parse(parts.group(2)!);
|
|
expect(a % b, 0);
|
|
expect(c.answer, '${a ~/ b}');
|
|
});
|
|
|
|
test('sort shows eight digits and answers them ascending', () {
|
|
final c = ChallengeGenerator().sort();
|
|
final shown = c.display.split(' ').map(int.parse).toList();
|
|
expect(shown, hasLength(8));
|
|
expect(c.answer, (List<int>.from(shown)..sort()).join());
|
|
expect(c.answer.split('').toSet(), hasLength(8));
|
|
});
|
|
|
|
test('flash displays the code it expects back', () {
|
|
final c = ChallengeGenerator().flash();
|
|
expect(c.kind, ChallengeKind.flash);
|
|
expect(c.display, c.answer);
|
|
expect(c.hint, contains('${kDismissFlashSeconds}s'));
|
|
});
|
|
|
|
test('sequence covers the arithmetic branch', () {
|
|
final c = ChallengeGenerator(
|
|
random: _FixedRandom(<int>[0, 3, 2]),
|
|
).sequence();
|
|
final terms = c.display
|
|
.replaceAll(', ?', '')
|
|
.split(', ')
|
|
.map(int.parse)
|
|
.toList();
|
|
final step = terms[1] - terms[0];
|
|
expect(terms[3] - terms[2], step);
|
|
expect(c.answer, '${terms[3] + step}');
|
|
});
|
|
|
|
test('sequence covers the geometric branch', () {
|
|
final c = ChallengeGenerator(
|
|
random: _FixedRandom(<int>[1, 2, 0]),
|
|
).sequence();
|
|
final terms = c.display
|
|
.replaceAll(', ?', '')
|
|
.split(', ')
|
|
.map(int.parse)
|
|
.toList();
|
|
final ratio = terms[1] ~/ terms[0];
|
|
expect(terms[3], terms[2] * ratio);
|
|
expect(c.answer, '${terms[3] * ratio}');
|
|
});
|
|
|
|
test('reverse expects the word backwards', () {
|
|
final c = ChallengeGenerator().reverse();
|
|
final word = c.display.split('\n').last;
|
|
expect(c.answer, word.split('').reversed.join());
|
|
});
|
|
|
|
test('next() never returns a challenge that hides itself', () {
|
|
// flash disappears after kDismissFlashSeconds, which is unsolvable on
|
|
// the phone: the user reaches the ring screen only after walking to
|
|
// another room and tapping a tag.
|
|
final kinds = <ChallengeKind>{};
|
|
for (var i = 0; i < 400; i++) {
|
|
kinds.add(ChallengeGenerator().next().kind);
|
|
}
|
|
expect(kinds, isNot(contains(ChallengeKind.flash)));
|
|
});
|
|
|
|
test('next() produces every kind that is safe to show', () {
|
|
final kinds = <ChallengeKind>{};
|
|
for (var i = 0; i < 400; i++) {
|
|
kinds.add(ChallengeGenerator().next().kind);
|
|
}
|
|
expect(kinds, <ChallengeKind>{
|
|
ChallengeKind.math,
|
|
ChallengeKind.sort,
|
|
ChallengeKind.sequence,
|
|
ChallengeKind.reverse,
|
|
});
|
|
});
|
|
});
|
|
}
|