Files
swiftcontrol/test/models/shifting_config_test.dart
Jonas Bark f44111f02a config: surface cadenceFilterEnabled on ShiftingConfig
Default off; forwarded to FitnessBikeDefinition in the proxy seed path.
The UI affordance is intentionally not added here — observe in-the-wild
behaviour before exposing a knob most users won't need.
2026-05-13 08:35:32 +02:00

237 lines
8.1 KiB
Dart

import 'package:bike_control/models/shifting_config.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:prop/emulators/definitions/fitness_bike_definition.dart';
void main() {
group('ShiftingConfig', () {
test('default() populates sensible values', () {
final cfg = ShiftingConfig.defaults(trainerKey: 'KICKR');
expect(cfg.name, 'Default');
expect(cfg.trainerKey, 'KICKR');
expect(cfg.isActive, true);
expect(cfg.mode, VirtualShiftingMode.targetPower);
expect(cfg.bikeWeightKg, 10.0);
expect(cfg.riderWeightKg, 75.0);
expect(cfg.gradeSmoothing, true);
expect(cfg.maxGear, ShiftingConfig.maxGearDefault);
expect(cfg.gearRatios, isNull);
});
test('default() honours an explicit maxGear override', () {
final cfg = ShiftingConfig.defaults(trainerKey: 'KICKR', maxGear: 30);
expect(cfg.maxGear, 30);
});
test('default() clamps an out-of-range maxGear', () {
expect(ShiftingConfig.defaults(trainerKey: 'KICKR', maxGear: 99).maxGear, 30);
expect(ShiftingConfig.defaults(trainerKey: 'KICKR', maxGear: 0).maxGear, 1);
});
test('fromJson round-trips maxGear + clamps out-of-range stored values', () {
final stored = {
'name': 'x',
'trainerKey': 'KICKR',
'isActive': true,
'mode': 'targetPower',
'bikeWeightKg': 10.0,
'riderWeightKg': 75.0,
'gradeSmoothing': true,
'maxGear': 99,
};
expect(ShiftingConfig.fromJson(stored).maxGear, 30);
final ok = {...stored, 'maxGear': 28};
expect(ShiftingConfig.fromJson(ok).maxGear, 28);
});
test('fromJson defaults maxGear to 24 when missing', () {
final restored = ShiftingConfig.fromJson({
'name': 'Minimal',
'trainerKey': 'KICKR',
'isActive': false,
'mode': 'targetPower',
'bikeWeightKg': 10.0,
'riderWeightKg': 75.0,
'gradeSmoothing': true,
});
expect(restored.maxGear, 24);
});
test('toJson/fromJson round-trips', () {
final cfg = ShiftingConfig(
name: 'Race',
trainerKey: 'KICKR',
isActive: true,
mode: VirtualShiftingMode.trackResistance,
bikeWeightKg: 8.2,
riderWeightKg: 68.5,
gradeSmoothing: false,
gearRatios: List.generate(24, (i) => 0.75 + i * 0.2),
);
final restored = ShiftingConfig.fromJson(cfg.toJson());
expect(restored, cfg);
});
test('fromJson accepts any 1..30 entry gearRatios list', () {
final restored24 = ShiftingConfig.fromJson({
'name': '24g',
'trainerKey': 'KICKR',
'isActive': true,
'mode': 'targetPower',
'bikeWeightKg': 10.0,
'riderWeightKg': 75.0,
'gradeSmoothing': true,
'gearRatios': List<double>.filled(24, 1.0),
});
expect(restored24.gearRatios?.length, 24);
final restored30 = ShiftingConfig.fromJson({
'name': '30g',
'trainerKey': 'KICKR',
'isActive': true,
'mode': 'targetPower',
'bikeWeightKg': 10.0,
'riderWeightKg': 75.0,
'gradeSmoothing': true,
'gearRatios': List<double>.filled(30, 1.0),
});
expect(restored30.gearRatios?.length, 30);
final restored3 = ShiftingConfig.fromJson({
'name': '3g',
'trainerKey': 'KICKR',
'isActive': true,
'mode': 'targetPower',
'bikeWeightKg': 10.0,
'riderWeightKg': 75.0,
'gradeSmoothing': true,
'gearRatios': [0.75, 1.0, 1.5],
});
expect(restored3.gearRatios?.length, 3);
});
test('fromJson drops empty and out-of-bounds gearRatios lists', () {
ShiftingConfig call(List raw) => ShiftingConfig.fromJson({
'name': 'x',
'trainerKey': 'KICKR',
'isActive': true,
'mode': 'targetPower',
'bikeWeightKg': 10.0,
'riderWeightKg': 75.0,
'gradeSmoothing': true,
'gearRatios': raw,
});
expect(call([]).gearRatios, isNull);
expect(call(List<double>.filled(31, 1.0)).gearRatios, isNull);
});
test('fromJson tolerates missing optional fields', () {
final restored = ShiftingConfig.fromJson({
'name': 'Minimal',
'trainerKey': 'KICKR',
'isActive': false,
'mode': 'targetPower',
'bikeWeightKg': 10.0,
'riderWeightKg': 75.0,
'gradeSmoothing': true,
});
expect(restored.gearRatios, isNull);
});
test('copyWith overrides specific fields', () {
final base = ShiftingConfig.defaults(trainerKey: 'KICKR');
final renamed = base.copyWith(name: 'Race');
expect(renamed.name, 'Race');
expect(renamed.trainerKey, base.trainerKey);
expect(renamed.mode, base.mode);
});
test('copyWith clearGearRatios drops the gear list', () {
final withRatios = ShiftingConfig.defaults(
trainerKey: 'KICKR',
).copyWith(gearRatios: List.generate(24, (_) => 1.0));
expect(withRatios.gearRatios, isNotNull);
expect(withRatios.copyWith(clearGearRatios: true).gearRatios, isNull);
});
test('copyWith drops custom ratios when maxGear changes to a mismatched count', () {
final withCustomRatios = ShiftingConfig.defaults(
trainerKey: 'KICKR',
).copyWith(gearRatios: List.generate(24, (i) => 1.0 + i * 0.1));
expect(withCustomRatios.gearRatios?.length, 24);
final resized = withCustomRatios.copyWith(maxGear: 30);
expect(resized.maxGear, 30);
expect(resized.gearRatios, isNull,
reason: 'mismatched length must clear ratios so listeners fall back to defaults');
});
test('copyWith keeps custom ratios when maxGear stays consistent', () {
final ratios = List.generate(24, (i) => 1.0 + i * 0.1);
final withCustomRatios = ShiftingConfig.defaults(
trainerKey: 'KICKR',
).copyWith(gearRatios: ratios);
final unchanged = withCustomRatios.copyWith(bikeWeightKg: 12.0);
expect(unchanged.gearRatios, ratios);
});
test('copyWith with maxGear and matching new ratios keeps the new ratios', () {
final base = ShiftingConfig.defaults(trainerKey: 'KICKR');
final newRatios = List.generate(30, (i) => 0.8 + i * 0.05);
final updated = base.copyWith(maxGear: 30, gearRatios: newRatios);
expect(updated.maxGear, 30);
expect(updated.gearRatios, newRatios);
});
test('values are clamped into legal ranges via fromJson', () {
final cfg = ShiftingConfig.fromJson({
'name': 'OutOfRange',
'trainerKey': 'KICKR',
'isActive': true,
'mode': 'targetPower',
'bikeWeightKg': 999.0,
'riderWeightKg': 5.0,
'gradeSmoothing': true,
});
expect(cfg.bikeWeightKg, lessThanOrEqualTo(ShiftingConfig.bikeWeightMaxKg));
expect(cfg.riderWeightKg, greaterThanOrEqualTo(ShiftingConfig.riderWeightMinKg));
});
group('cadenceFilterEnabled field (#9)', () {
test('defaults to false', () {
final cfg = ShiftingConfig.defaults(trainerKey: 't');
expect(cfg.cadenceFilterEnabled, isFalse);
});
test('round-trips through toJson / fromJson', () {
final cfg = ShiftingConfig.defaults(trainerKey: 't').copyWith(
cadenceFilterEnabled: true,
);
final restored = ShiftingConfig.fromJson(cfg.toJson());
expect(restored.cadenceFilterEnabled, isTrue);
expect(restored, equals(cfg));
});
test('fromJson treats missing key as false', () {
final restored = ShiftingConfig.fromJson(<String, dynamic>{
'name': 'X',
'trainerKey': 't',
'isActive': true,
'mode': 'targetPower',
'bikeWeightKg': 10.0,
'riderWeightKg': 75.0,
'gradeSmoothing': true,
'maxGear': 24,
});
expect(restored.cadenceFilterEnabled, isFalse);
});
test('copyWith preserves and overrides the flag', () {
final cfg = ShiftingConfig.defaults(trainerKey: 't');
expect(cfg.copyWith().cadenceFilterEnabled, isFalse);
expect(cfg.copyWith(cadenceFilterEnabled: true).cadenceFilterEnabled, isTrue);
});
});
});
}