import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/settings/settings_search_page.dart'; import 'package:pipelines_app/features/settings/settings_section.dart'; import 'package:pipelines_app/features/settings/settings_server_load.dart'; import 'package:pipelines_app/features/settings/settings_server_view.dart'; /// Fills the page so a section placed after it starts far below the viewport. List _filler(int count) => [ for (var i = 0; i < count; i++) SettingsSection( id: 'filler-$i', title: 'Filler $i', builder: (_) => const SizedBox(height: 400, child: Text('filler body')), ), ]; Future _pump( WidgetTester tester, List sections, { Size size = const Size(800, 600), }) async { await tester.binding.setSurfaceSize(size); addTearDown(() => tester.binding.setSurfaceSize(null)); await tester.pumpWidget( MaterialApp( home: Scaffold(body: SettingsSearchPage(sections: sections)), ), ); await tester.pump(); await tester.pump(); } void main() { testWidgets('local settings render while a server section has not answered', ( tester, ) async { var fetches = 0; final reply = Completer(); final load = ServerLoad(() { fetches++; return reply.future; }); addTearDown(load.dispose); await _pump(tester, [ ..._filler(6), SettingsSection( id: 'server', title: 'Phone notifications', lazy: true, builder: (_) => ServerSettingsView( load: load, builder: (_, value) => Text('Server $value'), ), ), ]); // The local sections are built and the server section is only a skeleton: // no request runs until the list reaches it. expect(find.text('filler body'), findsWidgets); expect(find.text('Phone notifications'), findsOneWidget); expect(fetches, 0); expect(tester.takeException(), isNull); await tester.ensureVisible(find.text('Phone notifications')); await tester.pump(); await tester.pump(); expect(fetches, 1); // The reply is still pending, and the page keeps working. expect(find.bySemanticsLabel('Loading'), findsOneWidget); expect(find.text('filler body'), findsWidgets); reply.complete(3); await tester.pump(); expect(find.text('Server 3'), findsOneWidget); }); testWidgets('search filters sections by title and keywords', (tester) async { await _pump(tester, [ SettingsSection( id: 'animations', title: 'Animations', keywords: 'motion speed', builder: _titled('Animations'), ), SettingsSection( id: 'terminal', title: 'Terminal', keywords: 'layout split', builder: _titled('Terminal'), ), ]); expect(find.text('Animations'), findsOneWidget); expect(find.text('Terminal'), findsOneWidget); await tester.enterText( find.byKey(const ValueKey('settings.search')), 'motion', ); await tester.pump(); expect(find.text('Animations'), findsOneWidget); expect(find.text('Terminal'), findsNothing); // Every word must match: "motion layout" matches neither section. await tester.enterText( find.byKey(const ValueKey('settings.search')), 'motion layout', ); await tester.pump(); expect(find.text('Animations'), findsNothing); expect(find.text('Terminal'), findsNothing); expect(find.textContaining('No settings match'), findsOneWidget); await tester.tap(find.byTooltip('Clear search')); await tester.pump(); expect(find.text('Animations'), findsOneWidget); expect(find.text('Terminal'), findsOneWidget); }); testWidgets('a search that matches a server section loads it at once', ( tester, ) async { var fetches = 0; final load = ServerLoad(() async { fetches++; return 9; }); addTearDown(load.dispose); await _pump(tester, [ ..._filler(6), SettingsSection( id: 'server', title: 'Phone notifications', keywords: 'ntfy push', lazy: true, builder: (_) => ServerSettingsView( load: load, builder: (_, value) => Text('Server $value'), ), ), ]); expect(fetches, 0); await tester.enterText( find.byKey(const ValueKey('settings.search')), 'ntfy', ); await tester.pump(); await tester.pump(); await tester.pump(); expect(fetches, 1); expect(find.text('Server 9'), findsOneWidget); expect(find.text('filler body'), findsNothing); }); testWidgets( 'a failing server section shows its own Retry; others are unaffected', (tester) async { var attempts = 0; final load = ServerLoad(() async { attempts++; if (attempts == 1) throw StateError('server unreachable'); return 'Stored'; }); addTearDown(load.dispose); await _pump(tester, [ SettingsSection( id: 'local', title: 'Animations', builder: (_) => const Column(children: [Text('Animations'), Text('Local value')]), ), SettingsSection( id: 'server', title: 'Sign-in security', lazy: true, builder: (_) => ServerSettingsView( load: load, retryKey: const Key('server-retry'), builder: (_, value) => Text('Security $value'), ), ), ]); await tester.pump(); expect(find.textContaining('server unreachable'), findsOneWidget); expect(find.byKey(const Key('server-retry')), findsOneWidget); expect(find.text('Local value'), findsOneWidget); expect(find.text('Animations'), findsOneWidget); await tester.tap(find.byKey(const Key('server-retry'))); await tester.pump(); await tester.pump(); expect(attempts, 2); expect(find.text('Security Stored'), findsOneWidget); expect(find.byKey(const Key('server-retry')), findsNothing); expect(find.text('Local value'), findsOneWidget); }, ); testWidgets('wide layouts list every visible section in the jump index', ( tester, ) async { final sections = [ SettingsSection( id: 'animations', title: 'Animations', builder: _titled('Animations'), ), SettingsSection( id: 'terminal', title: 'Terminal', builder: _titled('Terminal'), ), ]; await _pump(tester, sections, size: const Size(1200, 800)); expect( find.byKey(const ValueKey('settings.index.animations')), findsOneWidget, ); expect( find.byKey(const ValueKey('settings.index.terminal')), findsOneWidget, ); await tester.tap(find.byKey(const ValueKey('settings.index.terminal'))); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); expect(tester.takeException(), isNull); }); testWidgets('a narrow layout has no jump index', (tester) async { await _pump(tester, [ SettingsSection( id: 'animations', title: 'Animations', builder: _titled('Animations'), ), ], size: const Size(390, 844)); expect( find.byKey(const ValueKey('settings.index.animations')), findsNothing, ); }); } WidgetBuilder _titled(String title) => (_) => Text(title);