mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-21 19:22:21 +01:00
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
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>
153 lines
5.2 KiB
Dart
153 lines
5.2 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/home_location_settings.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();
|
|
|
|
var steps = 0;
|
|
var elapsed = Duration.zero;
|
|
Map<String, Object?>? presence;
|
|
|
|
void mockChannel() {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(
|
|
const MethodChannel(AlarmScheduler.channelName),
|
|
(call) async {
|
|
if (call.method == 'stepCount') return steps;
|
|
if (call.method == 'hasNfc') return true;
|
|
if (call.method == 'readPresence') return presence;
|
|
return null;
|
|
},
|
|
);
|
|
}
|
|
|
|
setUp(() {
|
|
steps = 0;
|
|
elapsed = Duration.zero;
|
|
presence = null;
|
|
SharedPreferences.setMockInitialValues(<String, Object>{});
|
|
mockChannel();
|
|
});
|
|
|
|
/// A ring screen with both tags registered and a controllable clock.
|
|
Future<void> 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();
|
|
await tester.pump();
|
|
}
|
|
|
|
testWidgets('at home the strict sequence is unchanged', (tester) async {
|
|
await HomeLocationSettings.capture(ssid: 'Orange_Home');
|
|
presence = <String, Object?>{'ssid': 'Orange_Home', 'reason': 'ok'};
|
|
await pumpRing(tester);
|
|
|
|
expect(find.byKey(const ValueKey<String>('ring-row-tag1')), findsOneWidget);
|
|
expect(find.byKey(const ValueKey<String>('ring-row-tag2')), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('away it skips the tags, keeps the walk, and asks 3', (
|
|
tester,
|
|
) async {
|
|
await HomeLocationSettings.capture(ssid: 'Orange_Home');
|
|
presence = <String, Object?>{'ssid': 'Hotel_Guest', 'reason': 'ok'};
|
|
await pumpRing(tester);
|
|
|
|
expect(find.byKey(const Key('ring-bypass')), findsWidgets);
|
|
// The persistent line, not the status that the step poll overwrites.
|
|
expect(statusOf(tester), contains('Away from home'));
|
|
expect(statusOf(tester), isNot(contains('Tags unavailable')));
|
|
expect(find.text('Tap tag 1'), findsNothing);
|
|
expect(find.text('Tap tag 2'), findsNothing);
|
|
// The walk is never skipped: away is where the tags are, not the bed.
|
|
expect(
|
|
find.byKey(const ValueKey<String>('ring-row-steps')),
|
|
findsOneWidget,
|
|
);
|
|
|
|
steps = 500;
|
|
elapsed = const Duration(minutes: 3);
|
|
await tester.pump(kStepPollInterval);
|
|
await tester.pump();
|
|
expect(find.text('1/3'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('the five-minute timer does not bump an away morning to 6', (
|
|
tester,
|
|
) async {
|
|
await HomeLocationSettings.capture(ssid: 'Orange_Home');
|
|
presence = <String, Object?>{'ssid': 'Hotel_Guest', 'reason': 'ok'};
|
|
await pumpRing(tester);
|
|
|
|
steps = 500;
|
|
elapsed = const Duration(minutes: 3);
|
|
await tester.pump(kStepPollInterval);
|
|
await tester.pump();
|
|
expect(find.text('1/3'), findsOneWidget);
|
|
|
|
// An away morning routinely outlasts the bypass timer.
|
|
await tester.pump(DismissGate.bypassAfter);
|
|
await tester.pump();
|
|
expect(find.text('1/3'), findsOneWidget);
|
|
expect(find.text('1/6'), findsNothing);
|
|
});
|
|
|
|
testWidgets('unreadable presence is away, per the chosen fail mode', (
|
|
tester,
|
|
) async {
|
|
await HomeLocationSettings.capture(ssid: 'Orange_Home');
|
|
presence = <String, Object?>{'reason': 'no-permission'};
|
|
await pumpRing(tester);
|
|
|
|
expect(statusOf(tester), contains('location permission denied'));
|
|
expect(find.text('Tap tag 1'), findsNothing);
|
|
});
|
|
|
|
testWidgets('away wins over missing tags, so the count stays 3', (
|
|
tester,
|
|
) async {
|
|
// Ordering regression: if the missing-tag bypass were applied first it
|
|
// would latch 6, and the away verdict arriving later could not lower it.
|
|
await HomeLocationSettings.capture(ssid: 'Orange_Home');
|
|
presence = <String, Object?>{'ssid': 'Hotel_Guest', 'reason': 'ok'};
|
|
await RoomProofSettings.saveNfcTagAt(0, _tag1);
|
|
await tester.pumpWidget(
|
|
MaterialApp(home: RingScreen(clock: () => elapsed)),
|
|
);
|
|
await tester.pump();
|
|
await tester.pump();
|
|
|
|
steps = 500;
|
|
elapsed = const Duration(minutes: 3);
|
|
await tester.pump(kStepPollInterval);
|
|
await tester.pump();
|
|
expect(find.text('1/3'), findsOneWidget);
|
|
});
|
|
}
|