import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/session/server_session.dart'; import 'package:pipelines_app/features/onboarding/onboarding_flow.dart'; void main() { testWidgets('sign-in uses a reachable backup within the selected server', ( tester, ) async { final primary = Uri.parse('wss://tail.example/connect'); final backup = Uri.parse('ws://192.168.1.2:8083/connect'); final checked = []; final signedIn = []; final saved = []; await tester.pumpWidget( MaterialApp( home: OnboardingFlow( initialServer: primary.toString(), serverAddresses: [primary, backup], check: (uri) async { checked.add(uri); return uri == primary ? SessionState.unreachable : SessionState.signedOut; }, signIn: (uri, _) async { signedIn.add(uri); return null; }, saveServer: (url) async => saved.add(url), onDone: (_) {}, ), ), ); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pumpAndSettle(); expect(checked, [primary, backup]); expect(saved, [backup.toString()]); await tester.enterText( find.byKey(const Key('onboarding-password')), 'fixture-password', ); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pump(); await tester.pump(); expect(signedIn, [backup]); }); testWidgets( 'typing a different server does not try another profile backups', (tester) async { final primary = Uri.parse('wss://tail.example/connect'); final checked = []; await tester.pumpWidget( MaterialApp( home: OnboardingFlow( initialServer: primary.toString(), serverAddresses: [primary, Uri.parse('ws://192.168.1.2/connect')], check: (uri) async { checked.add(uri); return SessionState.unreachable; }, onDone: (_) {}, ), ), ); await tester.enterText( find.byKey(const Key('onboarding-server')), 'wss://different.example/connect', ); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pumpAndSettle(); expect(checked, [Uri.parse('wss://different.example/connect')]); }, ); testWidgets('server, then password; both kept, then the app opens', ( tester, ) async { final saved = []; final signIns = <(Uri, String)>[]; Uri? done; await tester.pumpWidget( MaterialApp( home: OnboardingFlow( initialServer: 'wss://build.example/connect', check: (_) async => SessionState.signedOut, signIn: (endpoint, password) async { signIns.add((endpoint, password)); return password == 'right-password' ? null : 'Wrong password.'; }, saveServer: (url) async => saved.add(url), onDone: (endpoint) => done = endpoint, ), ), ); // The build's server is pre-filled; another one can be typed. expect(find.text('wss://build.example/connect'), findsOneWidget); await tester.enterText( find.byKey(const Key('onboarding-server')), 'wss://home.example/connect', ); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pumpAndSettle(); expect(saved, ['wss://home.example/connect']); expect(find.byKey(const Key('onboarding-password')), findsOneWidget); await tester.enterText( find.byKey(const Key('onboarding-password')), 'nope-nope', ); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pumpAndSettle(); expect(find.text('Wrong password.'), findsOneWidget); expect(done, isNull); await tester.enterText( find.byKey(const Key('onboarding-password')), 'right-password', ); await tester.tap(find.byKey(const Key('onboarding-next'))); // Signed in: the app replaces this screen (the spinner stays until then). await tester.pump(); await tester.pump(); expect(done, Uri.parse('wss://home.example/connect')); expect(signIns.last.$1, Uri.parse('wss://home.example/connect')); }); testWidgets('an unreachable server is explained and nothing is kept', ( tester, ) async { final saved = []; await tester.pumpWidget( MaterialApp( home: OnboardingFlow( check: (_) async => SessionState.unreachable, signIn: (_, _) async => null, saveServer: (url) async => saved.add(url), onDone: (_) {}, ), ), ); await tester.enterText( find.byKey(const Key('onboarding-server')), 'wss://nowhere.example/connect', ); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('onboarding-error')), findsOneWidget); expect(find.byKey(const Key('onboarding-password')), findsNothing); expect(saved, isEmpty); }); testWidgets('a refused session asks only for the password again', ( tester, ) async { await tester.pumpWidget( MaterialApp( home: OnboardingFlow( initialServer: 'wss://home.example/connect', startAtPassword: true, check: (_) async => SessionState.signedOut, signIn: (_, _) async => null, onDone: (_) {}, ), ), ); expect(find.byKey(const Key('onboarding-password')), findsOneWidget); expect(find.byKey(const Key('onboarding-server')), findsNothing); expect(find.text('Change server'), findsNothing); }); testWidgets('an unreachable address shows the probe\'s own reason', ( tester, ) async { await tester.pumpWidget( MaterialApp( home: OnboardingFlow( probe: (uri) async => ServerProbeResult( SessionState.unreachable, reason: 'The HTTPS certificate of ${uri.host}:${uri.port} does not ' 'cover an IP address.', ), onDone: (_) {}, ), ), ); await tester.enterText( find.byKey(const Key('onboarding-server')), 'wss://100.85.25.33:8443/connect', ); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pumpAndSettle(); expect( find.text( 'The HTTPS certificate of 100.85.25.33:8443 does not cover an IP ' 'address.', ), findsOneWidget, ); expect(find.byKey(const Key('onboarding-password')), findsNothing); }); }