test(#779): chat and channel screens against the fake radio

FakeRadioAppServices wires the app's services and providers as
main.dart does (minus map tiles, background service, notifications and
file logging, which no screen under test needs). The real ChatScreen
and ChannelChatScreen run attached in-process to the fake:
- a DM typed and sent reaches the radio, shows in the conversation, and
  shows acked (MessageStatusIcon.isAcked) once the ACK lands
- a channel message reaches the radio on its slot and shows

Two things learned and encoded: the retry service and screens schedule
on the widget test's fake clock while the database answers in real
time, so waits move both; and without tracing the bubble marks a
message acked only when it was delivered over a known path, so the test
contact has a learned route.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
feat/758-fake-radio-ci
diffwireauto 2 months ago
parent 76af695e87
commit 2d3304cff4

@ -0,0 +1,147 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/models/message.dart';
import 'package:meshcore_open/screens/channel_chat_screen.dart';
import 'package:meshcore_open/screens/chat_screen.dart';
import 'package:meshcore_open/widgets/message_status_icon.dart';
import '../support/fake_radio/fake_radio.dart';
import '../support/fake_radio/fake_radio_adapters.dart';
import '../support/fake_radio/fake_radio_app.dart';
import '../support/fake_radio/fake_radio_seed.dart';
// #779 (C2 of #755): the real chat screens, attached in-process to the fake
// radio. A composer send reaches the radio and its outcome shows.
void main() {
late FakeRadioAppServices services;
late FakeRadio radio;
late FakeRadioInProcess link;
Future<void> realWait(bool Function() done, String what) async {
final sw = Stopwatch()..start();
while (!done()) {
if (sw.elapsed > const Duration(seconds: 15)) fail('timed out: $what');
await Future<void>.delayed(const Duration(milliseconds: 10));
}
}
/// Waits for [done] while moving both clocks: the widget test's fake clock
/// (the retry service and screens schedule on it) and real time (the
/// in-memory database answers on it).
Future<void> settle(
WidgetTester tester,
bool Function() done,
String what,
) async {
for (var i = 0; i < 300 && !done(); i++) {
await tester.pump(const Duration(milliseconds: 100));
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 5)),
);
}
if (!done()) fail('timed out: $what');
await tester.pump();
}
Future<void> connect(WidgetTester tester) async {
await tester.runAsync(() async {
services = await FakeRadioAppServices.create();
radio = FakeRadio(
seed: FakeRadioSeed(
// A contact with a learned route: the screen marks a message as
// acked only when it was delivered over a known path.
contacts: [
FakeContact(
publicKey: Uint8List.fromList(List<int>.filled(32, 0x11)),
name: 'Alpha',
outPathLength: 1,
outPath: Uint8List.fromList([0x9A]),
),
],
channels: [FakeChannel(index: 0, name: 'Public')],
),
)..ackDelay = const Duration(seconds: 1);
link = await FakeRadioInProcess.connect(services.connector, radio);
await realWait(
() =>
services.connector.contacts.isNotEmpty &&
services.connector.channels.isNotEmpty,
'contacts and channels',
);
});
}
Future<void> teardown(WidgetTester tester) async {
await tester.runAsync(() async {
await link.close();
await services.dispose();
});
// Let screen timers (typing indicators, retry timeouts) run out.
await tester.pumpWidget(const SizedBox());
await tester.pump(const Duration(minutes: 5));
}
Future<void> typeAndSend(WidgetTester tester, String text) async {
await tester.enterText(find.byType(TextField).last, text);
await tester.pump();
await tester.tap(find.byIcon(Icons.send).last);
await tester.pump();
}
testWidgets('chat screen: a sent DM shows delivered when the ACK lands', (
tester,
) async {
await connect(tester);
final alpha = services.connector.contacts.single;
await tester.pumpWidget(services.app(ChatScreen(contact: alpha)));
await tester.pump();
await typeAndSend(tester, 'hello from the screen');
await settle(
tester,
() => radio.sentDirect.any((s) => s.text == 'hello from the screen'),
'the DM at the radio',
);
expect(find.text('hello from the screen'), findsOneWidget);
bool acked() => tester
.widgetList<MessageStatusIcon>(find.byType(MessageStatusIcon))
.any((i) => i.isAcked);
expect(acked(), isFalse);
// The recipient's ACK arrives.
radio.clock.advance(const Duration(seconds: 1));
await settle(tester, acked, 'the delivered mark');
expect(
services.connector.getMessages(alpha).single.status,
MessageStatus.delivered,
);
await teardown(tester);
});
testWidgets('channel screen: a sent message reaches the radio and shows', (
tester,
) async {
await connect(tester);
final channel = services.connector.channels.firstWhere(
(c) => c.name == 'Public',
);
await tester.pumpWidget(services.app(ChannelChatScreen(channel: channel)));
await tester.pump();
await typeAndSend(tester, 'hi channel');
await settle(
tester,
() => radio.sentChannel.any((s) => s.text == 'hi channel'),
'the channel message at the radio',
);
expect(radio.sentChannel.single.index, 0);
expect(find.textContaining('hi channel'), findsWidgets);
await teardown(tester);
});
}

