add e2e testing (#167)

Reviewed-on: #167
This commit is contained in:
Henry Hiles 2026-11-10 18:29:02 -05:00 • committed by Henry Hiles
commit af99de75da
6 changed files with 1346 additions and 41 deletions

View file

@ -0,0 +1,47 @@
import "dart:async";
import "package:app_links_platform_interface/app_links_platform_interface.dart";
import "package:url_launcher_platform_interface/link.dart";
import "package:url_launcher_platform_interface/url_launcher_platform_interface.dart";
/// Delivers links to the app as if the OS had opened them.
class FakeAppLinks extends AppLinksPlatform {
final _links = StreamController<Uri>.broadcast();
void open(Uri uri) => _links.add(uri);
@override
Future<Uri?> getInitialLink() async => null;
@override
Future<String?> getInitialLinkString() async => null;
@override
Future<Uri?> getLatestLink() async => null;
@override
Future<String?> getLatestLinkString() async => null;
@override
Stream<Uri> get uriLinkStream => _links.stream;
@override
Stream<String> get stringLinkStream =>
_links.stream.map((uri) => uri.toString());
}
/// Hands launched URLs to [onLaunch] instead of opening a browser.
class FakeUrlLauncher(final void Function(Uri url) onLaunch)
extends UrlLauncherPlatform {
@override
LinkDelegate? get linkDelegate => null;
@override
Future<bool> canLaunch(String url) async => true;
@override
Future<bool> launchUrl(String url, LaunchOptions options) async {
onLaunch(.parse(url));
return true;
}
}

View file

@ -0,0 +1,174 @@
import "dart:convert";
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:http/http.dart" as http;
const homeserver = String.fromEnvironment("NEXUS_E2E_HOMESERVER");
/// A user acting over the client-server API directly, as a control for the
/// tests: it doesn't go through gomuks or any other Nexus code, so it sets
/// things up and checks the server's side independently of what's being
/// tested. That includes Nexus' models, so the JSON it sends is written out
/// here rather than built the way Nexus builds it.
class const MatrixUser(final String userId, final String accessToken) {
static Future<MatrixUser> login(String username, String password) async {
final json = await request(
"POST",
"/_matrix/client/v3/login",
body: {
"type": "m.login.password",
"identifier": {"type": "m.id.user", "user": username},
"password": password,
},
);
return MatrixUser(json["user_id"], json["access_token"]);
}
static Future<Map<String, dynamic>> request(
String method,
String path, {
String? accessToken,
Object? body,
}) async {
final httpRequest =
http.Request(method, Uri.parse(homeserver).resolve(path))
..headers.addAll({
"Content-Type": "application/json",
if (accessToken != null) "Authorization": "Bearer $accessToken",
});
if (method != "GET") httpRequest.body = jsonEncode(body ?? {});
final response = await http.Response.fromStream(await httpRequest.send());
if (response.statusCode == 429) {
final retryAfter = jsonDecode(response.body)["retry_after_ms"] ?? 1000;
await Future.delayed(.new(milliseconds: retryAfter));
return request(method, path, accessToken: accessToken, body: body);
}
if (response.statusCode >= 300) {
throw StateError(
"$method $path failed with ${response.statusCode}: ${response.body}",
);
}
return jsonDecode(response.body);
}
Future<String> createRoom(String name) async => (await request(
"POST",
"/_matrix/client/v3/createRoom",
accessToken: accessToken,
body: {"name": name, "preset": "private_chat"},
))["room_id"];
Future<void> invite(String roomId, String userId) => request(
"POST",
"/_matrix/client/v3/rooms/${Uri.encodeComponent(roomId)}/invite",
accessToken: accessToken,
body: {"user_id": userId},
);
Future<void> join(String roomId) => request(
"POST",
"/_matrix/client/v3/join/${Uri.encodeComponent(roomId)}",
accessToken: accessToken,
);
Future<void> logout() =>
request("POST", "/_matrix/client/v3/logout", accessToken: accessToken);
Future<String> sendEvent(
String roomId,
String type,
Map<String, Object> content,
) async => (await request(
"PUT",
"/_matrix/client/v3/rooms/${Uri.encodeComponent(roomId)}/send/$type/"
"${DateTime.now().microsecondsSinceEpoch}",
accessToken: accessToken,
body: content,
))["event_id"];
/// Sends a text message. With [threadRoot] it's a reply in that thread, and
/// otherwise with [replyTo] it's a reply to that event.
Future<String> send(
String roomId,
String body, {
String? threadRoot,
String? replyTo,
}) => sendEvent(roomId, "m.room.message", {
"msgtype": "m.text",
"body": body,
"m.relates_to": ?switch ((threadRoot, replyTo)) {
(final threadRoot?, _) => {
"rel_type": "m.thread",
"event_id": threadRoot,
"is_falling_back": true,
"m.in_reply_to": {"event_id": threadRoot},
},
(null, final replyTo?) => {
"m.in_reply_to": {"event_id": replyTo},
},
(null, null) => null,
},
});
/// Sends [rootBody], then [replies] in a thread from it, oldest first.
Future<({String rootId, IList<String> replyIds})> sendThread(
String roomId,
String rootBody,
IList<String> replies,
) async {
final rootId = await send(roomId, rootBody);
// One at a time, so they're in order (oops earlier tests weren't reproducable)
final replyIds = await Stream.fromIterable(replies)
.asyncMap((body) => send(roomId, body, threadRoot: rootId))
.toList();
return (rootId: rootId, replyIds: replyIds.toIList());
}
/// Edits the text message [eventId] to say [body].
Future<String> edit(String roomId, String eventId, String body) =>
sendEvent(roomId, "m.room.message", {
"msgtype": "m.text",
"body": "* $body",
"m.new_content": {"msgtype": "m.text", "body": body},
"m.relates_to": {"rel_type": "m.replace", "event_id": eventId},
});
Future<void> redact(String roomId, String eventId) => request(
"PUT",
"/_matrix/client/v3/rooms/${Uri.encodeComponent(roomId)}/redact/"
"${Uri.encodeComponent(eventId)}/${DateTime.now().microsecondsSinceEpoch}",
accessToken: accessToken,
);
/// The bodies of the replies in the thread from [threadRoot], oldest first.
Future<IList<String>> threadReplies(String roomId, String threadRoot) async {
final json = await request(
"GET",
"/_matrix/client/v1/rooms/${Uri.encodeComponent(roomId)}/relations/"
"${Uri.encodeComponent(threadRoot)}/m.thread?dir=f&limit=100",
accessToken: accessToken,
);
return (json["chunk"] as List)
.map((event) => event["content"]["body"] as String)
.toIList();
}
Future<IList<String>> threadRepliesIncluding(
String roomId,
String threadRoot,
String body, {
int attempts = 20,
}) async {
final replies = await threadReplies(roomId, threadRoot);
if (replies.contains(body) || attempts <= 1) return replies;
await Future.delayed(const .new(milliseconds: 500));
return threadRepliesIncluding(
roomId,
threadRoot,
body,
attempts: attempts - 1,
);
}
}

View file

@ -0,0 +1,68 @@
import "package:http/http.dart" as http;
import "fakes.dart";
/// Fake platforms that do the browser's part of OAuth on Continuwuity, logging
/// in as [username], then hand the redirect back to Nexus.
class OAuthFakes(final String username, final String password) {
final links = FakeAppLinks();
late final launcher = FakeUrlLauncher(onLaunch);
Future<void> onLaunch(Uri url) async {
if (!url.queryParameters.containsKey("code_challenge")) return;
final redirect = await authorize(url);
// Nexus listens for the redirect after the launch returns
await Future.delayed(const .new(milliseconds: 500));
links.open(redirect);
}
/// Logs in on [authorizationUrl], then confirms the grant, returning the URI
/// Continuwuity redirects back to the app with.
Future<Uri> authorize(Uri authorizationUrl) async {
final client = http.Client();
String? cookie;
Future<Uri> followRedirect(
String method,
Uri url, [
Map<String, String>? form,
]) async {
final request = http.Request(method, url)
..followRedirects = false
..headers.addAll({
// Continuwuity only accepts its forms from top-level navigations
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Dest": "document",
"Cookie": ?cookie,
});
if (form != null) request.bodyFields = form;
final response = await http.Response.fromStream(
await client.send(request),
);
if (response.headers["set-cookie"] case final setCookie?) {
cookie = setCookie.split(";").first;
}
return switch ((response.statusCode, response.headers["location"])) {
(302 || 303 || 307, final location?) => url.resolve(location),
(final status, _) => throw StateError(
"Expected $method $url to redirect, got $status: ${response.body}",
),
};
}
try {
final loginUrl = await followRedirect("GET", authorizationUrl);
final grantUrl = await followRedirect("POST", loginUrl, {
"identifier": username,
"password": password,
});
return await followRedirect("POST", grantUrl, {"stage": "confirm"});
} finally {
client.close();
}
}
}

View file

@ -0,0 +1,56 @@
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:flutter_test/flutter_test.dart";
import "package:material_ui/material_ui.dart";
import "package:nexus/widgets/common/error_dialog.dart";
// Custom because `patrol_finder` gives me jQuery vibes, and that's Not GoodTM
extension PumpUntil on WidgetTester {
/// Pumps until [finder] finds something, failing if it doesn't within 30
/// seconds, or if the app shows an error.
Future<void> pumpUntil(Finder finder) async {
const timeout = Duration(seconds: 30);
String textsIn(Finder finder) => widgetList<Text>(
find.descendant(of: finder, matching: find.byType(Text)),
).map((text) => text.data).nonNulls.join("\n");
final stopwatch = Stopwatch()..start();
while (stopwatch.elapsed < timeout) {
await pump(const .new(milliseconds: 100));
if (finder.evaluate().isNotEmpty) return;
final errorDialog = find.byType(ErrorDialog);
if (errorDialog.evaluate().isNotEmpty) {
fail(
"The app showed an error while waiting for $finder:\n"
"${textsIn(errorDialog)}",
);
}
}
final snackBar = find.byType(SnackBar);
final shown = find
.byType(RichText)
.hitTestable()
.evaluate()
.map((element) => (element.widget as RichText).text.toPlainText())
.where((text) => text.trim().isNotEmpty)
.toIList()
.removeDuplicates()
.join(" | ");
fail(
"Timed out after $timeout waiting for $finder"
"${snackBar.evaluate().isEmpty ? "" : "\nSnack bar: ${textsIn(snackBar)}"}"
"\nShown: $shown",
);
}
/// Taps [finder] once it can be tapped, like after animating in.
Future<void> tapWhenShown(Finder finder) async {
final tappable = finder.hitTestable();
await pumpUntil(tappable);
await tap(tappable.first);
await pump();
}
}

View file

@ -0,0 +1,115 @@
import "package:app_links_platform_interface/app_links_platform_interface.dart";
import "package:flutter_test/flutter_test.dart";
import "package:material_ui/material_ui.dart";
import "package:media_kit/media_kit.dart";
import "package:nexus/controllers/active_account.dart";
import "package:nexus/main.dart" as nexus;
import "package:nexus/widgets/account_scope.dart";
import "package:nexus/widgets/pages/chat.dart";
import "package:nexus/widgets/pages/select_server.dart";
import "package:nexus/widgets/pages/verify.dart";
import "package:nexus/widgets/room_chat/room_chat.dart";
import "package:url_launcher_platform_interface/url_launcher_platform_interface.dart";
import "matrix_user.dart";
import "oauth.dart";
import "pump_until.dart";
/// Nexus, run as the account [username], which is set up through the UI the
/// first time it's opened.
class TestApp(final String username, final String password) {
late final fakes = OAuthFakes(username, password);
void setUp() {
nexus.isInBackground = false;
MediaKit.ensureInitialized();
AppLinksPlatform.instance = fakes.links;
UrlLauncherPlatform.instance = fakes.launcher;
// A tap that misses would otherwise only warn
WidgetController.hitTestWarningShouldBeFatal = true;
}
Future<void> join(MatrixUser inviter, String roomId) async {
final user = await MatrixUser.login(username, password);
await inviter.invite(roomId, user.userId);
await user.join(roomId);
await user.logout();
}
/// Starts Nexus like [open], then opens [roomName].
Future<void> openRoom(WidgetTester tester, String roomName) async {
await open(tester);
await selectRoom(tester, roomName);
}
/// Taps [roomName] in the room list, opening the drawer it's in first when
/// the window is narrow enough for the mobile layout.
Future<void> selectRoom(WidgetTester tester, String roomName) async {
// Shown once the first sync is done, as is the drawer button on mobile
await tester.pumpUntil(find.byType(RoomChat));
final drawerButton = find
.byTooltip(const DefaultMaterialLocalizations().openAppDrawerTooltip)
.hitTestable();
if (drawerButton.evaluate().isNotEmpty) await tester.tap(drawerButton);
await tester.tapWhenShown(find.text(roomName));
}
/// Starts Nexus, logged in and verified, logging in and setting up
/// encryption through the UI first if it isn't.
Future<void> open(WidgetTester tester) async {
await tester.pumpWidget(
AccountScope(
initialAccountId: await ActiveAccountController.stored(),
retry: (_, _) => null,
child: nexus.App(),
),
);
await tester.pumpUntil(
find.byWidgetPredicate(
(widget) =>
widget is SelectServerPage ||
widget is VerifyPage ||
widget is ChatPage,
),
);
if (find.byType(SelectServerPage).evaluate().isNotEmpty) {
await logIn(tester);
}
if (find.byType(VerifyPage).evaluate().isNotEmpty) {
await setUpEncryption(tester);
}
await tester.pumpUntil(find.byType(ChatPage));
}
Future<void> logIn(WidgetTester tester) async {
final homeserverField = find.widgetWithText(TextField, "Homeserver URL");
await tester.pumpUntil(homeserverField);
await tester.enterText(homeserverField, homeserver);
await tester.testTextInput.receiveAction(.done);
// Discovery only uses HTTPS, which the test homeserver doesn't serve, so
// Nexus offers to log in with the URL as typed
await tester.tapWhenShown(find.text("Attempt log in anyways"));
// Nexus opens the authorization page, which fakes authorizes
await tester.pumpUntil(
find.byWidgetPredicate(
(widget) => widget is VerifyPage || widget is ChatPage,
),
);
}
/// Sets up encryption from the verify page, for an account without it.
Future<void> setUpEncryption(WidgetTester tester) async {
await tester.pumpUntil(find.text("Set Up Encryption"));
await tester.tapWhenShown(find.widgetWithText(TextButton, "Set Up"));
await tester.tapWhenShown(find.text("I've saved my recovery key"));
await tester.tapWhenShown(find.widgetWithText(TextButton, "Set Up"));
await tester.tapWhenShown(find.widgetWithText(TextButton, "Done"));
}
}

View file

@ -0,0 +1,34 @@
import "package:flutter_test/flutter_test.dart";
import "package:material_ui/material_ui.dart";
import "pump_until.dart";
extension MessageFinders on CommonFinders {
Finder message(String body) => textContaining(body, findRichText: true);
}
extension RelativeFinders on Finder {
Finder inside(Type type) =>
find.descendant(of: find.byType(type), matching: this);
/// The [InkWell] ancestor around what this finds
Finder get inkWell => find.ancestor(of: this, matching: find.byType(InkWell));
}
extension ReplyCounts on WidgetTester {
/// Waits for a reply count in widgets of [type], then checks it, so a wrong
/// count fails straight away with what was shown.
Future<void> expectReplyCount(Type type, int expected) async {
final repliesLabel = RegExp(r"^(\d+) repl(?:y|ies)$");
final labels = find.textContaining(repliesLabel).inside(type);
await pumpUntil(labels);
final shown = widgetList<Text>(labels)
.map((label) => label.data)
.nonNulls
.map(repliesLabel.firstMatch)
.nonNulls
.map((match) => int.parse(match[1]!));
expect(shown, everyElement(expected), reason: "Reply count in $type");
}
}