import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart' as transport; import 'package:pipelines_app/features/settings/settings_copy.dart'; import 'package:pipelines_app/features/settings/settings_keys.dart'; import 'package:pipelines_app/features/settings/settings_screen.dart'; import 'package:pipelines_app/features/settings/settings_store.dart'; import 'fake_channel.dart'; import 'settings_section_test_helper.dart'; void main() { testWidgets('renders idle settings without opening a connection', ( tester, ) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump( tester, client, store: MemorySettingsStore( serverUrl: 'ws://saved.test/connect', workspace: '/srv/pipelines', ), ); expect(find.text('Settings'), findsOneWidget); expect(find.text('Connection').last, findsOneWidget); expect(find.text('Workspace'), findsOneWidget); expect(find.text('Not connected'), findsOneWidget); expect(find.text('Saved workspace: /srv/pipelines'), findsOneWidget); expect( tester.widget(serverUrlField).controller?.text, 'ws://saved.test/connect', ); expect(find.byKey(SettingsKeys.connect), findsOneWidget); expect(find.byKey(SettingsKeys.reconnect), findsOneWidget); await tester.pump(); expect(factory.channels, isEmpty); }); testWidgets('long server URLs can wrap in a narrow settings viewport', ( tester, ) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); final client = transport.PipelinesClient( channelFactory: FakeChannelFactory().call, ); addTearDown(client.dispose); await _pump( tester, client, store: MemorySettingsStore( serverUrl: 'wss://a-very-long-server-hostname-that-exceeds-a-mobile-field.test:8123/connect', ), ); final field = tester.widget(serverUrlField); expect(field.maxLines, 2); expect(field.controller?.text, contains('/connect')); expect(tester.takeException(), isNull); }); testWidgets('a short server URL keeps a one-line field', (tester) async { final client = transport.PipelinesClient( channelFactory: FakeChannelFactory().call, ); addTearDown(client.dispose); await _pump( tester, client, store: MemorySettingsStore(serverUrl: 'ws://127.0.0.1:8080/connect'), ); final url = tester.widget(serverUrlField); expect(url.minLines, 1); // The URL field grows only when its text wraps: one line is the same // height as the single-line workspace field below it. Size editable(Finder field) => tester.getSize( find.descendant(of: field, matching: find.byType(EditableText)), ); expect(editable(serverUrlField).height, editable(workspaceField).height); }); testWidgets('an invalid URL shows the field error and opens no socket', ( tester, ) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client); await tester.enterText(serverUrlField, 'http://example.test'); await tester.pump(); await tester.tap(find.byKey(SettingsKeys.connect)); await tester.pump(); await tester.pump(); expect(find.textContaining('Unsupported scheme'), findsOneWidget); expect(factory.channels, isEmpty); }); testWidgets('connect pings once and shows the reported build and time', ( tester, ) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client); await tester.enterText(serverUrlField, 'ws://example.test:8123'); await tester.pump(); await tester.tap(find.byKey(SettingsKeys.connect)); await tester.pump(); await tester.pump(); expect(factory.channels, hasLength(1)); expect(factory.uris.single.toString(), 'ws://example.test:8123/connect'); expect(find.text('Connecting (attempt 1)\u2026'), findsOneWidget); factory.last.markReady(); await tester.pump(); await tester.pump(); final outgoing = factory.last.decodedOutgoing; expect(outgoing, hasLength(1)); expect(outgoing.single['type'], 'ping'); factory.last.reply({ 'id': outgoing.single['id'], 'type': 'pong', 'build': {'version': 'v9', 'commit': 'deadbee'}, 'time': '2026-10-21T12:00:00Z', }); await tester.pump(); await tester.pump(); expect(find.text('Connected'), findsOneWidget); expect(find.text('Server build v9 (deadbee)'), findsOneWidget); expect(find.text('Server time 2026-10-21T12:00:00Z'), findsOneWidget); expect(find.byKey(SettingsKeys.disconnect), findsOneWidget); expect(find.byKey(SettingsKeys.connect), findsOneWidget); // Beside Disconnect, the second action reconnects; it never says Connect. expect( find.descendant( of: find.byKey(SettingsKeys.connect), matching: find.text('Reconnect'), ), findsOneWidget, ); expect(find.text('Connect'), findsNothing); // Notification settings load once their lazy section is near the list: // this tall window reaches it on open, after the single ping. expect(factory.last.decodedOutgoing.map((message) => message['type']), [ 'ping', 'notification_settings', ]); await openSettingsSection(tester, 'Phone notifications'); _answerNtfySettings(factory.last); await tester.pump(); await tester.pump(); expect(factory.last.decodedOutgoing, hasLength(2)); }); testWidgets('connected without a ping reply names unavailable values', ( tester, ) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client); await tester.enterText(serverUrlField, 'ws://example.test:8123'); await tester.pump(); await tester.tap(find.byKey(SettingsKeys.connect)); await tester.pump(); await tester.pump(); factory.last.markReady(); await tester.pump(); await tester.pump(); expect(factory.last.decodedOutgoing.single['type'], 'ping'); expect(find.text('Connected'), findsOneWidget); expect(find.text(serverDetailsPending), findsOneWidget); expect(find.text('Server build unavailable'), findsNothing); factory.last.reply({ 'id': factory.last.decodedOutgoing.single['id'], 'type': 'pong', }); await tester.pump(); await tester.pump(); expect(find.text(serverDetailsPending), findsNothing); expect(find.text('Server build unavailable'), findsOneWidget); expect(find.text('Server time unavailable'), findsOneWidget); await openSettingsSection(tester, 'Phone notifications'); _answerNtfySettings(factory.last); await tester.pump(); }); testWidgets( 'workspace validation gates Save and only a save updates the line', (tester) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); final store = MemorySettingsStore(); await _pump(tester, client, store: store); final workspace = workspaceField; await tester.enterText(workspace, 'relative/path'); await tester.pump(); expect(find.textContaining('must be an absolute path'), findsOneWidget); expect( tester .widget(find.byKey(SettingsKeys.saveWorkspace)) .onPressed, isNull, ); expect(find.text('Saved workspace: (none)'), findsOneWidget); await tester.enterText(workspace, ' /srv/pipelines '); await tester.pump(); await tester.tap(find.byKey(SettingsKeys.saveWorkspace)); await tester.pump(); await tester.pump(); expect(find.text('Saved workspace: /srv/pipelines'), findsOneWidget); expect(store.workspace, '/srv/pipelines'); }, ); testWidgets('iOS uses the same Material fields and buttons', (tester) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client, platform: TargetPlatform.iOS); expect(find.byType(TextField), findsWidgets); expect(find.byKey(SettingsKeys.connect), findsOneWidget); }); testWidgets('narrow window with large text scrolls without overflow', ( tester, ) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client, textScale: 2, size: const Size(320, 640)); expect(tester.takeException(), isNull); await tester.scrollUntilVisible( find.byKey(SettingsKeys.saveWorkspace), 200, scrollable: find .descendant( of: find.byKey(const ValueKey('settings.scroll')), matching: find.byType(Scrollable), ) .first, ); expect(find.byKey(SettingsKeys.saveWorkspace), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('large text keeps the full labels and supporting copy', ( tester, ) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client, textScale: 2, size: const Size(320, 640)); expect(find.text('Workspace path (on the server)'), findsOneWidget); expect(find.text('Server URL'), findsOneWidget); for (final copy in [ 'Workspace path (on the server)', serverUrlHelper, workspaceHelper, ]) { final text = tester.widget(find.text(copy)); expect(text.maxLines, isNull, reason: 'no line cap on $copy'); final paragraph = tester.renderObject(find.text(copy)); expect( paragraph.didExceedMaxLines, isFalse, reason: 'the whole copy is laid out for $copy', ); } expect(tester.takeException(), isNull); }); testWidgets('the text fields expose their labels to accessibility', ( tester, ) async { final handle = tester.ensureSemantics(); final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client); final labels = [ tester.getSemantics(serverUrlField).getSemanticsData().label, tester.getSemantics(workspaceField).getSemanticsData().label, ]; expect(labels[0], contains('Server URL')); expect(labels[1], contains('Workspace path (on the server)')); handle.dispose(); }); testWidgets('Help and about keeps the keyboard shortcuts row', ( tester, ) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client); await openSettingsSection(tester, 'Help and about'); expect(find.text('Help and about'), findsOneWidget); expect(find.byKey(SettingsKeys.keyboardShortcuts), findsOneWidget); // Help is a plain section of the page, so the search reaches it too. await tester.enterText( find.byKey(const ValueKey('settings.search')), 'shortcuts', ); await tester.pump(); expect(find.byKey(SettingsKeys.keyboardShortcuts), findsOneWidget); expect(find.text('Server URL'), findsNothing); }); testWidgets('Help and about has a Show the walkthrough row', (tester) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); var shown = 0; await _pump(tester, client, onShowWalkthrough: () => shown++); await openSettingsSection(tester, 'Help and about'); final row = find.byKey(SettingsKeys.showWalkthrough); expect(find.text('Show the walkthrough'), findsOneWidget); expect(row, findsOneWidget); expect( tester .widget(find.descendant(of: row, matching: find.byType(ListTile))) .enabled, isTrue, ); await tester.tap(row); await tester.pump(); expect(shown, 1); // The search reaches the row as well. await tester.enterText( find.byKey(const ValueKey('settings.search')), 'walkthrough', ); await tester.pump(); expect(find.byKey(SettingsKeys.showWalkthrough), findsOneWidget); }); testWidgets('Show the walkthrough row is disabled without a handler', ( tester, ) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client); await openSettingsSection(tester, 'Help and about'); final row = find.byKey(SettingsKeys.showWalkthrough); expect(row, findsOneWidget); expect( tester .widget(find.descendant(of: row, matching: find.byType(ListTile))) .enabled, isFalse, ); }); testWidgets('unmounting leaves the shared client usable', (tester) async { final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); await _pump(tester, client); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); final connecting = client.connect(Uri.parse('ws://example.test:8123')); await tester.pump(); expect(factory.channels, hasLength(1)); factory.last.markReady(); await tester.pump(); await connecting; }); testWidgets('phone: the page scrolls to rest above the floating tab bar', ( tester, ) async { await tester.binding.setSurfaceSize(const Size(390, 844)); addTearDown(() => tester.binding.setSurfaceSize(null)); final client = transport.PipelinesClient( channelFactory: FakeChannelFactory().call, ); addTearDown(client.dispose); // The shell's shape: a body that extends behind a bottom bar, which the // body reads as its bottom padding. const barKey = ValueKey('test.tabBar'); await tester.pumpWidget( MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: Scaffold( extendBody: true, body: SettingsScreen( client: client, store: MemorySettingsStore(), embedded: true, onShowWalkthrough: () {}, ), bottomNavigationBar: const SizedBox( key: barKey, width: double.infinity, height: 80, ), ), ), ); await tester.pump(); await tester.pump(); final scroll = find.byKey(const ValueKey('settings.scroll')); // Each lazy section that the scroll reaches grows the list, so keep // scrolling until the page stops moving. for (var i = 0; i < 6; i++) { await tester.drag(scroll, const Offset(0, -10000)); await tester.pump(); await tester.pump(); } final barTop = tester.getTopLeft(find.byKey(barKey)).dy; // Nothing scrolls under the bar: the list's viewport ends at its top edge, // and the last row (the page's end) sits above it at rest. expect(tester.getRect(scroll).bottom, lessThanOrEqualTo(barTop + 0.5)); expect( tester.getRect(find.byKey(SettingsKeys.showWalkthrough)).bottom, lessThanOrEqualTo(barTop + 0.5), ); expect(tester.takeException(), isNull); }); } /// The Server URL and Workspace fields, addressed by their keys: the search /// field is the page's first text field. Finder get serverUrlField => find.descendant( of: find.byKey(SettingsKeys.serverUrl), matching: find.byType(TextField), ); Finder get workspaceField => find.descendant( of: find.byKey(SettingsKeys.workspace), matching: find.byType(TextField), ); Future _pump( WidgetTester tester, transport.PipelinesClient client, { SettingsStore? store, TargetPlatform platform = TargetPlatform.android, double textScale = 1, Size size = const Size(800, 1200), VoidCallback? onShowWalkthrough, }) async { await tester.binding.setSurfaceSize(size); addTearDown(() => tester.binding.setSurfaceSize(null)); await tester.pumpWidget( MaterialApp( theme: ThemeData(platform: platform), builder: (context, child) => MediaQuery( data: MediaQuery.of( context, ).copyWith(textScaler: TextScaler.linear(textScale)), child: child ?? const SizedBox.shrink(), ), home: SettingsScreen( client: client, store: store ?? MemorySettingsStore(), onShowWalkthrough: onShowWalkthrough, ), ), ); await tester.pump(); await tester.pump(); await openSettingsSection(tester, 'Connection'); } /// Answers the Settings screen's queued ntfy-settings load on [channel]. void _answerNtfySettings(FakeChannel channel) { final request = channel.decodedOutgoing.last; expect(request['type'], 'notification_settings'); channel.reply({ 'id': request['id'], 'type': 'notification_settings', 'notification_settings': { 'run_finished': true, 'chat_needs_you': true, 'chat_reply_finished': false, }, }); }