import 'package:flutter_test/flutter_test.dart'; import 'package:rippr/src/config/config.dart'; import 'package:rippr/src/hud/hud_layout_controller.dart'; import 'package:rippr/src/hud/hud_metric.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { Future freshConfig() async { SharedPreferences.setMockInitialValues({}); return Config.load(); } group('HudLayoutController', () { test('updatePosition updates only the given metric, clamped', () async { final controller = HudLayoutController(await freshConfig()); final before = controller.state[HudMetric.distance]!; controller.updatePosition(HudMetric.speed, 5.0, 5.0); expect(controller.state[HudMetric.speed]!.x, lessThanOrEqualTo(1.0)); expect(controller.state[HudMetric.distance]!.x, before.x, reason: 'moving one metric must not disturb another'); }); test('updateSize clamps to the legibility floor/ceiling', () async { final controller = HudLayoutController(await freshConfig()); controller.updateSize(HudMetric.speed, 0.0, 0.0); expect(controller.state[HudMetric.speed]!.width, greaterThan(0.0)); expect(controller.state[HudMetric.speed]!.height, greaterThan(0.0)); }); test('setVisible(false) then setVisible(true) restores the last saved position, ' 'not a fresh default', () async { final controller = HudLayoutController(await freshConfig()); controller.updatePosition(HudMetric.avgSpeed, 0.6, 0.6); final movedX = controller.state[HudMetric.avgSpeed]!.x; controller.setVisible(HudMetric.avgSpeed, false); expect(controller.state[HudMetric.avgSpeed]!.visible, isFalse); controller.setVisible(HudMetric.avgSpeed, true); expect(controller.state[HudMetric.avgSpeed]!.visible, isTrue); expect(controller.state[HudMetric.avgSpeed]!.x, movedX, reason: 'a metric with a real saved position keeps it when re-enabled'); }); test('persist writes the current state to Config', () async { final config = await freshConfig(); final controller = HudLayoutController(config); controller.updatePosition(HudMetric.speed, 0.4, 0.4); await controller.persist(); final reloaded = config.hudLayout; expect(reloaded[HudMetric.speed]!.x, 0.4); }); test('edits before persist are not visible to a fresh read of Config', () async { final config = await freshConfig(); final controller = HudLayoutController(config); controller.updatePosition(HudMetric.speed, 0.4, 0.4); // No persist() call yet. final reloaded = config.hudLayout; expect(reloaded[HudMetric.speed]!.x, isNot(0.4), reason: 'a drag in progress must only touch in-memory state -- ' 'persisting every frame is the ticket\'s own named risk'); }); }); }