Add web serial support and USB tests

This commit is contained in:
just_stuff_tm
2026-03-02 00:27:49 -05:00
committed by just-stuff-tm
parent 22a53439b1
commit c23a1da430
10 changed files with 997 additions and 331 deletions
@@ -0,0 +1,84 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/services/usb_serial_frame_codec.dart';
void main() {
test('wrapUsbSerialTxFrame prefixes tx header and payload length', () {
final packet = wrapUsbSerialTxFrame(Uint8List.fromList(<int>[0x16, 0x03]));
expect(
packet,
orderedEquals(<int>[usbSerialTxFrameStart, 0x02, 0x00, 0x16, 0x03]),
);
});
test('UsbSerialFrameDecoder buffers partial frames until complete', () {
final decoder = UsbSerialFrameDecoder();
final firstChunk = decoder.ingest(
Uint8List.fromList(<int>[usbSerialRxFrameStart, 0x03]),
);
final secondChunk = decoder.ingest(
Uint8List.fromList(<int>[0x00, 0x05, 0x06, 0x07]),
);
expect(firstChunk, isEmpty);
expect(secondChunk, hasLength(1));
expect(secondChunk.single.isRxFrame, isTrue);
expect(secondChunk.single.payload, orderedEquals(<int>[0x05, 0x06, 0x07]));
});
test(
'UsbSerialFrameDecoder drops leading noise and parses multiple frames',
() {
final decoder = UsbSerialFrameDecoder();
final packets = decoder.ingest(
Uint8List.fromList(<int>[
0x00,
0x01,
usbSerialRxFrameStart,
0x01,
0x00,
0x55,
usbSerialRxFrameStart,
0x02,
0x00,
0x66,
0x77,
]),
);
expect(packets, hasLength(2));
expect(packets[0].payload, orderedEquals(<int>[0x55]));
expect(packets[1].payload, orderedEquals(<int>[0x66, 0x77]));
},
);
test(
'UsbSerialFrameDecoder preserves tx packets so caller can ignore them',
() {
final decoder = UsbSerialFrameDecoder();
final packets = decoder.ingest(
Uint8List.fromList(<int>[
usbSerialTxFrameStart,
0x01,
0x00,
0x22,
usbSerialRxFrameStart,
0x01,
0x00,
0x33,
]),
);
expect(packets, hasLength(2));
expect(packets[0].isRxFrame, isFalse);
expect(packets[0].payload, orderedEquals(<int>[0x22]));
expect(packets[1].isRxFrame, isTrue);
expect(packets[1].payload, orderedEquals(<int>[0x33]));
},
);
}
+76
View File
@@ -0,0 +1,76 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/utils/usb_port_labels.dart';
void main() {
test('normalizeUsbPortName strips friendly suffix from composite label', () {
expect(
normalizeUsbPortName(
'COM6 - USB Serial Device (COM6) - USB\\VID_2886&PID_1667',
),
'COM6',
);
});
test('friendlyUsbPortName prefers suffix when present', () {
expect(
friendlyUsbPortName(
'COM6 - USB Serial Device (COM6) - USB\\VID_2886&PID_1667',
),
'USB Serial Device (COM6) - USB\\VID_2886&PID_1667',
);
});
test(
'friendlyUsbPortName falls back to normalized port when suffix is empty',
() {
expect(friendlyUsbPortName('COM6 - '), 'COM6');
},
);
test('describeWebUsbPort uses known VID/PID names when available', () {
expect(
describeWebUsbPort(
vendorId: 0x2886,
productId: 0x1667,
knownUsbNames: const <String, String>{
'2886:1667': 'Seeed Wio Tracker L1',
},
),
'Seeed Wio Tracker L1 (VID:2886 PID:1667)',
);
});
test('describeWebUsbPort falls back to generic label for unknown device', () {
expect(
describeWebUsbPort(vendorId: 0x1234, productId: 0x5678),
'Web Serial Device (VID:1234 PID:5678)',
);
});
test('describeWebUsbPort returns chooser label when no usb ids exist', () {
expect(
describeWebUsbPort(vendorId: null, productId: null),
usbRequestPortLabel,
);
});
test('buildUsbDisplayLabel appends device-reported name when available', () {
expect(
buildUsbDisplayLabel(
basePortLabel: 'Seeed Wio Tracker L1 (VID:2886 PID:1667)',
deviceName: 'KD3CGK mesh-utility.org',
),
'Seeed Wio Tracker L1 (VID:2886 PID:1667) - KD3CGK mesh-utility.org',
);
});
test('buildUsbDisplayLabel keeps base label when custom name is blank', () {
expect(
buildUsbDisplayLabel(
basePortLabel: 'Seeed Wio Tracker L1 (VID:2886 PID:1667)',
deviceName: ' ',
),
'Seeed Wio Tracker L1 (VID:2886 PID:1667)',
);
});
}