wake-alarm/phone_app/test/session_screen_relaunch_test.dart
Krzysztof kuhy Rudnicki c3c129b068 Make the morning session the carrot: earn 11:00 by finishing it in time
On 2026-09-19 the phone was powered off after the bathroom tag and nothing
on the PC noticed: every finished session was published and nothing read
it. Now wake-alarm distils the phone's morning into an HMAC-signed
~/.local/state/wake_alarm/morning_session.json (wake-alarm-session.timer,
07:00-11:00 every 2 min) that screen-locker and leetcode-guard read to
defer their locks -- provisionally until min(start + 90 min, 09:30) while
the session is live, until 11:00 once it completed inside that cutoff,
and not at all for a session finished later (the phone switched on at
noon completes the legs then; that must un-lock nothing).

The phone publishes the session at start as well as at finish
(current_session, cleared with the finish stamp), so the PC can tell
"started, not finished" from "never started". A required 1-5 energy tap
joins the weigh-in and ships as latest_energy with a PC reader mirroring
_weight (the shared half is _field_reader).

Also fixes the vmbox harness, red since the 2026-09-11 home-tidy: the
guest tree lives at ~/wake-alarm, and the INSTALL-OK grep matched vm's
echo of the command line. Takes Flutter 3.47.5.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tp54ewdXRLaimUuT8LgYyn
2026-09-19 16:40:51 +02:00

84 lines
3.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wake_alarm_sync/screens/session_screen.dart';
import 'package:wake_alarm_sync/services/alarm_scheduler.dart';
import 'package:wake_alarm_sync/services/morning_session.dart';
import 'package:wake_alarm_sync/services/session_rules.dart';
import 'package:wake_alarm_sync/services/session_store.dart';
import 'package:wake_alarm_sync/services/session_tag_settings.dart';
/// The session screen reached cold by the launcher redirect, not by a ring.
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late List<MethodCall> calls;
final now = DateTime(2026, 9, 17, 8, 40);
setUp(() async {
calls = <MethodCall>[];
SharedPreferences.setMockInitialValues(<String, Object>{});
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
const MethodChannel(AlarmScheduler.channelName),
(call) async {
calls.add(call);
return call.method == 'hasNfc' ? true : null;
},
);
await SessionTagSettings.replaceAll(<String>['bath', 'ward', 'desk'], '');
});
Future<void> pumpCold(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: SessionScreen(clock: () => now, publish: () async => 'ok'),
),
);
await tester.pump();
await tester.pump();
}
testWidgets('resumes the persisted session at its live leg', (tester) async {
final at = now.subtract(const Duration(minutes: 4));
final session =
SessionRules.start(
now: now.subtract(const Duration(minutes: 5)),
tags: await SessionTagSettings.load(),
nfcAvailable: true,
)
..enterCheckIn(72, 3, at)
..onTag(SessionTagSlot.bathroom, at);
await SessionStore().save(session);
await pumpCold(tester);
expect(find.text('Weigh in — 72.0 kg · energy 3/5'), findsOneWidget);
expect(find.textContaining('tag counts in'), findsOneWidget);
final notice = calls.lastWhere((c) => c.method == 'updateSessionNotice');
expect(
(notice.arguments as Map<Object?, Object?>)['label'],
'Shower, then tap the bathroom tag again',
);
expect(calls.map((c) => c.method), isNot(contains('closeRingScreen')));
});
testWidgets('with today already finished it clears the record and closes', (
tester,
) async {
final done = SessionRules.start(
now: now.subtract(const Duration(minutes: 30)),
tags: const SessionTagSettings(),
nfcAvailable: false,
)..enterCheckIn(72, 3, now.subtract(const Duration(minutes: 29)));
expect(done.outcome, SessionOutcome.completed);
await SessionStore().archive(done);
await pumpCold(tester);
final methods = calls.map((c) => c.method).toList();
expect(methods, contains('finishSession'));
expect(methods.last, 'closeRingScreen');
expect(methods, isNot(contains('updateSessionNotice')));
expect(await SessionStore().loadCurrent(), isNull);
expect((await SessionStore().history()).length, 1);
});
}