Consolidate the test helpers in test/helpers/

This commit is contained in:
Roland Geider
2026-08-10 12:40:16 +02:00
parent 6536f2ce26
commit 024d6f246c
25 changed files with 55 additions and 135 deletions

View File

@@ -29,7 +29,7 @@ import 'package:wger/core/network/auth_credentials_storage.dart';
import 'package:wger/core/network/secure_token_storage.dart';
import 'package:wger/core/shared_preferences.dart';
import '../../fake_auth_environment.dart';
import '../../helpers/fake_auth_environment.dart';
import 'auth_credentials_storage_test.mocks.dart';
@GenerateMocks([SecureTokenStorage])

View File

@@ -26,10 +26,6 @@ import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import 'package:wger/core/consts.dart';
import 'package:wger/core/exceptions/http_exception.dart';
import 'package:wger/core/exceptions/mfa_required_exception.dart';
@@ -42,6 +38,7 @@ import 'package:wger/features/account/models/account.dart';
import 'package:wger/features/account/providers/account_notifier.dart';
import 'package:wger/features/account/providers/account_repository.dart';
import '../../helpers/fake_auth_environment.dart';
import 'auth_notifier_login_test.mocks.dart';
/// Storage that simulates the process dying at the moment login claims DB
@@ -65,7 +62,7 @@ void main() {
// Needed to install the path_provider mock channel below.
TestWidgetsFlutterBinding.ensureInitialized();
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
installFakeAuthEnvironment();
late MockClient mockClient;
late MockSecureTokenStorage mockSecureStorage;
@@ -144,16 +141,6 @@ void main() {
}
});
SharedPreferences.setMockInitialValues({});
PackageInfo.setMockInitialValues(
appName: 'wger',
packageName: 'com.example.example',
version: '1.2.3',
buildNumber: '2',
buildSignature: 'buildSignature',
);
await PreferenceHelper.asyncPref.clear();
// Default gating-chain mocks (happy path). Login-flow tests override
// only what they care about.
when(mockClient.get(tVersion)).thenAnswer((_) async => Response('"99.99.99"', 200));

View File

