import 'dart:async'; 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/session_store.dart'; import 'package:wake_alarm_sync/services/session_tag_settings.dart'; /// The check-in half of the session screen (energy + weight) and the two /// publishes: split from `session_screen_test.dart` at the 250-line cap. void main() { TestWidgetsFlutterBinding.ensureInitialized(); late DateTime now; late List publishes; late List storeHadSession; Completer? holdFirst; void mockChannel() { TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger .setMockMethodCallHandler( const MethodChannel(AlarmScheduler.channelName), (call) async => call.method == 'hasNfc' ? true : null, ); } Future tap(String tagId) async { const codec = StandardMethodCodec(); await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger .handlePlatformMessage( AlarmScheduler.channelName, codec.encodeMethodCall(MethodCall('onNfcTag', tagId)), (_) {}, ); } setUp(() async { now = DateTime(2026, 9, 19, 8, 4); publishes = []; storeHadSession = []; holdFirst = null; SharedPreferences.setMockInitialValues({}); mockChannel(); await SessionTagSettings.replaceAll(['bath', 'ward', 'desk'], ''); }); Future pumpSession(WidgetTester tester) async { await tester.pumpWidget( MaterialApp( routes: { '/': (_) => SessionScreen( clock: () => now, publish: () async { // What the real publisher stages: is the session on disk yet? storeHadSession.add(await SessionStore().loadCurrent() != null); final hold = holdFirst; if (publishes.isEmpty && hold != null) { publishes.add('start (held)'); return await hold.future; } publishes.add(publishes.isEmpty ? 'start' : 'finish'); return 'pushed'; }, ), '/ring': (_) => const Scaffold(body: Text('RING')), }, ), ); await tester.pump(); await tester.pump(); } Future submit(WidgetTester tester, String kg) async { await tester.enterText(find.byType(TextField), kg); await tester.tap(find.text('Log weight')); await tester.pump(); } testWidgets('the start publish runs after the session is stored', ( tester, ) async { await pumpSession(tester); expect(publishes, ['start']); expect(storeHadSession, [true]); }); testWidgets('no energy tapped: the weight is refused and nothing sticks', ( tester, ) async { await pumpSession(tester); await submit(tester, '72'); expect(find.textContaining('Tap your energy'), findsOneWidget); expect(find.byType(TextField), findsOneWidget); expect((await SessionStore().loadCurrent())?.pendingWeightKg, isNull); }); testWidgets('energy then weight records both on the session', (tester) async { await pumpSession(tester); await tester.tap(find.widgetWithText(ChoiceChip, '5')); await tester.pump(); await submit(tester, '72'); expect(find.textContaining('now tap the bathroom tag'), findsOneWidget); final session = await SessionStore().loadCurrent(); expect(session?.energy, 5); expect(session?.pendingWeightKg, 72); }); testWidgets('an unknown tag and bad weights are reported', (tester) async { await pumpSession(tester); await tester.tap(find.widgetWithText(ChoiceChip, '3')); await tester.pump(); await submit(tester, 'x'); expect(find.textContaining('Enter your weight'), findsOneWidget); await submit(tester, '1'); expect(find.textContaining('not a body weight'), findsOneWidget); expect(find.byType(TextField), findsOneWidget); await tap('mystery'); await tester.pump(); expect(find.text('Unknown tag'), findsOneWidget); }); testWidgets('the finish publish waits for a start publish in flight', ( tester, ) async { // No session tags: the check-in is the whole morning, so it finishes // seconds after it started, while the start publish may still be out. await SessionTagSettings.replaceAll(['', '', ''], ''); holdFirst = Completer(); await pumpSession(tester); await tester.tap(find.widgetWithText(ChoiceChip, '2')); await tester.pump(); await submit(tester, '72'); await tester.pump(); expect(publishes, ['start (held)']); holdFirst!.complete('late'); await tester.pump(); await tester.pump(); expect(publishes, ['start (held)', 'finish']); }); }