update to zjs81/dev 98f6cf3745
This commit is contained in:
commit
1fc6a9bf68
128 files changed
+11006
-1179
No files matched your search
@@ -22,15 +22,64 @@ Contact _contact({
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('resolvePathNames ignores chat nodes and keeps repeater/room nodes', () {
|
||||
test('splitPathBytes groups bytes by hash width', () {
|
||||
final hops = PathHelper.splitPathBytes([0xA1, 0xA2, 0xC1, 0xC2], 2);
|
||||
|
||||
expect(hops, hasLength(2));
|
||||
expect(hops.first, equals(Uint8List.fromList([0xA1, 0xA2])));
|
||||
expect(hops.last, equals(Uint8List.fromList([0xC1, 0xC2])));
|
||||
expect(PathHelper.formatHopHex(hops.first), equals('A1A2'));
|
||||
});
|
||||
|
||||
test(
|
||||
'resolvePathNames ignores chat nodes and keeps repeater/room nodes with 1-byte paths',
|
||||
() {
|
||||
final contacts = [
|
||||
_contact(firstByte: 0xF2, name: 'MunTui', type: advTypeChat),
|
||||
_contact(firstByte: 0x7E, name: 'zrepeater', type: advTypeRepeater),
|
||||
_contact(firstByte: 0xBA, name: 'USS Ronald Reagan', type: advTypeRoom),
|
||||
];
|
||||
|
||||
final resolved = PathHelper.resolvePathNames(
|
||||
[0xF2, 0x7E, 0xBA],
|
||||
contacts,
|
||||
1, // 1-byte hash width
|
||||
);
|
||||
|
||||
expect(resolved, equals('F2 → zrepeater → USS Ronald Reagan'));
|
||||
},
|
||||
);
|
||||
|
||||
test('resolvePathNames supports multi-byte paths', () {
|
||||
final contacts = [
|
||||
_contact(firstByte: 0xF2, name: 'MunTui', type: advTypeChat),
|
||||
_contact(firstByte: 0x7E, name: 'zrepeater', type: advTypeRepeater),
|
||||
_contact(firstByte: 0xBA, name: 'USS Ronald Reagan', type: advTypeRoom),
|
||||
_contact(firstByte: 0xA1, name: 'Repeater1', type: advTypeRepeater),
|
||||
_contact(firstByte: 0xC1, name: 'Room42', type: advTypeRoom),
|
||||
];
|
||||
|
||||
final resolved = PathHelper.resolvePathNames([0xF2, 0x7E, 0xBA], contacts);
|
||||
// Contacts with 2-byte public keys
|
||||
contacts[0] = Contact(
|
||||
publicKey: Uint8List.fromList([0xA1, 0xA2, ...List.filled(30, 0)]),
|
||||
name: 'Repeater1',
|
||||
type: advTypeRepeater,
|
||||
pathLength: 0,
|
||||
path: Uint8List(0),
|
||||
lastSeen: DateTime.now(),
|
||||
);
|
||||
contacts[1] = Contact(
|
||||
publicKey: Uint8List.fromList([0xC1, 0xC2, ...List.filled(30, 0)]),
|
||||
name: 'Room42',
|
||||
type: advTypeRoom,
|
||||
pathLength: 0,
|
||||
path: Uint8List(0),
|
||||
lastSeen: DateTime.now(),
|
||||
);
|
||||
|
||||
expect(resolved, equals('F2 → zrepeater → USS Ronald Reagan'));
|
||||
final resolved = PathHelper.resolvePathNames(
|
||||
[0xA1, 0xA2, 0xC1, 0xC2],
|
||||
contacts,
|
||||
2, // 2-byte hash width
|
||||
);
|
||||
|
||||
expect(resolved, equals('Repeater1 → Room42'));
|
||||
});
|
||||
}
|
||||
@@ -1,10 +1,19 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/models/channel_message.dart';
|
||||
import 'package:meshcore_open/models/contact.dart';
|
||||
import 'package:meshcore_open/models/path_history.dart';
|
||||
import 'package:meshcore_open/models/app_settings.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/storage/contact_store.dart';
|
||||
import 'package:meshcore_open/storage/channel_message_store.dart';
|
||||
import 'package:meshcore_open/storage/contact_discovery_store.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:meshcore_open/storage/message_store.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
// Builds a valid contact frame with the given pathLen and optional overrides.
|
||||
// Frame layout: [respCode(1)][pubKey(32)][type(1)][flags(1)][pathLen(1)][path(64)][name(32)][timestamp(4)][lat(4)][lon(4)]
|
||||
@@ -37,7 +46,108 @@ Uint8List _buildContactFrame({
|
||||
return Uint8List.fromList(writer.toBytes());
|
||||
}
|
||||
|
||||
Uint8List _buildChannelMessageFrameV3({
|
||||
required int pathHashWidth,
|
||||
required int hopCount,
|
||||
required bool hasPath,
|
||||
int channelIndex = 7,
|
||||
String senderName = 'Alice',
|
||||
|
||||
String text = 'Hello world',
|
||||
int txtType = txtTypePlain,
|
||||
}) {
|
||||
final writer = BytesBuilder();
|
||||
writer.addByte(respCodeChannelMsgRecvV3);
|
||||
writer.addByte(0x10);
|
||||
writer.addByte(hasPath ? 0x01 : 0x00);
|
||||
writer.addByte(0x00);
|
||||
writer.addByte(channelIndex);
|
||||
writer.addByte(((pathHashWidth - 1) << 6) | hopCount);
|
||||
|
||||
if (hasPath && hopCount > 0) {
|
||||
writer.add(
|
||||
Uint8List.fromList(List.generate(hopCount * pathHashWidth, (i) => i + 1)),
|
||||
);
|
||||
}
|
||||
|
||||
writer.addByte(txtType);
|
||||
writer.add(Uint8List.fromList([0x01, 0x00, 0x00, 0x00]));
|
||||
writer.add(utf8.encode('$senderName: $text'));
|
||||
writer.addByte(0x00);
|
||||
|
||||
return Uint8List.fromList(writer.toBytes());
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
});
|
||||
|
||||
group('ChannelMessage.fromFrame — V3 packed path decoding', () {
|
||||
test(
|
||||
'hasPath reads width, hop count, path bytes, and txtType correctly',
|
||||
() {
|
||||
final frame = _buildChannelMessageFrameV3(
|
||||
pathHashWidth: 2,
|
||||
hopCount: 3,
|
||||
hasPath: true,
|
||||
);
|
||||
|
||||
final message = ChannelMessage.fromFrame(frame);
|
||||
|
||||
expect(message, isNotNull);
|
||||
expect(message!.pathHashWidth, equals(2));
|
||||
expect(message.pathLength, equals(3));
|
||||
expect(message.pathBytes.length, equals(6));
|
||||
expect(message.senderName, equals('Alice'));
|
||||
expect(message.text, equals('Hello world'));
|
||||
},
|
||||
);
|
||||
|
||||
test('no path still reads width, hop count, and txtType correctly', () {
|
||||
final frame = _buildChannelMessageFrameV3(
|
||||
pathHashWidth: 4,
|
||||
hopCount: 5,
|
||||
hasPath: false,
|
||||
);
|
||||
|
||||
final message = ChannelMessage.fromFrame(frame);
|
||||
|
||||
expect(message, isNotNull);
|
||||
expect(message!.pathHashWidth, equals(4));
|
||||
expect(message.pathLength, equals(5));
|
||||
expect(message.pathBytes, isEmpty);
|
||||
expect(message.senderName, equals('Alice'));
|
||||
expect(message.text, equals('Hello world'));
|
||||
});
|
||||
|
||||
test('non-plain txtType with path -> returns null', () {
|
||||
final frame = _buildChannelMessageFrameV3(
|
||||
pathHashWidth: 2,
|
||||
hopCount: 2,
|
||||
hasPath: true,
|
||||
txtType: txtTypeCliData,
|
||||
);
|
||||
final message = ChannelMessage.fromFrame(frame);
|
||||
expect(message, isNull);
|
||||
});
|
||||
|
||||
test('non-plain txtType without path -> returns null', () {
|
||||
final frame = _buildChannelMessageFrameV3(
|
||||
pathHashWidth: 2,
|
||||
hopCount: 2,
|
||||
hasPath: false,
|
||||
txtType: txtTypeCliData,
|
||||
);
|
||||
final message = ChannelMessage.fromFrame(frame);
|
||||
expect(message, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('Contact.fromFrame — pathLen mapping', () {
|
||||
test('pathLen == 0 → pathLength == 0 (direct, NOT flood)', () {
|
||||
final frame = _buildContactFrame(pathLen: 0);
|
||||
@@ -53,11 +163,11 @@ void main() {
|
||||
expect(contact!.pathLength, equals(1));
|
||||
});
|
||||
|
||||
test('pathLen == 64 (maxPathSize) → pathLength == 64', () {
|
||||
final frame = _buildContactFrame(pathLen: maxPathSize);
|
||||
test('pathLen == 64 (mode 1, 0 hops) → pathLength == 0', () {
|
||||
final frame = _buildContactFrame(pathLen: 64);
|
||||
final contact = Contact.fromFrame(frame);
|
||||
expect(contact, isNotNull);
|
||||
expect(contact!.pathLength, equals(maxPathSize));
|
||||
expect(contact!.pathLength, equals(0));
|
||||
});
|
||||
|
||||
test('pathLen == 0xFF → pathLength == -1 (flood)', () {
|
||||
@@ -67,11 +177,18 @@ void main() {
|
||||
expect(contact!.pathLength, equals(-1));
|
||||
});
|
||||
|
||||
test('pathLen == 65 (over maxPathSize) → pathLength == -1 (flood)', () {
|
||||
test('pathLen == 65 (mode 1, 1 hop) → pathLength == 1', () {
|
||||
final frame = _buildContactFrame(pathLen: 65);
|
||||
final contact = Contact.fromFrame(frame);
|
||||
expect(contact, isNotNull);
|
||||
expect(contact!.pathLength, equals(-1));
|
||||
expect(contact!.pathLength, equals(1));
|
||||
});
|
||||
|
||||
test('pathLen == 129 (mode 2, 1 hop) → pathLength == 1', () {
|
||||
final frame = _buildContactFrame(pathLen: 129);
|
||||
final contact = Contact.fromFrame(frame);
|
||||
expect(contact, isNotNull);
|
||||
expect(contact!.pathLength, equals(1));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -358,4 +475,213 @@ void main() {
|
||||
expect(updated.maxRouteWeight, equals(settings.maxRouteWeight));
|
||||
});
|
||||
});
|
||||
|
||||
group('Storage migration — multi-byte paths compatibility', () {
|
||||
test(
|
||||
'ContactStore decodes and migrates legacy mode-encoded paths',
|
||||
() async {
|
||||
final store = ContactStore()..publicKeyHex = '1234567890';
|
||||
|
||||
// Let's create a contact with legacy 64 path length (mode 1, 0 hops)
|
||||
final rawPath = Uint8List(64); // 64 bytes of zeroes
|
||||
final contactJson = [
|
||||
{
|
||||
'publicKey': base64Encode(Uint8List(32)..[0] = 0xAA),
|
||||
'name': 'LegacyNode',
|
||||
'type': 2,
|
||||
'flags': 0,
|
||||
'pathLength': 64, // encoded pathLength (mode 1, 0 hops)
|
||||
'path': base64Encode(rawPath),
|
||||
'lastSeen': DateTime.now().millisecondsSinceEpoch,
|
||||
'lastMessageAt': DateTime.now().millisecondsSinceEpoch,
|
||||
'isActive': true,
|
||||
},
|
||||
];
|
||||
|
||||
final prefs = PrefsManager.instance;
|
||||
await prefs.setString(store.keyFor, jsonEncode(contactJson));
|
||||
|
||||
final contacts = await store.loadContacts();
|
||||
expect(contacts, hasLength(1));
|
||||
expect(contacts.first.pathLength, equals(0));
|
||||
expect(contacts.first.path, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'ContactStore decodes and migrates legacy mode-1 paths with hops',
|
||||
() async {
|
||||
final store = ContactStore()..publicKeyHex = '1234567890';
|
||||
|
||||
// Contact with legacy 65 path length (mode 1, 1 hop)
|
||||
final rawPath = Uint8List(64)
|
||||
..[0] = 0xBB
|
||||
..[1] = 0xCC;
|
||||
final contactJson = [
|
||||
{
|
||||
'publicKey': base64Encode(Uint8List(32)..[0] = 0xAA),
|
||||
'name': 'LegacyNode2',
|
||||
'type': 2,
|
||||
'flags': 0,
|
||||
'pathLength': 65, // encoded pathLength (mode 1, 1 hop)
|
||||
'path': base64Encode(rawPath),
|
||||
'lastSeen': DateTime.now().millisecondsSinceEpoch,
|
||||
'lastMessageAt': DateTime.now().millisecondsSinceEpoch,
|
||||
'isActive': true,
|
||||
},
|
||||
];
|
||||
|
||||
final prefs = PrefsManager.instance;
|
||||
await prefs.setString(store.keyFor, jsonEncode(contactJson));
|
||||
|
||||
final contacts = await store.loadContacts();
|
||||
expect(contacts, hasLength(1));
|
||||
expect(contacts.first.pathLength, equals(1));
|
||||
expect(contacts.first.path, equals(Uint8List.fromList([0xBB, 0xCC])));
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'MessageStore decodes and migrates legacy mode-encoded message paths',
|
||||
() async {
|
||||
final store = MessageStore()..publicKeyHex = '1234567890';
|
||||
final contactKeyHex = pubKeyToHex(Uint8List(32)..[0] = 0xAA);
|
||||
|
||||
final rawPath = Uint8List(64);
|
||||
final messageJson = [
|
||||
{
|
||||
'senderKey': base64Encode(Uint8List(32)..[0] = 0xAA),
|
||||
'text': 'Hello',
|
||||
'timestamp': DateTime.now().millisecondsSinceEpoch,
|
||||
'isOutgoing': false,
|
||||
'status': 2, // delivered
|
||||
'messageId': 'msg_1',
|
||||
'pathLength': 64, // encoded pathLength (mode 1, 0 hops)
|
||||
'pathBytes': base64Encode(rawPath),
|
||||
},
|
||||
];
|
||||
|
||||
final prefs = PrefsManager.instance;
|
||||
await prefs.setString(
|
||||
'${store.keyFor}$contactKeyHex',
|
||||
jsonEncode(messageJson),
|
||||
);
|
||||
|
||||
final messages = await store.loadMessages(contactKeyHex);
|
||||
expect(messages, hasLength(1));
|
||||
expect(messages.first.pathLength, equals(0));
|
||||
expect(messages.first.pathBytes, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'ChannelMessageStore decodes and migrates legacy mode-encoded paths',
|
||||
() async {
|
||||
final store = ChannelMessageStore()..publicKeyHex = '1234567890';
|
||||
final channelIndex = 1;
|
||||
|
||||
final rawPath = Uint8List(64)
|
||||
..[0] = 0xBB
|
||||
..[1] = 0xCC;
|
||||
final messageJson = [
|
||||
{
|
||||
'senderName': 'Alice',
|
||||
'text': 'Hello',
|
||||
'timestamp': DateTime.now().millisecondsSinceEpoch,
|
||||
'isOutgoing': false,
|
||||
'status': 2,
|
||||
'channelIndex': channelIndex,
|
||||
'pathLength': 65, // encoded pathLength (mode 1, 1 hop)
|
||||
'pathBytes': base64Encode(rawPath),
|
||||
},
|
||||
];
|
||||
|
||||
final prefs = PrefsManager.instance;
|
||||
await prefs.setString(
|
||||
'${store.keyFor}$channelIndex',
|
||||
jsonEncode(messageJson),
|
||||
);
|
||||
|
||||
final messages = await store.loadChannelMessages(channelIndex);
|
||||
expect(messages, hasLength(1));
|
||||
expect(messages.first.pathLength, equals(1));
|
||||
expect(
|
||||
messages.first.pathBytes,
|
||||
equals(Uint8List.fromList([0xBB, 0xCC])),
|
||||
);
|
||||
expect(messages.first.pathHashWidth, equals(2));
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'ContactDiscoveryStore decodes and migrates legacy mode-encoded paths',
|
||||
() async {
|
||||
final store = ContactDiscoveryStore();
|
||||
|
||||
final rawPath = Uint8List(64)
|
||||
..[0] = 0x11
|
||||
..[1] = 0x22;
|
||||
final contactJson = [
|
||||
{
|
||||
'publicKey': base64Encode(Uint8List(32)..[0] = 0xBB),
|
||||
'name': 'DiscoveredNode',
|
||||
'type': 1,
|
||||
'flags': 0,
|
||||
'pathLength': 65, // encoded pathLength (mode 1, 1 hop)
|
||||
'path': base64Encode(rawPath),
|
||||
'lastSeen': DateTime.now().millisecondsSinceEpoch,
|
||||
'lastMessageAt': DateTime.now().millisecondsSinceEpoch,
|
||||
},
|
||||
];
|
||||
|
||||
final prefs = PrefsManager.instance;
|
||||
await prefs.setString('discovered_contacts', jsonEncode(contactJson));
|
||||
|
||||
final contacts = await store.loadContacts();
|
||||
expect(contacts, hasLength(1));
|
||||
expect(contacts.first.pathLength, equals(1));
|
||||
expect(contacts.first.path, equals(Uint8List.fromList([0x11, 0x22])));
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('AppSettingsService — gps interval fallback', () {
|
||||
test('resolvedGpsIntervalSeconds prefers device custom var', () {
|
||||
final service = AppSettingsService();
|
||||
|
||||
expect(
|
||||
service.resolvedGpsIntervalSeconds(const {'gps_interval': '120'}),
|
||||
equals(120),
|
||||
);
|
||||
});
|
||||
|
||||
test('resolvedGpsIntervalSeconds falls back to stored value', () async {
|
||||
final service = AppSettingsService();
|
||||
await service.updateSettings(AppSettings(gpsIntervalSeconds: 900));
|
||||
|
||||
expect(service.resolvedGpsIntervalSeconds(null), equals(900));
|
||||
expect(
|
||||
service.resolvedGpsIntervalSeconds(const {'gps_interval': 'bad'}),
|
||||
equals(900),
|
||||
);
|
||||
});
|
||||
|
||||
test('resolvedGpsIntervalSeconds keeps an explicit device zero', () async {
|
||||
final service = AppSettingsService();
|
||||
await service.updateSettings(AppSettings(gpsIntervalSeconds: 900));
|
||||
|
||||
expect(
|
||||
service.resolvedGpsIntervalSeconds(const {'gps_interval': '0'}),
|
||||
equals(0),
|
||||
);
|
||||
});
|
||||
|
||||
test('toJson/fromJson preserves gpsIntervalSeconds', () {
|
||||
final settings = AppSettings(gpsIntervalSeconds: 321);
|
||||
|
||||
final restored = AppSettings.fromJson(settings.toJson());
|
||||
|
||||
expect(restored.gpsIntervalSeconds, equals(321));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,403 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:meshcore_open/connector/meshcore_connector.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/screens/path_trace_map.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/services/map_tile_cache_service.dart';
|
||||
import 'package:meshcore_open/models/contact.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/services/path_history_service.dart';
|
||||
import 'package:meshcore_open/services/storage_service.dart';
|
||||
import 'package:meshcore_open/models/path_history.dart';
|
||||
|
||||
class _FakeStorageService extends StorageService {
|
||||
@override
|
||||
Future<void> savePathHistory(
|
||||
String contactPubKeyHex,
|
||||
ContactPathHistory history,
|
||||
) async {}
|
||||
@override
|
||||
Future<ContactPathHistory?> loadPathHistory(String contactPubKeyHex) async =>
|
||||
null;
|
||||
@override
|
||||
Future<void> clearPathHistory(String contactPubKeyHex) async {}
|
||||
}
|
||||
|
||||
class _FakeMeshCoreConnector extends MeshCoreConnector {
|
||||
final StreamController<Uint8List> _receivedFramesController =
|
||||
StreamController<Uint8List>.broadcast();
|
||||
|
||||
@override
|
||||
Stream<Uint8List> get receivedFrames => _receivedFramesController.stream;
|
||||
|
||||
@override
|
||||
Uint8List? get selfPublicKey =>
|
||||
Uint8List.fromList(List.generate(32, (i) => i));
|
||||
|
||||
@override
|
||||
double? get selfLatitude => 48.8566;
|
||||
|
||||
@override
|
||||
double? get selfLongitude => 2.3522;
|
||||
|
||||
@override
|
||||
List<Contact> get allContactsUnfiltered => [];
|
||||
|
||||
@override
|
||||
List<Contact> get contacts => [];
|
||||
|
||||
void emitFrame(Uint8List frame) {
|
||||
_receivedFramesController.add(frame);
|
||||
}
|
||||
|
||||
final List<Uint8List> sentFrames = [];
|
||||
|
||||
@override
|
||||
Future<void> sendFrame(
|
||||
Uint8List frame, {
|
||||
String? channelSendQueueId,
|
||||
bool expectsGenericAck = false,
|
||||
bool waitForGenericAck = false,
|
||||
}) async {
|
||||
sentFrames.add(frame);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildTestApp({
|
||||
required MeshCoreConnector connector,
|
||||
required Widget child,
|
||||
}) {
|
||||
final appSettingsService = AppSettingsService();
|
||||
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider<MeshCoreConnector>.value(value: connector),
|
||||
ChangeNotifierProvider<AppSettingsService>.value(
|
||||
value: appSettingsService,
|
||||
),
|
||||
ChangeNotifierProvider<MapTileCacheService>(
|
||||
create: (_) =>
|
||||
MapTileCacheService(appSettingsService: appSettingsService),
|
||||
),
|
||||
ChangeNotifierProvider<PathHistoryService>(
|
||||
create: (_) => PathHistoryService(_FakeStorageService()),
|
||||
),
|
||||
],
|
||||
child: MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('PathTraceMapScreen parses response frames correctly', (
|
||||
tester,
|
||||
) async {
|
||||
final connector = _FakeMeshCoreConnector();
|
||||
|
||||
final screen = PathTraceMapScreen(
|
||||
title: 'Test Trace',
|
||||
path: Uint8List.fromList([0x12, 0x34]),
|
||||
pathHashByteWidth: 1,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(_buildTestApp(connector: connector, child: screen));
|
||||
await tester.pump();
|
||||
|
||||
// Verify a send request was made
|
||||
expect(connector.sentFrames.length, 1);
|
||||
final sentFrame = connector.sentFrames.first;
|
||||
expect(sentFrame[0], cmdSendTracePath);
|
||||
|
||||
// Extract the tag sent in the request (bytes 2-5)
|
||||
final tag = sentFrame.sublist(2, 6);
|
||||
|
||||
// Emit respCodeSent (6)
|
||||
final respCodeSentFrame = Uint8List(10);
|
||||
respCodeSentFrame[0] = respCodeSent;
|
||||
respCodeSentFrame[1] = 0; // reserved / is_flood
|
||||
respCodeSentFrame.setRange(2, 6, tag);
|
||||
// timeout milliseconds = 5000 (0x1388)
|
||||
respCodeSentFrame[6] = 0x88;
|
||||
respCodeSentFrame[7] = 0x13;
|
||||
respCodeSentFrame[8] = 0x00;
|
||||
respCodeSentFrame[9] = 0x00;
|
||||
|
||||
connector.emitFrame(respCodeSentFrame);
|
||||
await tester.pump();
|
||||
|
||||
// Now emit PUSH_CODE_TRACE_DATA (0x89)
|
||||
// Structure:
|
||||
// Offset 0: pushCodeTraceData (0x89)
|
||||
// Offset 1: reserved (0)
|
||||
// Offset 2: pathLength (2)
|
||||
// Offset 3: flag (0)
|
||||
// Offset 4..7: tag
|
||||
// Offset 8..11: auth (0)
|
||||
// Offset 12..13: pathBytes [0x12, 0x34]
|
||||
// Offset 14..16: SNR bytes [12, 16, 20] -> to be mapped to signed 8 bit Snr/4
|
||||
final pushTraceFrame = Uint8List(17);
|
||||
pushTraceFrame[0] = pushCodeTraceData;
|
||||
pushTraceFrame[1] = 0; // reserved
|
||||
pushTraceFrame[2] = 2; // pathLength
|
||||
pushTraceFrame[3] = 0; // flag
|
||||
pushTraceFrame.setRange(4, 8, tag);
|
||||
// auth bytes (8..11) = 0
|
||||
pushTraceFrame.setRange(12, 14, [0x12, 0x34]); // pathBytes
|
||||
pushTraceFrame.setRange(14, 17, [12, 16, 20]); // SNR bytes
|
||||
|
||||
connector.emitFrame(pushTraceFrame);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
}
|
||||
|
||||
// Verify it doesn't show "Path trace not available" or similar
|
||||
expect(find.text('Path trace not available.'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'PathTraceMapScreen parses multi-byte encoded path trace response correctly',
|
||||
(tester) async {
|
||||
final connector = _FakeMeshCoreConnector();
|
||||
|
||||
final screen = PathTraceMapScreen(
|
||||
title: 'Test Trace Multi-byte',
|
||||
path: Uint8List.fromList([0x12, 0x34]),
|
||||
pathHashByteWidth: 2,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildTestApp(connector: connector, child: screen),
|
||||
);
|
||||
await tester.pump();
|
||||
|
||||
// Verify a send request was made
|
||||
expect(connector.sentFrames.length, 1);
|
||||
final sentFrame = connector.sentFrames.first;
|
||||
expect(sentFrame[0], cmdSendTracePath);
|
||||
|
||||
// Extract the tag sent in the request (bytes 2-5)
|
||||
final tag = sentFrame.sublist(2, 6);
|
||||
|
||||
// Emit respCodeSent (6)
|
||||
final respCodeSentFrame = Uint8List(10);
|
||||
respCodeSentFrame[0] = respCodeSent;
|
||||
respCodeSentFrame[1] = 0;
|
||||
respCodeSentFrame.setRange(2, 6, tag);
|
||||
respCodeSentFrame[6] = 0x88;
|
||||
respCodeSentFrame[7] = 0x13;
|
||||
respCodeSentFrame[8] = 0x00;
|
||||
respCodeSentFrame[9] = 0x00;
|
||||
|
||||
connector.emitFrame(respCodeSentFrame);
|
||||
await tester.pump();
|
||||
|
||||
// Now emit PUSH_CODE_TRACE_DATA (0x89)
|
||||
// Structure:
|
||||
// Offset 0: pushCodeTraceData (0x89)
|
||||
// Offset 1: reserved (0)
|
||||
// Offset 2: pathLength (65) -> mode 1 (width 2), 1 hop
|
||||
// Offset 3: flag (0)
|
||||
// Offset 4..7: tag
|
||||
// Offset 8..11: auth (0)
|
||||
// Offset 12..13: pathBytes [0x12, 0x34]
|
||||
// Offset 14..15: SNR bytes [12, 16]
|
||||
final pushTraceFrame = Uint8List(16);
|
||||
pushTraceFrame[0] = pushCodeTraceData;
|
||||
pushTraceFrame[1] = 0; // reserved
|
||||
pushTraceFrame[2] = 65; // pathLength encoded (mode 1, 1 hop -> 2 bytes)
|
||||
pushTraceFrame[3] = 0; // flag
|
||||
pushTraceFrame.setRange(4, 8, tag);
|
||||
pushTraceFrame.setRange(12, 14, [0x12, 0x34]); // pathBytes
|
||||
pushTraceFrame.setRange(14, 16, [12, 16]); // SNR bytes
|
||||
|
||||
connector.emitFrame(pushTraceFrame);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
}
|
||||
|
||||
// Verify it parsed correctly (should not show the unavailable message)
|
||||
expect(find.text('Path trace not available.'), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('PathTraceMapScreen buildPath: Direct repeater ping (0-hop)', (
|
||||
tester,
|
||||
) async {
|
||||
final connector = _FakeMeshCoreConnector();
|
||||
final target = Contact(
|
||||
publicKey: Uint8List.fromList([0xAA, 0xBB, 0xCC]),
|
||||
name: 'Repeater Node',
|
||||
type: advTypeRepeater,
|
||||
path: Uint8List(0),
|
||||
pathLength: 0,
|
||||
lastSeen: DateTime.now(),
|
||||
);
|
||||
|
||||
final screen = PathTraceMapScreen(
|
||||
title: 'Direct Repeater Ping',
|
||||
path: Uint8List.fromList([0xAA]),
|
||||
targetContact: target,
|
||||
flipPathAround: true,
|
||||
pathHashByteWidth: 1,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(_buildTestApp(connector: connector, child: screen));
|
||||
await tester.pump();
|
||||
|
||||
expect(connector.sentFrames.length, 1);
|
||||
final sentFrame = connector.sentFrames.first;
|
||||
expect(sentFrame[0], cmdSendTracePath);
|
||||
final payload = sentFrame.sublist(10);
|
||||
expect(payload, Uint8List.fromList([0xAA]));
|
||||
});
|
||||
|
||||
testWidgets('PathTraceMapScreen buildPath: 1-hop chat contact trace', (
|
||||
tester,
|
||||
) async {
|
||||
final connector = _FakeMeshCoreConnector();
|
||||
final target = Contact(
|
||||
publicKey: Uint8List.fromList([0xCC, 0xDD, 0xEE]),
|
||||
name: 'Chat User',
|
||||
type: advTypeChat,
|
||||
path: Uint8List.fromList([0xBB]),
|
||||
pathLength: 1,
|
||||
lastSeen: DateTime.now(),
|
||||
);
|
||||
|
||||
final screen = PathTraceMapScreen(
|
||||
title: '1-Hop Chat Trace',
|
||||
path: Uint8List.fromList([0xBB]),
|
||||
targetContact: target,
|
||||
flipPathAround: true,
|
||||
pathHashByteWidth: 1,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(_buildTestApp(connector: connector, child: screen));
|
||||
await tester.pump();
|
||||
|
||||
expect(connector.sentFrames.length, 1);
|
||||
final sentFrame = connector.sentFrames.first;
|
||||
expect(sentFrame[0], cmdSendTracePath);
|
||||
final payload = sentFrame.sublist(10);
|
||||
expect(payload, Uint8List.fromList([0xBB, 0xCC, 0xBB]));
|
||||
});
|
||||
|
||||
testWidgets('PathTraceMapScreen buildPath: Multi-hop chat contact trace', (
|
||||
tester,
|
||||
) async {
|
||||
final connector = _FakeMeshCoreConnector();
|
||||
final target = Contact(
|
||||
publicKey: Uint8List.fromList([0x33, 0x44, 0x55]),
|
||||
name: 'Chat User Multi-hop',
|
||||
type: advTypeChat,
|
||||
path: Uint8List.fromList([0x11, 0x22]),
|
||||
pathLength: 2,
|
||||
lastSeen: DateTime.now(),
|
||||
);
|
||||
|
||||
final screen = PathTraceMapScreen(
|
||||
title: 'Multi-Hop Chat Trace',
|
||||
path: Uint8List.fromList([0x11, 0x22]),
|
||||
targetContact: target,
|
||||
flipPathAround: true,
|
||||
pathHashByteWidth: 1,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(_buildTestApp(connector: connector, child: screen));
|
||||
await tester.pump();
|
||||
|
||||
expect(connector.sentFrames.length, 1);
|
||||
final sentFrame = connector.sentFrames.first;
|
||||
expect(sentFrame[0], cmdSendTracePath);
|
||||
final payload = sentFrame.sublist(10);
|
||||
expect(payload, Uint8List.fromList([0x11, 0x22, 0x33, 0x22, 0x11]));
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'PathTraceMapScreen buildPath: Map trace with null target contact',
|
||||
(tester) async {
|
||||
final connector = _FakeMeshCoreConnector();
|
||||
|
||||
final screen = PathTraceMapScreen(
|
||||
title: 'Map Trace No Target',
|
||||
path: Uint8List.fromList([0x11, 0x22]),
|
||||
targetContact: null,
|
||||
flipPathAround: true,
|
||||
pathHashByteWidth: 1,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(
|
||||
_buildTestApp(connector: connector, child: screen),
|
||||
);
|
||||
await tester.pump();
|
||||
|
||||
expect(connector.sentFrames.length, 1);
|
||||
final sentFrame = connector.sentFrames.first;
|
||||
expect(sentFrame[0], cmdSendTracePath);
|
||||
final payload = sentFrame.sublist(10);
|
||||
expect(payload, Uint8List.fromList([0x11, 0x22, 0x11]));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('PathTraceMapScreen parses raw byte length fallback correctly', (
|
||||
tester,
|
||||
) async {
|
||||
final connector = _FakeMeshCoreConnector();
|
||||
|
||||
final screen = PathTraceMapScreen(
|
||||
title: 'Test Raw Fallback',
|
||||
path: Uint8List.fromList([0x12, 0x34]),
|
||||
pathHashByteWidth: 2,
|
||||
);
|
||||
|
||||
await tester.pumpWidget(_buildTestApp(connector: connector, child: screen));
|
||||
await tester.pump();
|
||||
|
||||
expect(connector.sentFrames.length, 1);
|
||||
final sentFrame = connector.sentFrames.first;
|
||||
// Extract the tag sent in the request (bytes 2-5)
|
||||
final tag = sentFrame.sublist(2, 6);
|
||||
|
||||
// Emit respCodeSent (6)
|
||||
final respCodeSentFrame = Uint8List(10);
|
||||
respCodeSentFrame[0] = respCodeSent;
|
||||
respCodeSentFrame[1] = 0;
|
||||
respCodeSentFrame.setRange(2, 6, tag);
|
||||
respCodeSentFrame[6] = 0x88;
|
||||
respCodeSentFrame[7] = 0x13;
|
||||
respCodeSentFrame[8] = 0x00;
|
||||
respCodeSentFrame[9] = 0x00;
|
||||
|
||||
connector.emitFrame(respCodeSentFrame);
|
||||
await tester.pump();
|
||||
|
||||
// Now emit PUSH_CODE_TRACE_DATA (0x89)
|
||||
// Structure:
|
||||
// Offset 2: pathLength (1) -> mode 0, hop count 1.
|
||||
// Offset 12..13: pathBytes [0x12, 0x34] (1 hop * 2 bytes/hop)
|
||||
final pushTraceFrame = Uint8List(16);
|
||||
pushTraceFrame[0] = pushCodeTraceData;
|
||||
pushTraceFrame[1] = 0; // reserved
|
||||
pushTraceFrame[2] = 1; // pathLength encoded (mode 0, 1 hop)
|
||||
pushTraceFrame[3] = 0; // flag
|
||||
pushTraceFrame.setRange(4, 8, tag);
|
||||
pushTraceFrame.setRange(12, 14, [0x12, 0x34]); // pathBytes
|
||||
pushTraceFrame.setRange(14, 16, [12, 16]); // SNR bytes
|
||||
|
||||
connector.emitFrame(pushTraceFrame);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
}
|
||||
|
||||
// Verify it parsed correctly (should not show the unavailable message)
|
||||
expect(find.text('Path trace not available.'), findsNothing);
|
||||
});
|
||||
}
|
||||
@@ -549,12 +549,15 @@ void main() {
|
||||
final pubKey = _hex('w004');
|
||||
await _seed(svc, pubKey, pathBytes: [0x01], hopCount: 1, weight: 0.3);
|
||||
|
||||
svc.recordPathResult(
|
||||
pubKey,
|
||||
const PathSelection(pathBytes: [0x01], hopCount: 1, useFlood: false),
|
||||
success: false,
|
||||
failureDecrement: 0.5, // 0.3 - 0.5 = -0.2 → remove
|
||||
);
|
||||
// The service requires failureCount >= 3 to remove a path
|
||||
for (int i = 0; i < 3; i++) {
|
||||
svc.recordPathResult(
|
||||
pubKey,
|
||||
const PathSelection(pathBytes: [0x01], hopCount: 1, useFlood: false),
|
||||
success: false,
|
||||
failureDecrement: 0.5, // 0.3 - 0.5 = -0.2 → remove
|
||||
);
|
||||
}
|
||||
await _flush();
|
||||
|
||||
final paths = svc.getRecentPaths(pubKey);
|
||||
|
||||
Reference in new issue
Block a user