Default to https://ipfs.io/ipfs/ like dev, keep saved gateways as they are, and keep an explicitly cleared gateway deactivated across restarts instead of rewriting public gateways to empty on every load.
750 lines
26 KiB
Dart
750 lines
26 KiB
Dart
import 'dart:convert';
|
||
import 'dart:typed_data';
|
||
|
||
import 'package:crypto/crypto.dart' as crypto;
|
||
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/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)]
|
||
Uint8List _buildContactFrame({
|
||
int pathLen = 0,
|
||
Uint8List? pubKey,
|
||
String name = 'TestNode',
|
||
}) {
|
||
final writer = BytesBuilder();
|
||
writer.addByte(respCodeContact); // 3
|
||
writer.add(
|
||
pubKey ?? Uint8List.fromList(List.generate(32, (i) => i + 1)),
|
||
); // valid pubkey
|
||
writer.addByte(1); // type
|
||
writer.addByte(0); // flags
|
||
writer.addByte(pathLen);
|
||
writer.add(Uint8List(64)); // path bytes (zeros)
|
||
// name (32 bytes, null-padded)
|
||
final nameBytes = Uint8List(32);
|
||
final encoded = name.codeUnits;
|
||
for (var i = 0; i < encoded.length && i < 31; i++) {
|
||
nameBytes[i] = encoded[i];
|
||
}
|
||
writer.add(nameBytes);
|
||
// timestamp (4 bytes LE) - some nonzero value
|
||
writer.add(Uint8List.fromList([0x01, 0x00, 0x00, 0x00]));
|
||
// lat, lon (4 bytes each)
|
||
writer.add(Uint8List(4)); // lat
|
||
writer.add(Uint8List(4)); // lon
|
||
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);
|
||
});
|
||
});
|
||
|
||
test('flood transport code uses the scope key HMAC', () {
|
||
final scopeKey = floodScopeKeyForRegion('Europe');
|
||
final payload = Uint8List.fromList([0xA1, 0xB2, 0xC3]);
|
||
|
||
expect(
|
||
floodTransportCode(
|
||
scopeKey: scopeKey,
|
||
payloadType: 0x05,
|
||
payload: payload,
|
||
),
|
||
orderedEquals(
|
||
crypto.Hmac(
|
||
crypto.sha256,
|
||
scopeKey,
|
||
).convert([0x05, ...payload]).bytes.sublist(0, 2),
|
||
),
|
||
);
|
||
});
|
||
|
||
group('Contact.fromFrame — pathLen mapping', () {
|
||
test('pathLen == 0 → pathLength == 0 (direct, NOT flood)', () {
|
||
final frame = _buildContactFrame(pathLen: 0);
|
||
final contact = Contact.fromFrame(frame);
|
||
expect(contact, isNotNull);
|
||
expect(contact!.pathLength, equals(0));
|
||
});
|
||
|
||
test('pathLen == 1 → pathLength == 1', () {
|
||
final frame = _buildContactFrame(pathLen: 1);
|
||
final contact = Contact.fromFrame(frame);
|
||
expect(contact, isNotNull);
|
||
expect(contact!.pathLength, equals(1));
|
||
});
|
||
|
||
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(0));
|
||
});
|
||
|
||
test('pathLen == 0xFF → pathLength == -1 (flood)', () {
|
||
final frame = _buildContactFrame(pathLen: 0xFF);
|
||
final contact = Contact.fromFrame(frame);
|
||
expect(contact, isNotNull);
|
||
expect(contact!.pathLength, equals(-1));
|
||
});
|
||
|
||
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));
|
||
});
|
||
|
||
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));
|
||
});
|
||
});
|
||
|
||
group('Contact.fromFrame — corrupt contact guards', () {
|
||
test('all-zero public key → returns null', () {
|
||
final zeroPubKey = Uint8List(32); // all zeros
|
||
final frame = _buildContactFrame(pubKey: zeroPubKey);
|
||
final contact = Contact.fromFrame(frame);
|
||
expect(contact, isNull);
|
||
});
|
||
|
||
test('mostly-zero public key (>16 zeros out of 32) → returns null', () {
|
||
// 17 zeros out of 32 bytes exceeds pubKeySize ~/ 2 == 16
|
||
final pubKey = Uint8List(32);
|
||
pubKey[0] = 0xAB;
|
||
pubKey[1] = 0xCD;
|
||
pubKey[2] = 0xEF;
|
||
pubKey[3] = 0x12;
|
||
pubKey[4] = 0x34;
|
||
pubKey[5] = 0x56;
|
||
pubKey[6] = 0x78;
|
||
pubKey[7] = 0x9A;
|
||
pubKey[8] = 0xBC;
|
||
pubKey[9] = 0xDE;
|
||
pubKey[10] = 0xF0;
|
||
pubKey[11] = 0x11;
|
||
pubKey[12] = 0x22;
|
||
pubKey[13] = 0x33;
|
||
pubKey[14] = 0x44;
|
||
// bytes 15–31 are zero: that is 17 zeros (indices 15..31 inclusive)
|
||
final frame = _buildContactFrame(pubKey: pubKey);
|
||
final contact = Contact.fromFrame(frame);
|
||
expect(contact, isNull);
|
||
});
|
||
|
||
test('valid public key (few zeros) → returns Contact', () {
|
||
// Only 1 zero → well below the threshold
|
||
final pubKey = Uint8List.fromList(List.generate(32, (i) => i + 1));
|
||
pubKey[5] = 0; // one zero byte
|
||
final frame = _buildContactFrame(pubKey: pubKey);
|
||
final contact = Contact.fromFrame(frame);
|
||
expect(contact, isNotNull);
|
||
});
|
||
|
||
test('name with all non-printable characters → returns null', () {
|
||
// Build frame with a name composed entirely of control characters (< 0x20)
|
||
final nameBytes = Uint8List(32);
|
||
nameBytes[0] = 0x01;
|
||
nameBytes[1] = 0x02;
|
||
nameBytes[2] = 0x03;
|
||
// remaining are 0x00 (null terminator ends the string after index 2,
|
||
// so readCStringGreedy returns a 3-char string of non-printables)
|
||
final writer = BytesBuilder();
|
||
writer.addByte(respCodeContact);
|
||
writer.add(Uint8List.fromList(List.generate(32, (i) => i + 1)));
|
||
writer.addByte(1); // type
|
||
writer.addByte(0); // flags
|
||
writer.addByte(0); // pathLen
|
||
writer.add(Uint8List(64)); // path
|
||
writer.add(nameBytes);
|
||
writer.add(Uint8List.fromList([0x01, 0x00, 0x00, 0x00])); // timestamp
|
||
writer.add(Uint8List(4)); // lat
|
||
writer.add(Uint8List(4)); // lon
|
||
final frame = Uint8List.fromList(writer.toBytes());
|
||
final contact = Contact.fromFrame(frame);
|
||
expect(contact, isNull);
|
||
});
|
||
|
||
test('name with valid printable characters → returns Contact', () {
|
||
final frame = _buildContactFrame(name: 'Alice');
|
||
final contact = Contact.fromFrame(frame);
|
||
expect(contact, isNotNull);
|
||
expect(contact!.name, equals('Alice'));
|
||
});
|
||
|
||
test(
|
||
'name with mix of printable and replacement chars → returns Contact (not all bad)',
|
||
() {
|
||
// Build a name with mostly printable chars and one replacement char (0xFFFD in codeUnits).
|
||
// utf8 allowMalformed: true maps invalid sequences to U+FFFD.
|
||
// We embed one invalid UTF-8 byte (0x80) among valid ASCII bytes.
|
||
// The decoded string will be "Hi\uFFFDThere" — not ALL bad, so should be accepted.
|
||
final nameBytes = Uint8List(32);
|
||
nameBytes[0] = 0x48; // 'H'
|
||
nameBytes[1] = 0x69; // 'i'
|
||
nameBytes[2] = 0x80; // invalid UTF-8 → decoded as U+FFFD
|
||
nameBytes[3] = 0x54; // 'T'
|
||
nameBytes[4] = 0x68; // 'h'
|
||
nameBytes[5] = 0x65; // 'e'
|
||
nameBytes[6] = 0x72; // 'r'
|
||
nameBytes[7] = 0x65; // 'e'
|
||
// rest are 0x00 (null terminator)
|
||
final writer = BytesBuilder();
|
||
writer.addByte(respCodeContact);
|
||
writer.add(Uint8List.fromList(List.generate(32, (i) => i + 1)));
|
||
writer.addByte(1); // type
|
||
writer.addByte(0); // flags
|
||
writer.addByte(0); // pathLen
|
||
writer.add(Uint8List(64)); // path
|
||
writer.add(nameBytes);
|
||
writer.add(Uint8List.fromList([0x01, 0x00, 0x00, 0x00])); // timestamp
|
||
writer.add(Uint8List(4)); // lat
|
||
writer.add(Uint8List(4)); // lon
|
||
final frame = Uint8List.fromList(writer.toBytes());
|
||
final contact = Contact.fromFrame(frame);
|
||
expect(contact, isNotNull);
|
||
},
|
||
);
|
||
});
|
||
|
||
group('PathRecord — routeWeight field', () {
|
||
test('default routeWeight is 1.0', () {
|
||
final record = PathRecord(
|
||
hopCount: 2,
|
||
tripTimeMs: 500,
|
||
timestamp: DateTime(2024),
|
||
wasFloodDiscovery: false,
|
||
pathBytes: [0x01, 0x02],
|
||
successCount: 1,
|
||
failureCount: 0,
|
||
);
|
||
expect(record.routeWeight, equals(1.0));
|
||
});
|
||
|
||
test('custom routeWeight is preserved', () {
|
||
final record = PathRecord(
|
||
hopCount: 3,
|
||
tripTimeMs: 800,
|
||
timestamp: DateTime(2024),
|
||
wasFloodDiscovery: false,
|
||
pathBytes: [0x01],
|
||
successCount: 5,
|
||
failureCount: 2,
|
||
routeWeight: 3.5,
|
||
);
|
||
expect(record.routeWeight, equals(3.5));
|
||
});
|
||
|
||
test('toJson includes route_weight', () {
|
||
final record = PathRecord(
|
||
hopCount: 1,
|
||
tripTimeMs: 200,
|
||
timestamp: DateTime(2024),
|
||
wasFloodDiscovery: true,
|
||
pathBytes: [],
|
||
successCount: 0,
|
||
failureCount: 0,
|
||
routeWeight: 2.25,
|
||
);
|
||
final json = record.toJson();
|
||
expect(json.containsKey('route_weight'), isTrue);
|
||
expect(json['route_weight'], equals(2.25));
|
||
});
|
||
|
||
test('fromJson reads route_weight', () {
|
||
final json = {
|
||
'hop_count': 2,
|
||
'trip_time_ms': 400,
|
||
'timestamp': DateTime(2024).toIso8601String(),
|
||
'was_flood': false,
|
||
'path_bytes': [1, 2, 3],
|
||
'success_count': 3,
|
||
'failure_count': 1,
|
||
'route_weight': 4.0,
|
||
};
|
||
final record = PathRecord.fromJson(json);
|
||
expect(record.routeWeight, equals(4.0));
|
||
});
|
||
|
||
test(
|
||
'fromJson with missing route_weight defaults to 1.0 (backward compat)',
|
||
() {
|
||
final json = {
|
||
'hop_count': 1,
|
||
'trip_time_ms': 100,
|
||
'timestamp': DateTime(2024).toIso8601String(),
|
||
'was_flood': false,
|
||
'path_bytes': [],
|
||
'success_count': 0,
|
||
'failure_count': 0,
|
||
// 'route_weight' intentionally omitted
|
||
};
|
||
final record = PathRecord.fromJson(json);
|
||
expect(record.routeWeight, equals(1.0));
|
||
},
|
||
);
|
||
});
|
||
|
||
group('AppSettings — new fields', () {
|
||
test('default values are correct', () {
|
||
final settings = AppSettings();
|
||
expect(settings.maxRouteWeight, equals(5.0));
|
||
expect(settings.initialRouteWeight, equals(3.0));
|
||
expect(settings.routeWeightSuccessIncrement, equals(0.5));
|
||
expect(settings.routeWeightFailureDecrement, equals(0.2));
|
||
expect(settings.maxMessageRetries, equals(5));
|
||
expect(settings.evictDiscoveredContactsEnabled, isTrue);
|
||
});
|
||
|
||
test('toJson includes all new fields', () {
|
||
final settings = AppSettings();
|
||
final json = settings.toJson();
|
||
expect(json.containsKey('max_route_weight'), isTrue);
|
||
expect(json.containsKey('initial_route_weight'), isTrue);
|
||
expect(json.containsKey('route_weight_success_increment'), isTrue);
|
||
expect(json.containsKey('route_weight_failure_decrement'), isTrue);
|
||
expect(json.containsKey('max_message_retries'), isTrue);
|
||
expect(json.containsKey('evict_discovered_contacts_enabled'), isTrue);
|
||
expect(json['max_route_weight'], equals(5.0));
|
||
expect(json['initial_route_weight'], equals(3.0));
|
||
expect(json['route_weight_success_increment'], equals(0.5));
|
||
expect(json['route_weight_failure_decrement'], equals(0.2));
|
||
expect(json['max_message_retries'], equals(5));
|
||
expect(json['evict_discovered_contacts_enabled'], isTrue);
|
||
});
|
||
|
||
test('fromJson reads all new fields', () {
|
||
final json = {
|
||
'max_route_weight': 10.0,
|
||
'initial_route_weight': 2.0,
|
||
'route_weight_success_increment': 1.0,
|
||
'route_weight_failure_decrement': 1.5,
|
||
'max_message_retries': 8,
|
||
'evict_discovered_contacts_enabled': false,
|
||
};
|
||
final settings = AppSettings.fromJson(json);
|
||
expect(settings.maxRouteWeight, equals(10.0));
|
||
expect(settings.initialRouteWeight, equals(2.0));
|
||
expect(settings.routeWeightSuccessIncrement, equals(1.0));
|
||
expect(settings.routeWeightFailureDecrement, equals(1.5));
|
||
expect(settings.maxMessageRetries, equals(8));
|
||
expect(settings.evictDiscoveredContactsEnabled, isFalse);
|
||
});
|
||
|
||
test('public IPFS gateway defaults to ipfs.io when never set', () {
|
||
expect(
|
||
AppSettings.fromJson({}).publicIpfsGateway,
|
||
'https://ipfs.io/ipfs/',
|
||
);
|
||
expect(AppSettings().publicIpfsGateway, 'https://ipfs.io/ipfs/');
|
||
});
|
||
|
||
test('public IPFS gateway keeps a saved empty value as deactivated', () {
|
||
final saved = AppSettings(publicIpfsGateway: '').toJson();
|
||
expect(AppSettings.fromJson(saved).publicIpfsGateway, isEmpty);
|
||
});
|
||
|
||
test('public IPFS gateway keeps saved ipfs.io and dweb.link values', () {
|
||
for (final gateway in [
|
||
'https://ipfs.io/ipfs/',
|
||
'https://dweb.link/ipfs/',
|
||
'https://gateway.example.com/ipfs/',
|
||
]) {
|
||
final saved = AppSettings(publicIpfsGateway: gateway).toJson();
|
||
expect(AppSettings.fromJson(saved).publicIpfsGateway, gateway);
|
||
}
|
||
});
|
||
|
||
test('empty upload gateway does not fall back to the download gateway', () {
|
||
final settings = AppSettings(
|
||
publicIpfsGateway: 'https://gateway.example.com/ipfs/',
|
||
uploadIpfsGateway: '',
|
||
);
|
||
|
||
expect(settings.effectiveUploadIpfsGateway, isEmpty);
|
||
});
|
||
|
||
test(
|
||
'fromJson with missing new fields uses defaults (backward compat)',
|
||
() {
|
||
// Simulate an old settings JSON with none of the new fields
|
||
final json = <String, dynamic>{};
|
||
final settings = AppSettings.fromJson(json);
|
||
expect(settings.maxRouteWeight, equals(5.0));
|
||
expect(settings.initialRouteWeight, equals(3.0));
|
||
expect(settings.routeWeightSuccessIncrement, equals(0.5));
|
||
expect(settings.routeWeightFailureDecrement, equals(0.2));
|
||
expect(settings.maxMessageRetries, equals(5));
|
||
},
|
||
);
|
||
|
||
test('copyWith works for maxRouteWeight', () {
|
||
final settings = AppSettings();
|
||
final updated = settings.copyWith(maxRouteWeight: 8.0);
|
||
expect(updated.maxRouteWeight, equals(8.0));
|
||
// Other fields should be unchanged
|
||
expect(updated.initialRouteWeight, equals(settings.initialRouteWeight));
|
||
expect(updated.maxMessageRetries, equals(settings.maxMessageRetries));
|
||
});
|
||
|
||
test('copyWith works for initialRouteWeight', () {
|
||
final settings = AppSettings();
|
||
final updated = settings.copyWith(initialRouteWeight: 3.0);
|
||
expect(updated.initialRouteWeight, equals(3.0));
|
||
expect(updated.maxRouteWeight, equals(settings.maxRouteWeight));
|
||
});
|
||
|
||
test('copyWith works for routeWeightSuccessIncrement', () {
|
||
final settings = AppSettings();
|
||
final updated = settings.copyWith(routeWeightSuccessIncrement: 0.25);
|
||
expect(updated.routeWeightSuccessIncrement, equals(0.25));
|
||
expect(
|
||
updated.routeWeightFailureDecrement,
|
||
equals(settings.routeWeightFailureDecrement),
|
||
);
|
||
});
|
||
|
||
test('copyWith works for routeWeightFailureDecrement', () {
|
||
final settings = AppSettings();
|
||
final updated = settings.copyWith(routeWeightFailureDecrement: 0.75);
|
||
expect(updated.routeWeightFailureDecrement, equals(0.75));
|
||
expect(
|
||
updated.routeWeightSuccessIncrement,
|
||
equals(settings.routeWeightSuccessIncrement),
|
||
);
|
||
});
|
||
|
||
test('copyWith works for maxMessageRetries', () {
|
||
final settings = AppSettings();
|
||
final updated = settings.copyWith(maxMessageRetries: 10);
|
||
expect(updated.maxMessageRetries, equals(10));
|
||
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;
|
||
const channelId = '0123456789abcdef';
|
||
|
||
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}_$channelId',
|
||
jsonEncode(messageJson),
|
||
);
|
||
|
||
final messages = await store.loadChannelMessages(channelId);
|
||
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()
|
||
..setPublicKeyHex = 'AABBCCDDEEFF00112233';
|
||
|
||
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])));
|
||
expect(prefs.getString('discovered_contacts'), isNull);
|
||
expect(prefs.getString(store.keyFor), isNotNull);
|
||
},
|
||
);
|
||
});
|
||
|
||
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));
|
||
});
|
||
});
|
||
}
|