import 'package:flutter/cupertino.dart'; 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'; 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'), findsOneWidget); expect(find.text('Workspace'), findsOneWidget); expect(find.text('Not connected'), findsOneWidget); expect(find.text('Saved workspace: /srv/pipelines'), findsOneWidget); expect( tester.widget(find.byType(TextField).first).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(find.byType(TextField).first); 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 fields = find.byType(TextField); final url = tester.widget(fields.first); 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(fields.first).height, editable(fields.last).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(find.byType(TextField).first, '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( find.byType(TextField).first, '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); expect(factory.last.decodedOutgoing, hasLength(1)); }); 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( find.byType(TextField).first, '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); }); 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 = find.byType(TextField).at(1); 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 Cupertino fields, buttons and navigation', ( 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(CupertinoNavigationBar), findsOneWidget); expect(find.byType(CupertinoTextField), findsNWidgets(2)); expect(find.byType(TextField), findsNothing); expect(find.byType(FilledButton), findsNothing); expect(find.byKey(SettingsKeys.connect), findsOneWidget); expect(find.text('Trusted LAN/VPN only'), 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.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(find.byType(TextField).at(0)) .getSemanticsData() .label, tester .getSemantics(find.byType(TextField).at(1)) .getSemanticsData() .label, ]; expect(labels[0], contains('Server URL')); expect(labels[1], contains('Workspace path (on the server)')); handle.dispose(); }); 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; }); } Future _pump( WidgetTester tester, transport.PipelinesClient client, { SettingsStore? store, TargetPlatform platform = TargetPlatform.android, double textScale = 1, Size size = const Size(800, 1200), }) 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(), ), ), ); await tester.pump(); await tester.pump(); }