Files
buzz/mobile/test/shared/mentions/mention_rules_test.dart
Matt Toohey 4e4ac3efd8 fix(mobile): fold accents when matching mention names
`@jos` listed José María, `@jose` listed nobody and `@josé` listed them
again: mobile only lowercased names. Name matching now follows section 3
of the shared mention rules, as buzz-app does (recommendation 6). Each
code point is NFKD-normalized, its combining accents (the Combining
Diacritical Marks blocks) are removed, it is lowercased and a final `ς`
becomes `σ`, so `jose` matches `José`, `fiona` matches `fio` and `jose`
matches `jose`, while `ø`, `ł`, `æ`, `ß`, `ı` and the marks of other
scripts stay distinct. Words are runs of letters, marks and digits, so a
hyphen or apostrophe separates them: `jane` finds `Mary-Jane` and `brien`
finds `O'Brien`. The folding lives in `name_match.dart` and drives the
chooser, Space selection, multi-word admission, the verified-name matches
and the recipient pickers, so `@jose maria` lists José María with no
accents typed.

The vendored fixture moves from version 1 (95 cases) to version 4 of
buzz-app's `mention-rules.fixtures.json` (125 cases), copied unchanged,
and the conformance test runs every section, with a new `matching` group.
No case is skipped. The rules the newer versions added are implemented
too: an exact public key (64 hex in either case, or a complete lowercase
or uppercase npub with an optional `nostr:` prefix) matches only its
owner, even one with no name, and Space selects that owner; the viewer's
own agent whose name continues the query with a new word leads (`Sol 6`
for `sol`), and an exact person leads over it; agents from the viewer's
other communities follow everyone here; an outside choice's longer name
no longer blocks a member's exact name on Space, and a choice's own longer
alias is not its own namesake.

The search cache's exact-key exemption uses the same `typedPublicKey`.
Refutation itself stays lowercase-only on purpose: it reasons about the
relay's prefix search, which is out of scope here, so an accented query
is a new search rather than a continuation. A refuted query still matches
members on the device by folding: after `z` finds nobody, `@zoe` lists
the member Zoë without a search.

Tests: the compose bar search journey types `@jose maria` and sees José
María, and `@zoe` after the `z` refutation sees Zoë; both and seven
fixture cases fail when accent stripping is removed. `dart format`,
`flutter analyze` and the full mobile `flutter test` suite pass.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Matt Toohey <contact@matttoohey.com>
2026-11-19 15:07:07 +11:00

