feat(#750): separate what a node calls itself from what the user calls it
Task 750.1 of the contact-naming plan: model and storage. Stock MeshCore has a per-node CUSTOM NAME field and keeps the original underneath; Offband could not rename a contact at all. Contact gains receivedName (the name the mesh gave us, wire and storage) and customName (the user's label, client-side only). name becomes a getter returning customName ?? receivedName, so all 85 existing display reads stay correct and a future read cannot forget. Empty and whitespace-only custom names count as unset at both the getter and the store, so clearing the field cannot leave a contact blank with no way back. The constructor and copyWith take receivedName, which made the rename a compile error at every write site. The compiler enumerated 23 of them across 5 lib files and 11 test files; none was found by reading. The advert handler is the one that matters: receivedName now updates while customName is untouched, so an advert improves accuracy without moving what the user sees. That was the defect making a client-held name impossible. customName is deliberately NOT in contact_store. That record is rebuilt from radio contact-sync data where no custom name exists, so a save after a sync would wipe it. It lives in contact_settings_store keyed by pubkey hex, which a sync cannot reach. Both stores persist receivedName, never the getter. The plan's correction on #750 found that persisting the getter is one-way data loss: the custom name would be read back as the received name and the real one destroyed with no advert able to restore it. The ?? 'Unknown' fallback is kept with a comment, since the rename is exactly the edit that drops a trailing fallback and would crash the load for a legacy row. 19 tests, positive and negative. T31 and T32 were falsified before being trusted: breaking the persist and the fallback made exactly those two fail and nothing else. Gates: analyze --fatal-infos --fatal-warnings clean, dart format 0-changed, 1305 tests passed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>pull/1001/head
parent
b50d4a73f9
commit
3a0ad8f273
@ -0,0 +1,101 @@
|
||||
// Custom Name override for contacts (#750), model half.
|
||||
//
|
||||
// `Contact.name` becomes a getter returning `customName ?? receivedName`, so
|
||||
// the 85 existing display reads stay correct and untouched while every write
|
||||
// site is forced to say which name it means.
|
||||
//
|
||||
// Tests T1 to T8 and T31 from the plan of record,
|
||||
// docs/initiatives/standalone/750-contact-naming/PLAN.md.
|
||||
//
|
||||
// The negatives here are the ones that matter. A positive-only suite would keep
|
||||
// passing while an advert quietly took the user's chosen name back.
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/models/contact.dart';
|
||||
|
||||
Contact _contact({required String receivedName, String? customName}) => Contact(
|
||||
publicKey: Uint8List.fromList(List<int>.filled(32, 0x11)),
|
||||
receivedName: receivedName,
|
||||
customName: customName,
|
||||
type: 1,
|
||||
pathLength: -1,
|
||||
path: Uint8List(0),
|
||||
lastSeen: DateTime.utc(2026, 7, 1),
|
||||
);
|
||||
|
||||
void main() {
|
||||
group('Contact.name resolution (#750)', () {
|
||||
test('T1 + returns the custom name when one is set', () {
|
||||
final c = _contact(receivedName: 'KQ4EZN-Repeater', customName: 'Chuck');
|
||||
|
||||
expect(c.name, 'Chuck');
|
||||
});
|
||||
|
||||
test('T2 - falls back to the received name, and does not invent one', () {
|
||||
final c = _contact(receivedName: 'KQ4EZN-Repeater');
|
||||
|
||||
expect(c.name, 'KQ4EZN-Repeater');
|
||||
expect(c.customName, isNull);
|
||||
});
|
||||
|
||||
test('T3 - an empty custom name counts as unset, not as a blank name', () {
|
||||
// Otherwise a user who clears the field by deleting its text gets a blank
|
||||
// display name with no way back.
|
||||
final c = _contact(receivedName: 'KQ4EZN-Repeater', customName: '');
|
||||
|
||||
expect(c.name, 'KQ4EZN-Repeater');
|
||||
});
|
||||
|
||||
test('T4 - a whitespace-only custom name counts as unset', () {
|
||||
final c = _contact(receivedName: 'KQ4EZN-Repeater', customName: ' ');
|
||||
|
||||
expect(c.name, 'KQ4EZN-Repeater');
|
||||
});
|
||||
|
||||
test(
|
||||
'T5 + copyWith(receivedName:) updates it and keeps the custom name',
|
||||
() {
|
||||
final c = _contact(
|
||||
receivedName: 'old-advert-name',
|
||||
customName: 'Chuck',
|
||||
).copyWith(receivedName: 'new-advert-name');
|
||||
|
||||
expect(c.receivedName, 'new-advert-name');
|
||||
expect(c.customName, 'Chuck');
|
||||
},
|
||||
);
|
||||
|
||||
test('T6 - copyWith(receivedName:) does not change the displayed name', () {
|
||||
// This is the whole point of the epic: an advert updates what the node
|
||||
// calls itself and never disturbs what the user calls it.
|
||||
final c = _contact(
|
||||
receivedName: 'old-advert-name',
|
||||
customName: 'Chuck',
|
||||
).copyWith(receivedName: 'new-advert-name');
|
||||
|
||||
expect(c.name, 'Chuck');
|
||||
});
|
||||
|
||||
test('T6b + clearing the custom name reveals the newest received name', () {
|
||||
final renamed = _contact(
|
||||
receivedName: 'old-advert-name',
|
||||
customName: 'Chuck',
|
||||
).copyWith(receivedName: 'new-advert-name', clearCustomName: true);
|
||||
|
||||
expect(renamed.customName, isNull);
|
||||
expect(renamed.name, 'new-advert-name');
|
||||
});
|
||||
|
||||
test('T28 - a custom name keeps CJK, emoji and ZWJ sequences intact', () {
|
||||
// A custom name never crosses the wire, so #636's grapheme truncation
|
||||
// must not be applied to it.
|
||||
const rich = '测试 🧙♂️ Chuck';
|
||||
final c = _contact(receivedName: 'plain', customName: rich);
|
||||
|
||||
expect(c.name, rich);
|
||||
expect(c.customName, rich);
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -0,0 +1,172 @@
|
||||
// Custom Name override for contacts (#750), storage half.
|
||||
//
|
||||
// Tests T7, T8, T9, T31 and T32 from the plan of record,
|
||||
// docs/initiatives/standalone/750-contact-naming/PLAN.md.
|
||||
//
|
||||
// T32 is the data-loss guard that the plan correction on #750 added. Persisting
|
||||
// `contact.name` instead of `contact.receivedName` would write a user's private
|
||||
// label into the `name` key, and the next load would read it back as what the
|
||||
// node calls itself. The real name would be gone permanently, with no advert
|
||||
// able to restore it because the contact would already appear to have it.
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/models/contact.dart';
|
||||
import 'package:meshcore_open/storage/contact_settings_store.dart';
|
||||
import 'package:meshcore_open/storage/contact_store.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';
|
||||
|
||||
const _radioKey = 'd549bbfbd003e580';
|
||||
const _contactKey = '00112233445566778899aabbccddeeff';
|
||||
|
||||
Contact _contact({required String receivedName, String? customName}) => Contact(
|
||||
publicKey: Uint8List.fromList(List<int>.filled(32, 0x22)),
|
||||
receivedName: receivedName,
|
||||
customName: customName,
|
||||
type: 1,
|
||||
pathLength: -1,
|
||||
path: Uint8List(0),
|
||||
lastSeen: DateTime.utc(2026, 7, 1),
|
||||
);
|
||||
|
||||
void main() {
|
||||
late OffbandDatabase db;
|
||||
late BlobStore blobs;
|
||||
late ContactStore store;
|
||||
late ContactSettingsStore settings;
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
PrefsManager.reset();
|
||||
await PrefsManager.initialize();
|
||||
db = OffbandDatabase(NativeDatabase.memory());
|
||||
blobs = BlobStore(db);
|
||||
BlobStore.overrideForTest(blobs);
|
||||
store = ContactStore()..setPublicKeyHex = _radioKey;
|
||||
settings = ContactSettingsStore()..setPublicKeyHex = _radioKey;
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
BlobStore.clearTestOverride();
|
||||
await db.close();
|
||||
});
|
||||
|
||||
group('contact_store and the received name (#750)', () {
|
||||
test('T7 + a stored contact round-trips its received name', () async {
|
||||
await store.saveContacts([_contact(receivedName: 'KQ4EZN-Repeater')]);
|
||||
final loaded = await store.loadContacts();
|
||||
|
||||
expect(loaded.single.receivedName, 'KQ4EZN-Repeater');
|
||||
expect(loaded.single.name, 'KQ4EZN-Repeater');
|
||||
});
|
||||
|
||||
test('T8 - loading never invents a custom name', () async {
|
||||
// If the migration populated customName from the stored name, every
|
||||
// existing contact would silently become "renamed" and adverts would
|
||||
// stop updating any of them. That is the worst failure in this epic and
|
||||
// a positive-only suite would never see it.
|
||||
await store.saveContacts([_contact(receivedName: 'KQ4EZN-Repeater')]);
|
||||
final loaded = await store.loadContacts();
|
||||
|
||||
expect(loaded.single.customName, isNull);
|
||||
});
|
||||
|
||||
test(
|
||||
'T32 - saving a renamed contact keeps the received name on disk',
|
||||
() async {
|
||||
// The data-loss guard. `contact.name` would be 'Chuck' here.
|
||||
await store.saveContacts([
|
||||
_contact(receivedName: 'KQ4EZN-Repeater', customName: 'Chuck'),
|
||||
]);
|
||||
final loaded = await store.loadContacts();
|
||||
|
||||
expect(
|
||||
loaded.single.receivedName,
|
||||
'KQ4EZN-Repeater',
|
||||
reason: 'the custom name must not be persisted as the received name',
|
||||
);
|
||||
expect(
|
||||
loaded.single.customName,
|
||||
isNull,
|
||||
reason:
|
||||
'contact_store does not own the custom name; a radio sync '
|
||||
'rebuilds this record and would wipe it',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'T31 - a legacy row with no name loads as Unknown, not a crash',
|
||||
() async {
|
||||
// `receivedName` is non-nullable, and the pre-#750 reader had
|
||||
// `json['name'] as String? ?? 'Unknown'`. Dropping that fallback during
|
||||
// the rename would crash the load for anyone holding such a row.
|
||||
await BlobStore.instance.write(
|
||||
store.keyFor,
|
||||
'[{"publicKey":"${base64OfKey()}","type":1,"pathLength":-1,"lastSeen":0}]',
|
||||
);
|
||||
|
||||
final loaded = await store.loadContacts();
|
||||
|
||||
expect(loaded.single.receivedName, 'Unknown');
|
||||
expect(loaded.single.name, 'Unknown');
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('contact_settings_store owns the custom name (#750)', () {
|
||||
test('T9 + a custom name round-trips', () async {
|
||||
await settings.saveCustomName(_contactKey, 'Chuck');
|
||||
|
||||
expect(await settings.loadCustomName(_contactKey), 'Chuck');
|
||||
});
|
||||
|
||||
test(
|
||||
'T9b - an unknown contact returns null, not an empty string',
|
||||
() async {
|
||||
expect(await settings.loadCustomName('ffffffffffffffff'), isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test('T9c + clearing with null removes it', () async {
|
||||
await settings.saveCustomName(_contactKey, 'Chuck');
|
||||
await settings.saveCustomName(_contactKey, null);
|
||||
|
||||
expect(await settings.loadCustomName(_contactKey), isNull);
|
||||
});
|
||||
|
||||
test(
|
||||
'T9d - saving an empty name stores nothing, not an empty string',
|
||||
() async {
|
||||
// The store is the second enforcement point for "empty counts as unset",
|
||||
// so a UI that passes '' cannot leave a contact with a blank name.
|
||||
await settings.saveCustomName(_contactKey, 'Chuck');
|
||||
await settings.saveCustomName(_contactKey, '');
|
||||
|
||||
expect(await settings.loadCustomName(_contactKey), isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test('T9e - saving a whitespace-only name stores nothing', () async {
|
||||
await settings.saveCustomName(_contactKey, 'Chuck');
|
||||
await settings.saveCustomName(_contactKey, ' ');
|
||||
|
||||
expect(await settings.loadCustomName(_contactKey), isNull);
|
||||
});
|
||||
|
||||
test('T9f + a custom name with emoji and CJK survives storage', () async {
|
||||
const rich = '测试 🧙♂️ Chuck';
|
||||
await settings.saveCustomName(_contactKey, rich);
|
||||
|
||||
expect(await settings.loadCustomName(_contactKey), rich);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Base64 of the 32-byte key used by [_contact], for the legacy-row fixture.
|
||||
String base64OfKey() => 'IiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiI=';
|
||||
Loading…
Reference in new issue