Fix manual path search ranking, hash matching, and layout

Rank name prefixes ahead of other substring matches, match hop hashes
in the path builder and contact search, and keep the hop list and hex
field on screen while the keyboard is open.

Co-authored-by: zjs81 <zjs81@users.noreply.github.com>
This commit is contained in:
Cursor Agentandzjs81 committed 2026-11-20 21:43:49 +00:00
1 parent 34ea0cb7ca
commit a1604af4d4
51 files changed
+882 -327

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/models/contact.dart';
import 'package:meshcore_open/utils/contact_search.dart';
Contact _contact(
String name,
List<int> key, {
int type = advTypeRepeater,
DateTime? lastSeen,
}) {
return Contact(
publicKey: Uint8List.fromList(key),
name: name,
type: type,
pathLength: -1,
path: Uint8List(0),
lastSeen: lastSeen ?? DateTime.fromMillisecondsSinceEpoch(0),
);
}
List<String> _ranked(List<Contact> contacts, String query) {
final matches = contacts.where((c) => matchesContactQuery(c, query)).toList()
..sort((a, b) => compareContactQueryRank(a, b, query));
return matches.map((c) => c.name).toList();
}
void main() {
final contacts = [
_contact('100 Acre', [0x11, 0x22, 0x33, 0x44]),
_contact('Acreage North', [0x33, 0x44, 0x55, 0x66]),
_contact('My Acre Site', [0x55, 0x66, 0x77, 0x88]),
_contact('2E0HQR-R', [0x2E, 0x0A, 0x1B, 0x2C]),
_contact('CR1', [0x60, 0x60, 0xAB, 0xCD]),
_contact('Node 6060', [0x01, 0x02, 0x03, 0x04]),
_contact('6060 Hub', [0x0A, 0x0B, 0x0C, 0x0D]),
];
test('name prefixes rank before other substring matches', () {
expect(_ranked(contacts, 'acre'), [
'Acreage North',
'100 Acre',
'My Acre Site',
]);
});
test('prefix matches stay alphabetical among themselves', () {
final extra = [
_contact('Acreage North', [0x01]),
_contact('Acre Beacon', [0x02]),
];
expect(_ranked(extra, 'acre'), ['Acre Beacon', 'Acreage North']);
});
test('a hop hash matches when the name does not', () {
expect(matchesContactQuery(contacts[4], '6060'), isTrue);
expect(matchesContactQuery(contacts[4], '60'), isTrue);
expect(matchesContactQuery(contacts[4], '6060abcd'), isTrue);
expect(matchesContactQuery(contacts[4], '6061'), isFalse);
expect(_ranked(contacts, '6060'), ['6060 Hub', 'Node 6060', 'CR1']);
});
test('hash search accepts 0x, spaces, and angle brackets', () {
expect(_ranked(contacts, '0x6060'), ['CR1']);
expect(_ranked(contacts, '60 60'), ['CR1']);
expect(_ranked(contacts, '<6060>'), ['CR1']);
});
test('a single hex digit does not match every key', () {
expect(matchesContactQuery(contacts[4], '6'), isFalse);
});
test('an empty query matches every contact and keeps the tie-break', () {
final timed = [
_contact('B', [0x01], lastSeen: DateTime.utc(2024, 1, 2)),
_contact('A', [0x02], lastSeen: DateTime.utc(2024, 1, 3)),
];
timed.sort(
(a, b) => compareContactQueryRank(
a,
b,
'',
(a, b) => b.lastSeen.compareTo(a.lastSeen),
),
);
expect(timed.map((c) => c.name), ['A', 'B']);
});
test('tie-break applies inside a rank, not across ranks', () {
final olderPrefix = _contact('Acre Older', [
0x01,
], lastSeen: DateTime.utc(2020));
final newerSubstring = _contact('My Acre', [
0x02,
], lastSeen: DateTime.utc(2024));
final ranked = [newerSubstring, olderPrefix]
..sort(
(a, b) => compareContactQueryRank(
a,
b,
'acre',
(a, b) => b.lastSeen.compareTo(a.lastSeen),
),
);
expect(ranked.map((c) => c.name), ['Acre Older', 'My Acre']);
});
}
+199
View File
@@ -0,0 +1,199 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/models/contact.dart';
import 'package:meshcore_open/widgets/path_editor_sheet.dart';
Contact _repeater(String name, List<int> prefix) {
final key = Uint8List(32);
for (var i = 0; i < prefix.length; i++) {
key[i] = prefix[i];
}
return Contact(
publicKey: key,
name: name,
type: advTypeRepeater,
pathLength: -1,
path: Uint8List(0),
lastSeen: DateTime.fromMillisecondsSinceEpoch(0),
);
}
final _contacts = [
_repeater('100 Acre', [0x11, 0x22]),
_repeater('Acreage North', [0x33, 0x44]),
_repeater('My Acre Site', [0x55, 0x66]),
_repeater('2E0HQR-R', [0x2E, 0x0A]),
_repeater('CR1', [0x60, 0x60]),
for (var i = 0; i < 24; i++) _repeater('Repeater $i', [0xA0, i]),
];
Future<void> _pumpSheet(
WidgetTester tester, {
Size size = const Size(400, 700),
double keyboard = 0,
List<int> initialPath = const [],
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: MediaQuery(
data: MediaQueryData(
size: size,
viewInsets: EdgeInsets.only(bottom: keyboard),
),
child: Scaffold(
body: SizedBox(
width: size.width,
height: size.height - keyboard,
child: PathEditorSheet(
availableContacts: _contacts,
initialPath: initialPath,
pathHashByteWidth: 2,
),
),
),
),
),
);
await tester.pumpAndSettle();
}
List<String> _repeaterNames(WidgetTester tester) {
return tester
.widgetList<Text>(
find.descendant(
of: find.byKey(const Key('path-editor-repeater-list')),
matching: find.byType(Text),
),
)
.map((text) => text.data)
.whereType<String>()
.where((text) => !text.contains('•'))
.toList();
}
void main() {
testWidgets('prefix matches are listed before other substring matches', (
tester,
) async {
await _pumpSheet(tester);
await tester.enterText(
find.byKey(const Key('path-editor-search-field')),
'acre',
);
await tester.pumpAndSettle();
final visible = _repeaterNames(tester);
expect(visible.first, 'Acreage North');
expect(visible[1], '100 Acre');
await tester.scrollUntilVisible(
find.text('My Acre Site'),
80,
scrollable: find.descendant(
of: find.byKey(const Key('path-editor-repeater-list')),
matching: find.byType(Scrollable),
),
);
expect(find.text('My Acre Site'), findsOneWidget);
});
testWidgets('search matches a hop hash the name does not contain', (
tester,
) async {
await _pumpSheet(tester);
await tester.enterText(
find.byKey(const Key('path-editor-search-field')),
'6060',
);
await tester.pumpAndSettle();
expect(find.text('No repeaters or room servers found.'), findsNothing);
expect(_repeaterNames(tester), ['CR1']);
});
testWidgets('hex entry is reachable without scrolling the repeater list', (
tester,
) async {
await _pumpSheet(tester, size: const Size(400, 900));
expect(
find.byKey(const Key('path-editor-hex-field')).hitTestable(),
findsOneWidget,
);
expect(find.text('Hex prefixes'), findsOneWidget);
expect(
find.text('4 hex characters per hop, separated by commas'),
findsOneWidget,
);
});
testWidgets('an added hop stays fully visible with the keyboard open', (
tester,
) async {
await _pumpSheet(
tester,
size: const Size(390, 640),
keyboard: 320,
initialPath: [0x60, 0x60],
);
final tile = find.ancestor(
of: find.byIcon(Icons.remove_circle_outline),
matching: find.byType(ListTile),
);
expect(tile.hitTestable(), findsOneWidget);
final title = tester.getRect(find.text('Build Path'));
final hopRect = tester.getRect(tile);
final sheet = tester.getRect(find.byType(PathEditorSheet));
expect(hopRect.top, greaterThanOrEqualTo(title.bottom));
expect(hopRect.bottom, lessThanOrEqualTo(sheet.bottom));
expect(hopRect.height, greaterThanOrEqualTo(48));
expect(find.text('CR1').hitTestable(), findsWidgets);
expect(find.text('6060').hitTestable(), findsWidgets);
expect(
find.byKey(const Key('path-editor-hex-field')).hitTestable(),
findsOneWidget,
);
expect(tester.takeException(), isNull);
});
testWidgets('action buttons stay below the repeater list on a short sheet', (
tester,
) async {
await _pumpSheet(
tester,
size: const Size(400, 560),
initialPath: [0x60, 0x60, 0x11, 0x22],
);
expect(tester.takeException(), isNull);
final button = tester.getRect(find.text('Use this path'));
final list = tester.getRect(
find.byKey(const Key('path-editor-repeater-list')),
);
expect(button.top, greaterThanOrEqualTo(list.bottom - 1));
});
testWidgets('tapping a repeater adds a visible hop', (tester) async {
await _pumpSheet(tester);
await tester.enterText(
find.byKey(const Key('path-editor-search-field')),
'6060',
);
await tester.pumpAndSettle();
await tester.tap(find.widgetWithText(ListTile, 'CR1'));
await tester.pumpAndSettle();
expect(find.text('1 of 64 hops'), findsOneWidget);
expect(find.byIcon(Icons.remove_circle_outline), findsOneWidget);
expect(find.text('CR1').hitTestable(), findsWidgets);
});
}