Files
buzz/mobile/test/features/channels/dm_channel_labels_test.dart
morgmart 70bb7b7bec feat(mobile): name blocking agents, leave agents out of DM names (#9875)
## Problem

Two follow-ups to growable DMs (#9153), matching desktop
block/buzz-app#3744:

1. When an agent's add policy blocks a DM, mobile shows a generic error.
The person can't tell which agent blocked it or whose owner to ask.
2. DM names list every member, agents included, so a DM with one person
and their agent reads like a group, and "+N more" mixes agents with
people.

## Change

**Name the blocking agent.** The relay names the agent in that refusal
(#9601). Mobile shows the agent's name: "Only Scout's owner can add it
to a DM." or "Scout can't be added to a DM.", starting with "Couldn't
add them." when adding to an existing DM, and "this agent" when the name
can't be read. Against a relay without #9601, the error is the same as
before.

**Leave agents out of DM names.** A DM's name lists its people and never
its agents, unless no other person is in it (just you and agents), when
it names the agents. "+N more" counts people only. Member counts and
member lists still include agents. The shared DM label feeds the list,
headers, details and Activity. Agent means the profile's existing agent
flag; a profile that hasn't loaded yet counts as a person until it says
otherwise.

## Screenshots

Temporary headless renders with test data, not committed.

### Name the blocking agent

Add people, refused by an agent's owner-only setting:

| | Light | Dark |
|---|---|---|
| Narrow | ![light
narrow](https://github.com/user-attachments/assets/61bfbfe2-5882-456e-8bff-b42f7389aa06)
| ![dark
narrow](https://github.com/user-attachments/assets/d7ac0dca-9081-4cde-a403-cde492d2cf4f)
|
| Wide | ![light
wide](https://github.com/user-attachments/assets/cc9f5a0f-0f3a-4d80-8fc6-0937a2246462)
| ![dark
wide](https://github.com/user-attachments/assets/87b48014-9740-4e17-ad83-f5bac272dda8)
|

### DM names without agents

Ana alone, Ana with two agents (named "Ana", count still 3), and only
the two agents (named by them):

| | Light | Dark |
|---|---|---|
| Narrow | ![light
narrow](https://github.com/user-attachments/assets/b413dbc1-a2bc-4410-8be9-b1a219374349)
| ![dark
narrow](https://github.com/user-attachments/assets/75c9cbc3-432f-45c7-83a4-624f80752b97)
|
| Wide | ![light
wide](https://github.com/user-attachments/assets/ae57c305-f4e4-4e26-8ba0-ecbcd128137e)
| ![dark
wide](https://github.com/user-attachments/assets/a75efc90-6b20-4409-bdd5-682de1f0301f)
|

## Tests

- Blocking agent: an open and an add name the agent, fall back to "this
agent", and leave other refusals as they were.
- DM names: people and agents, only agents, more than three people and
agents, and a profile that hasn't loaded yet.

The lost-reply recovery was taken out after review. It comes back as a
relay change that answers a replayed signed command with its saved
result, so the app can resend the same event instead of guessing from
rosters.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: morgmart <98432065+morgmart@users.noreply.github.com>
Co-authored-by: Carl <c217fe6b9d958f41c3a5e030dccc7f626775a923089cb6491305eade75ea1f1b@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 12:10:23 -08:00

209 lines
6.6 KiB
Dart

import 'package:buzz/features/channels/channel.dart';
import 'package:buzz/features/channels/dm_channel_labels.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(int number) => number.toRadixString(16).padLeft(64, '0');
Channel _dm(
String id, {
String name = 'DM',
List<String> keys = const [],
List<String> labels = const [],
}) => Channel(
id: id,
name: name,
channelType: 'dm',
visibility: 'private',
description: '',
createdBy: _key(1),
createdAt: DateTime(2026),
memberCount: keys.length,
participantPubkeys: keys,
participants: labels,
isMember: true,
);
// Count real contextual resolutions, without replacing the naming policy.
class _CountingSources extends IdentityNameSources {
final List<List<String>> scopes = [];
_CountingSources({required super.profiles});
@override
IdentityNames scope(
Iterable<String> candidates, {
Set<String> agentPubkeys = const {},
Map<String, String> fallbackNames = const {},
Map<String, String> ownerPubkeys = const {},
}) {
scopes.add(candidates.toList());
return super.scope(
candidates,
agentPubkeys: agentPubkeys,
fallbackNames: fallbackNames,
ownerPubkeys: ownerPubkeys,
);
}
}
void main() {
test('sorting resolves each DM context once, independent of comparisons', () {
final channels = [
for (var index = 0; index < 80; index++)
_dm('dm-$index', keys: [_key(1), _key(index + 2)]),
];
final names = _CountingSources(
profiles: {
for (var index = 0; index < channels.length; index++)
_key(index + 2): UserProfile(
pubkey: _key(index + 2),
displayName: 'Person ${(79 - index).toString().padLeft(2, '0')}',
),
},
);
final sorted = sortDmChannelsByDisplayLabel(
channels,
currentPubkey: _key(1),
names: names,
);
expect(sorted, channels.reversed.toList());
expect(names.scopes, hasLength(channels.length));
expect(
names.scopes,
unorderedEquals(channels.map((channel) => channel.participantPubkeys)),
);
expect(channels.first.id, 'dm-0', reason: 'Do not mutate the source list');
});
test(
'each sort uses current labels, identity, participants and community',
() {
final me = _key(1);
final alice = _key(2);
final agent = _key(3);
final otherAgent = _key(4);
final owner = _key(5);
final channels = [
_dm('direct', keys: [me, alice], labels: ['Zoe', 'Alice']),
_dm(
'group',
name: 'Group DM (4)',
keys: [me, alice, agent, otherAgent],
labels: ['Zoe', 'Alice', 'Scout', 'Scout'],
),
_dm('self', keys: [me], labels: ['Zoe']),
_dm('empty'),
_dm('legacy', keys: ['legacy-key'], labels: ['Bob']),
_dm('tie-b', name: 'ALICE'),
_dm('tie-a', name: 'alice'),
];
final updatedNames = IdentityNameSources(
viewer: me,
profiles: {
me: UserProfile(pubkey: me, displayName: 'Zoe'),
alice: UserProfile(pubkey: alice, displayName: 'Yvonne'),
agent: UserProfile(
pubkey: agent,
displayName: 'Scout',
ownerPubkey: me,
),
otherAgent: UserProfile(
pubkey: otherAgent,
displayName: 'Scout',
ownerPubkey: owner,
),
owner: UserProfile(pubkey: owner, displayName: 'Aaron'),
},
);
final cases = [
(channels: channels, viewer: me, names: null),
(channels: channels, viewer: me, names: updatedNames),
(channels: channels, viewer: alice, names: updatedNames),
(
channels: [
channels.first.copyWith(name: 'Renamed'),
_dm('group', keys: [agent, owner], labels: ['Scout', 'Aaron']),
...channels.skip(2),
],
viewer: me,
names: updatedNames,
),
(channels: channels, viewer: me, names: const IdentityNameSources()),
(channels: <Channel>[], viewer: me, names: updatedNames),
(channels: [channels.first], viewer: me, names: updatedNames),
];
for (final scenario in cases) {
// Independent reference: the previous comparator's ordering contract.
final expected = scenario.channels.toList()
..sort((left, right) {
String label(Channel channel) => resolveDmChannelDisplayLabel(
channel,
currentPubkey: scenario.viewer,
names: scenario.names,
).toLowerCase();
final order = label(left).compareTo(label(right));
return order != 0 ? order : left.id.compareTo(right.id);
});
expect(
sortDmChannelsByDisplayLabel(
scenario.channels,
currentPubkey: scenario.viewer,
names: scenario.names,
),
expected,
);
}
expect(
sortDmChannelsByDisplayLabel(
channels,
currentPubkey: me,
).map((channel) => channel.id),
['direct', 'tie-a', 'tie-b', 'group', 'legacy', 'empty', 'self'],
);
},
);
test('a DM is named by its people, never its agents, unless it has none', () {
final me = _key(1);
final ana = _key(2), ben = _key(3), cy = _key(4), dee = _key(5);
final bot = _key(6), helper = _key(7), unloaded = _key(8);
final names = IdentityNameSources(
profiles: {
for (final (key, name) in [
(ana, 'Ana'),
(ben, 'Ben'),
(cy, 'Cy'),
(dee, 'Dee'),
])
key: UserProfile(pubkey: key, displayName: name),
bot: UserProfile(pubkey: bot, displayName: 'Bot', ownerPubkey: me),
helper: UserProfile(
pubkey: helper,
displayName: 'Helper',
ownerPubkey: me,
),
},
);
String label(List<String> keys) => resolveDmChannelDisplayLabel(
_dm('dm', keys: [me, ...keys]),
currentPubkey: me,
names: names,
);
// People and agents: only the people.
expect(label([bot, ana]), 'Ana');
// Only agents: no other person, so the agents.
expect(label([bot, helper]), 'Bot, Helper');
// More than three people and agents: "+N more" counts people only.
expect(label([bot, ana, ben, helper, cy, dee]), 'Ana, Ben, Cy, +1 more');
// A profile not loaded yet is named until it says it is an agent.
expect(label([bot, unloaded]), shortPubkey(unloaded));
});
}