me_coffee_protocol_test.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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('keeps the shtmx cutoff duration when the release line follows', () {
  102. // V10 simulation, shtmx 2 with a 4 s shot signal.
  103. var state = MachineState();
  104. for (final line in [
  105. 'sht 4 4823 OK', // stray line before the start
  106. 'sht 4 0 OK',
  107. 'sht 6 2001 OK', // cutoff
  108. 'sht 8 3971 OK', // signal released
  109. ]) {
  110. state = MeCoffeeProtocol.parse(line, state);
  111. if (line == 'sht 4 4823 OK') {
  112. expect(state.isBrewing, isFalse);
  113. expect(state.shotTimer, 0);
  114. }
  115. }
  116. expect(state.isBrewing, isFalse);
  117. expect(state.shotTimer, 2);
  118. });
  119. test('treats pid 65535 0 0 0 as the steam marker, not full power', () {
  120. final heating = MachineState(heatingPower: 0.3, isHeating: true);
  121. final steaming = MeCoffeeProtocol.parse('pid 65535 0 0 0 OK', heating);
  122. expect(steaming.isSteaming, isTrue);
  123. expect(steaming.isHeating, isTrue);
  124. expect(steaming.heatingPower, 0);
  125. final released = MeCoffeeProtocol.parse('pid 0 0 0 0 OK', steaming);
  126. expect(released.isSteaming, isFalse);
  127. expect(released.isHeating, isFalse);
  128. expect(released.heatingPower, 0);
  129. final resumed = MeCoffeeProtocol.parse('pid 2200 1258 0 0 OK', steaming);
  130. expect(resumed.isSteaming, isFalse);
  131. expect(resumed.heatingPower, closeTo(3458 / 65535, 1e-9));
  132. });
  133. test('V4 three-field pid 65535 0 0 is still full power', () {
  134. final state = MeCoffeeProtocol.parse('pid 65535 0 0', MachineState());
  135. expect(state.isSteaming, isFalse);
  136. expect(state.heatingPower, 1.0);
  137. });
  138. test('parses scaled cmd get values for key-value settings', () {
  139. var state = MachineState();
  140. state = MeCoffeeProtocol.parse('cmd get tmpsp 10100 OK', state);
  141. state = MeCoffeeProtocol.parse('cmd get tmpstm 12500 OK', state);
  142. state = MeCoffeeProtocol.parse('cmd get pd1i 35 OK', state);
  143. state = MeCoffeeProtocol.parse('cmd get pd1imm 32768 OK', state);
  144. state = MeCoffeeProtocol.parse('cmd get pd1imx 65536 OK', state);
  145. state = MeCoffeeProtocol.parse('cmd get pivlv 1 OK', state);
  146. expect(state.brewTemperature, 101.0);
  147. expect(state.targetTemperature, 101.0);
  148. expect(state.steamTemperature, 125.0);
  149. expect(state.pidI, 0.35);
  150. expect(state.pidIWindDownMin, closeTo(50.0, 0.01));
  151. expect(state.pidIWindDownMax, 100.0);
  152. expect(state.valveClosedWhilePaused, isTrue);
  153. });
  154. test('parses firmware grinder counter key and set echoes', () {
  155. var state = MachineState();
  156. state = MeCoffeeProtocol.parse('cmd get cntgrndr 42 OK', state);
  157. expect(state.grinderCounter, 42);
  158. state = MeCoffeeProtocol.parse('cmd set s_tmpsp 9350 OK', state);
  159. expect(state.brewTemperature, 93.5);
  160. final rejected = MeCoffeeProtocol.parse('cmd set NOT OK', state);
  161. expect(rejected.brewTemperature, 93.5);
  162. });
  163. test('normalizes parsed schedule seconds to HH:mm', () {
  164. var state = MachineState();
  165. state = MeCoffeeProtocol.parse('cmd get tmron -60 OK', state);
  166. state = MeCoffeeProtocol.parse('cmd get tmroff 90000 OK', state);
  167. expect(state.wakeupTime, '23:59');
  168. expect(state.shutdownTime, '01:00');
  169. });
  170. test('encodeSet applies correct scaling rules', () {
  171. expect(MeCoffeeProtocol.encodeSet('tmpsp', 93.5), 'cmd set tmpsp 9350\n');
  172. expect(MeCoffeeProtocol.encodeSet('pd1i', 0.32), 'cmd set pd1i 32\n');
  173. expect(
  174. MeCoffeeProtocol.encodeSet('pistrt', 3.4),
  175. 'cmd set pistrt 3400\n',
  176. );
  177. expect(
  178. MeCoffeeProtocol.encodeSet('pd1imm', 12.5),
  179. 'cmd set pd1imm 8192\n',
  180. );
  181. });
  182. test('single-key writes preserve schedule, bool and V4 units', () {
  183. expect(MeCoffeeProtocol.encodeSet('pinbl', true), 'cmd set pinbl 1\n');
  184. expect(
  185. MeCoffeeProtocol.encodeSet('tmron', '07:30'),
  186. 'cmd set tmron 27000\n',
  187. );
  188. expect(
  189. MeCoffeeProtocol.encodeSet('pd1i', 3.0, isLegacyV4: true),
  190. 'cmd set pd1i 3\n',
  191. );
  192. expect(
  193. MeCoffeeProtocol.encodeSet('pd1imx', 3750.0, isLegacyV4: true),
  194. 'cmd set pd1imx 3750\n',
  195. );
  196. expect(
  197. MeCoffeeProtocol.encodeSet('pistrt', 3.0, isLegacyV4: true),
  198. 'cmd set pistrt 3\n',
  199. );
  200. });
  201. test('encodePreinfusion includes all preinfusion keys', () {
  202. final payload = String.fromCharCodes(
  203. MeCoffeeProtocol.encodePreinfusion(true, 3.2, 2.8, true),
  204. );
  205. expect(payload, contains('cmd set pinbl 1\n'));
  206. expect(payload, contains('cmd set pistrt 3200\n'));
  207. expect(payload, contains('cmd set piprd 2800\n'));
  208. expect(payload, contains('cmd set pivlv 1\n'));
  209. });
  210. test('invalid input leaves state unchanged', () {
  211. final original = MachineState(
  212. temperature: 96.2,
  213. targetTemperature: 100.0,
  214. );
  215. final result = MeCoffeeProtocol.parse('tmp invalid line', original);
  216. expect(result.temperature, 96.2);
  217. expect(result.targetTemperature, 100.0);
  218. });
  219. test('nonfinite temperature frames cannot count as fresh telemetry', () {
  220. final original = MachineState(temperature: 96.2, targetTemperature: 100);
  221. for (final command in ['tmp', 'T']) {
  222. for (final value in ['NaN', 'Infinity', '-Infinity']) {
  223. for (final message in [
  224. '$command $value 10000 9600 0',
  225. '$command 123 $value 9600 0',
  226. '$command 123 10000 $value 0',
  227. '$command 123 10000 9600 $value',
  228. ]) {
  229. expect(MeCoffeeProtocol.parse(message, original), same(original));
  230. }
  231. }
  232. }
  233. });
  234. test('uname containing V4 sets isLegacyV4Firmware flag', () {
  235. final state = MeCoffeeProtocol.parse(
  236. 'cmd uname meCoffee V4 OK',
  237. MachineState(),
  238. );
  239. expect(state.isLegacyV4Firmware, isTrue);
  240. });
  241. test('uname without V4 leaves isLegacyV4Firmware false', () {
  242. final state = MeCoffeeProtocol.parse(
  243. 'cmd uname meCoffee_v5 OK',
  244. MachineState(),
  245. );
  246. expect(state.isLegacyV4Firmware, isFalse);
  247. });
  248. test('pistrt decodes with scale 1 for V4 firmware', () {
  249. final v4State = MachineState(isLegacyV4Firmware: true);
  250. final result = MeCoffeeProtocol.parse('cmd get pistrt 3 OK', v4State);
  251. expect(result.preinfusionTime, 3.0); // scale=1: 3/1=3
  252. });
  253. test('pistrt decodes with scale 1000 for new firmware', () {
  254. final result = MeCoffeeProtocol.parse(
  255. 'cmd get pistrt 3000 OK',
  256. MachineState(),
  257. );
  258. expect(result.preinfusionTime, 3.0); // scale=1000: 3000/1000=3
  259. });
  260. test('encodePreinfusion uses scale 1 for V4 firmware', () {
  261. final payload = String.fromCharCodes(
  262. MeCoffeeProtocol.encodePreinfusion(
  263. true,
  264. 3.0,
  265. 2.0,
  266. false,
  267. isLegacyV4: true,
  268. ),
  269. );
  270. expect(payload, contains('cmd set pistrt 3\n'));
  271. expect(payload, contains('cmd set piprd 2\n'));
  272. });
  273. for (final identity in [
  274. 'cmd uname meCoffee V4 OK',
  275. 'cmd uname meCoffee V4',
  276. 'uname meCoffee V4 OK',
  277. 'cmd uname meCoffee_v4.1 OK',
  278. ]) {
  279. test('recognizes legacy identity "$identity"', () {
  280. final state = MeCoffeeProtocol.parse(identity, MachineState());
  281. expect(state.isLegacyV4Firmware, isTrue);
  282. expect(state.firmwareVersion, isNot(contains('OK')));
  283. });
  284. }
  285. test('identity does not mistake V40 for V4 or erase an empty reply', () {
  286. final original = MachineState(
  287. isLegacyV4Firmware: true,
  288. firmwareVersion: 'meCoffee V4',
  289. );
  290. for (final identity in ['cmd uname OK', 'uname OK']) {
  291. expect(MeCoffeeProtocol.parse(identity, original), same(original));
  292. }
  293. final modern = MeCoffeeProtocol.parse(
  294. 'cmd uname meCoffee V40 OK',
  295. original,
  296. );
  297. expect(modern.isLegacyV4Firmware, isFalse);
  298. expect(modern.firmwareVersion, 'meCoffee V40');
  299. });
  300. for (final legacy in [false, true]) {
  301. test('PID read/write scales match ${legacy ? 'V4' : 'modern'}', () {
  302. final initial = MachineState(isLegacyV4Firmware: legacy);
  303. final rawI = legacy ? 3 : 30;
  304. for (final prefix in ['get', 'cmd get', 'cmd set']) {
  305. final parsed = MeCoffeeProtocol.parse(
  306. '$prefix pd1i $rawI OK',
  307. initial,
  308. );
  309. expect(parsed.pidI, legacy ? 3 : 0.3);
  310. }
  311. final payload = String.fromCharCodes(
  312. MeCoffeeProtocol.encodePID(
  313. legacy ? 25 : 20,
  314. legacy ? 3 : 0.3,
  315. 128,
  316. isLegacyV4: legacy,
  317. ),
  318. );
  319. expect(
  320. payload,
  321. 'cmd set pd1p ${legacy ? 25 : 20}\n'
  322. 'cmd set pd1i $rawI\ncmd set pd1d 128\n',
  323. );
  324. });
  325. test('preinfusion read/write matches ${legacy ? 'V4' : 'modern'}', () {
  326. final initial = MachineState(isLegacyV4Firmware: legacy);
  327. final raw = legacy ? 10 : 10000;
  328. var parsed = MeCoffeeProtocol.parse('get pistrt $raw', initial);
  329. parsed = MeCoffeeProtocol.parse('get piprd $raw', parsed);
  330. expect(parsed.preinfusionTime, 10);
  331. expect(parsed.preinfusionPauseTime, 10);
  332. final payload = String.fromCharCodes(
  333. MeCoffeeProtocol.encodePreinfusion(
  334. true,
  335. 10,
  336. 10,
  337. false,
  338. isLegacyV4: legacy,
  339. ),
  340. );
  341. expect(
  342. payload,
  343. 'cmd set pinbl 1\ncmd set pistrt $raw\n'
  344. 'cmd set piprd $raw\ncmd set pivlv 0\n',
  345. );
  346. });
  347. }
  348. test('explicit parse V4 flag works before identity is stored', () {
  349. final result = MeCoffeeProtocol.parse(
  350. 'get pd1i 3',
  351. MachineState(),
  352. isLegacyV4: true,
  353. );
  354. expect(result.pidI, 3);
  355. });
  356. test('V4 maximum winddown stays in direct units without percent clamp', () {
  357. final initial = MachineState(isLegacyV4Firmware: true);
  358. for (final value in [0, 3750, 6000, 6250]) {
  359. expect(
  360. MeCoffeeProtocol.parse('get pd1imx $value', initial).pidIWindDownMax,
  361. value,
  362. );
  363. }
  364. final payload = String.fromCharCodes(
  365. MeCoffeeProtocol.encodePIDLimits(42, 3750, 5000, isLegacyV4: true),
  366. );
  367. expect(payload, 'cmd set pd1imx 3750\ncmd set pd1sz 5000\n');
  368. for (final key in ['pd1imn', 'pd1imm']) {
  369. expect(
  370. MeCoffeeProtocol.parse('get $key 32768', initial),
  371. same(initial),
  372. );
  373. }
  374. });
  375. test(
  376. 'modern minimum accepts XML and firmware spelling, sends firmware key',
  377. () {
  378. for (final key in ['pd1imm', 'pd1imn']) {
  379. expect(
  380. MeCoffeeProtocol.parse(
  381. 'get $key 32768',
  382. MachineState(),
  383. ).pidIWindDownMin,
  384. 50,
  385. );
  386. }
  387. expect(
  388. String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(25, 48, 1000)),
  389. 'cmd set pd1imn 16384\ncmd set pd1imx 31457\ncmd set pd1sz 1000\n',
  390. );
  391. // Raw values above 32000 are reset to defaults at controller startup,
  392. // and 65536 would store 0 in the 16-bit field.
  393. expect(
  394. String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(50, 100, 1000)),
  395. 'cmd set pd1imn 32000\ncmd set pd1imx 32000\ncmd set pd1sz 1000\n',
  396. );
  397. },
  398. );
  399. test('settings ranges preserve direct P, D and percentage units', () {
  400. var state = MachineState();
  401. for (final entry in {'pd1p': 100, 'pd1d': 256, 'tmppap': 100}.entries) {
  402. state = MeCoffeeProtocol.parse(
  403. 'cmd get ${entry.key} ${entry.value}',
  404. state,
  405. );
  406. }
  407. expect(state.pidP, 100);
  408. expect(state.pidD, 256);
  409. expect(state.proActivePercentage, 100);
  410. });
  411. test(
  412. 'unknown and nonfinite settings do not alter state or guess scaling',
  413. () {
  414. final initial = MachineState();
  415. for (final message in [
  416. 'get pd1unknown 3',
  417. 'get pd1i NaN',
  418. 'get pd1imx Infinity',
  419. ]) {
  420. expect(MeCoffeeProtocol.parse(message, initial), same(initial));
  421. }
  422. expect(
  423. MeCoffeeProtocol.encodeSet('tmpunknown', 3.0),
  424. 'cmd set tmpunknown 3\n',
  425. );
  426. expect(
  427. MeCoffeeProtocol.encodeSet('piunknown', 3.0),
  428. 'cmd set piunknown 3\n',
  429. );
  430. },
  431. );
  432. });
  433. }