Files
swiftcontrol/test/integration/emulation_misc_profiles_test.dart
Jonas Bark db5cc74a12 fix(erg,sram): shared ERG stepping with boundary guards; scope §7 guard to shifting-enabled; coalesce iOS echo presses
ERG (review findings on the ±5W change):
- New ErgPowerStepping extension is the single home for the 5W step and
  500W manual cap, replacing 7 inline copies (physical buttons, hero-card
  buttons/slider, three overlay controllers).
- Boundaries are guards, not clamps: up at >=500W is a no-op like the
  disabled UI button (a 600W FTMS target is no longer slashed to 500),
  down from >500W steps relative, and an unknown baseline (hub-commanded
  ERG reports no target) is a no-op with a new localized message instead
  of slamming the trainer to ~150W.
- ERG shift-up Success carries the button again so +5W presses show
  activity feedback (missed conversion from 843f7985).

SRAM gesture:
- Same-lever repeats inside the 90ms combo window coalesce as duplicates:
  a finger can't re-press in 90ms, but iOS re-delivers a colliding read
  response of the 0xFF edge through the notify callback — it must not
  actuate twice (nor break a combo when it misdecodes to the other lever).
- The §7 multishift guard only runs while shifting is enabled — the one
  state where a held paddle actually bursts (per c94b87f0's --observe
  capture). Bonded rapid tapping and both-lever combos are never
  throttled. Timer-driven instead of DateTime.now() (deterministic under
  fakeAsync) and cleared by _resetGesture on (re)connect.
- The "set up SRAM control" warning fires once per connection, not on
  every undecoded press.

Harness:
- EmulationSession.notify drops notifications for removed peripherals so
  delayed notifies (SRAM double-tap) can't land after teardown.
- The 0xFF press edge is SramAxsConstants.triggerEdge at all call sites;
  the emulation test waits on the named TRIGGER_UUID constant.
- New coverage: guard behavior through processCharacteristic in both
  states, burst suppression while unbonded, all-taps-delivered once
  bonded, ERG boundary/game-set-target cases.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 17:51:35 +02:00

149 lines
6.5 KiB
Dart

