import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:mebarista/main.dart'; import 'package:mebarista/models/machine_state.dart'; import 'package:mebarista/services/bluetooth_service.dart'; class DashboardService extends MeCoffeeService { void showState(MachineState value) { state = value; } } void main() { setUp(() { SharedPreferences.setMockInitialValues({'brewTemperature': 99.5}); }); testWidgets( 'dashboard displays the saved brew target and disconnected state', (tester) async { await tester.pumpWidget(const ProviderScope(child: MyApp())); await tester.pumpAndSettle(); expect(find.text('meBarista Dashboard'), findsOneWidget); expect(find.text('Boiler Temperature'), findsOneWidget); expect(find.text('BREW TARGET'), findsOneWidget); expect(find.text('99.5°C'), findsOneWidget); expect(find.byIcon(Icons.bluetooth_searching), findsOneWidget); expect(find.text('No data yet'), findsOneWidget); expect(tester.takeException(), isNull); }, ); testWidgets('dashboard settings button opens Machine Settings', ( tester, ) async { await tester.pumpWidget(const ProviderScope(child: MyApp())); await tester.pumpAndSettle(); await tester.tap(find.byIcon(Icons.settings)); await tester.pumpAndSettle(); expect(find.text('Machine Settings'), findsOneWidget); expect(find.text('Brew Temperature'), findsOneWidget); expect(find.text('99.5°C'), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('dashboard distinguishes stale readings from live temperature', ( tester, ) async { final service = DashboardService(); await tester.pump(); service.showState( MachineState( connectionStatus: 'Connected', temperature: 98.5, lastTelemetryAt: DateTime(2026, 10, 6), telemetryStale: true, heatingPower: 1, isHeating: true, isBrewing: true, shotTimer: 12, ), ); await tester.pumpWidget( ProviderScope( overrides: [bluetoothServiceProvider.overrideWith((ref) => service)], child: const MyApp(), ), ); await tester.pumpAndSettle(); expect(find.textContaining('Telemetry stale'), findsOneWidget); expect(find.text('98.5°C'), findsOneWidget); expect(find.text('Unavailable'), findsOneWidget); expect( tester .widget(find.byType(LinearProgressIndicator)) .value, 0, ); service.showState(service.state.copyWith(telemetryStale: false)); await tester.pumpAndSettle(); expect(find.textContaining('Telemetry stale'), findsNothing); await tester.pumpWidget(const SizedBox()); }); testWidgets('dashboard shows Steam instead of a PID percentage', ( tester, ) async { final service = DashboardService(); await tester.pump(); service.showState( MachineState( connectionStatus: 'Connected', temperature: 120, lastTelemetryAt: DateTime(2026, 10, 6), telemetryStale: false, isHeating: true, isSteaming: true, ), ); await tester.pumpWidget( ProviderScope( overrides: [bluetoothServiceProvider.overrideWith((ref) => service)], child: const MyApp(), ), ); await tester.pump(const Duration(milliseconds: 100)); expect(find.text('Steam'), findsOneWidget); expect(find.text('0%'), findsNothing); expect( tester .widget(find.byType(LinearProgressIndicator)) .value, isNull, ); await tester.pumpWidget(const SizedBox()); }); testWidgets('dashboard diagnostics button opens local logs', (tester) async { await tester.pumpWidget(const ProviderScope(child: MyApp())); await tester.pumpAndSettle(); await tester.tap(find.byTooltip('Diagnostics')); await tester.pumpAndSettle(); expect(find.text('Diagnostics'), findsOneWidget); expect(find.text('No diagnostic entries yet'), findsOneWidget); }); testWidgets('dashboard connection control clears the bottom navigation bar', ( tester, ) async { tester.view.physicalSize = const Size(800, 900); tester.view.devicePixelRatio = 1; tester.view.padding = const FakeViewPadding(bottom: 48); addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.view.resetPadding); await tester.pumpWidget(const ProviderScope(child: MyApp())); await tester.pumpAndSettle(); await tester.drag( find.byType(SingleChildScrollView), const Offset(0, -2000), ); await tester.pumpAndSettle(); final button = find.ancestor( of: find.text('Connect to meCoffee'), matching: find.byWidgetPredicate((widget) => widget is ElevatedButton), ); final safeBottom = tester.view.physicalSize.height - tester.view.padding.bottom; expect(tester.getRect(button).bottom, lessThanOrEqualTo(safeBottom - 16)); expect(button.hitTestable(), findsOneWidget); expect(tester.takeException(), isNull); }); }