Merge remote-tracking branch 'origin/dev' into feat/fast-everywhere-bulk-selection

# Conflicts:
#	lib/screens/chat_screen.dart
This commit is contained in:
zjs81 committed 2026-11-23 21:16:49 -07:00
commit d11865b175
58 files changed
+1524 -851

No files matched your search

@@ -0,0 +1,265 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/models/app_settings.dart';
import 'package:meshcore_open/theme/mesh_theme.dart';
import 'package:meshcore_open/screens/chat_compression_screen.dart';
final _l10n = lookupAppLocalizations(const Locale('en'));
final _profiles = [
Cyr2LatProfile(id: 'ru', name: 'Russian', charMap: const {'А': 'A'}),
Cyr2LatProfile(id: 'sr', name: 'Serbian', charMap: const {'З': '3'}),
];
/// Stands in for a chat screen: keeps the "saved" state and applies a change
/// only when the test completes the pending write.
class _Host extends ChangeNotifier {
ChatCompressionMode mode;
String? profileId;
final bool enabled;
final List<ChatCompressionMode> modeCalls = [];
final List<String> profileCalls = [];
final List<Completer<void>> pending = [];
_Host({
this.mode = ChatCompressionMode.off,
this.enabled = true,
this.profileId,
});
ChatCompressionState read() => ChatCompressionState(
mode: mode,
enabled: enabled,
disabledReason: enabled ? null : _l10n.chatCompression_noStorageId,
profiles: _profiles,
profileId: profileId,
defaultProfileId: 'ru',
);
Future<void> _write(void Function() apply) async {
final completer = Completer<void>();
pending.add(completer);
await completer.future;
apply();
notifyListeners();
}
Future<void> setMode(ChatCompressionMode m) {
modeCalls.add(m);
return _write(() => mode = m);
}
Future<void> setProfile(String id) {
profileCalls.add(id);
return _write(() => profileId = id);
}
}
void main() {
late _Host host;
Future<void> pumpPage(
WidgetTester tester, {
ChatCompressionMode mode = ChatCompressionMode.off,
bool enabled = true,
String? profileId,
Size size = const Size(400, 800),
double textScale = 1.0,
}) async {
host = _Host(mode: mode, enabled: enabled, profileId: profileId);
addTearDown(host.dispose);
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await tester.pumpWidget(
MaterialApp(
theme: MeshTheme.light(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
builder: (context, child) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: child!,
),
home: ChatCompressionScreen(
listenable: host,
read: host.read,
onModeChanged: host.setMode,
onProfileChanged: host.setProfile,
),
),
);
}
Finder optionRow(String label) =>
find.ancestor(of: find.text(label), matching: find.byType(ListTile));
bool isSelected(String label) {
return find
.descendant(
of: optionRow(label),
matching: find.byIcon(Icons.radio_button_checked),
)
.evaluate()
.isNotEmpty;
}
testWidgets(
'lists the three modes with short notes and marks the saved one',
(tester) async {
await pumpPage(tester, mode: ChatCompressionMode.smaz);
expect(find.text(_l10n.chatCompression_title), findsWidgets);
expect(find.text(_l10n.chatCompression_off), findsOneWidget);
expect(find.text(_l10n.chatCompression_smaz), findsOneWidget);
expect(find.text(_l10n.chatCompression_cyr2lat), findsOneWidget);
expect(find.text(_l10n.chatCompression_offDescription), findsOneWidget);
expect(find.text(_l10n.chatCompression_smazDescription), findsOneWidget);
expect(
find.text(_l10n.chatCompression_cyr2latDescription),
findsOneWidget,
);
expect(isSelected(_l10n.chatCompression_smaz), isTrue);
expect(isSelected(_l10n.chatCompression_off), isFalse);
expect(find.text(_l10n.chatCompression_profile), findsNothing);
for (final mode in [
_l10n.chatCompression_off,
_l10n.chatCompression_smaz,
_l10n.chatCompression_cyr2lat,
]) {
expect(
tester.getSize(optionRow(mode)).height,
greaterThanOrEqualTo(48),
reason: mode,
);
}
},
);
testWidgets('a choice shows only after it is saved', (tester) async {
await pumpPage(tester);
await tester.tap(find.text(_l10n.chatCompression_smaz));
await tester.pump();
expect(host.modeCalls, [ChatCompressionMode.smaz]);
// Still saving: old mode shown, options locked against rapid taps.
expect(isSelected(_l10n.chatCompression_off), isTrue);
expect(
tester.widget<ListTile>(optionRow(_l10n.chatCompression_cyr2lat)).enabled,
isFalse,
);
await tester.tap(find.text(_l10n.chatCompression_cyr2lat));
await tester.pump();
expect(host.modeCalls, [ChatCompressionMode.smaz]);
host.pending.single.complete();
await tester.pumpAndSettle();
expect(isSelected(_l10n.chatCompression_smaz), isTrue);
expect(isSelected(_l10n.chatCompression_off), isFalse);
expect(
tester.widget<ListTile>(optionRow(_l10n.chatCompression_cyr2lat)).enabled,
isTrue,
);
});
testWidgets('Cyr2Lat reveals the profile picker', (tester) async {
await pumpPage(tester);
await tester.tap(find.text(_l10n.chatCompression_cyr2lat));
await tester.pump();
host.pending.removeAt(0).complete();
await tester.pumpAndSettle();
expect(isSelected(_l10n.chatCompression_cyr2lat), isTrue);
expect(find.text(_l10n.chatCompression_profile), findsWidgets);
// No profile saved for the chat: the app-wide one is what sending uses.
expect(find.text('Russian'), findsOneWidget);
await tester.tap(find.text('Russian'));
await tester.pumpAndSettle();
await tester.tap(find.text('Serbian').last);
await tester.pump();
expect(host.profileCalls, ['sr']);
host.pending.removeAt(0).complete();
await tester.pumpAndSettle();
expect(find.text('Serbian'), findsOneWidget);
});
testWidgets('a failed save keeps the saved mode and says so', (tester) async {
await pumpPage(tester, mode: ChatCompressionMode.smaz);
await tester.tap(find.text(_l10n.chatCompression_cyr2lat));
await tester.pump();
host.pending.single.completeError(StateError('disk full'));
await tester.pumpAndSettle();
expect(find.text(_l10n.chatCompression_saveFailed), findsOneWidget);
expect(isSelected(_l10n.chatCompression_smaz), isTrue);
expect(isSelected(_l10n.chatCompression_cyr2lat), isFalse);
expect(
tester.widget<ListTile>(optionRow(_l10n.chatCompression_off)).enabled,
isTrue,
);
});
testWidgets('a failed profile save shows the saved profile again', (
tester,
) async {
await pumpPage(tester, mode: ChatCompressionMode.cyr2lat, profileId: 'ru');
await tester.tap(find.text('Russian'));
await tester.pumpAndSettle();
await tester.tap(find.text('Serbian').last);
await tester.pump();
expect(host.profileCalls, ['sr']);
host.pending.single.completeError(StateError('disk full'));
await tester.pumpAndSettle();
expect(find.text(_l10n.chatCompression_saveFailed), findsOneWidget);
await tester.scrollUntilVisible(find.text('Russian'), 100);
expect(find.text('Russian'), findsOneWidget);
expect(find.text('Serbian'), findsNothing);
});
testWidgets('disabled shows the saved mode, says why and allows no change', (
tester,
) async {
await pumpPage(tester, mode: ChatCompressionMode.smaz, enabled: false);
expect(find.text(_l10n.chatCompression_noStorageId), findsOneWidget);
expect(isSelected(_l10n.chatCompression_smaz), isTrue);
for (final mode in [
_l10n.chatCompression_off,
_l10n.chatCompression_smaz,
_l10n.chatCompression_cyr2lat,
]) {
expect(tester.widget<ListTile>(optionRow(mode)).enabled, isFalse);
}
await tester.tap(find.text(_l10n.chatCompression_off), warnIfMissed: false);
await tester.pumpAndSettle();
expect(host.modeCalls, isEmpty);
});
for (final scale in [1.3, 2.0]) {
testWidgets('fits a 320dp phone at ${scale}x text', (tester) async {
await pumpPage(
tester,
mode: ChatCompressionMode.cyr2lat,
size: const Size(320, 568),
textScale: scale,
);
expect(tester.takeException(), isNull);
// The page scrolls to its last option and the profile picker.
await tester.scrollUntilVisible(
find.byType(DropdownButtonFormField<String>),
200,
scrollable: find.byType(Scrollable).first,
);
expect(tester.takeException(), isNull);
});
}
}
+31 -26
View File
@@ -42,9 +42,8 @@ import 'package:meshcore_open/storage/last_radio_store.dart';
import 'package:meshcore_open/storage/message_store.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:meshcore_open/theme/mesh_theme.dart';
import 'package:meshcore_open/widgets/chat_background.dart';
import 'package:meshcore_open/widgets/chat_composer.dart';
import 'package:meshcore_open/widgets/chat_compression_selector.dart';
import 'package:meshcore_open/screens/chat_compression_screen.dart';
import 'package:meshcore_open/widgets/connect_sheet.dart';
final _radioKey = Uint8List.fromList(List<int>.generate(32, (i) => 0xA0 + i));
@@ -349,10 +348,18 @@ void main() {
await tester.pump(const Duration(minutes: 1));
});
group('compression selector', () {
Future<void> choose(WidgetTester tester, String mode) async {
await tester.tap(find.byType(ChatCompressionSelector));
group('compression menu item', () {
Future<void> openMenu(WidgetTester tester) async {
await tester.tap(find.byIcon(Icons.more_vert));
await _settle(tester);
}
/// Opens the page from the overflow menu, picks [mode], and returns.
Future<void> choose(WidgetTester tester, String mode) async {
await openMenu(tester);
await tester.tap(find.textContaining('${_l10n.chatCompression_title}:'));
await _settle(tester);
expect(find.byType(ChatCompressionScreen), findsOneWidget);
await tester.tap(find.text(mode));
await _settle(tester);
// Connector notifications are debounced on a real timer.
@@ -360,9 +367,11 @@ void main() {
() => Future<void>.delayed(const Duration(milliseconds: 200)),
);
await _settle(tester);
await tester.pageBack();
await _settle(tester);
}
testWidgets('DM chat shows it at the top and saves offline', (
testWidgets('DM chat shows it in the menu and saves offline', (
tester,
) async {
final connector = await _offlineConnector(tester);
@@ -373,32 +382,27 @@ void main() {
);
await _settle(tester);
expect(find.byType(ChatCompressionSelector), findsOneWidget);
// No strip above the transcript any more.
expect(find.byType(ChatCompressionScreen), findsNothing);
expect(find.text(_l10n.chatCompression_label('Off')), findsNothing);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('Off')), findsOneWidget);
// Above the transcript, outside its scroll view.
expect(
find.ancestor(
of: find.byType(ChatCompressionSelector),
matching: find.byType(Scrollable),
),
findsNothing,
);
expect(
tester.getBottomLeft(find.byType(ChatCompressionSelector)).dy,
lessThanOrEqualTo(tester.getTopLeft(find.byType(ChatBackdrop)).dy),
);
expect(find.byIcon(Icons.compress), findsOneWidget);
await tester.tapAt(const Offset(1, 1));
await _settle(tester);
await choose(tester, _l10n.chatCompression_smaz);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('SMAZ')), findsOneWidget);
await tester.tapAt(const Offset(1, 1));
await _settle(tester);
expect(
await tester.runAsync(() => store.loadSmazEnabled(alice.publicKeyHex)),
isTrue,
);
await choose(tester, _l10n.chatCompression_cyr2lat);
// Cyr2Lat keeps the sheet open for its profile picker.
await tester.tap(find.byTooltip('Close'));
await _settle(tester);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('Cyr2Lat')), findsOneWidget);
expect(
connector.contactCompressionMode(alice.publicKeyHex),
@@ -416,8 +420,6 @@ void main() {
);
// The settings dialog no longer carries the compression switches.
await tester.tap(find.byIcon(Icons.more_vert));
await _settle(tester);
await tester.tap(find.text(_l10n.contact_settings).last);
await _settle(tester);
expect(find.byType(AlertDialog), findsOneWidget);
@@ -431,7 +433,7 @@ void main() {
await tester.pumpWidget(const SizedBox());
});
testWidgets('channel chat uses the same selector and saves offline', (
testWidgets('channel chat uses the same page and saves offline', (
tester,
) async {
final connector = await _offlineConnector(tester);
@@ -442,10 +444,13 @@ void main() {
);
await _settle(tester);
expect(find.byType(ChatCompressionSelector), findsOneWidget);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('Off')), findsOneWidget);
await tester.tapAt(const Offset(1, 1));
await _settle(tester);
await choose(tester, _l10n.chatCompression_smaz);
await openMenu(tester);
expect(find.text(_l10n.chatCompression_label('SMAZ')), findsOneWidget);
expect(
await tester.runAsync(() => store.loadSmazEnabled(channel.storageId!)),
+73
View File
@@ -37,6 +37,7 @@ final _l10n = AppLocalizationsEn();
MeshCoreConnector? _sharedConnector;
AppSettingsService? _sharedSettings;
final _frameLog = BleDebugLogService();
Future<MeshCoreConnector> _connectedConnector(WidgetTester tester) async {
if (_sharedConnector != null) return _sharedConnector!;
@@ -51,6 +52,7 @@ Future<MeshCoreConnector> _connectedConnector(WidgetTester tester) async {
pathHistoryService: PathHistoryService(storage),
appSettingsService: settings,
translationService: translation,
bleDebugLogService: _frameLog,
);
connector.reviewRadioFactoryForTest = () => ReviewRadio(
scheduledMessageInterval: const Duration(hours: 1),
@@ -338,6 +340,77 @@ void main() {
await tester.pumpWidget(const SizedBox());
});
testWidgets('Contact Settings sends the hop limit and reads it back', (
tester,
) async {
final connector = await _connectedConnector(tester);
final original = connector.autoAddMaxHops;
expect(original, isNotNull, reason: 'review radio reports the hop limit');
await tester.pumpWidget(
_app(connector, _launcher(const ContactSettingsScreen())),
);
await _open(tester);
String option(int hops) => _l10n.contactsSettings_hopLimitOption(hops);
expect(find.text(option(original!)), findsOneWidget);
Future<void> setHops(int hops) async {
final field = find.byType(DropdownButtonFormField<int>);
await tester.ensureVisible(field);
await tester.pumpAndSettle();
await tester.tap(field);
await tester.pumpAndSettle();
// The open menu builds only the rows around the current value.
final item = find.text(option(hops));
await tester.scrollUntilVisible(
item,
hops < connector.autoAddMaxHops! ? -48 : 48,
scrollable: find.byType(Scrollable).last,
);
await tester.pumpAndSettle();
await tester.tap(item.last);
await tester.pumpAndSettle();
await tester.tap(_saveAction);
await _waitFor(tester, () => connector.autoAddMaxHops == hops);
}
// The last frame of [code] the app sent to the radio.
List<int> lastSent(int code) => _frameLog.entries
.lastWhere((e) => e.outgoing && e.payload.first == code)
.payload;
int sentCount(int code) => _frameLog.entries
.where((e) => e.outgoing && e.payload.first == code)
.length;
final fetchesBefore = sentCount(cmdGetAutoAddConfig);
await setHops(8);
expect(connector.autoAddMaxHops, 8);
expect(find.text(option(8)), findsOneWidget);
expect(_saveAction, findsNothing);
// [CMD_SET_AUTOADD_CONFIG][flags][max hops], then a fetch to read it back.
expect(lastSent(cmdSetAutoAddConfig), hasLength(3));
expect(lastSent(cmdSetAutoAddConfig).last, 8);
expect(sentCount(cmdGetAutoAddConfig), fetchesBefore + 1);
// Changing only another setting sends the old two-byte frame.
final overwrite = find.text(_l10n.contactsSettings_overwriteOldestTitle);
for (var i = 0; i < 2; i++) {
await tester.ensureVisible(overwrite);
await tester.pumpAndSettle();
await tester.tap(overwrite);
await tester.pump();
await tester.tap(_saveAction);
await _waitFor(tester, () => _saveAction.evaluate().isEmpty);
expect(lastSent(cmdSetAutoAddConfig), hasLength(2));
}
expect(connector.autoAddMaxHops, 8);
await setHops(original);
expect(connector.autoAddMaxHops, original);
expect(lastSent(cmdSetAutoAddConfig), hasLength(3));
expect(lastSent(cmdSetAutoAddConfig).last, original);
await tester.pumpWidget(const SizedBox());
});
testWidgets('Node Name counts bytes and saves to the radio', (tester) async {
final connector = await _connectedConnector(tester);
final originalName = connector.selfName!;