wake-alarm/phone_app/test/services/session_handoff_test.dart
Krzysztof kuhy Rudnicki 65df7f6682 Name a bypass's session re-ring for its cause
A bypass during a live morning session restarted the ring natively, and
the session recorded that re-ring as deadline:<leg> (C7, 2026-11-22).
Bypass.record now stamps the cause (dated) when nothing was ringing; the
session takes it on its tick or on the dismiss and records bypass:<cause>.
A bypass during a deadline re-ring stamps nothing: one sound, one re-ring.

Also a debug-only sync backend: a probe build with WAKE_SYNC_EMULATOR
syncs to the PC's RTDB emulator instead of prod and the GitHub mirror,
with scripts/emu_published.py reading it back for _fallback --published.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 15:53:26 +01:00

143 lines
5.0 KiB
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/services/alarm_scheduler.dart';
import 'package:wake_alarm_sync/services/morning_session.dart';
import 'package:wake_alarm_sync/services/session_handoff.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';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final now = DateTime(2026, 9, 17, 8, 4);
var hasNfc = true;
String? bypassCause;
late List<String> calls;
setUp(() {
hasNfc = true;
bypassCause = null;
calls = <String>[];
SharedPreferences.setMockInitialValues(<String, Object>{});
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(
const MethodChannel(AlarmScheduler.channelName),
(call) async {
calls.add(call.method);
if (call.method == 'takeBypassReRing') return bypassCause;
return call.method == 'hasNfc' ? hasNfc : null;
},
);
});
SessionHandoff handoff() =>
SessionHandoff(scheduler: AlarmScheduler(), now: () => now);
test("the first dismiss starts today's session", () async {
await SessionTagSettings.saveTagAt(SessionTagSlot.desk, 'd');
expect(await handoff().afterDismiss(away: false), isTrue);
final session = (await SessionStore().loadCurrent())!;
expect(session.date, '2026-09-17');
expect(session.startedAt, now);
expect(session.reRings, 0);
expect(session.passedHow[SessionLeg.dressed], LegPass.skipped);
expect(session.passedHow.containsKey(SessionLeg.atDesk), isFalse);
});
test('without NFC the session is a bare weigh-in', () async {
hasNfc = false;
expect(await handoff().afterDismiss(away: false), isTrue);
final session = (await SessionStore().loadCurrent())!;
expect(session.bathroomTagRequired, isFalse);
});
test('away from home there is no session', () async {
expect(await handoff().afterDismiss(away: true), isFalse);
expect(await SessionStore().loadCurrent(), isNull);
});
test(
'every dismiss tells native the ring was answered, even when away',
() async {
await handoff().afterDismiss(away: true);
expect(calls, contains('markRingDismissed'));
},
);
test('a bypass that rang during a live session names the re-ring', () async {
final store = SessionStore();
await store.save(
SessionRules.start(
now: now.subtract(const Duration(minutes: 2)),
tags: const SessionTagSettings(tagIds: <String>['b', 'w', 'd']),
nfcAvailable: true,
),
);
bypassCause = 'fsi_revoked';
expect(await handoff().afterDismiss(away: false), isTrue);
final session = (await store.loadCurrent())!;
expect(session.reRings, 1);
expect(session.reRingReasons, <String>['bypass:fsi_revoked']);
expect(calls, contains('takeBypassReRing'));
});
test('a dismiss during a live session is a re-ring', () async {
final store = SessionStore();
await store.save(
SessionRules.start(
now: now.subtract(const Duration(minutes: 12)),
tags: const SessionTagSettings(tagIds: <String>['b', 'w', 'd']),
nfcAvailable: true,
),
);
expect(await handoff().afterDismiss(away: false), isTrue);
final session = (await store.loadCurrent())!;
expect(session.reRings, 1);
expect(session.reRingReasons, <String>['deadline:bathroomIn']);
expect(session.deadlineRung, <SessionLeg>{SessionLeg.bathroomIn});
});
test("a ring after today's session finished just closes", () async {
final store = SessionStore();
await store.save(
SessionRules.start(
now: now,
tags: const SessionTagSettings(),
nfcAvailable: false,
)..outcome = SessionOutcome.completed,
);
expect(await handoff().afterDismiss(away: false), isFalse);
});
test("a ring after today's session was archived just closes", () async {
final store = SessionStore();
await store.archive(
SessionRules.start(
now: now.subtract(const Duration(minutes: 40)),
tags: const SessionTagSettings(),
nfcAvailable: false,
)..outcome = SessionOutcome.completed,
);
expect(await handoff().afterDismiss(away: false), isFalse);
expect(await store.loadCurrent(), isNull);
});
test("yesterday's unfinished session is archived as failed", () async {
final store = SessionStore();
await store.save(
SessionRules.start(
now: now.subtract(const Duration(days: 1)),
tags: const SessionTagSettings(),
nfcAvailable: false,
),
);
expect(await handoff().afterDismiss(away: false), isTrue);
final history = await store.history();
expect(history.single.date, '2026-09-16');
expect(history.single.outcome, SessionOutcome.failed);
expect((await store.loadCurrent())!.date, '2026-09-17');
});
}