v10_firmware_conformance_test.dart 12 KB

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