@@ -29,18 +29,15 @@ import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import 'package:wger/core/consts.dart';
import 'package:wger/core/network/auth_notifier.dart';
import 'package:wger/core/network/auth_state.dart';
import 'package:wger/core/network/secure_token_storage.dart';
import 'package:wger/core/shared_preferences.dart';
import '../../fake_connectivity.dart';
import '../../helpers/fake_auth_environment.dart';
import '../../helpers/fake_connectivity.dart';
import 'auth_notifier_powersync_test.mocks.dart';
@GenerateMocks([http.Client, SecureTokenStorage])
@@ -51,7 +48,7 @@ void main() {
// Replacement for SharedPreferences.setMockInitialValues() for the
// async API used by the auth notifier.
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
installFakeAuthEnvironment();
// The background revalidation observes connectivity to re-probe on
// reconnect; stub the connectivity platform so no real plugin is needed.
@@ -103,15 +100,6 @@ void main() {
when(mockSecureStorage.readRefreshToken()).thenAnswer((_) async => null);
when(mockSecureStorage.writeRefreshToken(any)).thenAnswer((_) async {});
SharedPreferences.setMockInitialValues({});
PackageInfo.setMockInitialValues(
appName: 'wger',
packageName: 'com.example.example',
version: '1.2.3',
buildNumber: '2',
buildSignature: 'buildSignature',
);
// Point the on-disk wipe path at a real temp dir so a wiping logout
// succeeds (no file present -> no-op). Set [failDbPathLookup] to make the
// lookup throw and exercise a failed wipe.
@@ -133,7 +121,6 @@ void main() {
// Wipe async prefs between tests (the platform instance is shared).
final prefs = PreferenceHelper.asyncPref;
await prefs.clear();
// Persist a logged-in user so auto-login actually runs.
await prefs.setString(

View File

@@ -29,14 +29,13 @@ import 'package:wger/core/exceptions/http_exception.dart';
import 'package:wger/core/network/base_provider.dart';
import '../../fixtures/fixture_reader.dart';
import '../../utils.dart';
import 'base_provider_test.mocks.dart';
@GenerateMocks([http.Client])
void main() {
group('test base provider', () {
test('Test the makeUrl helper', () {
final WgerBaseProvider provider = buildTestBaseProvider();
final WgerBaseProvider provider = WgerBaseProvider(serverUrl: 'https://localhost');
expect(
Uri.https('localhost', '/api/v2/endpoint/'),
@@ -70,9 +69,7 @@ void main() {
test('Test the makeUrl helper with sub url', () {
// Trailing slash is removed when saving the server URL
final WgerBaseProvider provider = buildTestBaseProvider(
serverUrl: 'https://example.com/wger-url',
);
final WgerBaseProvider provider = WgerBaseProvider(serverUrl: 'https://example.com/wger-url');
expect(
Uri.https('example.com', '/wger-url/api/v2/endpoint/'),
@@ -141,7 +138,10 @@ void main() {
).thenAnswer((_) => Future.value(response3));
// Act
final WgerBaseProvider provider = buildTestBaseProvider(client: mockHttpClient);
final WgerBaseProvider provider = WgerBaseProvider(
serverUrl: 'https://localhost',
client: mockHttpClient,
);
final data = await provider.fetchPaginated(paginationUri1);
// Assert
@@ -160,7 +160,7 @@ void main() {
setUp(() {
mockClient = MockClient();
repo = buildTestBaseProvider(client: mockClient);
repo = WgerBaseProvider(serverUrl: 'https://localhost', client: mockClient);
});
final uri = Uri.https('localhost', '/api/v2/endpoint/');

View File

@@ -28,7 +28,7 @@ import 'package:wger/core/network/auth_credentials_storage.dart';
import 'package:wger/core/network/secure_token_storage.dart';
import 'package:wger/core/network/server_gating.dart';
import '../../fake_auth_environment.dart';
import '../../helpers/fake_auth_environment.dart';
/// The version gates don't touch storage; this stand-in satisfies the
/// constructor without pulling in secure-storage plumbing.

View File

@@ -26,10 +26,6 @@ import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import 'package:wger/core/consts.dart';
import 'package:wger/core/network/auth_notifier.dart';
import 'package:wger/core/network/secure_token_storage.dart';
@@ -37,6 +33,7 @@ import 'package:wger/core/powersync_unreachable_screen.dart';
import 'package:wger/core/shared_preferences.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../helpers/fake_auth_environment.dart';
import 'recovery_screens_test.mocks.dart';
/// Widget tests for the recovery screens.
@@ -44,7 +41,7 @@ import 'recovery_screens_test.mocks.dart';
void main() {
// Replacement for SharedPreferences.setMockInitialValues() for the
// async API used by the auth notifier.
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
installFakeAuthEnvironment();
late MockClient mockClient;
late MockSecureTokenStorage mockSecureStorage;
@@ -99,18 +96,8 @@ void main() {
when(mockSecureStorage.readRefreshToken()).thenAnswer((_) async => null);
when(mockSecureStorage.writeRefreshToken(any)).thenAnswer((_) async {});
SharedPreferences.setMockInitialValues({});
PackageInfo.setMockInitialValues(
appName: 'wger',
packageName: 'com.example.example',
version: '1.2.3',
buildNumber: '2',
buildSignature: 'buildSignature',
);
// Wipe async prefs between tests (the platform instance is shared).
final prefs = PreferenceHelper.asyncPref;
await prefs.clear();
// Saved login → autoLogin runs the full probe path.
await prefs.setString(

View File

@@ -30,14 +30,7 @@ import 'package:wger/core/network/auth_state.dart';
import 'package:wger/features/account/widgets/settings/certs_not_verified.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
class _FakeAuthNotifier extends AuthNotifier {
_FakeAuthNotifier(this._state);
final AuthState _state;
@override
Future<AuthState> build() async => _state;
}
import '../../../../helpers/fake_auth_notifier.dart';
void main() {
const selfHosted = 'https://gym.example.com';
@@ -58,7 +51,7 @@ void main() {
overrides: [
appSettingsPrefsProvider.overrideWithValue(prefs),
authProvider.overrideWith(
() => _FakeAuthNotifier(
() => FakeAuthNotifier(
AuthState(status: AuthStatus.loggedIn, serverUrl: serverUrl),
),
),

View File

@@ -25,11 +25,7 @@ import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:wger/core/consts.dart';
import 'package:wger/core/http_overrides.dart';
@@ -41,7 +37,8 @@ import 'package:wger/features/auth/screens/auth_screen.dart';
import 'package:wger/features/auth/screens/mfa_challenge_screen.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../fake_connectivity.dart';
import '../../../helpers/fake_auth_environment.dart';
import '../../../helpers/fake_connectivity.dart';
import 'auth_screen_test.mocks.dart';
/// Captures `launchUrl(...)` calls so the web-handoff test can inspect the
@@ -62,6 +59,9 @@ void main() {
// The auth screen watches networkStatusProvider to gate the action button.
// Stub connectivity so that probe is deterministic.
installFakeConnectivity();
// Installs the prefs/PackageInfo fakes and clears the store between tests,
// so values a test writes (e.g. PREFS_LAST_SERVER) don't leak into the next.
installFakeAuthEnvironment();
late MockClient mockClient;
late MockSecureTokenStorage mockSecureStorage;
@@ -121,20 +121,6 @@ void main() {
// Default: online. The offline test overrides this.
reachabilityCheck = (_, _, _) async => (reachable: true, reason: 'test');
SharedPreferences.setMockInitialValues({});
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
// PreferenceHelper caches its SharedPreferencesAsync, so swapping the
// platform instance above does not isolate it; clear the store so values a
// test writes (e.g. PREFS_LAST_SERVER) don't leak into the next one.
await PreferenceHelper.asyncPref.clear();
PackageInfo.setMockInitialValues(
appName: 'wger',
packageName: 'com.example.example',
version: '1.2.3',
buildNumber: '2',
buildSignature: 'buildSignature',
);
// Happy-path stubs for the headless login / signup endpoints; tests
// that exercise error responses override these as needed.
when(

View File

@@ -25,23 +25,19 @@ import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import 'package:wger/core/network/auth_notifier.dart';
import 'package:wger/core/network/secure_token_storage.dart';
import 'package:wger/core/shared_preferences.dart';
import 'package:wger/core/update_app_screen.dart';
import 'package:wger/core/update_server_screen.dart';
import 'package:wger/features/auth/screens/mfa_challenge_screen.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../helpers/fake_auth_environment.dart';
import 'mfa_challenge_screen_test.mocks.dart';
@GenerateMocks([http.Client, SecureTokenStorage])
void main() {
SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty();
installFakeAuthEnvironment();
late MockClient mockClient;
late MockSecureTokenStorage mockSecureStorage;
@@ -95,16 +91,6 @@ void main() {
when(mockSecureStorage.readRefreshToken()).thenAnswer((_) async => null);
when(mockSecureStorage.writeRefreshToken(any)).thenAnswer((_) async {});
SharedPreferences.setMockInitialValues({});
PackageInfo.setMockInitialValues(
appName: 'wger',
packageName: 'com.example.example',
version: '1.2.3',
buildNumber: '2',
buildSignature: 'buildSignature',
);
await PreferenceHelper.asyncPref.clear();
// Gating-chain happy-path stubs so a successful completeMfa reaches
// AuthStatus.loggedIn rather than getting stuck on a probe.
when(mockClient.get(tVersion)).thenAnswer((_) async => Response('"99.99.99"', 200));

View File

@@ -37,7 +37,7 @@ import 'package:wger/features/exercises/widgets/add_exercise/steps/step_1_basics
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../test_data/exercises.dart';
import '../../../fake_connectivity.dart';
import '../../../helpers/fake_connectivity.dart';
import 'add_exercise_screen_test.mocks.dart';
/// Test suite for the exercise-contribution screen.

View File

@@ -31,7 +31,7 @@ import 'package:wger/features/exercises/widgets/exercises.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../test_data/exercises.dart';
import '../../../fake_connectivity.dart';
import '../../../helpers/fake_connectivity.dart';
import 'exercises_detail_widget_test.mocks.dart';
@GenerateMocks([ExerciseRepository])

View File

@@ -27,7 +27,7 @@ import 'package:wger/features/nutrition/providers/ingredient_repository.dart';
import 'package:wger/features/nutrition/providers/nutrition_notifier.dart';
import 'package:wger/features/nutrition/providers/nutrition_repository.dart';
import '../../../fake_connectivity.dart';
import '../../../helpers/fake_connectivity.dart';
import 'nutrition_notifier_test.mocks.dart';
@GenerateMocks([NutritionRepository, IngredientRepository])

View File

@@ -37,9 +37,9 @@ import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../test_data/body_weight.dart';
import '../../../../test_data/nutritional_plans.dart';
import '../../../fake_auth_environment.dart';
import '../../../fake_connectivity.dart';
import '../../../fixtures/fixture_reader.dart';
import '../../../helpers/fake_auth_environment.dart';
import '../../../helpers/fake_connectivity.dart';
import 'nutritional_plan_screen_test.mocks.dart';
@GenerateMocks([

View File

@@ -34,7 +34,7 @@ import 'package:wger/features/nutrition/widgets/forms.dart';
import 'package:wger/features/weight/providers/body_weight_repository.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../fake_connectivity.dart';
import '../../../helpers/fake_connectivity.dart';
import 'nutritional_plans_screen_test.mocks.dart';
@GenerateMocks([

View File

@@ -33,7 +33,7 @@ import 'package:wger/features/nutrition/widgets/widgets.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../test_data/nutritional_plans.dart';
import '../../../fake_connectivity.dart';
import '../../../helpers/fake_connectivity.dart';
import 'ingredient_typeahead_test.mocks.dart';
@GenerateMocks([IngredientRepository])

View File

@@ -35,7 +35,7 @@ import 'package:wger/features/nutrition/widgets/forms.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../test_data/nutritional_plans.dart';
import '../../../fake_connectivity.dart';
import '../../../helpers/fake_connectivity.dart';
import 'nutritional_meal_form_test.mocks.dart';
@GenerateMocks([IngredientRepository, NutritionRepository])

View File

@@ -43,8 +43,8 @@ import 'package:wger/features/nutrition/widgets/forms.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../test_data/nutritional_plans.dart';
import '../../../fake_connectivity.dart';
import '../../../fixtures/fixture_reader.dart';
import '../../../helpers/fake_connectivity.dart';
import 'nutritional_meal_item_form_test.mocks.dart';
@GenerateMocks([http.Client, IngredientRepository, NutritionRepository])

View File

@@ -40,7 +40,7 @@ import 'package:wger/features/routines/providers/routines_repository.dart';
import '../../../../test_data/exercises.dart';
import '../../../../test_data/routines.dart';
import '../../../fake_connectivity.dart';
import '../../../helpers/fake_connectivity.dart';
import '../helpers/routine_form_test_overrides.dart';
import 'routines_notifier_test.mocks.dart';

View File

@@ -54,7 +54,7 @@ import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../test_data/exercises.dart';
import '../../../../test_data/routines.dart';
import '../../../fake_connectivity.dart';
import '../../../helpers/fake_connectivity.dart';
import 'gym_mode_test.mocks.dart';
@GenerateMocks([

View File

@@ -29,7 +29,7 @@ import 'package:wger/features/routines/screens/routine_screen.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../test_data/routines.dart';
import '../../../fake_auth_environment.dart';
import '../../../helpers/fake_auth_environment.dart';
class _StubRoutinesRiverpod extends RoutinesRiverpod {
_StubRoutinesRiverpod(this._routines);

View File

@@ -44,7 +44,7 @@ import 'package:wger/features/routines/widgets/forms/routine.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../../../test_data/routines.dart';
import '../../../../fake_connectivity.dart';
import '../../../../helpers/fake_connectivity.dart';
import './routine_form_test.mocks.dart';
@GenerateMocks([

View File

@@ -1,6 +1,6 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 wger Team
* Copyright (c) 2026 - 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@@ -16,17 +16,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:http/http.dart' as http;
import 'package:wger/core/network/base_provider.dart';
import 'package:wger/core/network/auth_notifier.dart';
import 'package:wger/core/network/auth_state.dart';
/// Builds a [WgerBaseProvider] pre-configured for tests with a fixed server
/// URL. Pass a mocked [http.Client] to intercept requests. Authentication is
/// the client's responsibility now, the test client is unauthenticated, so
/// tests that assert on the `Authorization` header have to stub the client
/// directly (or use a wrapped `AuthHttpClient`).
WgerBaseProvider buildTestBaseProvider({
http.Client? client,
String serverUrl = 'https://localhost',
}) {
return WgerBaseProvider(serverUrl: serverUrl, client: client);
/// An [AuthNotifier] that resolves to a fixed state.
///
/// Use it via `authProvider.overrideWith(() => FakeAuthNotifier(state))` in
/// widget tests instead of seeding preferences and stubbing every request the
/// real notifier makes on its eager build.
class FakeAuthNotifier extends AuthNotifier {
FakeAuthNotifier(this._state);
final AuthState _state;
@override
Future<AuthState> build() async => _state;
}

View File

@@ -45,17 +45,9 @@ import '../../test_data/nutritional_plans.dart';
import '../../test_data/profile.dart';
import '../../test_data/routines.dart';
import '../../test_data/trophies.dart';
import '../helpers/fake_auth_notifier.dart';
import 'screenshots_01_dashboard.mocks.dart';
class _FakeAuthNotifier extends AuthNotifier {
_FakeAuthNotifier(this._state);
final AuthState _state;
@override
Future<AuthState> build() async => _state;
}
@GenerateMocks([
GalleryRepository,
NutritionRepository,
@@ -122,7 +114,7 @@ Widget createDashboardScreen({Locale? locale}) {
overrides: [
bodyWeightRepositoryProvider.overrideWithValue(mockBodyWeightRepository),
measurementRepositoryProvider.overrideWithValue(mockMeasurementRepo),
authProvider.overrideWith(() => _FakeAuthNotifier(loggedInAuth)),
authProvider.overrideWith(() => FakeAuthNotifier(loggedInAuth)),
userProfileRepositoryProvider.overrideWithValue(mockUserProfileRepo),
nutritionRepositoryProvider.overrideWithValue(mockNutritionRepo),
ingredientRepositoryProvider.overrideWithValue(mockIngredientRepo),