mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-11-29 09:11:17 +01:00
The home screen is being rebuilt as the setup chain: one card per link, each carrying the same status light and the same tick-off checklist. This lands the parts that decide *what* the screen says, ahead of the widgets that draw it. chain_state.dart is pure data with no Flutter, no BLE and no `core`, and steps carry ids rather than sentences, so "which step is done or pending" is unit-testable without a locale or a radio. chain_builder.dart turns a plain ChainInputs snapshot into cards; deriveBanner reads the cards back, so the banner can never contradict what is under it. Remembered devices fix a real gap: a controller that is switched off or out of range disappeared from the app entirely, so riders could not see what they owned or edit a mapping away from the bike. We now store the advertisement a device connected with — exactly the fields BluetoothDevice.fromScanResult reads — and rebuild it into a real device object of the right subclass, kept out of the connect queue. Trainers are remembered as data only, since their card needs a name, not a contour. Tracking which ids connected this session is what separates a red "lost connection" from an amber "out of range"; a fresh launch with the controller in a drawer must not paint the screen red. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
213 lines
8.1 KiB
Dart
213 lines
8.1 KiB
Dart
import 'dart:convert';
|
||
import 'dart:typed_data';
|
||
|
||
import 'package:bike_control/models/remembered_device.dart';
|
||
import 'package:bike_control/repositories/remembered_devices_repository.dart';
|
||
import 'package:flutter_test/flutter_test.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
import 'package:universal_ble/universal_ble.dart';
|
||
|
||
final _now = DateTime.utc(2026, 8, 11, 8, 30);
|
||
|
||
RememberedDevice remembered({
|
||
String deviceId = 'AA:BB:CC',
|
||
String? name = 'Zwift Click',
|
||
RememberedDeviceKind kind = RememberedDeviceKind.controller,
|
||
List<String> services = const ['0000fc82-0000-1000-8000-00805f9b34fb'],
|
||
List<ManufacturerData> manufacturerData = const [],
|
||
DateTime? lastConnected,
|
||
}) {
|
||
return RememberedDevice(
|
||
deviceId: deviceId,
|
||
name: name,
|
||
kind: kind,
|
||
lastConnected: lastConnected ?? _now,
|
||
services: services,
|
||
manufacturerData: manufacturerData,
|
||
);
|
||
}
|
||
|
||
void main() {
|
||
group('RememberedDevice serialization', () {
|
||
test('round-trips every field', () {
|
||
final original = remembered(
|
||
manufacturerData: [
|
||
ManufacturerData(0x0651, Uint8List.fromList([0x03, 0x11, 0xFF])),
|
||
],
|
||
);
|
||
|
||
final restored = RememberedDevice.tryFromJson(jsonDecode(jsonEncode(original.toJson())));
|
||
|
||
expect(restored, isNotNull);
|
||
expect(restored!.deviceId, original.deviceId);
|
||
expect(restored.name, original.name);
|
||
expect(restored.kind, original.kind);
|
||
expect(restored.lastConnected, original.lastConnected);
|
||
expect(restored.services, original.services);
|
||
expect(restored.manufacturerData.single.companyId, 0x0651);
|
||
expect(restored.manufacturerData.single.payload, [0x03, 0x11, 0xFF]);
|
||
});
|
||
|
||
test('rebuilds the scan result the device factory reads', () {
|
||
final scanResult = remembered(
|
||
manufacturerData: [
|
||
ManufacturerData(0x0651, Uint8List.fromList([0x09])),
|
||
],
|
||
).toScanResult();
|
||
|
||
expect(scanResult.deviceId, 'AA:BB:CC');
|
||
expect(scanResult.name, 'Zwift Click');
|
||
expect(scanResult.services, ['0000fc82-0000-1000-8000-00805f9b34fb']);
|
||
expect(scanResult.manufacturerDataList.single.companyId, 0x0651);
|
||
// We have not heard from it this session, so it must not claim a signal.
|
||
expect(scanResult.rssi, isNull);
|
||
});
|
||
|
||
test('captures a live advertisement, keeping the raw name', () {
|
||
final device = RememberedDevice.fromScanResult(
|
||
BleDevice(deviceId: 'X', name: 'Zwift Click |