Files
swiftcontrol/test/integration/trainer_app_not_connected_message_test.dart
Jonas Bark b5c25c5b21 Clearer log message when a trainer-app action can't be delivered (issue #367)
Pressing a virtual-shifting button after the trainer app (Zwift) had
disconnected logged the misleading "Could not perform <key>: No action
assigned" — but an action *is* assigned; the trainer app just isn't
connected to receive it.

Root cause: a pure in-game action (gear shift) has no keyboard/touch
fallback, so when no trainer app accepts it the press fell through to the
platform keyboard path, which reports "no action assigned". This only
surfaced with local control enabled, because `local` counts as a
"connected" trainer connection yet its sendAction always returns
NotHandled — so isTrainerConnected() was true and the generic
"no connection method active" guard was bypassed.

Fix: detect delivery failure after _handleDirectConnect. When a
trainer-app-only keypair (KeyPair.isTrainerAppActionOnly — an in-game
action with no keyboard/touch/command/screenshot fallback, excluding
headwind/trainer-control which target a proxy/accessory) goes unhandled,
report "Could not perform <key>: <app> is not connected" instead. Such
keypairs also now skip the generic isTrainerConnected early-return so the
specific message wins consistently. Keypairs with a keyboard fallback are
untouched and still fall through to the keyboard.

Tests: KeyPair.isTrainerAppActionOnly discrimination (keyboard/touch/
command fallback and outside-trainer-app actions excluded) and an
end-to-end regression covering the exact #367 trigger (local control on).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 09:46:38 +02:00

90 lines
3.3 KiB
Dart

import 'package:bike_control/bluetooth/devices/zwift/constants.dart';
import 'package:bike_control/gen/l10n.dart';
import 'package:bike_control/utils/actions/base_actions.dart';
import 'package:bike_control/utils/core.dart';
import 'package:bike_control/utils/keymap/apps/zwift.dart';
import 'package:bike_control/utils/keymap/buttons.dart';
import 'package:bike_control/widgets/keymap_explanation.dart' show SplitByUppercase;
import 'package:flutter_test/flutter_test.dart';
import 'harness/test_env.dart';
/// Real [BaseActions.performAction] — only [cleanup] is stubbed, so the
/// keymap → pro-guard → connection-routing logic under test runs for real.
class _RealChainActions extends BaseActions {
_RealChainActions() : super(supportedModes: const [SupportedMode.keyboard, SupportedMode.touch]);
@override
void cleanup() {}
}
/// Regression for issue #367: when the trainer app (Zwift) has disconnected,
/// pressing a virtual-shifting button reported the misleading
/// "Could not perform <key>: No action assigned" instead of saying the trainer
/// app is not connected.
Future<void> main() async {
final env = await IntegrationEnv.setUp();
core.connection.initialize();
late _RealChainActions actions;
setUp(() async {
// A Zwift mDNS connection is enabled (so a connection method exists) but
// nothing is connected to it — exactly the "Zwift was closed" state.
await env.resetState(prefs: {
'trainer_app': 'Zwift',
'zwift_mdns_emulator_enabled': true,
});
actions = _RealChainActions()..supportedApp = Zwift();
core.actionHandler = actions;
});
tearDown(() async {
await env.resetConnection();
});
Future<ActionResult> press(ControllerButton button) =>
actions.performAction(button, isKeyDown: true, isKeyUp: true);
test('a virtual-shifting press with no connected trainer names the trainer app', () async {
final result = await press(ZwiftButtons.shiftUpRight);
expect(result, isA<Error>());
expect((result as Error).type, ErrorType.trainerNotConnected);
// The real reason — not the misleading "No action assigned".
expect(result.message, contains('Zwift'));
expect(result.message, contains('not connected'));
expect(result.message, isNot(contains('No action assigned')));
});
test('still fires when local control is enabled (the exact #367 trigger)', () async {
// Local keyboard control makes isTrainerConnected() true, which previously
// let the press slip through to the "no action assigned" keyboard fallback.
await env.resetState(prefs: {
'trainer_app': 'Zwift',
'zwift_mdns_emulator_enabled': true,
'local_control_enabled': true,
});
actions = _RealChainActions()..supportedApp = Zwift();
core.actionHandler = actions;
final result = await press(ZwiftButtons.shiftUpRight);
expect(result, isA<Error>());
expect((result as Error).type, ErrorType.trainerNotConnected);
expect(result.message, isNot(contains('No action assigned')));
});
test('localized message matches the new ARB string', () async {
final result = await press(ZwiftButtons.shiftUpRight);
expect(
result.message,
AppLocalizations.current.trainerAppNotConnectedForButton(
ZwiftButtons.shiftUpRight.name.splitByUpperCase(),
'Zwift',
),
);
});
}