v10_firmware_conformance_test.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Conformance of the protocol layer against transcripts recorded from the
  2. // original meCoffee V10 firmware image running in simavr.
  3. //
  4. // Regenerate the fixture with `python3 sim/generate_transcripts.py` in the
  5. // meCoffee firmware workspace and copy it to test/fixtures/. Every received
  6. // line was produced by the unmodified firmware; the `send` strings are what
  7. // this client must emit for the listed settings.
  8. import 'dart:convert';
  9. import 'dart:io';
  10. import 'package:flutter_test/flutter_test.dart';
  11. import 'package:mebarista/models/machine_state.dart';
  12. import 'package:mebarista/services/me_coffee_protocol.dart';
  13. final Map<String, dynamic> fixture =
  14. jsonDecode(
  15. File('test/fixtures/v10_golden_transcripts.json').readAsStringSync(),
  16. )
  17. as Map<String, dynamic>;
  18. Object? field(MachineState s, String name) => switch (name) {
  19. 'brewTemperature' => s.brewTemperature,
  20. 'steamTemperature' => s.steamTemperature,
  21. 'pidP' => s.pidP,
  22. 'pidI' => s.pidI,
  23. 'pidD' => s.pidD,
  24. 'pidIWindDownMin' => s.pidIWindDownMin,
  25. 'pidIWindDownMax' => s.pidIWindDownMax,
  26. 'pidPollingInterval' => s.pidPollingInterval,
  27. 'preinfusionEnabled' => s.preinfusionEnabled,
  28. 'preinfusionTime' => s.preinfusionTime,
  29. 'preinfusionPauseTime' => s.preinfusionPauseTime,
  30. 'valveClosedWhilePaused' => s.valveClosedWhilePaused,
  31. 'proActivePercentage' => s.proActivePercentage,
  32. 'autoShutoffMinutes' => s.autoShutoffMinutes,
  33. 'autoShutoffEnabled' => s.autoShutoffEnabled,
  34. 'maxShotTime' => s.maxShotTime,
  35. 'continuousMode' => s.continuousMode,
  36. 'installedAsTimer' => s.installedAsTimer,
  37. 'powerButtonFlipsBack' => s.powerButtonFlipsBack,
  38. 'output0' => s.output0,
  39. 'output1' => s.output1,
  40. 'output2' => s.output2,
  41. 'wakeupEnabled' => s.wakeupEnabled,
  42. 'wakeupTime' => s.wakeupTime,
  43. 'shutdownEnabled' => s.shutdownEnabled,
  44. 'shutdownTime' => s.shutdownTime,
  45. 'pressureStart' => s.pressureStart,
  46. 'pressureEnd' => s.pressureEnd,
  47. 'pressurePeriod' => s.pressurePeriod,
  48. _ => throw ArgumentError('unmapped MachineState field $name'),
  49. };
  50. Object clientValue(Map<String, dynamic> w) => switch (w['kind']) {
  51. 'double' => (w['value'] as num).toDouble(),
  52. 'int' => w['value'] as int,
  53. 'bool' => w['value'] as bool,
  54. 'time' => w['value'] as String,
  55. _ => throw ArgumentError('unknown kind ${w['kind']}'),
  56. };
  57. Matcher expectation(Object? expected) =>
  58. expected is double ? closeTo(expected, 1e-6) : equals(expected);
  59. MachineState parseAll(Iterable<String> lines, [MachineState? start]) =>
  60. lines.fold(
  61. start ?? MachineState(),
  62. (state, line) => MeCoffeeProtocol.parse(line, state),
  63. );
  64. List<String> scenarioLines(String name) {
  65. final scenario = (fixture['scenarios'] as List)
  66. .cast<Map<String, dynamic>>()
  67. .firstWhere((s) => s['name'] == name);
  68. return [
  69. for (final e in (scenario['receive'] as List).cast<Map<String, dynamic>>())
  70. e['line'] as String,
  71. ];
  72. }
  73. void main() {
  74. final writes = (fixture['setting_writes'] as List)
  75. .cast<Map<String, dynamic>>();
  76. test('fixture comes from the bundled V10 image', () {
  77. final hex = File('meBarista_for_Android/assets/meCoffee-V10.hex');
  78. if (!hex.existsSync()) return;
  79. expect(
  80. fixture['firmware_hex_sha256'],
  81. 'ba8a21559881bac6a624b5cccb372fc31cafeee70c76babcb41e57f5e476c4ac',
  82. );
  83. });
  84. group('V10 firmware conformance: settings writes', () {
  85. for (final w in writes) {
  86. final key = w['key'] as String;
  87. test('$key: encoder emits the command the firmware accepted', () {
  88. expect(MeCoffeeProtocol.encodeSet(key, clientValue(w)), w['send']);
  89. final reply = (w['reply'] as List).cast<String>();
  90. expect(reply, hasLength(1));
  91. expect(reply.single, startsWith('cmd set s_$key '));
  92. expect(reply.single, endsWith(' OK'));
  93. expect(reply.single, isNot(contains('NOT OK')));
  94. final stored = reply.single.split(' ')[3];
  95. expect((w['readback'] as List).single, 'cmd get $key $stored OK');
  96. });
  97. test('$key: firmware echo and readback parse to the intended value', () {
  98. final expected = expectation(w['expect']);
  99. final echoed = parseAll((w['reply'] as List).cast<String>());
  100. expect(field(echoed, w['field'] as String), expected);
  101. final readBack = parseAll((w['readback'] as List).cast<String>());
  102. expect(field(readBack, w['field'] as String), expected);
  103. });
  104. }
  105. test('every written setting survives a controller reboot', () {
  106. final dump = (fixture['dump_after_reboot'] as List).cast<String>();
  107. expect(dump.first, 'cmd dump OK');
  108. expect(dump.last, 'cmd dump 35 OK');
  109. final state = parseAll(dump);
  110. for (final w in writes) {
  111. expect(
  112. field(state, w['field'] as String),
  113. expectation(w['expect']),
  114. reason: w['key'] as String,
  115. );
  116. }
  117. });
  118. test('a batched settings write is acknowledged command by command', () {
  119. final batch = fixture['batched_write'] as Map<String, dynamic>;
  120. final byKey = {for (final w in writes) w['key'] as String: w};
  121. final payload = ['pinbl', 'pistrt', 'piprd', 'pivlv']
  122. .map((k) => MeCoffeeProtocol.encodeSet(k, clientValue(byKey[k]!)))
  123. .join();
  124. expect(payload, batch['send']);
  125. final replies = (batch['reply'] as List).cast<String>();
  126. expect(replies, hasLength(4));
  127. expect(
  128. replies.every((r) => r.endsWith(' OK') && !r.contains('NOT')),
  129. isTrue,
  130. );
  131. });
  132. });
  133. group('V10 firmware conformance: connection start-up', () {
  134. final init = fixture['init'] as Map<String, dynamic>;
  135. final received = (init['receive'] as List).cast<String>();
  136. final state = parseAll([
  137. ...(init['boot'] as List).cast<String>(),
  138. ...received,
  139. ]);
  140. test('controller identity is recognised as modern (not V4) firmware', () {
  141. expect(received, contains('cmd uname meCoffee pcb V9 30 149 15 OK'));
  142. expect(state.firmwareVersion, contains('pcb V9'));
  143. expect(state.isLegacyV4Firmware, isFalse);
  144. });
  145. test('dump delivers all 35 settings between start and end markers', () {
  146. final gets = received.where((l) => l.startsWith('cmd get '));
  147. expect(gets, hasLength(35));
  148. expect(received, contains('cmd dump OK'));
  149. expect(received, contains('cmd dump 35 OK'));
  150. });
  151. test('factory defaults decode with the client scaling', () {
  152. expect(state.brewTemperature, 101.0);
  153. expect(state.steamTemperature, 125.0);
  154. expect(state.pidP, 20);
  155. expect(state.pidI, closeTo(0.30, 1e-9));
  156. expect(state.pidD, 128);
  157. expect(state.pidIWindDownMin, closeTo(1000 / 655.36, 1e-9));
  158. expect(state.pidIWindDownMax, closeTo(13107 / 655.36, 1e-9));
  159. expect(state.pidPollingInterval, 1000);
  160. expect(state.preinfusionEnabled, isFalse);
  161. expect(state.preinfusionTime, 3.0);
  162. expect(state.preinfusionPauseTime, 3.0);
  163. expect(state.continuousMode, isTrue);
  164. expect(state.proActivePercentage, 33);
  165. expect(state.autoShutoffMinutes, 60);
  166. expect([state.output0, state.output1, state.output2], [112, 98, 118]);
  167. expect(
  168. [state.pressureStart, state.pressureEnd, state.pressurePeriod],
  169. [100, 100, 25],
  170. );
  171. expect(state.maxShotTime, 0);
  172. expect(state.grinderCounter, 0);
  173. });
  174. test('telemetry decodes the boiler sensor', () {
  175. expect(state.temperature, closeTo(92.83, 1e-9));
  176. expect(state.targetTemperature, 101.0);
  177. });
  178. });
  179. group('V10 firmware conformance: replies the client must ignore', () {
  180. final rejections = (fixture['rejections'] as List)
  181. .cast<Map<String, dynamic>>();
  182. test('unknown keys, CR-terminated lines and diagnostics leave state', () {
  183. final base = parseAll(['cmd get tmpsp 9350 OK']);
  184. for (final r in rejections) {
  185. final send = r['send'] as String;
  186. if (send.startsWith('cmd set tmpsp 9350 OK')) continue;
  187. for (final line in (r['reply'] as List).cast<String>()) {
  188. final next = MeCoffeeProtocol.parse(line, base);
  189. expect(identical(next, base), isTrue, reason: '$send -> "$line"');
  190. }
  191. }
  192. });
  193. test('firmware rejects keys only some clients use', () {
  194. String replyTo(String send) =>
  195. (rejections.firstWhere((r) => r['send'] == send)['reply'] as List)
  196. .single
  197. as String;
  198. expect(replyTo('cmd get grndr_cnt\n'), 'cmd get NOT OK');
  199. expect(replyTo('cmd set tmpoff 5\n'), 'cmd set NOT OK');
  200. expect(replyTo('cmd get pd1imm\n'), 'cmd get NOT OK');
  201. expect(replyTo('cmd get tmpsp\r\n'), 'cmd get NOT OK');
  202. });
  203. });
  204. group('V10 firmware conformance: machine events', () {
  205. test('a 6 s shot is timed from the firmware sht frames', () {
  206. final lines = scenarioLines('shot_6s');
  207. var state = MachineState();
  208. var sawBrewing = false;
  209. for (final line in lines) {
  210. state = MeCoffeeProtocol.parse(line, state);
  211. sawBrewing |= state.isBrewing;
  212. }
  213. expect(sawBrewing, isTrue);
  214. expect(state.isBrewing, isFalse);
  215. expect(state.shotTimer, 6);
  216. });
  217. test('a shtmx cutoff keeps the cutoff duration, not the release', () {
  218. final state = parseAll(scenarioLines('shot_cutoff_shtmx_2'));
  219. expect(state.isBrewing, isFalse);
  220. expect(state.shotTimer, 2);
  221. });
  222. test('preinfusion gap is included in the shot duration', () {
  223. final state = parseAll(scenarioLines('shot_preinfusion_1s_gap_1s'));
  224. expect(state.shotTimer, 5);
  225. });
  226. test('steam marker, steam setpoint and return to brew', () {
  227. final lines = scenarioLines('steam_3s');
  228. var state = MachineState();
  229. var sawSteam = false;
  230. for (final line in lines) {
  231. state = MeCoffeeProtocol.parse(line, state);
  232. if (state.isSteaming) {
  233. sawSteam = true;
  234. if (line.startsWith('tmp ')) expect(state.targetTemperature, 125.0);
  235. }
  236. }
  237. expect(sawSteam, isTrue);
  238. expect(state.isSteaming, isFalse);
  239. expect(state.targetTemperature, 101.0);
  240. });
  241. for (final name in ['steam_reaching_tmpstm', 'steam_start_above_tmpstm']) {
  242. test('$name: steam persists while the heater cycles', () {
  243. final lines = scenarioLines(name);
  244. final start = lines.indexOf('pid 65535 0 0 0 OK');
  245. final end = lines.lastIndexWhere(
  246. (l) => l.startsWith('tmp ') && l.split(' ')[2] == '12500',
  247. );
  248. expect(start, greaterThanOrEqualTo(0));
  249. var state = MachineState();
  250. for (var i = 0; i < lines.length; i++) {
  251. state = MeCoffeeProtocol.parse(lines[i], state);
  252. if (i >= start && i <= end) {
  253. expect(state.isSteaming, isTrue, reason: lines[i]);
  254. }
  255. if (lines[i].startsWith('pid ') && state.isSteaming) {
  256. final heaterOn = lines[i] != 'pid 0 0 0 0 OK';
  257. expect(state.isHeating, heaterOn, reason: lines[i]);
  258. expect(state.heatingPower, heaterOn ? 1.0 : 0.0, reason: lines[i]);
  259. }
  260. }
  261. expect(state.isSteaming, isFalse);
  262. expect(state.targetTemperature, 101.0);
  263. });
  264. }
  265. test('power switch off reports zero heating', () {
  266. final state = parseAll(scenarioLines('power_switch_off'));
  267. expect(state.heatingPower, 0);
  268. expect(state.isHeating, isFalse);
  269. });
  270. test('sensor range switch above 110 C is transparent to the client', () {
  271. final lines = scenarioLines('sensor_saturated_range_switch');
  272. final temps = <double>[];
  273. var state = MachineState();
  274. for (final line in lines) {
  275. state = MeCoffeeProtocol.parse(line, state);
  276. if (line.startsWith('tmp ')) temps.add(state.temperature);
  277. }
  278. expect(temps.reduce((a, b) => a > b ? a : b), closeTo(129.79, 1e-9));
  279. expect(temps.last, closeTo(79.94, 1e-9));
  280. });
  281. });
  282. }