fix(#1028): keep a contact only when the radio confirms it
The owner's log showed CMD_ADD_UPDATE_CONTACT drawing ERR 3 (table full) while the app logged "Added new contact" and kept it. Every DM to that contact then drew ERR 2 (not found) and went red only when the 8 s timer ran out, with no reason. - Both add paths (by key, and from Discovery, the map or a channel sender) wait for the radio's OK or ERR. The contact is kept only on OK. A refusal or no answer keeps nothing and tells the user which, with "full" said plainly. Owner decision 2026-11-23. - The Discovery add now waits for its own reply, so its ERR can no longer be taken as the answer to the next command. - A DM the radio refuses fails at once and keeps the radio's error code, which the failed bubble shows. Matched only when exactly one DM is waiting and no ERR-drawing stream is running. - The radio removing a contact to make room (push 0x8F) removes it from the app and names it; its messages are kept. The radio reporting itself full (push 0x90), a refusal for a full table, or a complete sync at capacity raises a notice on the contacts screen. - New strings are English-only; untranslated.json updated. Tests: contact_add_confirm_test.dart uses only dev's API, and 5 of its 9 tests fail on dev (refused add kept twice, the Discovery ERR taking the next command's reply, the refused DM pending, the removed contact kept). contact_add_result_test.dart (17) and 3 new dialog tests cover the results, the notice, the stored reason and the negative cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>pull/1188/head
parent
d770bdec49
commit
243070f586
@ -0,0 +1,42 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../connector/meshcore_connector.dart';
|
||||
import '../helpers/snack_bar_builder.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
import '../models/contact.dart';
|
||||
|
||||
/// Tells the user how a contact add ended. A refusal or no reply stays on
|
||||
/// screen until dismissed: the contact was not added, and they have to know
|
||||
/// why. (#1028)
|
||||
void showContactAddResult(
|
||||
BuildContext context,
|
||||
ContactAddResult result,
|
||||
String name,
|
||||
) {
|
||||
final l10n = context.l10n;
|
||||
final String text;
|
||||
switch (result.outcome) {
|
||||
case ContactAddOutcome.added:
|
||||
text = l10n.contacts_addByKeyAdded(name);
|
||||
case ContactAddOutcome.refused:
|
||||
text = result.radioFull
|
||||
? l10n.contacts_addRadioFull(name)
|
||||
: l10n.contacts_addRadioRefused(name, result.errCode ?? -1);
|
||||
case ContactAddOutcome.noReply:
|
||||
text = l10n.contacts_addNoReply(name);
|
||||
case ContactAddOutcome.notConnected:
|
||||
text = l10n.contacts_addByKeyFailed;
|
||||
}
|
||||
showDismissibleSnackBar(context, content: Text(text), persist: !result.added);
|
||||
}
|
||||
|
||||
/// Adds a contact heard on the mesh and reports the outcome.
|
||||
Future<void> importDiscoveredContactWithFeedback(
|
||||
BuildContext context,
|
||||
MeshCoreConnector connector,
|
||||
Contact contact,
|
||||
) async {
|
||||
final result = await connector.importDiscoveredContact(contact);
|
||||
if (!context.mounted) return;
|
||||
showContactAddResult(context, result, contact.name);
|
||||
}
|
||||
@ -0,0 +1,207 @@
|
||||
// #1028: the app kept contacts the radio had refused, and a DM the radio
|
||||
// refused sat pending for 8 s with no reason.
|
||||
//
|
||||
// From the owner's log: CMD_ADD_UPDATE_CONTACT drew ERR 3 (table full), the
|
||||
// app logged "Added new contact" anyway, and every DM to that contact then
|
||||
// drew ERR 2 (not found) and was marked failed only when the 8 s timer ran
|
||||
// out. These tests use only what dev already has, so each one fails there.
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:drift/native.dart';
|
||||
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/models/message.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/drift/blob_store.dart';
|
||||
import 'package:meshcore_open/storage/drift/offband_database.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
Uint8List _key(int b) => Uint8List.fromList(List<int>.filled(32, b));
|
||||
Uint8List _psk(int fill) => Uint8List.fromList(List<int>.filled(16, fill));
|
||||
|
||||
Contact _contact(int keyByte, String name) => Contact(
|
||||
publicKey: _key(keyByte),
|
||||
receivedName: name,
|
||||
type: advTypeChat,
|
||||
pathLength: -1,
|
||||
path: Uint8List(0),
|
||||
lastSeen: DateTime.fromMillisecondsSinceEpoch(0),
|
||||
);
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
late OffbandDatabase db;
|
||||
late MeshCoreConnector connector;
|
||||
late List<Uint8List> sent;
|
||||
|
||||
void ok() => connector.handleFrameForTest([respCodeOk]);
|
||||
void err(int code) => connector.handleFrameForTest([respCodeErr, code]);
|
||||
bool holds(int keyByte) => connector.contactsForTest.any(
|
||||
(c) => c.publicKeyHex == _contact(keyByte, '').publicKeyHex,
|
||||
);
|
||||
|
||||
Future<void> settle([int turns = 25]) async {
|
||||
for (var i = 0; i < turns; i++) {
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
}
|
||||
}
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
db = OffbandDatabase(NativeDatabase.memory());
|
||||
BlobStore.overrideForTest(BlobStore(db));
|
||||
connector = MeshCoreConnector();
|
||||
connector.contactsForTest.clear();
|
||||
connector.setConnectedForTest();
|
||||
sent = [];
|
||||
connector.sendFrameOverrideForTest = sent.add;
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await db.close();
|
||||
BlobStore.clearTestOverride();
|
||||
});
|
||||
|
||||
group('add by key', () {
|
||||
test('the radio refusing the add (table full) keeps no contact', () async {
|
||||
final add = connector.addContactByKey(_contact(0x11, 'Alpha'));
|
||||
await settle(2);
|
||||
err(3);
|
||||
await add;
|
||||
|
||||
expect(holds(0x11), isFalse);
|
||||
});
|
||||
|
||||
test('the radio confirming the add keeps the contact', () async {
|
||||
final add = connector.addContactByKey(_contact(0x11, 'Alpha'));
|
||||
await settle(2);
|
||||
ok();
|
||||
await add;
|
||||
|
||||
expect(holds(0x11), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('add from Discovery', () {
|
||||
test('the radio refusing the add (table full) keeps no contact', () async {
|
||||
final add = connector.importDiscoveredContact(_contact(0x22, 'Bravo'));
|
||||
await settle(2);
|
||||
err(3);
|
||||
await add;
|
||||
|
||||
expect(holds(0x22), isFalse);
|
||||
});
|
||||
|
||||
test('the radio confirming the add keeps the contact', () async {
|
||||
final add = connector.importDiscoveredContact(_contact(0x22, 'Bravo'));
|
||||
await settle(2);
|
||||
ok();
|
||||
await add;
|
||||
|
||||
expect(holds(0x22), isTrue);
|
||||
});
|
||||
|
||||
// The Discovery add never waited for its own reply, so its ERR was taken
|
||||
// as the answer to whatever command was waiting next.
|
||||
test('its refusal is not taken as the next command\'s answer', () async {
|
||||
final add = connector.importDiscoveredContact(_contact(0x22, 'Bravo'));
|
||||
await settle(2);
|
||||
final write = connector.setChannel(
|
||||
3,
|
||||
'Weather',
|
||||
_psk(0x22),
|
||||
refresh: false,
|
||||
);
|
||||
await settle(2);
|
||||
err(3); // the radio refuses the add
|
||||
ok(); // and accepts the channel write
|
||||
|
||||
await add;
|
||||
expect(await write, ChannelWrite.ok);
|
||||
});
|
||||
});
|
||||
|
||||
group('a DM the radio refuses', () {
|
||||
late Contact bravo;
|
||||
|
||||
setUp(() {
|
||||
connector.initialize(
|
||||
retryService: MessageRetryService(),
|
||||
pathHistoryService: PathHistoryService(StorageService()),
|
||||
);
|
||||
connector.sendFrameOverrideForTest = sent.add;
|
||||
bravo = _contact(0x33, 'Charlie');
|
||||
});
|
||||
|
||||
Future<void> untilDmSent() async {
|
||||
for (var i = 0; i < 500; i++) {
|
||||
if (sent.any((f) => f.isNotEmpty && f[0] == cmdSendTxtMsg)) return;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 2));
|
||||
}
|
||||
fail('the DM was never sent');
|
||||
}
|
||||
|
||||
MessageStatus? statusOfLast() =>
|
||||
connector.conversationsForTest[bravo.publicKeyHex]?.last.status;
|
||||
|
||||
test('fails at once when the radio answers not found', () async {
|
||||
await connector.sendMessage(bravo, 'hello');
|
||||
await untilDmSent();
|
||||
err(errCodeNotFound);
|
||||
await settle();
|
||||
|
||||
expect(statusOfLast(), MessageStatus.failed);
|
||||
});
|
||||
|
||||
test('is not failed by the radio confirming the send', () async {
|
||||
await connector.sendMessage(bravo, 'hello');
|
||||
await untilDmSent();
|
||||
connector.handleFrameForTest([
|
||||
respCodeSent,
|
||||
1,
|
||||
0x01, 0x02, 0x03, 0x04, // expected ack hash
|
||||
0x10, 0x27, 0x00, 0x00, // timeout 10000 ms
|
||||
]);
|
||||
await settle();
|
||||
|
||||
expect(statusOfLast(), isNot(MessageStatus.failed));
|
||||
});
|
||||
});
|
||||
|
||||
group('the radio removing a contact to make room', () {
|
||||
test('removes it from the app\'s contacts too', () async {
|
||||
final add = connector.addContactByKey(_contact(0x44, 'Delta'));
|
||||
await settle(2);
|
||||
ok();
|
||||
await add;
|
||||
expect(holds(0x44), isTrue);
|
||||
|
||||
connector.handleFrameForTest([0x8F, ..._key(0x44)]);
|
||||
await settle();
|
||||
|
||||
expect(holds(0x44), isFalse);
|
||||
});
|
||||
|
||||
test('leaves every other contact alone', () async {
|
||||
for (final b in [0x44, 0x55]) {
|
||||
final add = connector.addContactByKey(_contact(b, 'N$b'));
|
||||
await settle(2);
|
||||
ok();
|
||||
await add;
|
||||
}
|
||||
|
||||
connector.handleFrameForTest([0x8F, ..._key(0x44)]);
|
||||
await settle();
|
||||
|
||||
expect(holds(0x55), isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -0,0 +1,268 @@
|
||||
// #1028: what the user is told when the radio refuses, ignores, or makes room
|
||||
// for a contact, and why a refused DM failed. contact_add_confirm_test.dart
|
||||
// covers the behavior with dev's API; this file covers what #1028 adds.
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:drift/native.dart';
|
||||
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/models/message.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/drift/blob_store.dart';
|
||||
import 'package:meshcore_open/storage/drift/offband_database.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
Uint8List _key(int b) => Uint8List.fromList(List<int>.filled(32, b));
|
||||
|
||||
Contact _contact(int keyByte, String name) => Contact(
|
||||
publicKey: _key(keyByte),
|
||||
receivedName: name,
|
||||
type: advTypeChat,
|
||||
pathLength: -1,
|
||||
path: Uint8List(0),
|
||||
lastSeen: DateTime.fromMillisecondsSinceEpoch(0),
|
||||
);
|
||||
|
||||
Uint8List _u32(int v) =>
|
||||
Uint8List(4)..buffer.asByteData().setUint32(0, v, Endian.little);
|
||||
|
||||
Uint8List _contactFrame(int keyByte, String name) {
|
||||
final b = BytesBuilder()
|
||||
..addByte(respCodeContact)
|
||||
..add(_key(keyByte))
|
||||
..addByte(advTypeChat)
|
||||
..addByte(0)
|
||||
..addByte(0xFF)
|
||||
..add(List<int>.filled(64, 0))
|
||||
..add(Uint8List(32)..setRange(0, name.length, utf8.encode(name)))
|
||||
..add(_u32(1790000000))
|
||||
..add(_u32(0))
|
||||
..add(_u32(0))
|
||||
..add(_u32(1790000000));
|
||||
return b.toBytes();
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
late OffbandDatabase db;
|
||||
late MeshCoreConnector connector;
|
||||
late List<Uint8List> sent;
|
||||
|
||||
void ok() => connector.handleFrameForTest([respCodeOk]);
|
||||
void err(int code) => connector.handleFrameForTest([respCodeErr, code]);
|
||||
|
||||
Future<void> settle([int turns = 25]) async {
|
||||
for (var i = 0; i < turns; i++) {
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
}
|
||||
}
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
db = OffbandDatabase(NativeDatabase.memory());
|
||||
BlobStore.overrideForTest(BlobStore(db));
|
||||
connector = MeshCoreConnector();
|
||||
connector.contactsForTest.clear();
|
||||
connector.setConnectedForTest();
|
||||
sent = [];
|
||||
connector.sendFrameOverrideForTest = sent.add;
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await db.close();
|
||||
BlobStore.clearTestOverride();
|
||||
});
|
||||
|
||||
group('the add result says what the radio did', () {
|
||||
test('OK is added', () async {
|
||||
final add = connector.addContactByKey(_contact(0x11, 'Alpha'));
|
||||
await settle(2);
|
||||
ok();
|
||||
final result = await add;
|
||||
expect(result.outcome, ContactAddOutcome.added);
|
||||
expect(connector.contactTableNotice, isNull);
|
||||
});
|
||||
|
||||
test('table full is refused, says full, and raises the notice', () async {
|
||||
final add = connector.addContactByKey(_contact(0x11, 'Alpha'));
|
||||
await settle(2);
|
||||
err(errCodeTableFull);
|
||||
final result = await add;
|
||||
expect(result.outcome, ContactAddOutcome.refused);
|
||||
expect(result.radioFull, isTrue);
|
||||
expect(connector.contactTableNotice, isNotNull);
|
||||
expect(connector.contactTableNotice!.removedName, isNull);
|
||||
});
|
||||
|
||||
test('another error is refused with its code, and is not "full"', () async {
|
||||
final add = connector.importDiscoveredContact(_contact(0x11, 'Alpha'));
|
||||
await settle(2);
|
||||
err(errCodeIllegalArg);
|
||||
final result = await add;
|
||||
expect(result.outcome, ContactAddOutcome.refused);
|
||||
expect(result.errCode, errCodeIllegalArg);
|
||||
expect(result.radioFull, isFalse);
|
||||
expect(connector.contactTableNotice, isNull);
|
||||
});
|
||||
|
||||
test('no answer is noReply and keeps no contact', () async {
|
||||
connector.contactAddReplyTimeout = const Duration(milliseconds: 20);
|
||||
final result = await connector.addContactByKey(_contact(0x11, 'Alpha'));
|
||||
expect(result.outcome, ContactAddOutcome.noReply);
|
||||
expect(connector.contactsForTest, isEmpty);
|
||||
});
|
||||
|
||||
test('a timed-out add cannot take the next command\'s reply', () async {
|
||||
connector.contactAddReplyTimeout = const Duration(milliseconds: 20);
|
||||
await connector.addContactByKey(_contact(0x11, 'Alpha'));
|
||||
|
||||
connector.contactAddReplyTimeout = const Duration(seconds: 5);
|
||||
final second = connector.addContactByKey(_contact(0x22, 'Bravo'));
|
||||
await settle(2);
|
||||
ok();
|
||||
expect((await second).outcome, ContactAddOutcome.added);
|
||||
});
|
||||
|
||||
test('disconnected sends nothing', () async {
|
||||
final fresh = MeshCoreConnector();
|
||||
fresh.sendFrameOverrideForTest = sent.add;
|
||||
final result = await fresh.addContactByKey(_contact(0x11, 'Alpha'));
|
||||
expect(result.outcome, ContactAddOutcome.notConnected);
|
||||
expect(sent, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('the contact table notice', () {
|
||||
test('names the contact the radio removed to make room', () async {
|
||||
final add = connector.addContactByKey(_contact(0x44, 'Delta'));
|
||||
await settle(2);
|
||||
ok();
|
||||
await add;
|
||||
|
||||
connector.handleFrameForTest([pushCodeContactDeleted, ..._key(0x44)]);
|
||||
expect(connector.contactTableNotice?.removedName, 'Delta');
|
||||
});
|
||||
|
||||
test('a removal of a contact the app did not hold raises nothing', () {
|
||||
connector.handleFrameForTest([pushCodeContactDeleted, ..._key(0x99)]);
|
||||
expect(connector.contactTableNotice, isNull);
|
||||
});
|
||||
|
||||
test('the radio reporting itself full raises it', () {
|
||||
connector.handleFrameForTest([pushCodeContactsFull]);
|
||||
expect(connector.contactTableNotice, isNotNull);
|
||||
});
|
||||
|
||||
test('a complete sync at capacity raises it', () async {
|
||||
connector.maxContactsForTest = 2;
|
||||
await connector.getContacts();
|
||||
connector.handleFrameForTest([respCodeContactsStart, ..._u32(2)]);
|
||||
connector.handleFrameForTest(_contactFrame(0x11, 'Alpha'));
|
||||
connector.handleFrameForTest(_contactFrame(0x22, 'Bravo'));
|
||||
connector.handleFrameForTest([respCodeEndOfContacts, ..._u32(0)]);
|
||||
await settle();
|
||||
expect(connector.contactTableNotice, isNotNull);
|
||||
});
|
||||
|
||||
test('a complete sync below capacity raises nothing', () async {
|
||||
connector.maxContactsForTest = 3;
|
||||
await connector.getContacts();
|
||||
connector.handleFrameForTest([respCodeContactsStart, ..._u32(2)]);
|
||||
connector.handleFrameForTest(_contactFrame(0x11, 'Alpha'));
|
||||
connector.handleFrameForTest(_contactFrame(0x22, 'Bravo'));
|
||||
connector.handleFrameForTest([respCodeEndOfContacts, ..._u32(0)]);
|
||||
await settle();
|
||||
expect(connector.contactTableNotice, isNull);
|
||||
});
|
||||
|
||||
test('dismissing clears it', () {
|
||||
connector.handleFrameForTest([pushCodeContactsFull]);
|
||||
connector.dismissContactTableNotice();
|
||||
expect(connector.contactTableNotice, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('a refused DM', () {
|
||||
late Contact charlie;
|
||||
|
||||
setUp(() {
|
||||
connector.initialize(
|
||||
retryService: MessageRetryService(),
|
||||
pathHistoryService: PathHistoryService(StorageService()),
|
||||
);
|
||||
connector.sendFrameOverrideForTest = sent.add;
|
||||
charlie = _contact(0x33, 'Charlie');
|
||||
});
|
||||
|
||||
Future<void> untilDmsSent(int count) async {
|
||||
for (var i = 0; i < 500; i++) {
|
||||
if (sent.where((f) => f.isNotEmpty && f[0] == cmdSendTxtMsg).length >=
|
||||
count) {
|
||||
return;
|
||||
}
|
||||
await Future<void>.delayed(const Duration(milliseconds: 2));
|
||||
}
|
||||
fail('the DM was never sent');
|
||||
}
|
||||
|
||||
Message? last(Contact c) =>
|
||||
connector.conversationsForTest[c.publicKeyHex]?.last;
|
||||
|
||||
test('keeps the radio\'s reason', () async {
|
||||
await connector.sendMessage(charlie, 'hello');
|
||||
await untilDmsSent(1);
|
||||
err(errCodeNotFound);
|
||||
await settle();
|
||||
expect(last(charlie)?.radioErrorCode, errCodeNotFound);
|
||||
});
|
||||
|
||||
test('an ERR during a contact sync is not taken as the DM\'s', () async {
|
||||
await connector.sendMessage(charlie, 'hello');
|
||||
await untilDmsSent(1);
|
||||
await connector.getContacts();
|
||||
err(errCodeNotFound);
|
||||
await settle();
|
||||
expect(last(charlie)?.status, MessageStatus.pending);
|
||||
});
|
||||
|
||||
test('with two DMs waiting, an ERR fails neither', () async {
|
||||
final delta = _contact(0x44, 'Delta');
|
||||
await connector.sendMessage(charlie, 'one');
|
||||
await connector.sendMessage(delta, 'two');
|
||||
await untilDmsSent(2);
|
||||
err(errCodeNotFound);
|
||||
await settle();
|
||||
expect(last(charlie)?.status, MessageStatus.pending);
|
||||
expect(last(delta)?.status, MessageStatus.pending);
|
||||
});
|
||||
});
|
||||
|
||||
group('the stored reason', () {
|
||||
final failed = Message(
|
||||
senderKey: _key(0x33),
|
||||
text: 'hello',
|
||||
timestamp: DateTime.utc(2026, 11, 24),
|
||||
isOutgoing: true,
|
||||
status: MessageStatus.failed,
|
||||
radioErrorCode: errCodeNotFound,
|
||||
);
|
||||
|
||||
test('a resend clears it', () {
|
||||
final resent = failed.copyWith(status: MessageStatus.pending);
|
||||
expect(resent.radioErrorCode, isNull);
|
||||
});
|
||||
|
||||
test('an unrelated update keeps it', () {
|
||||
expect(failed.copyWith(retryCount: 1).radioErrorCode, errCodeNotFound);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue