Merge dev; fix share-location and discovered-contact import/export; update docs

Merge dev (#588 share location, #436 discovered-contact import/export)
into fix/review-fixes-and-issue-batch. Conflicts: keep the node-scan
button alongside dev's import/export menu; take dev's marker label
prompt.

Share location (#588):
- Never share a missing or 0,0 radio position; hide the chat action and
  show "Your radio has no location set"
- Sanitise marker labels ('|' -> '/', newlines -> spaces) on every path
- One shared MarkerLabelDialog owning its controller/focus node, with
  keyboard submit, replacing three copies
- Map pins default to "Point of interest" again; "Share my location"
  uses the node name
- Location shares honour the send cooldown and handle send errors

Discovered-contact import/export (#436):
- Per-entry decoding with imported/skipped/invalid counts; reject keys
  that are not 32 bytes; skip in-file duplicates and the radio's own key
- Strip routing paths on import and stop exporting them; clamp future
  timestamps; enforce the discovered-contact cap; require a connection;
  2 MB file limit
- Export via in-memory XFile (fixes web); pass sharePositionOrigin for
  iPad (also GPX export and image save); macOS user-selected read-write
  entitlement for save dialogs

Docs: cover share location, import/export, node scan, TCP reconnect,
channel auto-resend options, arrival ordering, badge, password length
and image limits.

Translations for all new strings in every locale.
This commit is contained in:
zjs81 committed 2026-10-11 01:54:07 -07:00
commit 5fb67abdde
66 files changed
+3118 -168

No files matched your search

@@ -0,0 +1,235 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/models/contact.dart';
import 'package:meshcore_open/review_mode/review_radio.dart';
import 'package:meshcore_open/services/message_retry_service.dart';
import 'package:meshcore_open/services/path_history_service.dart';
import 'package:meshcore_open/services/storage_service.dart';
import 'package:meshcore_open/storage/contact_discovery_store.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
List<int> _key(int seed) => List<int>.generate(32, (i) => (seed + i) & 0xFF);
Map<String, dynamic> _entry(
int seed, {
List<int>? key,
int lastSeen = 1000,
Map<String, dynamic> extra = const {},
}) => {
'publicKey': base64Encode(key ?? _key(seed)),
'name': 'Node $seed',
'type': advTypeRepeater,
'lastSeen': lastSeen,
...extra,
};
Contact _existing(int seed) => Contact(
publicKey: Uint8List.fromList(_key(seed)),
name: 'Existing $seed',
type: advTypeRepeater,
pathLength: -1,
path: Uint8List(0),
lastSeen: DateTime.fromMillisecondsSinceEpoch(5000),
);
Future<void> _waitFor(bool Function() condition, {int seconds = 8}) async {
final end = DateTime.now().add(Duration(seconds: seconds));
while (!condition()) {
if (DateTime.now().isAfter(end)) fail('Timed out waiting for condition');
await Future<void>.delayed(const Duration(milliseconds: 25));
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('ContactDiscoveryStore.importContactsJson', () {
final store = ContactDiscoveryStore()..setPublicKeyHex = 'aabbccddeeff';
final now = DateTime.fromMillisecondsSinceEpoch(1000000);
DiscoveredContactsImportResult run(
Object fileContent, {
List<Contact>? existing,
Set<String> known = const {},
String? self,
}) => store.importContactsJson(
json: fileContent is String ? fileContent : jsonEncode(fileContent),
existingContacts: existing ?? [],
knownContactKeys: known,
selfPublicKeyHex: self,
now: now,
);
test('an invalid entry is skipped, not the whole file', () {
final existing = <Contact>[];
final result = run([
_entry(1),
'not an object',
{'name': 'no key'},
{'publicKey': '%%%not base64', 'name': 'bad'},
_entry(2, extra: {'lastSeen': 'yesterday'}),
_entry(3),
], existing: existing);
expect(result.total, 6);
expect(result.imported, 2);
expect(result.invalid, 4);
expect(existing.map((c) => c.name), ['Node 1', 'Node 3']);
});
test('a public key that is not 32 bytes is rejected', () {
final existing = <Contact>[];
final result = run([
_entry(1, key: List<int>.filled(5, 7)),
_entry(2, key: List<int>.filled(40, 7)),
_entry(3),
], existing: existing);
expect(result.imported, 1);
expect(result.invalid, 2);
expect(existing.single.publicKey.length, pubKeySize);
});
test('imported routing paths are stripped', () {
final existing = <Contact>[];
run([
_entry(
1,
extra: {
'pathLength': 2,
'path': base64Encode([0xA1, 0xB2]),
'pathHashWidth': 1,
'pathOverride': 1,
'pathOverrideBytes': base64Encode([0xC3]),
},
),
], existing: existing);
final contact = existing.single;
expect(contact.pathLength, -1);
expect(contact.path, isEmpty);
expect(contact.pathOverride, isNull);
expect(contact.pathOverrideBytes, isNull);
});
test('duplicates in the file, known contacts and self are skipped', () {
final existing = [_existing(9)];
final selfHex = pubKeyToHex(Uint8List.fromList(_key(4)));
final knownHex = pubKeyToHex(Uint8List.fromList(_key(5)));
final result = run(
[_entry(1), _entry(1), _entry(4), _entry(5), _entry(9), _entry(2)],
existing: existing,
known: {knownHex},
self: selfHex,
);
expect(result.imported, 2);
expect(result.skipped, 4);
expect(result.invalid, 0);
expect(existing.length, 3);
// An existing discovered contact is fresher and is kept as is.
expect(existing.first.name, 'Existing 9');
});
test('future timestamps are clamped to now', () {
final existing = <Contact>[];
run([
_entry(
1,
lastSeen: now.millisecondsSinceEpoch + 999999,
extra: {'lastMessageAt': now.millisecondsSinceEpoch + 5},
),
], existing: existing);
expect(existing.single.lastSeen, now);
expect(existing.single.lastMessageAt, now);
});
test('a file that is not a JSON list throws FormatException', () {
expect(() => run('{"publicKey": "x"}'), throwsFormatException);
expect(() => run('not json at all'), throwsFormatException);
expect(() => run(''), throwsFormatException);
});
test('an empty list reports zero entries', () {
final result = run(<Object>[]);
expect(result.total, 0);
expect(result.imported, 0);
});
test('exports leave out routing paths but stay importable', () {
final source = Contact(
publicKey: Uint8List.fromList(_key(1)),
name: 'Pathy',
type: advTypeRepeater,
pathLength: 2,
path: Uint8List.fromList([0xA1, 0xB2]),
lastSeen: DateTime.fromMillisecondsSinceEpoch(1000),
);
final exported = store.exportContactsJson([source]);
final decoded = (jsonDecode(exported) as List).single as Map;
expect(decoded.containsKey('path'), isFalse);
expect(decoded.containsKey('pathLength'), isFalse);
final existing = <Contact>[];
final result = run(exported, existing: existing);
expect(result.imported, 1);
expect(existing.single.name, 'Pathy');
});
});
group('MeshCoreConnector.importDiscoveredContactsJson', () {
late MeshCoreConnector connector;
setUpAll(() async {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
connector = MeshCoreConnector();
final retryService = MessageRetryService();
connector.initialize(
retryService: retryService,
pathHistoryService: PathHistoryService(StorageService()),
);
addTearDown(() {
connector.dispose();
retryService.dispose();
});
});
test('does nothing while disconnected', () async {
final result = await connector.importDiscoveredContactsJson(
jsonEncode([_entry(1)]),
);
expect(result.imported, 0);
expect(connector.discoveredContacts, isEmpty);
});
test('an import is trimmed to the discovered-contacts cap', () async {
connector.reviewRadioFactoryForTest = () => ReviewRadio(
scheduledMessageInterval: const Duration(hours: 1),
replyDelay: const Duration(hours: 1),
);
await connector.connectReview();
addTearDown(connector.disconnect);
await _waitFor(() => connector.channels.isNotEmpty);
const count = MeshCoreConnector.maxDiscoveredContacts + 50;
final entries = [
for (var i = 0; i < count; i++)
_entry(
0,
key: [i & 0xFF, (i >> 8) & 0xFF, ...List<int>.filled(30, 0x5A)],
lastSeen: 1000 + i,
),
];
final result = await connector.importDiscoveredContactsJson(
jsonEncode(entries),
);
expect(result.imported, count);
expect(
connector.discoveredContacts.length,
lessThanOrEqualTo(MeshCoreConnector.maxDiscoveredContacts),
);
});
});
}
+106
View File
@@ -0,0 +1,106 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/helpers/utf8_length_limiter.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/widgets/marker_label_dialog.dart';
/// Pumps a button that opens the dialog and records what it returns.
Future<List<String?>> _open(
WidgetTester tester, {
String defaultLabel = 'Camp',
}) async {
final results = <String?>[];
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) => Scaffold(
body: TextButton(
onPressed: () async {
results.add(
await showMarkerLabelDialog(
context,
lat: 47.5,
lon: -122.25,
defaultLabel: defaultLabel,
flags: 'poi',
confirmText: 'Share',
),
);
},
child: const Text('open'),
),
),
),
),
);
await tester.tap(find.text('open'));
await tester.pumpAndSettle();
return results;
}
void main() {
group('marker label helpers', () {
test('sanitizeMarkerLabel replaces the field separator and newlines', () {
expect(sanitizeMarkerLabel(' Camp | north\nside '), 'Camp / north side');
});
test('formatMarkerText cannot be split by a label', () {
expect(
formatMarkerText(1.5, -2.25, 'a|b', 'poi'),
'm:1.500000,-2.250000|a/b|poi',
);
});
test('label budget keeps the whole marker within both limits', () {
final budget = markerLabelByteBudget(-90, -180, 'poi');
final marker = formatMarkerText(-90, -180, 'x' * budget, 'poi');
expect(budget, greaterThan(0));
expect(utf8ByteLength(marker), lessThanOrEqualTo(127));
});
test('hasShareableLocation rejects missing and 0,0 positions', () {
expect(hasShareableLocation(null, 10), isFalse);
expect(hasShareableLocation(0, 0), isFalse);
expect(hasShareableLocation(91, 0), isFalse);
expect(hasShareableLocation(47.5, -122.25), isTrue);
});
});
group('MarkerLabelDialog', () {
testWidgets('confirm returns the sanitised label', (tester) async {
final results = await _open(tester);
await tester.enterText(find.byType(TextField), 'Camp | north');
await tester.tap(find.text('Share'));
await tester.pumpAndSettle();
expect(results, ['Camp / north']);
});
testWidgets('keyboard Send submits, and empty falls back to default', (
tester,
) async {
final results = await _open(tester, defaultLabel: 'Home');
await tester.enterText(find.byType(TextField), ' ');
await tester.testTextInput.receiveAction(TextInputAction.send);
await tester.pumpAndSettle();
expect(results, ['Home']);
});
testWidgets('cancel returns null', (tester) async {
final results = await _open(tester);
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();
expect(results, [null]);
});
testWidgets('input is limited to the byte budget', (tester) async {
final budget = markerLabelByteBudget(47.5, -122.25, 'poi');
final results = await _open(tester);
await tester.enterText(find.byType(TextField), 'y' * (budget + 50));
await tester.tap(find.text('Share'));
await tester.pumpAndSettle();
expect(utf8ByteLength(results.single!), lessThanOrEqualTo(budget));
});
});
}