import 'package:bike_control/bluetooth/devices/cycplus/cycplus_bc2.dart';
import 'package:bike_control/bluetooth/devices/openbikecontrol/openbikecontrol_device.dart';
import 'package:bike_control/bluetooth/devices/shimano/shimano_di2.dart';
import 'package:bike_control/bluetooth/devices/sram/sram_axs.dart';
import 'package:bike_control/bluetooth/devices/thinkrider/thinkrider_vs200.dart';
import 'package:bike_control/bluetooth/emulation/emulation_profile.dart';
import 'package:bike_control/bluetooth/emulation/profiles/misc_profiles.dart';
import 'package:bike_control/utils/actions/base_actions.dart';
import 'package:bike_control/utils/core.dart';
import 'package:bike_control/utils/keymap/apps/custom_app.dart';
import 'package:bike_control/widgets/title.dart' show packageInfoValue;
import 'package:flutter_test/flutter_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'harness/test_env.dart';
Future<void> main() async {
final env = await IntegrationEnv.setUp();
late StubActions stubActions;
core.connection.initialize();
setUp(() async {
await env.resetState();
stubActions = StubActions();
core.actionHandler = stubActions;
core.emulation.reset();
core.emulation.attach(env.ble);
// OpenBikeControlDevice.handleServices writes an app-info payload that
// reads packageInfoValue!.version. Production sets this global from
// AppTitle's initState (PackageInfo.fromPlatform()); the harness never
// runs that widget, so seed it directly or the write crashes with a
// null-check error before the app-info write test assertion ever runs.
packageInfoValue = PackageInfo(
appName: 'BikeControl',
packageName: 'app.bikecontrol',
version: '1.0.0',
buildNumber: '1',
);
});
tearDown(() async {
await env.resetConnection();
});
Future<void> connect<T extends Object>(EmulationProfile profile) async {
core.emulation.start(profile);
await core.connection.performScanning();
await IntegrationEnv.waitFor(
() => core.connection.devices.whereType<T>().isNotEmpty,
description: '$T detected',
);
}
test('Cycplus BC2: shift-up press performs shiftUp', () async {
await connect<CycplusBc2>(cycplusBc2Profile);
final session = core.emulation.sessionFor('emulated:cycplus-bc2')!;
final up = session.inputs.whereType<EmulatedButton>().firstWhere((b) => b.label == 'Shift up');
up.onDown();
up.onUp();
await IntegrationEnv.waitFor(() => stubActions.performedActions.isNotEmpty, description: 'cycplus action');
// CycplusBc2 passes allowMultiple: true, so BluetoothDevice copies its
// static buttons with sourceDeviceId set to this session's deviceId —
// the performed button no longer == the bare CycplusBc2Buttons.shiftUp
// constant (sourceDeviceId participates in ControllerButton equality).
// Compare by name, like the dynamically-created SRAM/OBC/Di2 buttons below.
expect(
stubActions.performedActions.map((a) => a.button.name),
contains(CycplusBc2Buttons.shiftUp.name),
);
});
test('ThinkRider VS200: shift-up pattern performs one click', () async {
await connect<ThinkRiderVs200>(thinkRiderVs200Profile);
final session = core.emulation.sessionFor('emulated:thinkrider-vs200')!;
session.inputs.whereType<EmulatedAction>().firstWhere((a) => a.label == 'Shift up').run();
await IntegrationEnv.waitFor(() => stubActions.performedActions.isNotEmpty, description: 'thinkrider action');
// Same allowMultiple: true caveat as CycplusBc2 above — compare by name.
expect(
stubActions.performedActions.map((a) => a.button.name),
contains(ThinkRiderVs200Buttons.shiftUp.name),
);
});
test('SRAM AXS: a single tap performs the generic SRAM Button (undecoded, no bond)', () async {
await connect<SramAxs>(sramAxsProfile);
final session = core.emulation.sessionFor('emulated:sram-axs')!;
// A real derailleur only notifies subscribers, but the emulated platform
// delivers regardless — wait for the app's trigger subscription so the
// press can't land mid-connect (handleServices resets gesture state).
await IntegrationEnv.waitFor(
() => session.peripheral.subscriptions.contains(SramAxsConstants.TRIGGER_UUID),
description: 'trigger-char subscription',
);
session.inputs.whereType<EmulatedAction>().firstWhere((a) => a.label == 'Tap').run();
// The tap fires after the double-click window (up to 600 ms) elapses.
await IntegrationEnv.waitFor(
() => stubActions.performedActions.isNotEmpty,
timeout: const Duration(seconds: 3),
description: 'sram tap action',
);
expect(stubActions.performedActions.map((a) => a.button.name), contains('SRAM Button'));
});
test('OpenBikeControl: Shift Up press performs the Shift Up button', () async {
await connect<OpenBikeControlDevice>(openBikeControlProfile);
final session = core.emulation.sessionFor('emulated:openbikecontrol')!;
// The app writes its app-info to the peripheral during handleServices.
await IntegrationEnv.waitFor(
() => session.peripheral.writes.isNotEmpty,
description: 'app-info write',
);
final up = session.inputs.whereType<EmulatedButton>().firstWhere((b) => b.label == 'Shift Up');
up.onDown();
up.onUp();
await IntegrationEnv.waitFor(() => stubActions.performedActions.isNotEmpty, description: 'obc action');
expect(stubActions.performedActions.map((a) => a.button.name), contains('Shift Up'));
});
test('Shimano Di2: channel 1 press performs the D-Fly Channel 1 button', () async {
// ShimanoDi2 creates its channel buttons via getOrAddButton, which is a
// no-op that never populates availableButtons while supportedApp is
// null (BaseDevice.getOrAddButton's bare-StubActions short circuit) —
// and the device's own click handling looks buttons up in
// availableButtons by name. Give it a real (non-CustomApp-duplicating)
// supportedApp so the channel buttons actually get registered.
stubActions.supportedApp = CustomApp();
await connect<ShimanoDi2>(shimanoDi2Profile);
final session = core.emulation.sessionFor('emulated:di2')!;
final channel1 = session.inputs.whereType<EmulatedButton>().firstWhere((b) => b.label == 'D-Fly Channel 1');
channel1.onDown();
channel1.onUp();
await IntegrationEnv.waitFor(() => stubActions.performedActions.isNotEmpty, description: 'di2 action');
expect(stubActions.performedActions.map((a) => a.button.name), contains('D-Fly Channel 1'));
});
}