import 'package:device_plugin_interface/device_plugin_interface.dart'; import 'package:eaimar/devices/eaicar/eaicar_device_plugin.dart'; import 'package:eaimar/devices/eaicar/eaicar_protocol.dart'; import 'package:flutter_test/flutter_test.dart'; /// 设备设置面板的 `settings.*` feature key → 发给 EaiCar 的字节,**冻结**。 /// /// 字节来自 lumi 真正发出的命令(`ble_service/.../BleService.kt`),不是协议文档: /// 文档的 CRC 写法是错的、0x15 应答字段也不全。改一个字节等于改协议,先上真机验。 void main() { List frame(String key, Map args) { final c = EaiCarSession.settingsCommand(key, args, random: (max) => 0x08 /* 固定挑"跳舞",只为字节可复现 */)!; return EaiCarProtocol.buildPacket(c.cmd, c.payload); } List withCrc(List body) => [...body, EaiCarProtocol.crc8Maxim(body)]; group('settings.* → 字节', () { test('六条写命令逐条固定', () { expect(frame(DeviceFeatures.settingsVolume, {'value': 5}), withCrc([0xAA, 0x0B, 0x01, 0x05])); expect(frame(DeviceFeatures.settingsBrightness, {'value': 10}), withCrc([0xAA, 0x0A, 0x01, 0x0A])); expect(frame(DeviceFeatures.settingsPower, {'on': true}), withCrc([0xAA, 0x0D, 0x01, 0x01])); expect(frame(DeviceFeatures.settingsPower, {'on': false}), withCrc([0xAA, 0x0D, 0x01, 0x00])); expect(frame(DeviceFeatures.settingsDrivingSense, {'on': true}), withCrc([0xAA, 0x11, 0x01, 0x01])); expect(frame(DeviceFeatures.settingsSwing, {'on': false}), withCrc([0xAA, 0x14, 0x01, 0x00])); // 频次是秒数原值,不是档位索引(lumi settings_view.dart:1517) expect( frame(DeviceFeatures.settingsRandomExpressionInterval, {'seconds': 60}), withCrc([0xAA, 0x13, 0x01, 0x3C])); }); test('随机表情走 0x06、单向不等应答,id 落在 0x00~0x09', () { final c = EaiCarSession.settingsCommand( DeviceFeatures.settingsPlayRandomExpression, const {}, random: (max) => max - 1)!; expect(c.cmd, EaiCarProtocol.cmdControlEmotion); expect(c.expectResponse, isFalse); expect(c.payload, [0x09]); for (var i = 0; i < 50; i++) { final r = EaiCarSession.settingsCommand( DeviceFeatures.settingsPlayRandomExpression, const {})!; expect(r.payload.single, inInclusiveRange(0, 9)); } }); test('其余写命令都等应答(设备会回 0x64 成功码)', () { for (final k in [ DeviceFeatures.settingsVolume, DeviceFeatures.settingsBrightness, ]) { expect(EaiCarSession.settingsCommand(k, {'value': 1})!.expectResponse, isTrue); } for (final k in [ DeviceFeatures.settingsPower, DeviceFeatures.settingsDrivingSense, DeviceFeatures.settingsSwing, ]) { expect(EaiCarSession.settingsCommand(k, {'on': true})!.expectResponse, isTrue); } }); test('参数越界 / 缺参抛 invalid_argument,不会发出去', () { void bad(String key, Map args) { expect( () => EaiCarSession.settingsCommand(key, args), throwsA(isA() .having((e) => e.code, 'code', DeviceErrorCode.invalidArgument)), reason: '$key $args', ); } bad(DeviceFeatures.settingsVolume, {'value': 11}); bad(DeviceFeatures.settingsVolume, {'value': -1}); bad(DeviceFeatures.settingsVolume, {}); bad(DeviceFeatures.settingsBrightness, {'value': 16}); bad(DeviceFeatures.settingsPower, {'on': 1}); bad(DeviceFeatures.settingsSwing, {}); // 45 秒不在 9 档里 bad(DeviceFeatures.settingsRandomExpressionInterval, {'seconds': 45}); }); test('不认识的键返回 null(由 invokeFeature 抛 not_supported)', () { expect(EaiCarSession.settingsCommand('settings.nope', const {}), isNull); }); }); group('0x15 → settings.query 返回值', () { test('14 字节逐项映射到通用键名', () { // 与 eaicar_protocol_test 里同一条样本:音量6/频次30/亮度8/电量80/1.2.3/ // 启停开/摇头风格3/音乐表情开/陀螺仪关/电机开 final s = EaiCarAllSettings.parse( [0x00, 0x06, 0x1E, 0x08, 0x50, 0x01, 0x02, 0x03, 0x01, 0x03, 0x01, 0x00, 0x00, 0x01])!; final m = s.toSettingsMap(); final items = m['items'] as Map; expect(items['volume'], {'value': 6, 'min': 0, 'max': 10}); expect(items['brightness'], {'value': 8, 'min': 0, 'max': 10}); expect((items['random_expression_interval'] as Map)['value'], 30); expect((items['random_expression_interval'] as Map)['options'], EaiCarProtocol.randomExpressionIntervalOptions); expect(items['power'], {'on': true}); expect(items['driving_sense'], {'on': false}); expect(items['swing'], {'on': true}); expect(items.containsKey('play_random_expression'), isTrue); expect(m['battery'], 80); expect(m['firmware'], '1.2.3'); expect(m['otaAbnormal'], false); }); test('copyWith 只改指定项(写成功后的本地回填靠它)', () { final s = EaiCarAllSettings.parse( [0x01, 0x00, 0x14, 0x0A, 0x3C, 0x02, 0x0C, 0x0C, 0x00, 0x02, 0x01, 0x01, 0x04, 0x01])!; final t = s.copyWith(volume: 7, gyroscope: false); expect(t.volume, 7); expect(t.gyroscope, isFalse); expect(t.brightness, s.brightness); expect(t.battery, s.battery); expect(t.firmwareVersion, s.firmwareVersion); expect(t.motor, s.motor); expect(t.otaAbnormal, s.otaAbnormal); }); }); test('EaiCar 会话声明了 settingsPanel(面板按它出现)', () { expect(EaiCarSession.caps.contains(DeviceCapability.settingsPanel), isTrue); }); }