Fix review findings, add default flood scope, and address recent issues

Review fixes:
- Keep stored message history beyond the 200-message window; stop
  messages leaking between radios; reset link/session state on disconnect
- Accept firmware's 176-byte RX frames over USB/TCP
- Hold APP_START during contact sync; contact-sync watchdog; replay DMs
  from not-yet-known senders; tombstone late command replies; finish
  channel sync on ERR_NOT_FOUND; fail region-scoped sends cleanly
- DM retry watchdog when RESP_CODE_SENT never arrives; late repeater CLI
  replies; ACK hash collisions; UTF-8-safe name truncation
- Image pipeline: delete-during-decode, lifecycle cancels, decoder memory
  release, entry cap, dead worker recovery, atomic model writes
- Storage: deterministic reaction hash across web/native, Smaz
  double-decode, per-entry parsing, per-radio path history and regions,
  review-mode data isolation
- UI: safe channel slot selection, byte-length name limits, double-send
  guard, mounted checks, localized hardcoded strings
- Skip persisted-store reload on repeat SELF_INFO

Features:
- Radio default flood scope (firmware ver 11+) and explicit unscoped
  sends (ver 12+)
- Scan for nearby nodes (#570)
- Optional distinct first-hop repeater counting for channel resend (#589)
- Optional region next to hop count (#580)

Issues:
- Message text follows system font size (#569)
- Repeater/room password field accepts 20 chars (#576)
- TCP auto-reconnect and keepalive (#571)
- Order feeds by local receive time; flag future timestamps (#584)
- Coding rate help text (#568), French "Favoris" (#575)
- iOS/macOS app badge now clears

Translations for all new strings in every locale.
This commit is contained in:
zjs81 committed 2026-10-11 01:25:31 -07:00
1 parent a64a33d0c0
commit 25f197c979
120 files changed
+10424 -687

No files matched your search

@@ -0,0 +1,223 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/helpers/smaz.dart';
import 'package:meshcore_open/models/channel_message.dart';
import 'package:meshcore_open/models/message.dart';
import 'package:meshcore_open/storage/channel_message_store.dart';
import 'package:meshcore_open/storage/message_store.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
const _device = 'abcdef012345';
const _contact = 'c1';
Future<void> _init([Map<String, Object> values = const {}]) async {
SharedPreferences.setMockInitialValues(values);
PrefsManager.reset();
await PrefsManager.initialize();
MessageStore().forgetArchive();
ChannelMessageStore().forgetArchive();
}
Message _dm(int i, {String? text}) => Message(
senderKey: Uint8List.fromList([1, 2, 3]),
text: text ?? 'm$i',
timestamp: DateTime.fromMillisecondsSinceEpoch(1000 * i),
isOutgoing: false,
status: MessageStatus.delivered,
messageId: 'id$i',
);
ChannelMessage _ch(int i) => ChannelMessage(
senderKey: null,
senderName: 'Bob',
text: 'c$i',
timestamp: DateTime.fromMillisecondsSinceEpoch(1000 * i),
isOutgoing: false,
status: ChannelMessageStatus.sent,
channelIndex: 0,
messageId: 'cid$i',
);
MessageStore _messageStore() => MessageStore()..setPublicKeyHex = _device;
void main() {
group('MessageStore window persistence', () {
test('saving the window keeps archived history ahead of it', () async {
await _init();
final store = _messageStore();
final all = [for (var i = 0; i < 10; i++) _dm(i)];
await store.saveMessages(_contact, all);
final loaded = await store.loadMessages(_contact);
final window = loaded.sublist(6);
store.setArchived(_contact, loaded.sublist(0, 6));
await store.saveMessages(_contact, [...window, _dm(10)]);
final reloaded = await store.loadMessages(_contact);
expect(reloaded.map((m) => m.text), [
for (var i = 0; i <= 10; i++) 'm$i',
]);
});
test('trimmed messages are archived, deletes stay deleted', () async {
await _init();
final store = _messageStore();
store.setArchived(_contact, const []);
final window = [for (var i = 0; i < 4; i++) _dm(i)];
await store.saveMessages(_contact, window);
// Trim the oldest two out of the window, delete one still in it.
store.archive(_contact, window.sublist(0, 2));
final remaining = [window[3], _dm(4)];
await store.saveMessages(_contact, remaining);
final reloaded = await store.loadMessages(_contact);
expect(reloaded.map((m) => m.text), ['m0', 'm1', 'm3', 'm4']);
expect(store.archivedCount(_contact), 2);
store.unarchiveNewest(_contact, 1);
expect(store.archivedCount(_contact), 1);
});
test('an unreconciled save merges with what is stored', () async {
await _init();
final store = _messageStore();
await store.saveMessages(_contact, [for (var i = 0; i < 5; i++) _dm(i)]);
// A fresh session saves a new message before the history was loaded.
store.forgetArchive();
await store.saveMessages(_contact, [_dm(5)]);
final reloaded = await store.loadMessages(_contact);
expect(reloaded.map((m) => m.text), [for (var i = 0; i <= 5; i++) 'm$i']);
});
test('clearMessages wipes archived history too', () async {
await _init();
final store = _messageStore();
store.setArchived(_contact, [_dm(0), _dm(1)]);
await store.saveMessages(_contact, [_dm(2)]);
await store.clearMessages(_contact);
await store.saveMessages(_contact, [_dm(3)]);
final reloaded = await store.loadMessages(_contact);
expect(reloaded.map((m) => m.text), ['m3']);
});
test('storage is capped by dropping the oldest archived entries', () async {
await _init();
final store = _messageStore();
const cap = MessageStore.maxStoredMessages;
store.setArchived(_contact, [for (var i = 0; i < cap; i++) _dm(i)]);
await store.saveMessages(_contact, [_dm(cap), _dm(cap + 1)]);
final reloaded = await store.loadMessages(_contact);
expect(reloaded.length, cap);
expect(reloaded.first.text, 'm2');
expect(reloaded.last.text, 'm${cap + 1}');
expect(store.archivedCount(_contact), cap - 2);
});
});
group('MessageStore decoding', () {
test('plain text that looks like Smaz survives a reload', () async {
await _init();
final store = _messageStore();
await store.saveMessages(_contact, [
_dm(0, text: 's:test'),
_dm(1, text: 's:abcd'),
]);
final reloaded = await store.loadMessages(_contact);
expect(reloaded.map((m) => m.text), ['s:test', 's:abcd']);
});
test('legacy records still decode real Smaz payloads', () async {
const plain = 'hello there how are you doing';
final encoded = Smaz.encodeIfSmaller(plain);
expect(encoded, startsWith('s:'));
await _init({
'messages_${_device.substring(0, 10)}$_contact': jsonEncode([
{
'senderKey': base64Encode([1, 2, 3]),
'text': encoded,
'timestamp': 1000,
},
{
'senderKey': base64Encode([1, 2, 3]),
'text': 's:test',
'timestamp': 2000,
},
]),
});
final reloaded = await _messageStore().loadMessages(_contact);
expect(reloaded.map((m) => m.text), [plain, 's:test']);
});
test('a corrupt reaction status does not drop the message', () async {
await _init({
'messages_${_device.substring(0, 10)}$_contact': jsonEncode([
{
'senderKey': base64Encode([1, 2, 3]),
'text': 'hi',
'timestamp': 1000,
'reactionStatuses': {'👍': 99, '❤': 'x', '😀': 1},
},
]),
});
final reloaded = await _messageStore().loadMessages(_contact);
expect(reloaded.single.text, 'hi');
expect(reloaded.single.reactionStatuses, {'😀': MessageStatus.values[1]});
});
});
group('ChannelMessageStore window persistence', () {
test('saving the window keeps archived history ahead of it', () async {
await _init();
final store = ChannelMessageStore()..setPublicKeyHex = _device;
final all = [for (var i = 0; i < 8; i++) _ch(i)];
await store.saveChannelMessages(0, all);
final loaded = await store.loadChannelMessages(0);
store.setArchived(0, loaded.sublist(0, 5));
await store.saveChannelMessages(0, [...loaded.sublist(5), _ch(8)]);
final reloaded = await store.loadChannelMessages(0);
expect(reloaded.map((m) => m.text), [for (var i = 0; i <= 8; i++) 'c$i']);
});
test('clearing through another instance resets the archive', () async {
await _init();
final store = ChannelMessageStore()..setPublicKeyHex = _device;
store.setArchived(0, [_ch(0), _ch(1)]);
await store.saveChannelMessages(0, [_ch(2)]);
await (ChannelMessageStore()..setPublicKeyHex = _device)
.clearChannelMessages(0);
await store.saveChannelMessages(0, [_ch(3)]);
final reloaded = await store.loadChannelMessages(0);
expect(reloaded.map((m) => m.text), ['c3']);
});
});
group('Smaz.tryDecodePrefixed', () {
test('rejects plain text that is not a canonical compressed payload', () {
expect(Smaz.tryDecodePrefixed('s:test'), isNull);
expect(Smaz.tryDecodePrefixed('s:done'), isNull);
expect(Smaz.tryDecodePrefixed('s:hello'), isNull);
});
test('decodes what encodeIfSmaller produced', () {
for (final plain in [
'hello there how are you doing',
'ok see you at the meeting',
]) {
expect(Smaz.tryDecodePrefixed(Smaz.encodeIfSmaller(plain)), plain);
}
});
});
}
+184
View File
@@ -0,0 +1,184 @@
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/helpers/cyr2lat.dart';
import 'package:meshcore_open/models/path_history.dart';
import 'package:meshcore_open/review_mode/review_mode_storage.dart';
import 'package:meshcore_open/review_mode/review_radio.dart';
import 'package:meshcore_open/services/storage_service.dart';
import 'package:meshcore_open/services/timeout_prediction_service.dart';
import 'package:meshcore_open/storage/channel_region_store.dart';
import 'package:meshcore_open/storage/channel_store.dart';
import 'package:meshcore_open/storage/contact_discovery_store.dart';
import 'package:meshcore_open/storage/legacy_key_migration.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:meshcore_open/storage/region_store.dart';
import 'package:meshcore_open/storage/unread_store.dart';
import 'package:shared_preferences/shared_preferences.dart';
const _radioA = 'aaaaaaaaaa';
const _radioB = 'bbbbbbbbbb';
Future<void> _init(Map<String, Object> values) async {
SharedPreferences.setMockInitialValues(values);
PrefsManager.reset();
await PrefsManager.initialize();
}
void main() {
tearDown(() {
scopesWithoutLegacyMigration.clear();
TimeoutPredictionService.recordingSuspended = false;
});
test(
'channel store skips a malformed channel instead of dropping all',
() async {
await _init({
'channels$_radioA': jsonEncode([
{'index': 0, 'name': 'Public'},
{'index': 'bad', 'name': 'Broken'},
{'index': 2, 'name': 'Second'},
]),
});
final store = ChannelStore()..setPublicKeyHex = _radioA;
final channels = await store.loadChannels();
expect(channels.map((c) => c.name), ['Public', 'Second']);
},
);
test('unread store keeps valid counts when one is malformed', () async {
await _init({
'contact_unread_count$_radioA': jsonEncode({'a': 3, 'b': 'oops', 'c': 1}),
});
final store = UnreadStore()..setPublicKeyHex = _radioA;
expect(await store.loadContactUnreadCount(), {'a': 3, 'c': 1});
});
test(
'discovery store does not migrate legacy data into review scope',
() async {
await _init({'discovered_contacts': '[]'});
final reviewScope = ReviewRadio.storagePrefix;
scopesWithoutLegacyMigration.add(reviewScope);
final store = ContactDiscoveryStore()
..setPublicKeyHex = '${reviewScope}00';
expect(await store.loadContacts(), isEmpty);
expect(PrefsManager.instance.getString('discovered_contacts'), '[]');
expect(PrefsManager.instance.getKeys(), isNot(contains(store.keyFor)));
},
);
group('path history scoping', () {
final history = jsonEncode(
ContactPathHistory(contactPubKeyHex: 'c1', recentPaths: []).toJson(),
);
test('clearAllPathHistories only clears the connected radio', () async {
await _init({
'path_history_${_radioA}_c1': history,
'path_history_${_radioB}_c1': history,
'path_history_c2': history,
});
final storage = StorageService()..setPublicKeyHex = '${_radioA}xx';
await storage.clearAllPathHistories();
final keys = PrefsManager.instance.getKeys();
expect(keys, contains('path_history_${_radioB}_c1'));
expect(keys, isNot(contains('path_history_${_radioA}_c1')));
expect(keys, isNot(contains('path_history_c2')));
});
test('legacy history migrates once into the first radio', () async {
await _init({'path_history_c1': history});
final a = StorageService()..setPublicKeyHex = '${_radioA}xx';
expect(await a.loadPathHistory('c1'), isNotNull);
final keys = PrefsManager.instance.getKeys();
expect(keys, contains('path_history_${_radioA}_c1'));
expect(keys, isNot(contains('path_history_c1')));
final b = StorageService()..setPublicKeyHex = '${_radioB}xx';
expect(await b.loadPathHistory('c1'), isNull);
});
test('review scope does not adopt legacy history', () async {
await _init({'path_history_c1': history});
scopesWithoutLegacyMigration.add(_radioA);
final a = StorageService()..setPublicKeyHex = '${_radioA}xx';
expect(await a.loadPathHistory('c1'), isNull);
expect(PrefsManager.instance.getKeys(), contains('path_history_c1'));
});
});
test('removing a region clears assignments on every radio', () async {
await _init({
'regions': ['EU', 'US'],
'channel_region_${_radioA}1': 'EU',
'channel_region_${_radioB}3': 'EU',
'channel_region_${_radioB}4': 'US',
'default_region_$_radioB': 'EU',
});
final store = RegionStore()..setPublicKeyHex = '${_radioA}xx';
await store.removeRegion('EU');
final prefs = PrefsManager.instance;
expect(prefs.getStringList('regions'), ['US']);
expect(prefs.getString('channel_region_${_radioA}1'), '');
expect(prefs.getString('channel_region_${_radioB}3'), '');
expect(prefs.getString('channel_region_${_radioB}4'), 'US');
expect(prefs.getString('default_region_$_radioB'), '');
final regionB = ChannelRegionStore()..setPublicKeyHex = '${_radioB}xx';
expect(regionB.loadDefaultRegion(), '');
});
group('review mode session', () {
test('restores repeater keys and suspends delivery recording', () async {
await _init({
'repeater_passwords': jsonEncode({'real': 'secret'}),
});
await beginReviewModeSession(null);
expect(TimeoutPredictionService.recordingSuspended, isTrue);
final storage = StorageService();
await storage.saveRepeaterPassword('fake', 'pw');
await storage.setRepeaterAutoClockSyncAfterLoginEnabled('fake', true);
final predictor = TimeoutPredictionService.noStorage();
predictor.recordObservation(
contactKey: 'fake',
pathLength: 1,
messageBytes: 10,
tripTimeMs: 1500,
);
expect(predictor.observationCount, 0);
await endReviewModeSession(null);
expect(TimeoutPredictionService.recordingSuspended, isFalse);
expect(await storage.loadRepeaterPasswords(), {'real': 'secret'});
expect(
PrefsManager.instance.containsKey(
'repeater_auto_clock_sync_after_login',
),
isFalse,
);
});
test('interrupted session snapshot survives a second begin', () async {
await _init({
'repeater_passwords': jsonEncode({'real': 'secret'}),
});
await beginReviewModeSession(null);
await StorageService().saveRepeaterPassword('fake', 'pw');
// App killed, review started again before startup cleanup ran.
await beginReviewModeSession(null);
await endReviewModeSession(null);
expect(await StorageService().loadRepeaterPasswords(), {
'real': 'secret',
});
});
});
group('Cyr2Lat sender prefix', () {
test('only the leading mention is kept untransliterated', () {
// Uses the default char map (р→p, о→o, с→c).
expect(Cyr2Lat.encode('@[Bob] рос [x] рос'), '@[Bob] poc [x] poc');
expect(Cyr2Lat.extractSenderName('@[Bob] рос [x] рос'), '@[Bob] ');
});
});
}