Files
pipelines/app/test/features/settings/settings_search_page_test.dart
T
alan 3136e5bb7e Redesign Settings as grouped searchable page
Replace the tabbed Settings flow with a single grouped page that searches and lazily loads server-backed sections. Preserve existing settings behavior while adding inline loading and retry states and updating coverage for the new navigation model.
2026-11-12 02:54:07 +00:00

250 lines
7.3 KiB
Dart

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<SettingsSection> _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<void> _pump(
WidgetTester tester,
List<SettingsSection> 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<int>();
final load = ServerLoad<int>(() {
fetches++;
return reply.future;
});
addTearDown(load.dispose);
await _pump(tester, [
..._filler(6),
SettingsSection(
id: 'server',
title: 'Phone notifications',
lazy: true,
builder: (_) => ServerSettingsView<int>(
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<int>(() 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<int>(
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<String>(() 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<String>(
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);