Files
alan 3933ff980c Remove Cupertino settings branches
Keep the Material settings implementation while preserving public Cupertino parameters for later cleanup. Narrow settings tests to the Material-only behavior.
2026-11-11 05:21:30 +00:00

397 lines
13 KiB
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';
import 'settings_topic_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<TextField>(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<TextField>(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<TextField>(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(<String, dynamic>{
'id': outgoing.single['id'],
'type': 'pong',
'build': <String, dynamic>{'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 are loaded only when their topic is opened.
expect(factory.last.decodedOutgoing, hasLength(1));
await selectSettingsTopic(tester, '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(
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(<String, dynamic>{
'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 selectSettingsTopic(tester, '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 = 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<FilledButton>(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-topic-connection')),
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 <String>[
'Workspace path (on the server)',
serverUrlHelper,
workspaceHelper,
]) {
final text = tester.widget<Text>(find.text(copy));
expect(text.maxLines, isNull, reason: 'no line cap on $copy');
final paragraph = tester.renderObject<RenderParagraph>(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 = <String>[
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<void> _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();
await selectSettingsTopic(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(<String, dynamic>{
'id': request['id'],
'type': 'notification_settings',
'notification_settings': <String, dynamic>{
'run_finished': true,
'chat_needs_you': true,
'chat_reply_finished': false,
},
});
}