3ec580c2d9
Servers become rows with a selection mark, addresses as subtitles and a Use/Edit/Remove menu plus an Add server row; the connection is a status row with address and Reconnect/Disconnect rows; Templates and Schedules are chevron rows in a Library group. Material groups are flat surfaceContainer surfaces with primary titleSmall headers (no Cards); Cupertino uses inset-grouped sections on the grouped background, switches for the ntfy choices. The trust warning is a footer, helper copy is shortened, Save workspace is a legible tonal/plain button, and the column starts at the bar's inset (max 720) instead of being centred. The page pads by the top media padding instead of a top SafeArea.
185 lines
6.4 KiB
Dart
185 lines
6.4 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/core/api_gateway.dart';
|
|
import 'package:pipelines_app/core/transport/connection_state.dart';
|
|
import 'package:pipelines_app/features/settings/settings_ntfy_section.dart';
|
|
|
|
/// A scripted gateway: records every request and answers from [respond].
|
|
class _Gateway implements ApiGateway {
|
|
_Gateway(this.respond);
|
|
|
|
final FutureOr<JsonMap> Function(String type, JsonMap fields) respond;
|
|
final calls = <(String, JsonMap)>[];
|
|
|
|
@override
|
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
|
calls.add((type, fields));
|
|
return respond(type, fields);
|
|
}
|
|
|
|
@override
|
|
Stream<JsonMap> get frames => const Stream.empty();
|
|
}
|
|
|
|
JsonMap _reply(bool run, bool needs, bool reply) => {
|
|
'type': 'notification_settings',
|
|
'notification_settings': {
|
|
'run_finished': run,
|
|
'chat_needs_you': needs,
|
|
'chat_reply_finished': reply,
|
|
},
|
|
};
|
|
|
|
Widget _host(ApiGateway gateway, {required bool cupertino}) => MaterialApp(
|
|
theme: ThemeData(
|
|
platform: cupertino ? TargetPlatform.iOS : TargetPlatform.android,
|
|
),
|
|
home: Scaffold(
|
|
body: SingleChildScrollView(
|
|
child: SettingsNtfySection(gateway: gateway, connected: true),
|
|
),
|
|
),
|
|
);
|
|
|
|
bool _checked(WidgetTester tester, String key, {required bool cupertino}) {
|
|
final tile = find.byKey(ntfySettingKey(key));
|
|
if (cupertino) {
|
|
return tester
|
|
.widget<CupertinoSwitch>(
|
|
find.descendant(of: tile, matching: find.byType(CupertinoSwitch)),
|
|
)
|
|
.value;
|
|
}
|
|
return tester.widget<CheckboxListTile>(tile).value!;
|
|
}
|
|
|
|
void main() {
|
|
for (final cupertino in [false, true]) {
|
|
final style = cupertino ? 'Cupertino' : 'Material';
|
|
|
|
testWidgets('$style loads the stored values', (tester) async {
|
|
final gateway = _Gateway((_, _) => _reply(true, false, true));
|
|
await tester.pumpWidget(_host(gateway, cupertino: cupertino));
|
|
await tester.pumpAndSettle();
|
|
expect(gateway.calls.single.$1, 'notification_settings');
|
|
expect(find.text('Run finished'), findsOneWidget);
|
|
expect(find.text('Chat needs your answer or approval'), findsOneWidget);
|
|
expect(find.text('Chat reply finished'), findsOneWidget);
|
|
expect(find.text(ntfySectionSubtitle), findsOneWidget);
|
|
expect(_checked(tester, 'run_finished', cupertino: cupertino), isTrue);
|
|
expect(_checked(tester, 'chat_needs_you', cupertino: cupertino), isFalse);
|
|
expect(
|
|
_checked(tester, 'chat_reply_finished', cupertino: cupertino),
|
|
isTrue,
|
|
);
|
|
});
|
|
|
|
testWidgets('$style toggling writes only that setting', (tester) async {
|
|
final gateway = _Gateway((type, fields) {
|
|
if (type == 'set_notification_settings') {
|
|
return _reply(true, true, true);
|
|
}
|
|
return _reply(true, true, false);
|
|
});
|
|
await tester.pumpWidget(_host(gateway, cupertino: cupertino));
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(ntfySettingKey('chat_reply_finished')));
|
|
await tester.pumpAndSettle();
|
|
expect(gateway.calls, hasLength(2));
|
|
expect(gateway.calls.last.$1, 'set_notification_settings');
|
|
expect(gateway.calls.last.$2, {
|
|
'notification_settings': {'chat_reply_finished': true},
|
|
});
|
|
expect(
|
|
_checked(tester, 'chat_reply_finished', cupertino: cupertino),
|
|
isTrue,
|
|
);
|
|
expect(find.byKey(ntfyErrorKey), findsNothing);
|
|
});
|
|
|
|
testWidgets('$style failure reverts and shows the refusal', (tester) async {
|
|
final pending = Completer<JsonMap>();
|
|
final gateway = _Gateway((type, _) {
|
|
if (type == 'set_notification_settings') return pending.future;
|
|
return _reply(true, true, false);
|
|
});
|
|
await tester.pumpWidget(_host(gateway, cupertino: cupertino));
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.byKey(ntfySettingKey('run_finished')));
|
|
await tester.pump();
|
|
// The change shows at once and the group is disabled while it saves.
|
|
expect(_checked(tester, 'run_finished', cupertino: cupertino), isFalse);
|
|
await tester.tap(find.byKey(ntfySettingKey('chat_needs_you')));
|
|
await tester.pump();
|
|
expect(gateway.calls, hasLength(2));
|
|
pending.completeError(const ProtocolError('settings are read-only'));
|
|
await tester.pumpAndSettle();
|
|
expect(_checked(tester, 'run_finished', cupertino: cupertino), isTrue);
|
|
expect(_checked(tester, 'chat_needs_you', cupertino: cupertino), isTrue);
|
|
expect(
|
|
find.text('Could not save: settings are read-only'),
|
|
findsOneWidget,
|
|
);
|
|
});
|
|
}
|
|
|
|
testWidgets('disabled while loading and offline', (tester) async {
|
|
final pending = Completer<JsonMap>();
|
|
final gateway = _Gateway((_, _) => pending.future);
|
|
await tester.pumpWidget(_host(gateway, cupertino: false));
|
|
await tester.pump();
|
|
expect(find.text('Loading…'), findsOneWidget);
|
|
await tester.tap(find.byKey(ntfySettingKey('run_finished')));
|
|
await tester.pump();
|
|
expect(gateway.calls, hasLength(1));
|
|
expect(
|
|
tester
|
|
.widget<CheckboxListTile>(find.byKey(ntfySettingKey('run_finished')))
|
|
.onChanged,
|
|
isNull,
|
|
);
|
|
pending.complete(_reply(true, true, false));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Loading…'), findsNothing);
|
|
|
|
final offline = _Gateway((_, _) => _reply(true, true, false));
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: Scaffold(
|
|
body: SettingsNtfySection(gateway: offline, connected: false),
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
expect(offline.calls, isEmpty);
|
|
expect(find.text(ntfyOfflineText), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a load failure is shown with a retry', (tester) async {
|
|
var fail = true;
|
|
final gateway = _Gateway((_, _) {
|
|
if (fail) throw const ProtocolError('no store');
|
|
return _reply(false, true, false);
|
|
});
|
|
await tester.pumpWidget(_host(gateway, cupertino: false));
|
|
await tester.pumpAndSettle();
|
|
expect(
|
|
find.text('Could not load notification settings: no store'),
|
|
findsOneWidget,
|
|
);
|
|
fail = false;
|
|
await tester.tap(find.text('Retry'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byKey(ntfyErrorKey), findsNothing);
|
|
expect(
|
|
tester
|
|
.widget<CheckboxListTile>(find.byKey(ntfySettingKey('run_finished')))
|
|
.value,
|
|
isFalse,
|
|
);
|
|
});
|
|
}
|