// Conformance of the protocol layer against transcripts recorded from the // original meCoffee V10 firmware image running in simavr. // // Regenerate the fixture with `python3 sim/generate_transcripts.py` in the // meCoffee firmware workspace and copy it to test/fixtures/. Every received // line was produced by the unmodified firmware; the `send` strings are what // this client must emit for the listed settings. import 'dart:convert'; import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; import 'package:mebarista/models/machine_state.dart'; import 'package:mebarista/services/me_coffee_protocol.dart'; final Map fixture = jsonDecode( File('test/fixtures/v10_golden_transcripts.json').readAsStringSync(), ) as Map; Object? field(MachineState s, String name) => switch (name) { 'brewTemperature' => s.brewTemperature, 'steamTemperature' => s.steamTemperature, 'pidP' => s.pidP, 'pidI' => s.pidI, 'pidD' => s.pidD, 'pidIWindDownMin' => s.pidIWindDownMin, 'pidIWindDownMax' => s.pidIWindDownMax, 'pidPollingInterval' => s.pidPollingInterval, 'preinfusionEnabled' => s.preinfusionEnabled, 'preinfusionTime' => s.preinfusionTime, 'preinfusionPauseTime' => s.preinfusionPauseTime, 'valveClosedWhilePaused' => s.valveClosedWhilePaused, 'proActivePercentage' => s.proActivePercentage, 'autoShutoffMinutes' => s.autoShutoffMinutes, 'maxShotTime' => s.maxShotTime, 'continuousMode' => s.continuousMode, 'installedAsTimer' => s.installedAsTimer, 'powerButtonFlipsBack' => s.powerButtonFlipsBack, 'output0' => s.output0, 'output1' => s.output1, 'output2' => s.output2, 'wakeupEnabled' => s.wakeupEnabled, 'wakeupTime' => s.wakeupTime, 'shutdownEnabled' => s.shutdownEnabled, 'shutdownTime' => s.shutdownTime, 'pressureStart' => s.pressureStart, 'pressureEnd' => s.pressureEnd, 'pressurePeriod' => s.pressurePeriod, _ => throw ArgumentError('unmapped MachineState field $name'), }; Object clientValue(Map w) => switch (w['kind']) { 'double' => (w['value'] as num).toDouble(), 'int' => w['value'] as int, 'bool' => w['value'] as bool, 'time' => w['value'] as String, _ => throw ArgumentError('unknown kind ${w['kind']}'), }; Matcher expectation(Object? expected) => expected is double ? closeTo(expected, 1e-6) : equals(expected); MachineState parseAll(Iterable lines, [MachineState? start]) => lines.fold( start ?? MachineState(), (state, line) => MeCoffeeProtocol.parse(line, state), ); List scenarioLines(String name) { final scenario = (fixture['scenarios'] as List) .cast>() .firstWhere((s) => s['name'] == name); return [ for (final e in (scenario['receive'] as List).cast>()) e['line'] as String, ]; } void main() { final writes = (fixture['setting_writes'] as List) .cast>(); test('fixture comes from the bundled V10 image', () { final hex = File('meBarista_for_Android/assets/meCoffee-V10.hex'); if (!hex.existsSync()) return; expect( fixture['firmware_hex_sha256'], 'ba8a21559881bac6a624b5cccb372fc31cafeee70c76babcb41e57f5e476c4ac', ); }); group('V10 firmware conformance: settings writes', () { for (final w in writes) { final key = w['key'] as String; test('$key: encoder emits the command the firmware accepted', () { expect(MeCoffeeProtocol.encodeSet(key, clientValue(w)), w['send']); final reply = (w['reply'] as List).cast(); expect(reply, hasLength(1)); expect(reply.single, startsWith('cmd set s_$key ')); expect(reply.single, endsWith(' OK')); expect(reply.single, isNot(contains('NOT OK'))); final stored = reply.single.split(' ')[3]; expect((w['readback'] as List).single, 'cmd get $key $stored OK'); }); test('$key: firmware echo and readback parse to the intended value', () { final expected = expectation(w['expect']); final echoed = parseAll((w['reply'] as List).cast()); expect(field(echoed, w['field'] as String), expected); final readBack = parseAll((w['readback'] as List).cast()); expect(field(readBack, w['field'] as String), expected); }); } test('every written setting survives a controller reboot', () { final dump = (fixture['dump_after_reboot'] as List).cast(); expect(dump.first, 'cmd dump OK'); expect(dump.last, 'cmd dump 35 OK'); final state = parseAll(dump); for (final w in writes) { expect( field(state, w['field'] as String), expectation(w['expect']), reason: w['key'] as String, ); } }); test('a batched settings write is acknowledged command by command', () { final batch = fixture['batched_write'] as Map; final byKey = {for (final w in writes) w['key'] as String: w}; final payload = ['pinbl', 'pistrt', 'piprd', 'pivlv'] .map((k) => MeCoffeeProtocol.encodeSet(k, clientValue(byKey[k]!))) .join(); expect(payload, batch['send']); final replies = (batch['reply'] as List).cast(); expect(replies, hasLength(4)); expect( replies.every((r) => r.endsWith(' OK') && !r.contains('NOT')), isTrue, ); }); }); group('V10 firmware conformance: connection start-up', () { final init = fixture['init'] as Map; final received = (init['receive'] as List).cast(); final state = parseAll([ ...(init['boot'] as List).cast(), ...received, ]); test('controller identity is recognised as modern (not V4) firmware', () { expect(received, contains('cmd uname meCoffee pcb V9 30 149 15 OK')); expect(state.firmwareVersion, contains('pcb V9')); expect(state.isLegacyV4Firmware, isFalse); }); test('dump delivers all 35 settings between start and end markers', () { final gets = received.where((l) => l.startsWith('cmd get ')); expect(gets, hasLength(35)); expect(received, contains('cmd dump OK')); expect(received, contains('cmd dump 35 OK')); }); test('factory defaults decode with the client scaling', () { expect(state.brewTemperature, 101.0); expect(state.steamTemperature, 125.0); expect(state.pidP, 20); expect(state.pidI, closeTo(0.30, 1e-9)); expect(state.pidD, 128); expect(state.pidIWindDownMin, closeTo(1000 / 655.36, 1e-9)); expect(state.pidIWindDownMax, closeTo(13107 / 655.36, 1e-9)); expect(state.pidPollingInterval, 1000); expect(state.preinfusionEnabled, isFalse); expect(state.preinfusionTime, 3.0); expect(state.preinfusionPauseTime, 3.0); expect(state.continuousMode, isTrue); expect(state.proActivePercentage, 33); expect(state.autoShutoffMinutes, 60); expect([state.output0, state.output1, state.output2], [112, 98, 118]); expect( [state.pressureStart, state.pressureEnd, state.pressurePeriod], [100, 100, 25], ); expect(state.maxShotTime, 0); expect(state.grinderCounter, 0); }); test('telemetry decodes the boiler sensor', () { expect(state.temperature, closeTo(92.83, 1e-9)); expect(state.targetTemperature, 101.0); }); }); group('V10 firmware conformance: replies the client must ignore', () { final rejections = (fixture['rejections'] as List) .cast>(); test('unknown keys, CR-terminated lines and diagnostics leave state', () { final base = parseAll(['cmd get tmpsp 9350 OK']); for (final r in rejections) { final send = r['send'] as String; if (send.startsWith('cmd set tmpsp 9350 OK')) continue; for (final line in (r['reply'] as List).cast()) { final next = MeCoffeeProtocol.parse(line, base); expect(identical(next, base), isTrue, reason: '$send -> "$line"'); } } }); test('firmware rejects keys only some clients use', () { String replyTo(String send) => (rejections.firstWhere((r) => r['send'] == send)['reply'] as List) .single as String; expect(replyTo('cmd get grndr_cnt\n'), 'cmd get NOT OK'); expect(replyTo('cmd set tmpoff 5\n'), 'cmd set NOT OK'); expect(replyTo('cmd get pd1imm\n'), 'cmd get NOT OK'); expect(replyTo('cmd get tmpsp\r\n'), 'cmd get NOT OK'); }); }); group('V10 firmware conformance: machine events', () { test('a 6 s shot is timed from the firmware sht frames', () { final lines = scenarioLines('shot_6s'); var state = MachineState(); var sawBrewing = false; for (final line in lines) { state = MeCoffeeProtocol.parse(line, state); sawBrewing |= state.isBrewing; } expect(sawBrewing, isTrue); expect(state.isBrewing, isFalse); expect(state.shotTimer, 6); }); test('a shtmx cutoff keeps the cutoff duration, not the release', () { final state = parseAll(scenarioLines('shot_cutoff_shtmx_2')); expect(state.isBrewing, isFalse); expect(state.shotTimer, 2); }); test('preinfusion gap is included in the shot duration', () { final state = parseAll(scenarioLines('shot_preinfusion_1s_gap_1s')); expect(state.shotTimer, 5); }); test('steam marker, steam setpoint and return to brew', () { final lines = scenarioLines('steam_3s'); var state = MachineState(); var sawSteam = false; for (final line in lines) { state = MeCoffeeProtocol.parse(line, state); if (state.isSteaming) { sawSteam = true; if (line.startsWith('tmp ')) expect(state.targetTemperature, 125.0); } } expect(sawSteam, isTrue); expect(state.isSteaming, isFalse); expect(state.targetTemperature, 101.0); }); for (final name in ['steam_reaching_tmpstm', 'steam_start_above_tmpstm']) { test('$name: steam persists while the heater cycles', () { final lines = scenarioLines(name); final start = lines.indexOf('pid 65535 0 0 0 OK'); final end = lines.lastIndexWhere( (l) => l.startsWith('tmp ') && l.split(' ')[2] == '12500', ); expect(start, greaterThanOrEqualTo(0)); var state = MachineState(); for (var i = 0; i < lines.length; i++) { state = MeCoffeeProtocol.parse(lines[i], state); if (i >= start && i <= end) { expect(state.isSteaming, isTrue, reason: lines[i]); } if (lines[i].startsWith('pid ') && state.isSteaming) { final heaterOn = lines[i] != 'pid 0 0 0 0 OK'; expect(state.isHeating, heaterOn, reason: lines[i]); expect(state.heatingPower, heaterOn ? 1.0 : 0.0, reason: lines[i]); } } expect(state.isSteaming, isFalse); expect(state.targetTemperature, 101.0); }); } test('power switch off reports zero heating', () { final state = parseAll(scenarioLines('power_switch_off')); expect(state.heatingPower, 0); expect(state.isHeating, isFalse); }); test('sensor range switch above 110 C is transparent to the client', () { final lines = scenarioLines('sensor_saturated_range_switch'); final temps = []; var state = MachineState(); for (final line in lines) { state = MeCoffeeProtocol.parse(line, state); if (line.startsWith('tmp ')) temps.add(state.temperature); } expect(temps.reduce((a, b) => a > b ? a : b), closeTo(129.79, 1e-9)); expect(temps.last, closeTo(79.94, 1e-9)); }); }); }