me_coffee_protocol_test.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. import 'package:flutter_test/flutter_test.dart';
  2. import 'package:mebarista/models/machine_state.dart';
  3. import 'package:mebarista/services/me_coffee_protocol.dart';
  4. void main() {
  5. group('MeCoffeeProtocol', () {
  6. test('parses tmp with timestamp field correctly', () {
  7. final state = MeCoffeeProtocol.parse(
  8. 'tmp 12345 10100 9850 0',
  9. MachineState(),
  10. );
  11. expect(state.targetTemperature, 101.0);
  12. expect(state.temperature, 98.5);
  13. });
  14. test('encodes schedule as seconds since midnight', () {
  15. final bytes = MeCoffeeProtocol.encodeSchedule(
  16. true,
  17. '07:30',
  18. true,
  19. '22:15',
  20. );
  21. final payload = String.fromCharCodes(bytes);
  22. expect(payload, contains('cmd set tmrwnbl 1\n'));
  23. expect(payload, contains('cmd set tmron 27000\n'));
  24. expect(payload, contains('cmd set tmrsnbl 1\n'));
  25. expect(payload, contains('cmd set tmroff 80100\n'));
  26. });
  27. test('parses schedule fields from cmd get', () {
  28. var state = MachineState();
  29. state = MeCoffeeProtocol.parse('cmd get tmrwnbl 1 OK', state);
  30. state = MeCoffeeProtocol.parse('cmd get tmron 27000 OK', state);
  31. state = MeCoffeeProtocol.parse('cmd get tmrsnbl 1 OK', state);
  32. state = MeCoffeeProtocol.parse('cmd get tmroff 80100 OK', state);
  33. expect(state.wakeupEnabled, isTrue);
  34. expect(state.wakeupTime, '07:30');
  35. expect(state.shutdownEnabled, isTrue);
  36. expect(state.shutdownTime, '22:15');
  37. });
  38. test('parses bare get format for legacy compatibility', () {
  39. final state = MeCoffeeProtocol.parse('get pivlv 1', MachineState());
  40. expect(state.valveClosedWhilePaused, isTrue);
  41. });
  42. test('parses pid power and heating state', () {
  43. final state = MeCoffeeProtocol.parse(
  44. 'pid 1000 2000 3000 4000',
  45. MachineState(),
  46. );
  47. expect(
  48. state.heatingPower,
  49. closeTo((1000 + 2000 + 3000 + 4000) / 65535.0, 0.00001),
  50. );
  51. expect(state.isHeating, isTrue);
  52. });
  53. for (final suffix in ['', ' OK']) {
  54. test('parses three-component PID frame with suffix "$suffix"', () {
  55. final state = MeCoffeeProtocol.parse(
  56. 'pid 1000 2000 3000$suffix',
  57. MachineState(),
  58. );
  59. expect(state.heatingPower, closeTo(6000 / 65535, 0.00001));
  60. expect(state.isHeating, isTrue);
  61. });
  62. test('parses four-component PID frame with suffix "$suffix"', () {
  63. final state = MeCoffeeProtocol.parse(
  64. 'pid 1000 2000 3000 4000$suffix',
  65. MachineState(),
  66. );
  67. expect(state.heatingPower, closeTo(10000 / 65535, 0.00001));
  68. });
  69. test('parses temperature without sensor 2 with suffix "$suffix"', () {
  70. final state = MeCoffeeProtocol.parse(
  71. 'tmp 12345 10100 9850$suffix',
  72. MachineState(sensor2Temperature: 97),
  73. );
  74. expect(state.temperature, 98.5);
  75. expect(state.targetTemperature, 101);
  76. expect(state.sensor2Temperature, 97);
  77. });
  78. }
  79. test('OK-only frame and missing numeric fields leave state unchanged', () {
  80. final original = MachineState(temperature: 96, heatingPower: 0.4);
  81. for (final message in ['OK', 'pid 1000 2000 OK', 'tmp 1 10100 OK']) {
  82. expect(MeCoffeeProtocol.parse(message, original), same(original));
  83. }
  84. });
  85. test('clamps pid heating power to 1.0', () {
  86. final state = MeCoffeeProtocol.parse(
  87. 'pid 65535 65535 65535 65535',
  88. MachineState(),
  89. );
  90. expect(state.heatingPower, 1.0);
  91. expect(state.isHeating, isTrue);
  92. });
  93. test('parses shot timer start and stop transitions', () {
  94. final started = MeCoffeeProtocol.parse('sht 0 0', MachineState());
  95. final stopped = MeCoffeeProtocol.parse('sht 0 25000', started);
  96. expect(started.isBrewing, isTrue);
  97. expect(started.shotTimer, 0);
  98. expect(stopped.isBrewing, isFalse);
  99. expect(stopped.shotTimer, 25);
  100. });
  101. test('parses scaled cmd get values for key-value settings', () {
  102. var state = MachineState();
  103. state = MeCoffeeProtocol.parse('cmd get tmpsp 10100 OK', state);
  104. state = MeCoffeeProtocol.parse('cmd get tmpstm 12500 OK', state);
  105. state = MeCoffeeProtocol.parse('cmd get pd1i 35 OK', state);
  106. state = MeCoffeeProtocol.parse('cmd get pd1imm 32768 OK', state);
  107. state = MeCoffeeProtocol.parse('cmd get pd1imx 65536 OK', state);
  108. state = MeCoffeeProtocol.parse('cmd get pivlv 1 OK', state);
  109. expect(state.brewTemperature, 101.0);
  110. expect(state.targetTemperature, 101.0);
  111. expect(state.steamTemperature, 125.0);
  112. expect(state.pidI, 0.35);
  113. expect(state.pidIWindDownMin, closeTo(50.0, 0.01));
  114. expect(state.pidIWindDownMax, 100.0);
  115. expect(state.valveClosedWhilePaused, isTrue);
  116. });
  117. test('parses firmware grinder counter key and set echoes', () {
  118. var state = MachineState();
  119. state = MeCoffeeProtocol.parse('cmd get cntgrndr 42 OK', state);
  120. expect(state.grinderCounter, 42);
  121. state = MeCoffeeProtocol.parse('cmd set s_tmpsp 9350 OK', state);
  122. expect(state.brewTemperature, 93.5);
  123. final rejected = MeCoffeeProtocol.parse('cmd set NOT OK', state);
  124. expect(rejected.brewTemperature, 93.5);
  125. });
  126. test('normalizes parsed schedule seconds to HH:mm', () {
  127. var state = MachineState();
  128. state = MeCoffeeProtocol.parse('cmd get tmron -60 OK', state);
  129. state = MeCoffeeProtocol.parse('cmd get tmroff 90000 OK', state);
  130. expect(state.wakeupTime, '23:59');
  131. expect(state.shutdownTime, '01:00');
  132. });
  133. test('encodeSet applies correct scaling rules', () {
  134. expect(MeCoffeeProtocol.encodeSet('tmpsp', 93.5), 'cmd set tmpsp 9350\n');
  135. expect(MeCoffeeProtocol.encodeSet('pd1i', 0.32), 'cmd set pd1i 32\n');
  136. expect(
  137. MeCoffeeProtocol.encodeSet('pistrt', 3.4),
  138. 'cmd set pistrt 3400\n',
  139. );
  140. expect(
  141. MeCoffeeProtocol.encodeSet('pd1imm', 12.5),
  142. 'cmd set pd1imm 8192\n',
  143. );
  144. });
  145. test('single-key writes preserve schedule, bool and V4 units', () {
  146. expect(MeCoffeeProtocol.encodeSet('pinbl', true), 'cmd set pinbl 1\n');
  147. expect(
  148. MeCoffeeProtocol.encodeSet('tmron', '07:30'),
  149. 'cmd set tmron 27000\n',
  150. );
  151. expect(
  152. MeCoffeeProtocol.encodeSet('pd1i', 3.0, isLegacyV4: true),
  153. 'cmd set pd1i 3\n',
  154. );
  155. expect(
  156. MeCoffeeProtocol.encodeSet('pd1imx', 3750.0, isLegacyV4: true),
  157. 'cmd set pd1imx 3750\n',
  158. );
  159. expect(
  160. MeCoffeeProtocol.encodeSet('pistrt', 3.0, isLegacyV4: true),
  161. 'cmd set pistrt 3\n',
  162. );
  163. });
  164. test('encodePreinfusion includes all preinfusion keys', () {
  165. final payload = String.fromCharCodes(
  166. MeCoffeeProtocol.encodePreinfusion(true, 3.2, 2.8, true),
  167. );
  168. expect(payload, contains('cmd set pinbl 1\n'));
  169. expect(payload, contains('cmd set pistrt 3200\n'));
  170. expect(payload, contains('cmd set piprd 2800\n'));
  171. expect(payload, contains('cmd set pivlv 1\n'));
  172. });
  173. test('invalid input leaves state unchanged', () {
  174. final original = MachineState(
  175. temperature: 96.2,
  176. targetTemperature: 100.0,
  177. );
  178. final result = MeCoffeeProtocol.parse('tmp invalid line', original);
  179. expect(result.temperature, 96.2);
  180. expect(result.targetTemperature, 100.0);
  181. });
  182. test('nonfinite temperature frames cannot count as fresh telemetry', () {
  183. final original = MachineState(temperature: 96.2, targetTemperature: 100);
  184. for (final command in ['tmp', 'T']) {
  185. for (final value in ['NaN', 'Infinity', '-Infinity']) {
  186. for (final message in [
  187. '$command $value 10000 9600 0',
  188. '$command 123 $value 9600 0',
  189. '$command 123 10000 $value 0',
  190. '$command 123 10000 9600 $value',
  191. ]) {
  192. expect(MeCoffeeProtocol.parse(message, original), same(original));
  193. }
  194. }
  195. }
  196. });
  197. test('uname containing V4 sets isLegacyV4Firmware flag', () {
  198. final state = MeCoffeeProtocol.parse(
  199. 'cmd uname meCoffee V4 OK',
  200. MachineState(),
  201. );
  202. expect(state.isLegacyV4Firmware, isTrue);
  203. });
  204. test('uname without V4 leaves isLegacyV4Firmware false', () {
  205. final state = MeCoffeeProtocol.parse(
  206. 'cmd uname meCoffee_v5 OK',
  207. MachineState(),
  208. );
  209. expect(state.isLegacyV4Firmware, isFalse);
  210. });
  211. test('pistrt decodes with scale 1 for V4 firmware', () {
  212. final v4State = MachineState(isLegacyV4Firmware: true);
  213. final result = MeCoffeeProtocol.parse('cmd get pistrt 3 OK', v4State);
  214. expect(result.preinfusionTime, 3.0); // scale=1: 3/1=3
  215. });
  216. test('pistrt decodes with scale 1000 for new firmware', () {
  217. final result = MeCoffeeProtocol.parse(
  218. 'cmd get pistrt 3000 OK',
  219. MachineState(),
  220. );
  221. expect(result.preinfusionTime, 3.0); // scale=1000: 3000/1000=3
  222. });
  223. test('encodePreinfusion uses scale 1 for V4 firmware', () {
  224. final payload = String.fromCharCodes(
  225. MeCoffeeProtocol.encodePreinfusion(
  226. true,
  227. 3.0,
  228. 2.0,
  229. false,
  230. isLegacyV4: true,
  231. ),
  232. );
  233. expect(payload, contains('cmd set pistrt 3\n'));
  234. expect(payload, contains('cmd set piprd 2\n'));
  235. });
  236. for (final identity in [
  237. 'cmd uname meCoffee V4 OK',
  238. 'cmd uname meCoffee V4',
  239. 'uname meCoffee V4 OK',
  240. 'cmd uname meCoffee_v4.1 OK',
  241. ]) {
  242. test('recognizes legacy identity "$identity"', () {
  243. final state = MeCoffeeProtocol.parse(identity, MachineState());
  244. expect(state.isLegacyV4Firmware, isTrue);
  245. expect(state.firmwareVersion, isNot(contains('OK')));
  246. });
  247. }
  248. test('identity does not mistake V40 for V4 or erase an empty reply', () {
  249. final original = MachineState(
  250. isLegacyV4Firmware: true,
  251. firmwareVersion: 'meCoffee V4',
  252. );
  253. for (final identity in ['cmd uname OK', 'uname OK']) {
  254. expect(MeCoffeeProtocol.parse(identity, original), same(original));
  255. }
  256. final modern = MeCoffeeProtocol.parse(
  257. 'cmd uname meCoffee V40 OK',
  258. original,
  259. );
  260. expect(modern.isLegacyV4Firmware, isFalse);
  261. expect(modern.firmwareVersion, 'meCoffee V40');
  262. });
  263. for (final legacy in [false, true]) {
  264. test('PID read/write scales match ${legacy ? 'V4' : 'modern'}', () {
  265. final initial = MachineState(isLegacyV4Firmware: legacy);
  266. final rawI = legacy ? 3 : 30;
  267. for (final prefix in ['get', 'cmd get', 'cmd set']) {
  268. final parsed = MeCoffeeProtocol.parse(
  269. '$prefix pd1i $rawI OK',
  270. initial,
  271. );
  272. expect(parsed.pidI, legacy ? 3 : 0.3);
  273. }
  274. final payload = String.fromCharCodes(
  275. MeCoffeeProtocol.encodePID(
  276. legacy ? 25 : 20,
  277. legacy ? 3 : 0.3,
  278. 128,
  279. isLegacyV4: legacy,
  280. ),
  281. );
  282. expect(
  283. payload,
  284. 'cmd set pd1p ${legacy ? 25 : 20}\n'
  285. 'cmd set pd1i $rawI\ncmd set pd1d 128\n',
  286. );
  287. });
  288. test('preinfusion read/write matches ${legacy ? 'V4' : 'modern'}', () {
  289. final initial = MachineState(isLegacyV4Firmware: legacy);
  290. final raw = legacy ? 10 : 10000;
  291. var parsed = MeCoffeeProtocol.parse('get pistrt $raw', initial);
  292. parsed = MeCoffeeProtocol.parse('get piprd $raw', parsed);
  293. expect(parsed.preinfusionTime, 10);
  294. expect(parsed.preinfusionPauseTime, 10);
  295. final payload = String.fromCharCodes(
  296. MeCoffeeProtocol.encodePreinfusion(
  297. true,
  298. 10,
  299. 10,
  300. false,
  301. isLegacyV4: legacy,
  302. ),
  303. );
  304. expect(
  305. payload,
  306. 'cmd set pinbl 1\ncmd set pistrt $raw\n'
  307. 'cmd set piprd $raw\ncmd set pivlv 0\n',
  308. );
  309. });
  310. }
  311. test('explicit parse V4 flag works before identity is stored', () {
  312. final result = MeCoffeeProtocol.parse(
  313. 'get pd1i 3',
  314. MachineState(),
  315. isLegacyV4: true,
  316. );
  317. expect(result.pidI, 3);
  318. });
  319. test('V4 maximum winddown stays in direct units without percent clamp', () {
  320. final initial = MachineState(isLegacyV4Firmware: true);
  321. for (final value in [0, 3750, 6000, 6250]) {
  322. expect(
  323. MeCoffeeProtocol.parse('get pd1imx $value', initial).pidIWindDownMax,
  324. value,
  325. );
  326. }
  327. final payload = String.fromCharCodes(
  328. MeCoffeeProtocol.encodePIDLimits(42, 3750, 5000, isLegacyV4: true),
  329. );
  330. expect(payload, 'cmd set pd1imx 3750\ncmd set pd1sz 5000\n');
  331. for (final key in ['pd1imn', 'pd1imm']) {
  332. expect(
  333. MeCoffeeProtocol.parse('get $key 32768', initial),
  334. same(initial),
  335. );
  336. }
  337. });
  338. test(
  339. 'modern minimum accepts XML and firmware spelling, sends firmware key',
  340. () {
  341. for (final key in ['pd1imm', 'pd1imn']) {
  342. expect(
  343. MeCoffeeProtocol.parse(
  344. 'get $key 32768',
  345. MachineState(),
  346. ).pidIWindDownMin,
  347. 50,
  348. );
  349. }
  350. expect(
  351. String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(25, 48, 1000)),
  352. 'cmd set pd1imn 16384\ncmd set pd1imx 31457\ncmd set pd1sz 1000\n',
  353. );
  354. // Raw values above 32000 are reset to defaults at controller startup,
  355. // and 65536 would store 0 in the 16-bit field.
  356. expect(
  357. String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(50, 100, 1000)),
  358. 'cmd set pd1imn 32000\ncmd set pd1imx 32000\ncmd set pd1sz 1000\n',
  359. );
  360. },
  361. );
  362. test('settings ranges preserve direct P, D and percentage units', () {
  363. var state = MachineState();
  364. for (final entry in {'pd1p': 100, 'pd1d': 256, 'tmppap': 100}.entries) {
  365. state = MeCoffeeProtocol.parse(
  366. 'cmd get ${entry.key} ${entry.value}',
  367. state,
  368. );
  369. }
  370. expect(state.pidP, 100);
  371. expect(state.pidD, 256);
  372. expect(state.proActivePercentage, 100);
  373. });
  374. test(
  375. 'unknown and nonfinite settings do not alter state or guess scaling',
  376. () {
  377. final initial = MachineState();
  378. for (final message in [
  379. 'get pd1unknown 3',
  380. 'get pd1i NaN',
  381. 'get pd1imx Infinity',
  382. ]) {
  383. expect(MeCoffeeProtocol.parse(message, initial), same(initial));
  384. }
  385. expect(
  386. MeCoffeeProtocol.encodeSet('tmpunknown', 3.0),
  387. 'cmd set tmpunknown 3\n',
  388. );
  389. expect(
  390. MeCoffeeProtocol.encodeSet('piunknown', 3.0),
  391. 'cmd set piunknown 3\n',
  392. );
  393. },
  394. );
  395. });
  396. }