276 lines
7.4 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:buzz/shared/mentions/mention_rules.dart';
import 'package:buzz/shared/mentions/mention_tags.dart';
import 'package:buzz/shared/mentions/name_match.dart';
import 'package:flutter_test/flutter_test.dart';
// Portable fixtures, copied unchanged from buzz-app
// src/bundled/mentions/mention-rules.fixtures.json (mention rules v4).
// Every case runs; none is skipped.
final Map<String, dynamic> _fixtures =
jsonDecode(
File(
'test/shared/mentions/mention-rules.fixtures.json',
).readAsStringSync(),
)
as Map<String, dynamic>;
List<Map<String, dynamic>> _cases(String section) => [
for (final item in _fixtures[section] as List) item as Map<String, dynamic>,
];
List<Map<String, dynamic>> _tagCases(String section) => [
for (final item in (_fixtures['tags'] as Map)[section] as List)
item as Map<String, dynamic>,
];
MentionChoice _choice(Map<String, dynamic> json) {
final name = json['name'] as String;
return MentionChoice(
pubkey: json['pubkey'] as String,
name: name,
label: json['label'] as String? ?? name,
aliases: [
for (final alias in json['aliases'] as List? ?? [name]) alias as String,
],
member: json['member'] as bool,
agent: json['agent'] as bool? ?? false,
owned: json['owned'] as bool? ?? false,
managed: json['managed'] as bool? ?? false,
elsewhere: json['elsewhere'] as String?,
);
}
List<MentionChoice> _choices(Map<String, dynamic> fixture) => [
for (final choice in fixture['choices'] as List)
_choice(choice as Map<String, dynamic>),
];
void main() {
test('reads mention fixtures version 4', () {
expect(_fixtures['version'], 4);
final sections = ['matching', 'ranking', 'space', 'query', 'admission'];
for (final section in sections) {
expect(_cases(section), isNotEmpty, reason: section);
}
expect(_tagCases('write'), isNotEmpty);
expect(_tagCases('read'), isNotEmpty);
// Every fixture section above has a group below that runs each case.
expect(_fixtures.keys.where((key) => !key.startsWith(r'$')).toSet(), {
...sections,
'version',
'tags',
});
});
group('matching fixtures', () {
for (final fixture in _cases('matching')) {
test(fixture['name'], () {
expect(
nameMatchTier(fixture['label'] as String, fixture['query'] as String),
fixture['expected'],
);
});
}
});
group('tag writer fixtures', () {
for (final fixture in _tagCases('write')) {
test(fixture['name'], () {
List<String> keys(String field) => [
for (final key in fixture[field] as List) key as String,
];
final expected = fixture['expected'] as Map;
// The sender is a member in addition to `members`.
final members = {...keys('members'), 'f' * 64};
Object? written() {
try {
return {
'tags': writeMentionTags(
recipients: keys('recipients'),
references: keys('references'),
members: members,
),
};
} on MentionTagException catch (error) {
return {
'error': switch (error.error) {
MentionTagError.notMember => 'not_member',
MentionTagError.invalid => 'invalid',
MentionTagError.tooMany => 'too_many',
},
};
}
}
expect(written(), expected);
});
}
});
group('tag reader fixtures', () {
for (final fixture in _tagCases('read')) {
test(fixture['name'], () {
final read = readMentionTags([
for (final tag in fixture['tags'] as List)
[for (final field in tag as List) field as String],
]);
expect({
'mentions': read.mentions,
'references': read.references,
}, fixture['expected']);
});
}
});
group('ranking fixtures', () {
for (final fixture in _cases('ranking')) {
test(fixture['name'], () {
final history = {
for (final entry in (fixture['history'] as Map? ?? const {}).entries)
entry.key as String: entry.value as int,
};
final presence = fixture['presence'] as Map? ?? const {};
final ranked = rankMentions(
_choices(fixture),
fixture['query'] as String,
history: history,
presence: (key) => presence[key] as String? ?? 'unknown',
);
expect([for (final c in ranked) c.pubkey], fixture['expected']);
});
}
});
group('Space fixtures', () {
for (final fixture in _cases('space')) {
test(fixture['name'], () {
expect(
exactMention(_choices(fixture), fixture['query'] as String),
fixture['expected'],
);
});
}
});
group('query syntax fixtures', () {
for (final fixture in _cases('query')) {
test(fixture['name'], () {
final query = findMentionQuery(
fixture['text'] as String,
fixture['caret'] as int,
);
final expected = fixture['expected'] as Map?;
expect(
query == null
? null
: {'start': query.start, 'end': query.end, 'query': query.query},
expected,
);
});
}
});
group('multi-word admission fixtures', () {
for (final fixture in _cases('admission')) {
test(fixture['name'], () {
expect(
matchesMentionQuery(fixture['query'] as String, [
for (final name in fixture['names'] as List) name as String,
]),
fixture['expected'],
);
});
}
});
test('a workflow owner attribution does not address its owner', () {
final me = 'a' * 64;
for (final (tags, addressed) in [
(
[
['p', me],
],
true,
),
(
[
['p', me.toUpperCase()],
],
true,
),
(
[
['p', me, '', 'owner'],
['buzz:workflow', 'true'],
],
false,
),
(
[
['p', me, '', 'owner'],
['buzz:workflow', 'true'],
['p', me],
],
true,
),
// Without the workflow marker the fourth field means something else,
// such as a NIP-29 roster role.
(
[
['p', me, '', 'owner'],
],
true,
),
(
[
['p', 'b' * 64],
],
false,
),
]) {
expect(tagsAddress(tags, me), addressed, reason: '$tags');
}
});
test('order does not depend on input order', () {
// Review case: owned Zed, unowned Alpha, human Mary.
const zed = MentionChoice(
pubkey: 'a',
name: 'Zed',
label: 'Zed',
aliases: ['Zed'],
member: true,
agent: true,
owned: true,
);
const alpha = MentionChoice(
pubkey: 'b',
name: 'Alpha',
label: 'Alpha',
aliases: ['Alpha'],
member: true,
agent: true,
);
const mary = MentionChoice(
pubkey: 'c',
name: 'Mary',
label: 'Mary',
aliases: ['Mary'],
member: true,
);
for (final rows in [
[zed, alpha, mary],
[zed, mary, alpha],
[alpha, zed, mary],
[alpha, mary, zed],
[mary, zed, alpha],
[mary, alpha, zed],
]) {
expect(rankMentions(rows, ''), [zed, alpha, mary]);
}
});
}