@ -0,0 +1,104 @@
import 'package:drift/native.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/services/app_debug_log_service.dart';
import 'package:meshcore_open/services/app_settings_service.dart';
import 'package:meshcore_open/services/ble_debug_log_service.dart';
import 'package:meshcore_open/services/block_service.dart';
import 'package:meshcore_open/services/chat_text_scale_service.dart';
import 'package:meshcore_open/services/mesh_topology_service.dart';
import 'package:meshcore_open/services/message_retry_service.dart';
import 'package:meshcore_open/services/path_history_service.dart';
import 'package:meshcore_open/services/storage_service.dart';
import 'package:meshcore_open/services/timeout_prediction_service.dart';
import 'package:meshcore_open/services/translation_service.dart';
import 'package:meshcore_open/services/ui_view_state_service.dart';
import 'package:meshcore_open/storage/drift/blob_store.dart';
import 'package:meshcore_open/storage/drift/offband_database.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// The app's services, wired as `main.dart` wires them, for screen tests
/// against the fake radio (#779). Heavy native pieces (map tiles, background
/// service, notifications, file logging) are left out: no screen under test
/// needs them.
class FakeRadioAppServices {
FakeRadioAppServices._(this.db);
static Future<FakeRadioAppServices> create() async {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
final db = OffbandDatabase(NativeDatabase.memory());
BlobStore.overrideForTest(BlobStore(db));
final s = FakeRadioAppServices._(db);
await s.appSettings.loadSettings();
s.connector.initialize(
retryService: s.retry,
pathHistoryService: s.pathHistory,
topologyService: s.topology,
appSettingsService: s.appSettings,
translationService: s.translation,
bleDebugLogService: s.bleLog,
appDebugLogService: s.appLog,
timeoutPredictionService: s.timeouts,
blockService: s.blocks,
);
return s;
}
final OffbandDatabase db;
final StorageService storage = StorageService();
final MeshCoreConnector connector = MeshCoreConnector();
final MessageRetryService retry = MessageRetryService();
late final PathHistoryService pathHistory = PathHistoryService(storage);
final MeshTopologyService topology = MeshTopologyService();
final AppSettingsService appSettings = AppSettingsService();
final BleDebugLogService bleLog = BleDebugLogService();
final AppDebugLogService appLog = AppDebugLogService();
final ChatTextScaleService textScale = ChatTextScaleService();
late final TranslationService translation = TranslationService(appSettings);
final UiViewStateService viewState = UiViewStateService();
final TimeoutPredictionService timeouts =
TimeoutPredictionService.noStorage();
final BlockService blocks = BlockService();
/// [home] inside the app's providers, localizations and theme.
Widget app(Widget home) => MultiProvider(
providers: [
ChangeNotifierProvider.value(value: connector),
ChangeNotifierProvider.value(value: retry),
ChangeNotifierProvider.value(value: pathHistory),
ChangeNotifierProvider.value(value: topology),
ChangeNotifierProvider.value(value: appSettings),
ChangeNotifierProvider.value(value: bleLog),
ChangeNotifierProvider.value(value: appLog),
ChangeNotifierProvider.value(value: textScale),
ChangeNotifierProvider.value(value: translation),
ChangeNotifierProvider.value(value: viewState),
Provider.value(value: storage),
ChangeNotifierProvider.value(value: timeouts),
ChangeNotifierProvider.value(value: blocks),
],
child: MaterialApp(
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: AppLocalizations.supportedLocales,
locale: const Locale('en'),
home: home,
),
);
Future<void> dispose() async {
await connector.disconnect();
BlobStore.clearTestOverride();
await db.close();
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.