Files
buzz/mobile/test/shared/identity_names/identity_names_test.dart
Logan Johnson 4ef23609b7 feat(mobile): show contextual names in channel conversations (#7895)
🤖
## Summary

In a channel conversation, two identities with the same display name
looked identical. In the example below, the channel shows "Bad Janet"
twice, for two different identities. A reader cannot tell which one was
added and then removed.

This PR uses the contextual names from the previous PR everywhere a
conversation names someone:

- Message authors, threads, forum posts and replies
- Reactions, the typing indicator, and system rows (joined, added,
removed)
- Inline mentions and the agent activity sheet
- The profile sheet title, which now matches the row that opened it

Names are compared against the channel's members, so a label only gets
longer when a real collision exists in that channel.

The mention picker labels each choice against every candidate, so two
"Honey" choices look different. When you pick one, it still inserts the
identity's own name ("@Honey"), so the message text does not change.

Part 2 of 3: #7894 (resolver) → #7895 (conversations) → #7896 (lists,
Search, Pulse).

### Related issue

Related to #2910.

### Testing

- A widget test checks that the profile sheet names an author the same
way the channel does.
- A unit test checks that two same-name mention choices get different
labels but insert the same wire name.
- `flutter analyze` is clean, and the full `flutter test` suite passes.
- Checked on an Android emulator against the live relay, in an archived
channel with two identities named "Bad Janet".

| Before | After |
| --- | --- |
| <img width="300" alt="Before: the channel shows two different
identities both as Bad Janet"
src="https://github.com/user-attachments/assets/c7f6c345-12d8-4e11-9434-bbd2c63722db"
/> | <img width="300" alt="After: the second identity shows as Bad Janet
· sqxl"
src="https://github.com/user-attachments/assets/51c6b9fc-8900-4200-8ce0-ad1887f41dba"
/> |

---------

Signed-off-by: Logan Johnson <loganj@squareup.com>
Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
2026-09-29 15:47:43 -04:00

