Files
swiftcontrol/test/bluetooth/sram_axs_button_model_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

108 lines
5.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:bike_control/bluetooth/devices/sram/sram_axs.dart';
import 'package:bike_control/utils/actions/base_actions.dart';
import 'package:bike_control/utils/core.dart';
import 'package:bike_control/utils/keymap/buttons.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:prop/prop.dart' show SramDeviceInfo;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:universal_ble/universal_ble.dart';
SramAxs _device() => SramAxs(BleDevice(deviceId: 'dev1', name: 'SRAM 42'));
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
// Constructing a BluetoothDevice touches `core.actionHandler` (BaseDevice's
// constructor checks `supportedApp`), so it must be initialized before any
// test builds a SramAxs, even though `logicalButtonName` itself is pure.
// `logicalButtonName` never touches `core.settings`, so unlike other device
// tests (e.g. test/shimano_di2_test.dart) we don't need `core.settings.init()`.
core.actionHandler = StubActions();
test('names a decoded paddle press by shifter, in first-seen order', () {
final d = _device();
// First unique serial → "Shifter A"; paddle mask (1) → "Paddle".
expect(d.logicalButtonName(100, 1), 'SRAM Shifter A – Paddle');
// Second serial → "Shifter B".
expect(d.logicalButtonName(200, 1), 'SRAM Shifter B – Paddle');
});
test('degraded press (mask null) names by serial only', () {
final d = _device();
expect(d.logicalButtonName(100, null), 'SRAM Shifter A');
});
test('button decoded before the serial arrives gets a shifter-less label (no double space)', () {
final d = _device();
final name = d.logicalButtonName(null, 1);
expect(name, 'SRAM Paddle');
expect(name.contains(' '), isFalse);
});
test('a decoded device_type (field 2) names left/right with no serial or advert', () {
final d = _device();
// Both paddles report mask 1; only the device_type tells them apart. Without
// it they'd both be "SRAM Paddle" — the tester's "both shifters do the same".
expect(d.logicalButtonName(null, 1, deviceType: 0), 'SRAM Left Shifter');
expect(d.logicalButtonName(null, 1, deviceType: 1), 'SRAM Right Shifter');
// The left/right names are distinct → distinct keymap entries.
expect(d.logicalButtonName(null, 1, deviceType: 0), isNot(d.logicalButtonName(null, 1, deviceType: 1)));
// A decoded side also labels the aux button per §6.4.
expect(d.logicalButtonName(null, 2, deviceType: 0), 'SRAM Left – Aux Top');
});
test('a stored device_type re-registers under the same name as the live press', () {
final d = _device();
// Live press named it "SRAM Left Shifter"; the persisted record (serial -1,
// mask 1, device_type 0) must reproduce that exact name — no duplicate entry.
expect(d.storedButtonName(-1, 1, storedDeviceType: 0), d.logicalButtonName(null, 1, deviceType: 0));
expect(d.storedButtonName(-1, 1, storedDeviceType: 1), 'SRAM Right Shifter');
});
test('the two paddles default to opposite actions so a lever can shift down', () {
final d = _device();
// Left paddle → shift DOWN; right paddle → shift UP. This is the fix for
// "shifting can only increase gears" — the shifters are no longer identical.
expect(d.defaultAction(0, 1), InGameAction.shiftDown); // left paddle
expect(d.defaultAction(1, 1), InGameAction.shiftUp); // right paddle
// Non-paddle buttons and unknown-side presses stay on the safe shift-up default.
expect(d.defaultAction(0, 2), InGameAction.shiftUp); // left aux
expect(d.defaultAction(null, 1), InGameAction.shiftUp); // side unknown
});
test('fully anonymous press falls back to the legacy label', () {
final d = _device();
expect(d.logicalButtonName(null, null), 'SRAM Button');
});
test('stored -1 sentinels re-register as the degraded label, not Button 0x-1', () {
final d = _device();
expect(d.storedButtonName(100, -1), 'SRAM Shifter A');
expect(d.storedButtonName(-1, -1), 'SRAM Button');
});
test('§6.4: names buttons from an advertised shifter type/model, degrades without one', () {
final d = _device();
final leftLever = SramDeviceInfo(serial: 100, model: 1007, deviceType: 0);
final rightLever = SramDeviceInfo(serial: 200, model: 1007, deviceType: 1);
expect(d.buttonNameFor(leftLever, 100, 1), 'SRAM Left Shifter');
expect(d.buttonNameFor(leftLever, 100, 2), 'SRAM Left – Aux Top');
expect(d.buttonNameFor(rightLever, 200, 1), 'SRAM Right Shifter');
expect(d.buttonNameFor(null, 100, 1), 'SRAM Shifter A – Paddle'); // no advert → fallback
});
test('§6.4 naming is stable across sessions via a persisted shifter advert', () async {
SharedPreferences.setMockInitialValues({});
core.settings.prefs = await SharedPreferences.getInstance();
// A left drop-bar lever (type 0, model 1007) seen in a PRIOR session, with
// no live advert this session — naming must still resolve from persistence.
core.settings.setSramShifter('dev1', 500, 0, 1007);
final d = _device(); // deviceId 'dev1' → matches the persisted scope
expect(d.logicalButtonName(500, 1), 'SRAM Left Shifter');
expect(d.logicalButtonName(500, 2), 'SRAM Left – Aux Top');
// An un-persisted serial still degrades to the legacy label.
expect(d.logicalButtonName(600, 1), 'SRAM Shifter A – Paddle');
});
}