import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; import 'package:flutter_test/flutter_test.dart'; import 'package:maibu_satabot_v2/core/logging/remote_control_diagnostics.dart'; import 'package:maibu_satabot_v2/core/protocol/machine_protocol_codec.dart'; import 'package:maibu_satabot_v2/features/remote_control/domain/usecase/diff_steer_usecase.dart'; List> records(List batches) => batches .join() .split('\n') .where((line) => line.isNotEmpty) .map((line) => jsonDecode(line) as Map) .toList(); void main() { test( 'frame correlation survives asynchronous completion and a new session', () async { final batches = []; final trace = RemoteControlDiagnostics( append: (s) async => batches.add(s), ); trace.begin('SN-1', {'intervalMs': 250}); final completion = Completer(); late Future observed; final result = trace.frame('loop', {'tick': 2}, () { trace.event('conversion', {'linearMps': 1.0}); observed = completion.future.then( (_) => trace.event('socket_flush_complete', {}), ); return true; }); trace.begin('SN-2', {'intervalMs': 250}); completion.complete(); await observed; await trace.flush(); final lines = records(batches); final conversion = lines.singleWhere((r) => r['stage'] == 'conversion'); final flushed = lines.singleWhere( (r) => r['stage'] == 'socket_flush_complete', ); expect(result, isTrue); expect(flushed['frame'], conversion['frame']); expect(flushed['session'], conversion['session']); expect(flushed['device'], 'SN-1'); expect(flushed['source'], 'loop'); }, ); test('file writes are serialized and preserve event order', () async { final batches = []; final held = Completer(); var active = 0; var maxActive = 0; final trace = RemoteControlDiagnostics( append: (s) async { active++; if (active > maxActive) maxActive = active; await held.future; batches.add(s); active--; }, ); trace.event('first', {}); final writing = trace.flush(); trace.event('second', {}); final again = trace.flush(); held.complete(); await Future.wait([writing, again]); await trace.flush(); expect(maxActive, 1); expect(records(batches).map((r) => r['stage']), ['first', 'second']); }); test( 'disk failures do not interrupt control and missing records are marked', () async { final batches = []; var fail = true; final trace = RemoteControlDiagnostics( append: (s) async { if (fail) throw StateError('test disk failure'); batches.add(s); }, ); final result = trace.frame('stop', {}, () { trace.event('socket_write_attempt', {}); return false; }); await trace.flush(); fail = false; trace.event('next', {}); await trace.flush(); expect(result, isFalse); expect(records(batches).first, { 'stage': 'diagnostic_overflow', 'dropped': 1, }); }, ); test('buffer is bounded and discarded records are explicit', () async { final batches = []; final trace = RemoteControlDiagnostics(append: (s) async => batches.add(s)); for (var i = 0; i < 2010; i++) { trace.event('tick', {'tick': i}); } await trace.flush(); final lines = records(batches); expect(lines.length, 2001); expect(lines.first['dropped'], 10); expect(lines[1]['tick'], 10); expect(lines.last['tick'], 2009); }); test( 'current velocity conversion and float64 little-endian payload stay intact', () { final algorithm = DiffSteerUseCase(); for (final y in [-100, -50, 0, 50, 100]) { final speeds = algorithm.calculate(0, y); final payload = MachineProtocolCodec.encodeRemoteControlPayload( linear: speeds['linear']!, angular: speeds['angular']!, ); final decoded = ByteData.sublistView(payload); expect(payload.length, 20); expect(decoded.getFloat64(0, Endian.little), y / 100); expect(decoded.getFloat64(8, Endian.little), 0); } }, ); }