| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- // 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<String, dynamic> fixture =
- jsonDecode(
- File('test/fixtures/v10_golden_transcripts.json').readAsStringSync(),
- )
- as Map<String, dynamic>;
- 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<String, dynamic> 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<String> lines, [MachineState? start]) =>
- lines.fold(
- start ?? MachineState(),
- (state, line) => MeCoffeeProtocol.parse(line, state),
- );
- List<String> scenarioLines(String name) {
- final scenario = (fixture['scenarios'] as List)
- .cast<Map<String, dynamic>>()
- .firstWhere((s) => s['name'] == name);
- return [
- for (final e in (scenario['receive'] as List).cast<Map<String, dynamic>>())
- e['line'] as String,
- ];
- }
- void main() {
- final writes = (fixture['setting_writes'] as List)
- .cast<Map<String, dynamic>>();
- 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<String>();
- 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<String>());
- expect(field(echoed, w['field'] as String), expected);
- final readBack = parseAll((w['readback'] as List).cast<String>());
- 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<String>();
- 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<String, dynamic>;
- 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<String>();
- 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<String, dynamic>;
- final received = (init['receive'] as List).cast<String>();
- final state = parseAll([
- ...(init['boot'] as List).cast<String>(),
- ...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<Map<String, dynamic>>();
- 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<String>()) {
- 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 = <double>[];
- 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));
- });
- });
- }
|