162 lines
5.8 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:buzz/features/channels/channel_management_provider.dart';
import 'package:buzz/features/channels/mentions/mention_candidates.dart';
import 'package:buzz/features/channels/mentions/mention_candidates_provider.dart';
import 'package:buzz/features/channels/mentions/mention_ranking.dart';
import 'package:buzz/shared/identity_names/identity_names.dart';
import 'package:buzz/shared/profile/user_profile.dart';
import 'package:buzz/shared/utils/string_utils.dart';
import 'package:flutter_test/flutter_test.dart';
String _key(String digit) => digit * 64;
final _logan = _key('1');
final _wes = _key('2');
final _human = _key('c');
final _mine = _key('a');
final _wesAgent = _key('b');
IdentityNameSources _sources() => IdentityNameSources(
viewer: _logan,
profiles: {
_logan: UserProfile(pubkey: _logan, displayName: 'Logan'),
_wes: UserProfile(pubkey: _wes, displayName: 'Wes'),
_human: UserProfile(pubkey: _human, displayName: 'Honey'),
_mine: UserProfile(
pubkey: _mine,
displayName: 'Honey',
ownerPubkey: _logan,
),
_wesAgent: UserProfile(
pubkey: _wesAgent,
displayName: 'Honey',
ownerPubkey: _wes,
),
},
);
void main() {
test('channel context: human, then mine, then owner-qualified others', () {
// Wes is not a member: his profile is only an owner lookup fact.
final names = _sources().scope([_logan, _human, _mine, _wesAgent]);
expect(names.labelFor(_human), 'Honey');
expect(names.labelFor(_mine), 'Honey (agent)');
expect(names.labelFor(_wesAgent), 'Wes’s Honey');
expect(names.labelFor(_logan), 'Logan');
});
test('a non-member reference is compared with the members plus itself', () {
final names = _sources().scope([_human]);
// Alone, Logan's agent is plain; against the human it is marked.
expect(_sources().scope([_mine]).labelFor(_mine), 'Honey');
expect(names.labelFor(_mine), 'Honey (agent)');
expect(names.labelFor(_human), 'Honey');
});
test('profiles outside the context do not create collisions', () {
final names = _sources().scope([_wesAgent]);
expect(names.labelFor(_wesAgent), 'Honey');
});
test('view-local agent roles and fallback names become facts', () {
final bot = _key('d');
final names = IdentityNameSources(
profiles: {_human: UserProfile(pubkey: _human, displayName: 'Honey')},
).scope([_human, bot], agentPubkeys: {bot}, fallbackNames: {bot: 'Honey'});
expect(names.labelFor(bot), 'Honey (agent)');
expect(names.labelFor(_human), 'Honey');
});
test(
'blank names fall back to the compact npub; invalid keys stay plain',
() {
final names = IdentityNameSources(
profiles: {
_human: UserProfile(pubkey: _human, displayName: ' '),
'legacy': const UserProfile(pubkey: 'legacy', displayName: 'Honey'),
},
).scope([_human, 'not-a-key', 'legacy']);
expect(names.candidates, {_human});
expect(names.labelFor(_human), shortPubkey(_human));
expect(names.labelFor('not-a-key'), unknownIdentityLabel);
// A malformed key is not an identity fact: plain name, no qualifier.
expect(names.labelFor('legacy'), 'Honey');
},
);
test('reports owner profiles to load, without inventing owner names', () {
final sources = IdentityNameSources(
viewer: _logan,
profiles: {
_human: UserProfile(pubkey: _human, displayName: 'Honey'),
_wesAgent: UserProfile(
pubkey: _wesAgent,
displayName: 'Honey',
ownerPubkey: _wes,
),
},
);
expect(sources.scope([_human, _wesAgent]).missingOwnerProfiles(), {_wes});
expect(
sources.scope([_human, _wesAgent]).labelFor(_wesAgent),
'Honey (agent)',
);
});
test('mention picker labels compare all choices but keep wire names', () {
final candidates = [
MentionCandidate(pubkey: _human, displayName: 'Honey', isMember: true),
MentionCandidate(
pubkey: _wesAgent,
displayName: 'Honey',
isAgent: true,
ownerPubkey: _wes,
),
];
final names = mentionPickerNames(_sources(), candidates);
final labeled = [
for (final c in candidates)
c.withContextLabel(names.resolve(c.pubkey)?.name),
];
// A query that matches only the agent still shows its contextual label.
final ranked = rankMentionCandidates(labeled, 'hon');
expect(ranked.map((c) => c.pickerLabel), ['Honey', 'Wes’s Honey']);
// The inserted mention text still uses the agent's own name.
expect(ranked.map((c) => c.label), ['Honey', 'Honey']);
});
test('a searched agent keeps its owner before its profile is cached', () {
final scout = _key('d');
final myScout = _key('e');
// Only the viewer's profile is cached; the viewer's newly searched agent
// is not, and the member bot has no known owner.
final sources = IdentityNameSources(
viewer: _logan,
profiles: {_logan: UserProfile(pubkey: _logan, displayName: 'Logan')},
);
final candidates = buildMentionCandidates(
members: [
ChannelMember(
pubkey: scout,
role: 'bot',
joinedAt: DateTime(2026),
displayName: 'Scout',
),
],
relayAgents: const [],
sharedChannelIds: const {},
userCache: sources.profiles,
ownerByAgentPubkey: const {},
searchResults: [
UserProfile(pubkey: myScout, displayName: 'Scout', ownerPubkey: _logan),
],
currentPubkey: _logan,
);
expect(candidates.map((c) => c.pubkey), [scout, myScout]);
final names = mentionPickerNames(sources, candidates);
expect(names.labelFor(myScout), 'Scout');
expect(names.labelFor(scout), isNot('Scout'));
// The owner is the viewer, whose profile is cached: nothing to load.
expect(names.missingOwnerProfiles(), isEmpty);
});
}