import 'dart:async'; 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/discovery/discovered_server.dart'; import 'package:pipelines_app/features/onboarding/discovery/server_discovery.dart'; import 'package:pipelines_app/features/onboarding/onboarding_flow.dart'; /// A discoverer the test drives: each scan is a controller the test feeds. class FakeDiscovery implements ServerDiscovery { final scans = >>[]; int cancelled = 0; StreamController> get current => scans.last; @override Stream> scan() { final controller = StreamController>( onCancel: () => cancelled++, ); scans.add(controller); return controller.stream; } } const box = DiscoveredServer(name: 'box', host: '192.168.1.20', port: 8083); const studio = DiscoveredServer( name: 'studio', host: '192.168.1.30', port: 8080, passwordRequired: false, ); Widget host(Widget child) => MaterialApp(home: child); void main() { late FakeDiscovery discovery; late List checked; late List saved; setUp(() { discovery = FakeDiscovery(); checked = []; saved = []; }); OnboardingFlow flow({ServerDiscovery? discoverer, ValueChanged? done}) => OnboardingFlow( discovery: discoverer, check: (uri) async { checked.add(uri); return SessionState.signedOut; }, saveServer: (url) async => saved.add(url), onDone: done ?? (_) {}, ); testWidgets('no discoverer (web) shows manual entry only', (tester) async { await tester.pumpWidget(host(flow())); expect(find.byKey(const Key('onboarding-server')), findsOneWidget); expect(find.byKey(const Key('discovery-scan')), findsNothing); }); testWidgets('scanning lists servers as they answer, then settles', ( tester, ) async { await tester.pumpWidget(host(flow(discoverer: discovery))); expect(find.text('Find servers on this network'), findsOneWidget); await tester.tap(find.byKey(const Key('discovery-scan'))); await tester.pump(); expect(find.byKey(const Key('discovery-scanning')), findsOneWidget); discovery.current.add([box]); await tester.pump(); expect(find.text('box'), findsOneWidget); expect(find.text('192.168.1.20:8083 · password'), findsOneWidget); discovery.current.add([box, studio]); await tester.pump(); expect(find.text('studio'), findsOneWidget); expect(find.text('192.168.1.30:8080'), findsOneWidget); await tester.pump(const Duration(seconds: 8)); expect(find.byKey(const Key('discovery-scanning')), findsNothing); expect(discovery.cancelled, 1); expect(find.text('box'), findsOneWidget); expect(find.text('Search again'), findsOneWidget); }); testWidgets('tapping a server reviews its name before connecting', ( tester, ) async { await tester.pumpWidget(host(flow(discoverer: discovery))); await tester.tap(find.byKey(const Key('discovery-scan'))); await tester.pump(); discovery.current.add([box]); await tester.pump(); await tester.tap(find.byKey(const Key('discovered-192.168.1.20:8083'))); await tester.pumpAndSettle(); expect(checked, isEmpty); await tester.ensureVisible(find.byKey(const Key('onboarding-next'))); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pumpAndSettle(); final endpoint = Uri.parse('ws://192.168.1.20:8083/connect'); expect(checked, [endpoint]); expect(saved, [endpoint.toString()]); // The server wants its password: the flow moved to sign-in. expect(find.byKey(const Key('onboarding-password')), findsOneWidget); expect(find.byKey(const Key('discovery-scan')), findsNothing); }); testWidgets('an empty scan says so and keeps manual entry', (tester) async { await tester.pumpWidget(host(flow(discoverer: discovery))); await tester.tap(find.byKey(const Key('discovery-scan'))); await tester.pump(); discovery.current.add(const []); await tester.pump(const Duration(seconds: 8)); expect(find.byKey(const Key('discovery-empty')), findsOneWidget); expect(find.byKey(const Key('onboarding-server')), findsOneWidget); await tester.tap(find.byKey(const Key('discovery-scan'))); await tester.pump(); expect(discovery.scans, hasLength(2)); expect(find.byKey(const Key('discovery-empty')), findsNothing); expect(find.byKey(const Key('discovery-scanning')), findsOneWidget); await tester.pump(const Duration(seconds: 8)); }); testWidgets('a browse failure is shown and the scan can be retried', ( tester, ) async { await tester.pumpWidget(host(flow(discoverer: discovery))); await tester.tap(find.byKey(const Key('discovery-scan'))); await tester.pump(); discovery.current.addError(StateError('multicast blocked')); await tester.pump(); expect(find.byKey(const Key('discovery-error')), findsOneWidget); expect(find.textContaining('multicast blocked'), findsOneWidget); expect(find.byKey(const Key('discovery-scanning')), findsNothing); // The timer was cancelled with the scan; nothing fires later. await tester.pump(const Duration(seconds: 8)); expect(find.byKey(const Key('discovery-error')), findsOneWidget); }); testWidgets('leaving the screen mid-scan stops browsing', (tester) async { await tester.pumpWidget(host(flow(discoverer: discovery))); await tester.tap(find.byKey(const Key('discovery-scan'))); await tester.pump(); await tester.pumpWidget(host(const SizedBox())); expect(discovery.cancelled, 1); }); testWidgets('a machine with several routes lets the operator choose them', ( tester, ) async { final lan = Uri.parse('ws://10.0.0.75:8083/connect'); final ts = Uri.parse('ws://100.85.25.33:8083/connect'); final name = Uri.parse('wss://mac.tail1.ts.net:8443/connect'); final mac = DiscoveredServer( name: 'mac', host: '10.0.0.75', port: 8083, addresses: [ DiscoveredAddress(route: DiscoveredRoute.lan, endpoint: lan), DiscoveredAddress(route: DiscoveredRoute.tailscale, endpoint: ts), DiscoveredAddress(route: DiscoveredRoute.public, endpoint: name), ], ); final machines = <(List, String)>[]; await tester.pumpWidget( host( OnboardingFlow( discovery: discovery, check: (uri) async { checked.add(uri); return SessionState.signedOut; }, saveMachine: (endpoints, name) async => machines.add((endpoints, name)), onDone: (_) {}, ), ), ); await tester.tap(find.byKey(const Key('discovery-scan'))); await tester.pump(); discovery.current.add([mac]); await tester.pump(); expect(find.text('3 addresses · password'), findsOneWidget); // Tapping a multi-route machine expands its routes instead of choosing one. await tester.tap(find.byKey(const Key('discovered-10.0.0.75:8083'))); await tester.pumpAndSettle(); expect(find.text('Local network'), findsOneWidget); expect(find.text('Tailscale IP'), findsOneWidget); expect(find.text('Tailscale name (HTTPS)'), findsOneWidget); expect(find.text(name.toString()), findsOneWidget); await tester.ensureVisible(find.byKey(Key('discovered-route-$name'))); await tester.tap(find.byKey(Key('discovered-route-$name'))); await tester.pump(); await tester.ensureVisible(find.byKey(const Key('discovered-use-10.0.0.75:8083'))); await tester.tap(find.byKey(const Key('discovered-use-10.0.0.75:8083'))); await tester.pumpAndSettle(); expect( tester.widget(find.byKey(const Key('onboarding-server'))).controller!.text, lan.toString(), ); expect(find.byKey(const Key('onboarding-routes')), findsOneWidget); await tester.ensureVisible(find.byKey(const Key('onboarding-next'))); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pumpAndSettle(); expect(checked, [lan]); expect(machines.single.$1, [lan.toString(), name.toString()]); expect(machines.single.$2, 'mac'); expect(find.byKey(const Key('onboarding-password')), findsOneWidget); }); testWidgets('a typed address drops the discovered routes', (tester) async { final lan = Uri.parse('ws://10.0.0.75:8083/connect'); final ts = Uri.parse('ws://100.85.25.33:8083/connect'); final mac = DiscoveredServer( name: 'mac', host: '10.0.0.75', port: 8083, addresses: [ DiscoveredAddress(route: DiscoveredRoute.lan, endpoint: lan), DiscoveredAddress(route: DiscoveredRoute.tailscale, endpoint: ts), ], ); final machines = <(List, String)>[]; await tester.pumpWidget( host( OnboardingFlow( discovery: discovery, check: (uri) async => SessionState.signedOut, saveMachine: (endpoints, name) async => machines.add((endpoints, name)), onDone: (_) {}, ), ), ); await tester.tap(find.byKey(const Key('discovery-scan'))); await tester.pump(); discovery.current.add([mac]); await tester.pump(); await tester.tap(find.byKey(const Key('discovered-10.0.0.75:8083'))); await tester.pumpAndSettle(); await tester.ensureVisible(find.byKey(const Key('discovered-use-10.0.0.75:8083'))); await tester.tap(find.byKey(const Key('discovered-use-10.0.0.75:8083'))); await tester.pumpAndSettle(); await tester.enterText( find.byKey(const Key('onboarding-server')), 'ws://10.0.0.200:8083/connect', ); await tester.ensureVisible(find.byKey(const Key('onboarding-next'))); await tester.tap(find.byKey(const Key('onboarding-next'))); await tester.pumpAndSettle(); expect(machines.single.$1, ['ws://10.0.0.200:8083/connect']); expect(machines.single.$2, 'mac'); }); }