Files
swiftcontrol/test/settings/sram_persistence_test.dart
Jonas Bark bdfbe4395c fix(sram): differentiate left/right shifters and default one lever to shift down
Testers reported both AXS shifters doing the same thing and gears only ever
increasing, with presses logged as a generic "SRAM Paddle". Root cause: both
drop-bar paddles report button mask 1, so the mask alone can't tell them apart,
and every discovered button was hardcoded to InGameAction.shiftUp.

- Use the button event's controller device_type (prop SramPress.deviceType,
  field 2) to name and key buttons, so left and right become distinct keymap
  entries even in a plaintext (serial-less) session — the common case in the
  field. Names now resolve to "SRAM Left/Right Shifter" without needing an
  advert or the sparse d9050003 serial.
- Default the LEFT paddle to shift DOWN and everything else to shift UP so the
  shifters aren't identical out of the box ("one lever changes down") without
  hunting through the keymap. Every button stays freely remappable.
- Persist the device_type in the button record so a restart re-registers the
  same left/right name + default instead of a duplicate entry; two serial-less
  paddles that differ only by side stay separate, and a legacy side-less record
  upgrades in place once the side is known.
- Remember a shifter's side against its serial (preserving any known model) when
  both are seen, so its other buttons resolve the same name.
- Bump the prop submodule to the field-2 decode.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H48NvkfXYm2spiiwcTQsv8
2026-07-09 09:12:41 +02:00

74 lines
2.9 KiB
Dart

import 'package:bike_control/utils/settings/settings.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:prop/prop.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() => SharedPreferences.setMockInitialValues({}));
test('key round-trips per serial', () async {
final s = Settings();
s.prefs = await SharedPreferences.getInstance();
await s.setSramKey('SRAM 42', 'ab7c706800759ce024498cb423e0adc9');
expect(s.getSramKey('SRAM 42'), 'ab7c706800759ce024498cb423e0adc9');
expect(s.getSramKey('SRAM 99'), isNull);
// Null removes an existing key (not stored as the literal "null").
await s.setSramKey('SRAM 42', 'abc');
await s.setSramKey('SRAM 42', null);
expect(s.getSramKey('SRAM 42'), isNull);
});
test('backup and disabled flag round-trip', () async {
final s = Settings();
s.prefs = await SharedPreferences.getInstance();
await s.setSramBackup('SRAM 42', {
'd9050028': const SramReactionTrigger([0], [3]),
});
expect(s.getSramBackup('SRAM 42')!['d9050028']!.buttonMasks, [3]);
// Absent backup returns null (not an empty map).
expect(s.getSramBackup('SRAM 99'), isNull);
expect(s.getSramShiftingDisabled('SRAM 42'), isFalse);
await s.setSramShiftingDisabled('SRAM 42', true);
expect(s.getSramShiftingDisabled('SRAM 42'), isTrue);
});
test('discovered buttons dedupe', () async {
final s = Settings();
s.prefs = await SharedPreferences.getInstance();
await s.addSramButton('SRAM 42', 100, 1);
await s.addSramButton('SRAM 42', 100, 1); // duplicate
await s.addSramButton('SRAM 42', 200, 1);
expect(s.getSramButtons('SRAM 42').length, 2);
// Degraded -1 serial/mask round-trips through the "serial:mask" encoding.
await s.addSramButton('SRAM 42', -1, 5);
expect(s.getSramButtons('SRAM 42'), contains((serial: -1, mask: 5, deviceType: null)));
});
test('device_type round-trips and upgrades a pre-side record in place', () async {
final s = Settings();
s.prefs = await SharedPreferences.getInstance();
// Two serial-less paddle presses that only differ by side must NOT collapse.
await s.addSramButton('SRAM 42', -1, 1, 0); // left paddle
await s.addSramButton('SRAM 42', -1, 1, 1); // right paddle
final both = s.getSramButtons('SRAM 42');
expect(both.length, 2);
expect(both, contains((serial: -1, mask: 1, deviceType: 0)));
expect(both, contains((serial: -1, mask: 1, deviceType: 1)));
// A legacy side-less record is upgraded in place (not duplicated) once the
// device_type becomes known.
await s.addSramButton('SRAM 99', 5, 2); // legacy: no device_type
await s.addSramButton('SRAM 99', 5, 2, 1); // now with a side
final upgraded = s.getSramButtons('SRAM 99');
expect(upgraded.length, 1);
expect(upgraded.single.deviceType, 1);
});
}