wake-alarm/phone_app/test/ring_bypass_test.dart
Krzysztof kuhy Rudnicki 8e6fa90c80
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / free-days-interop (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Bump Flutter to 3.50.0 via material_ui
Flutter 3.50 deprecates package:flutter/material.dart, so the app
moves to material_ui 1.11.0 together with sync_settings_ui 0.8.0
and github_device_auth 0.5.0 (crdt_sync 0.13.1 rides along).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:06:16 +01:00

142 lines
4.7 KiB
Dart

import 'dart:math';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wake_alarm_sync/screens/ring_screen.dart';
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
import 'package:wake_alarm_sync/services/challenge.dart';
import 'package:wake_alarm_sync/services/dismiss_gate.dart';
import 'package:wake_alarm_sync/services/room_proof_settings.dart';
import 'package:wake_alarm_sync/ui/cues.dart';
const String _tag1 = '04a2b3c4';
const String _tag2 = '04ffee11';
/// The ring's status sentence, now spoken only (StatusCue.semanticLabel).
String statusOf(WidgetTester tester) =>
tester.widget<StatusCue>(find.byType(StatusCue)).semanticLabel;
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late List<String> calls;
var steps = 0;
var hasNfc = true;
var elapsed = Duration.zero;
/// Installs the platform mock. Call after changing [steps] / [hasNfc].
void mockChannel() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
const MethodChannel(AlarmScheduler.channelName),
(call) async {
calls.add(call.method);
if (call.method == 'stepCount') return steps;
if (call.method == 'hasNfc') return hasNfc;
return null;
},
);
}
/// Lets the periodic step poll run once.
Future<void> pollSteps(WidgetTester tester) async {
await tester.pump(kStepPollInterval);
await tester.pump();
}
setUp(() {
calls = <String>[];
steps = 0;
hasNfc = true;
elapsed = Duration.zero;
SharedPreferences.setMockInitialValues(<String, Object>{});
mockChannel();
});
/// A ring screen with both tags registered and a controllable clock.
Future<ChallengeGenerator> pumpRing(WidgetTester tester) async {
await RoomProofSettings.saveNfcTagAt(0, _tag1);
await RoomProofSettings.saveNfcTagAt(1, _tag2);
await tester.pumpWidget(
MaterialApp(
home: RingScreen(
generator: ChallengeGenerator(random: Random(1)),
clock: () => elapsed,
),
),
);
await tester.pump();
return ChallengeGenerator(random: Random(1));
}
testWidgets('it is not offered before the timer elapses', (tester) async {
await pumpRing(tester);
await tester.pump(DismissGate.bypassAfter - const Duration(seconds: 1));
expect(find.byKey(const Key('ring-bypass')), findsNothing);
expect(find.byKey(const ValueKey<String>('ring-row-tag1')), findsOneWidget);
});
testWidgets('after the timer it skips the tags but still needs the walk', (
tester,
) async {
await pumpRing(tester);
await tester.pump(DismissGate.bypassAfter);
await tester.pump();
expect(find.byKey(const Key('ring-bypass')), findsOneWidget);
// A hardware bypass must not borrow the away wording.
expect(find.byKey(const Key('ring-bypass')), findsOneWidget);
expect(find.byKey(const ValueKey<String>('ring-row-tag1')), findsNothing);
expect(
find.byKey(const ValueKey<String>('ring-row-steps')),
findsOneWidget,
);
expect(find.byKey(const Key('ring-submit')), findsNothing);
steps = 500;
elapsed = const Duration(minutes: 6);
await pollSteps(tester);
expect(find.text('1/6'), findsOneWidget);
});
testWidgets('under bypass the walk still needs elapsed time', (tester) async {
// The anti-shake floor lives here now and only here: with no tag to
// prove the walk, 25 "steps" in five seconds is a phone shaken in bed.
// Only an immediate bypass can hit it -- the five-minute one has already
// outlasted the floor by the time it fires.
hasNfc = false;
mockChannel();
await pumpRing(tester);
steps = 500;
elapsed = const Duration(seconds: 5);
await pollSteps(tester);
expect(statusOf(tester), contains('Keep moving'));
expect(find.text('Submit'), findsNothing);
});
testWidgets('an unregistered second tag bypasses immediately', (
tester,
) async {
await RoomProofSettings.saveNfcTagAt(0, _tag1);
await tester.pumpWidget(
MaterialApp(home: RingScreen(clock: () => elapsed)),
);
await tester.pump();
expect(find.byKey(const Key('ring-bypass')), findsOneWidget);
expect(find.byIcon(Icons.block), findsOneWidget);
});
testWidgets('no NFC hardware bypasses immediately', (tester) async {
hasNfc = false;
mockChannel();
await pumpRing(tester);
expect(statusOf(tester), contains('NFC is off'));
expect(find.text('Tap tag 1'), findsNothing);
});
}
/// Local helper so the test need not import dart:async for one call.
void unawaited(Future<void> future) {}