kirakira/test/recall_flow_test.dart

105 lines
3.6 KiB
Dart
Raw Normal View History

// Regression tests for the onboarding recall flows: restoring a seed and
// recalling the registered address must land the user in the inbox. The
// client's network operations are stubbed; what is under test is the UI and
// router flow itself.
import "dart:io";
import "package:flutter/material.dart";
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:flutter_test/flutter_test.dart";
import "package:hive_flutter/hive_flutter.dart";
import "package:smol_mail/data/providers/providers.dart";
import "package:smol_mail/presentation/app_widget.dart";
import "package:smol_mail/smol/client.dart";
import "package:smol_mail/smol/crypto.dart";
import "package:smol_mail/smol/proto.dart";
import "package:smol_mail/smol/store.dart";
/// A [SmolClient] whose recall completes instantly, so the test exercises
/// the flow rather than the network.
class StubClient extends SmolClient {
StubClient(super.store);
@override
Future<SmolAddress> recallAccount(String addressText) async {
final addr = parseAddress(addressText);
store.setAccount(addr);
return addr;
}
}
void main() {
// Hive box opening is real file IO and must happen outside testWidgets'
// fake-async zone — including the per-test stores.
late final SmolStore storeA, storeB;
setUpAll(() async {
TestWidgetsFlutterBinding.ensureInitialized();
final dir = await Directory.systemTemp.createTemp("smol-recall-flow");
Hive.init(dir.path);
storeA = await SmolStore.open(
stateBox: "recall-a-state", mailBox: "recall-a-mail");
storeB = await SmolStore.open(
stateBox: "recall-b-state", mailBox: "recall-b-mail");
});
Widget app(SmolStore store) => ProviderScope(
overrides: [
storeProvider.overrideWithValue(store),
clientProvider.overrideWithValue(StubClient(store)),
],
child: const AppWidget(),
);
testWidgets("restore with an address recalls and lands in the inbox",
(tester) async {
final store = storeA;
final seed = randomBytes(32);
await tester.pumpWidget(app(store));
await tester.pumpAndSettle();
expect(find.text("Create Identity"), findsOneWidget);
await tester.tap(find.text("Restore From Seed"));
await tester.pumpAndSettle();
final fields = find.byType(TextField);
expect(fields, findsNWidgets(2));
await tester.enterText(fields.at(0), hex(seed));
await tester.enterText(fields.at(1), "randogoth@smol.place");
await tester.tap(find.text("Restore"));
await tester.pumpAndSettle();
expect(find.text("Inbox"), findsOneWidget);
expect(store.seed(), seed);
expect(store.account()!.user, "randogoth");
});
testWidgets("restore without an address recalls from the register step",
(tester) async {
final store = storeB;
final seed = randomBytes(32);
await tester.pumpWidget(app(store));
await tester.pumpAndSettle();
await tester.tap(find.text("Restore From Seed"));
await tester.pumpAndSettle();
var fields = find.byType(TextField);
await tester.enterText(fields.at(0), hex(seed));
await tester.tap(find.text("Restore"));
await tester.pumpAndSettle();
// The register step: address, server key, optional invite token.
fields = find.byType(TextField);
expect(fields, findsNWidgets(3));
await tester.enterText(fields.at(0), "randogoth@smol.place");
await tester.enterText(fields.at(1), b32encode(randomBytes(32)));
await tester.tap(find.text("Already registered? Recall"));
await tester.pumpAndSettle();
expect(find.text("Inbox"), findsOneWidget);
expect(store.account()!.user, "randogoth");
});
}