LifeMate — Personal, Family & Learning Companion
feat/local-signing-preflight
chore/phase1-repository-cleanup
deploy/stable-web-apk
deployment-static
design/mobile-accessibility-v2
design/mobile-navigation-review
docs/child-education-access-decision
docs/offline-data-rescue-options
docs/owner-guide-and-delivery-plan
docs/phase1-policy-options
docs/public-review-live-2026-10-15
feat/aparat-learning-library
feat/child-device-safety
feat/child-identity-schema
feat/flutter-mobile-navigation
feat/learning-content-operations
feat/local-signing-preflight
feat/online-family-stage0
feat/personal-encrypted-recovery
feat/private-planner-series-api
feat/public-review-page
feat/standalone-weekly-planner
feat/weekly-planner-android
final-feature-1/iran-family-school
fix/auth-abuse-expiry-safety
fix/auth-expiry-after-lock
fix/backend-request-safety
fix/lifeguide-bootstrap-naming-ci
fix/lifeguide-pages-qa-base-preflight
fix/lifeguide-qa-pages-env-deployment
fix/local-task-persistence
fix/managed-child-session-boundary
fix/personal-jalali-dates
fix/personal-notification-controls
fix/personal-upgrade-delivery-guard
fix/sms-approval-gates
fix/study-duration-display
infra/cloudflare-neon-migration
infra/lifeguide-cloudflare-qa
infra/lifeguide-github-pages-qa
infra/lifeguide-supabase-qa
main
phase-1/foundation-completion
phase-2/product-foundation-identity
phase-3/planner-school-family-core
phase-4/ai-learning-wellbeing
phase-5/hardening-observability-release
test/android-reminder-acceptance
test/personal-notification-settings
test/personal-recovery-acceptance
test/role-entry-apk
| 1 | import 'dart:convert'; |
| 2 | import 'package:http/http.dart' as http; |
| 3 | import 'package:http/testing.dart'; |
| 4 | import 'package:lifemate/runtime_config.dart'; |
| 5 | import 'package:lifemate/runtime_controller.dart'; |
| 6 | import 'package:lifemate/session_store.dart'; |
| 7 | import 'package:lifemate/offline_store.dart'; |
| 8 | import 'package:lifemate/plan_sync.dart'; |
| 9 | import 'package:flutter/services.dart'; |
| 10 | import 'package:flutter/material.dart'; |
| 11 | import 'package:flutter_test/flutter_test.dart'; |
| 12 | import 'package:lifemate/api.dart'; |
| 13 | import 'package:lifemate/main.dart'; |
| 14 | import 'package:lifemate/phase3_ui.dart'; |
| 15 | import 'package:lifemate/phase4_ui.dart'; |
| 16 | import 'package:lifemate/sync_changes_ui.dart'; |
| 17 | import 'package:shared_preferences/shared_preferences.dart'; |
| 18 | import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; |
| 19 | import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; |
| 20 | |
| 21 | import 'app_test.dart' show FakeApi; |
| 22 | |
| 23 | class _FailingProfileApi extends FakeApi { |
| 24 | @override |
| 25 | Future<Map<String, dynamic>> updateProfile( |
| 26 | {String? displayName, |
| 27 | String? birthDate, |
| 28 | String? themePreference, |
| 29 | String? profileCategory}) async => |
| 30 | throw const ApiException(500, 'server_failed'); |
| 31 | } |
| 32 | |
| 33 | class _MemorySession extends SessionStore { |
| 34 | String? value; |
| 35 | @override |
| 36 | Future<String?> read() async => value; |
| 37 | @override |
| 38 | Future<void> write(String next) async { |
| 39 | value = next; |
| 40 | } |
| 41 | |
| 42 | @override |
| 43 | Future<void> clear() async { |
| 44 | value = null; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | class _ServerFailureApi extends FakeApi { |
| 49 | @override |
| 50 | Future<List<Map<String, dynamic>>> listPlanItems( |
| 51 | {DateTime? from, DateTime? to, String? kind}) async => |
| 52 | throw const ApiException(500, 'server_failed'); |
| 53 | } |
| 54 | |
| 55 | class _PlannerWritesApi extends FakeApi { |
| 56 | final mutations = <Map<String, dynamic>>[]; |
| 57 | @override |
| 58 | Future<Map<String, dynamic>> submitSyncMutations( |
| 59 | List<Map<String, dynamic>> changes) async { |
| 60 | mutations.addAll(changes); |
| 61 | return { |
| 62 | 'results': changes |
| 63 | .map((m) => { |
| 64 | 'id': m['id'], |
| 65 | 'status': 'accepted', |
| 66 | 'item': { |
| 67 | 'id': m['entityId'], |
| 68 | ...(m['payload'] as Map), |
| 69 | 'version': (m['expectedVersion'] as int) + 1, |
| 70 | 'status': 'planned' |
| 71 | }, |
| 72 | 'acceptedAt': '2026-10-08T10:00:00Z' |
| 73 | }) |
| 74 | .toList() |
| 75 | }; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | class _CategoryApi extends FakeApi { |
| 80 | _CategoryApi(this.category, {super.role}); |
| 81 | String category; |
| 82 | @override |
| 83 | Future<Map<String, dynamic>> getProfile() async => |
| 84 | {...await super.getProfile(), 'profile_category': category}; |
| 85 | @override |
| 86 | Future<Map<String, dynamic>> updateProfile( |
| 87 | {String? displayName, |
| 88 | String? birthDate, |
| 89 | String? themePreference, |
| 90 | String? profileCategory}) async { |
| 91 | if (profileCategory != null) category = profileCategory; |
| 92 | return { |
| 93 | ...await super.updateProfile( |
| 94 | displayName: displayName, |
| 95 | birthDate: birthDate, |
| 96 | themePreference: themePreference), |
| 97 | 'profile_category': category |
| 98 | }; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | class _OfflinePlannerApi extends FakeApi { |
| 103 | @override |
| 104 | Future<List<Map<String, dynamic>>> listPlanItems( |
| 105 | {DateTime? from, DateTime? to, String? kind}) async => |
| 106 | throw const ApiException(0, 'network_unavailable'); |
| 107 | @override |
| 108 | Future<Map<String, dynamic>> submitSyncMutations( |
| 109 | List<Map<String, dynamic>> changes) async => |
| 110 | throw const ApiException(0, 'network_unavailable'); |
| 111 | } |
| 112 | |
| 113 | class _CategoryWithoutFamiliesApi extends _CategoryApi { |
| 114 | _CategoryWithoutFamiliesApi() : super('adult'); |
| 115 | @override |
| 116 | Future<List<Map<String, dynamic>>> listFamilies() async => |
| 117 | throw const ApiException(500, 'server_failed'); |
| 118 | } |
| 119 | |
| 120 | void main() { |
| 121 | setUp(() { |
| 122 | SharedPreferences.setMockInitialValues({}); |
| 123 | SharedPreferencesAsyncPlatform.instance = |
| 124 | InMemorySharedPreferencesAsync.withData({}); |
| 125 | String? pending; |
| 126 | TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger |
| 127 | .setMockMethodCallHandler( |
| 128 | const MethodChannel('lifeguide/local_write_guard'), (call) async { |
| 129 | if (call.method == 'getBlocked') return pending != null; |
| 130 | if (call.method == 'beginWrite') return pending = 'owned-ui-test-token'; |
| 131 | if (call.method == 'completeWrite' && call.arguments == pending) { |
| 132 | pending = null; |
| 133 | return true; |
| 134 | } |
| 135 | throw PlatformException(code: 'local_storage_restart_required'); |
| 136 | }); |
| 137 | }); |
| 138 | |
| 139 | testWidgets('empty runtime endpoints open setup before constructing an API', |
| 140 | (tester) async { |
| 141 | await tester.pumpWidget(LifeMateApp()); |
| 142 | await tester.pumpAndSettle(); |
| 143 | expect(find.text('اتصال لایفگاید'), findsOneWidget); |
| 144 | expect(find.text('ورود'), findsNothing); |
| 145 | }); |
| 146 | |
| 147 | test('auth errors do not disclose an existing account', () { |
| 148 | expect(errorText(const ApiException(409, 'account_exists')), |
| 149 | 'عملیات انجام نشد. دوباره تلاش کن.'); |
| 150 | }); |
| 151 | |
| 152 | testWidgets('authenticated notification inbox is offered only in home drawer', |
| 153 | (tester) async { |
| 154 | await tester.pumpWidget(LifeMateApp(api: FakeApi())); |
| 155 | await tester.pumpAndSettle(); |
| 156 | expect(find.text('اعلانهای درون برنامه'), findsNothing); |
| 157 | await tester.pumpWidget(MaterialApp(home: HomeShell(api: FakeApi()))); |
| 158 | await tester.pumpAndSettle(); |
| 159 | tester.firstState<ScaffoldState>(find.byType(Scaffold)).openDrawer(); |
| 160 | await tester.pumpAndSettle(); |
| 161 | expect(find.text('اعلانهای درون برنامه'), findsOneWidget); |
| 162 | }); |
| 163 | |
| 164 | testWidgets('home drawer opens durable pending changes screen', |
| 165 | (tester) async { |
| 166 | await tester.pumpWidget(MaterialApp(home: HomeShell(api: FakeApi()))); |
| 167 | await tester.pumpAndSettle(); |
| 168 | tester.firstState<ScaffoldState>(find.byType(Scaffold)).openDrawer(); |
| 169 | await tester.pumpAndSettle(); |
| 170 | await tester.scrollUntilVisible(find.text('تغییرهای معلق و تعارضها'), 100, |
| 171 | scrollable: find |
| 172 | .descendant( |
| 173 | of: find.byType(Drawer), matching: find.byType(Scrollable)) |
| 174 | .first); |
| 175 | await tester.pumpAndSettle(); |
| 176 | await tester.tap(find.text('تغییرهای معلق و تعارضها')); |
| 177 | await tester.pumpAndSettle(); |
| 178 | expect(find.byType(SyncChangesPage), findsOneWidget); |
| 179 | expect(find.text('تغییر معلقی باقی نمانده است.'), findsOneWidget); |
| 180 | }); |
| 181 | |
| 182 | testWidgets('sensitive guide remains disabled in the online shell', |
| 183 | (tester) async { |
| 184 | await tester.pumpWidget(MaterialApp(home: HomeShell(api: FakeApi()))); |
| 185 | await tester.pumpAndSettle(); |
| 186 | tester.firstState<ScaffoldState>(find.byType(Scaffold)).openDrawer(); |
| 187 | await tester.pumpAndSettle(); |
| 188 | await tester.tap(find.text('همراه هوشمند')); |
| 189 | await tester.pumpAndSettle(); |
| 190 | expect(find.byType(Phase4Hub), findsNothing); |
| 191 | expect(find.textContaining('در این مرحله فعال نیست'), findsOneWidget); |
| 192 | }); |
| 193 | |
| 194 | testWidgets('online planner offers homework without school setup', |
| 195 | (tester) async { |
| 196 | await tester.pumpWidget(MaterialApp( |
| 197 | home: Scaffold(body: PlannerPage(api: FakeApi())), |
| 198 | )); |
| 199 | await tester.pumpAndSettle(); |
| 200 | await tester.tap(find.text('جدید')); |
| 201 | await tester.pumpAndSettle(); |
| 202 | await tester.tap(find.byType(DropdownButtonFormField<String>).first); |
| 203 | await tester.pumpAndSettle(); |
| 204 | expect(find.text('تکلیف'), findsOneWidget); |
| 205 | }); |
| 206 | testWidgets('profile save failure is visible and dialog remains editable', |
| 207 | (tester) async { |
| 208 | await tester.pumpWidget( |
| 209 | MaterialApp(home: ProfileFamilyPage(api: _FailingProfileApi()))); |
| 210 | await tester.pumpAndSettle(); |
| 211 | await tester.tap(find.byIcon(Icons.edit_outlined)); |
| 212 | await tester.pumpAndSettle(); |
| 213 | await tester.tap(find.text('ذخیره')); |
| 214 | await tester.pumpAndSettle(); |
| 215 | expect(tester.takeException(), isNull); |
| 216 | expect(find.textContaining('سرور پاسخ معتبر نداد'), findsOneWidget); |
| 217 | expect(find.byType(AlertDialog), findsOneWidget); |
| 218 | }); |
| 219 | testWidgets( |
| 220 | 'configured authenticated runtime opens home and settings uses child navigator', |
| 221 | (tester) async { |
| 222 | final session = _MemorySession(); |
| 223 | final api = HttpIdentityApi( |
| 224 | baseUrl: 'https://synthetic.example.test/service', |
| 225 | sessionStore: session, |
| 226 | client: MockClient((req) async { |
| 227 | final value = req.url.path.endsWith('/me') |
| 228 | ? { |
| 229 | 'user_id': 'child', |
| 230 | 'display_name': 'فرزند', |
| 231 | 'theme_preference': 'girl_pink' |
| 232 | } |
| 233 | : req.url.path.endsWith('/families') |
| 234 | ? {'families': []} |
| 235 | : {'items': []}; |
| 236 | return http.Response(jsonEncode(value), 200, |
| 237 | headers: {'content-type': 'application/json'}); |
| 238 | })) |
| 239 | ..accessToken = 'synthetic-token' |
| 240 | ..currentUserId = 'child'; |
| 241 | final runtime = RuntimeController(sessionStore: session) |
| 242 | ..initialized = true |
| 243 | ..config = RuntimeConfig(apiBaseUrl: api.baseUrl) |
| 244 | ..api = api; |
| 245 | await tester.pumpWidget(LifeMateApp(runtime: runtime)); |
| 246 | await tester.pumpAndSettle(); |
| 247 | expect(find.byType(HomeShell), findsOneWidget); |
| 248 | expect(find.text('ورود'), findsNothing); |
| 249 | tester.firstState<ScaffoldState>(find.byType(Scaffold)).openDrawer(); |
| 250 | await tester.pumpAndSettle(); |
| 251 | await tester.scrollUntilVisible(find.text('تنظیم اتصال'), 100, |
| 252 | scrollable: find |
| 253 | .descendant( |
| 254 | of: find.byType(Drawer), matching: find.byType(Scrollable)) |
| 255 | .first); |
| 256 | await tester.pumpAndSettle(); |
| 257 | await tester.tap(find.text('تنظیم اتصال')); |
| 258 | await tester.pumpAndSettle(); |
| 259 | expect(tester.takeException(), isNull); |
| 260 | expect(find.text('اتصال لایفگاید'), findsOneWidget); |
| 261 | await runtime.signOut(); |
| 262 | await tester.pumpAndSettle(); |
| 263 | expect(find.text('ورود'), findsOneWidget); |
| 264 | expect(find.text('اتصال لایفگاید'), findsNothing); |
| 265 | await tester.pumpWidget(const SizedBox()); |
| 266 | runtime.dispose(); |
| 267 | }); |
| 268 | testWidgets('server failure never presents cached planner as live', |
| 269 | (tester) async { |
| 270 | final api = _ServerFailureApi(); |
| 271 | await OfflineStore.forApi(api).cachePlanner([ |
| 272 | {'id': 'old', 'title': 'نسخه قدیمی', 'kind': 'task', 'status': 'planned'} |
| 273 | ]); |
| 274 | await tester |
| 275 | .pumpWidget(MaterialApp(home: Scaffold(body: PlannerPage(api: api)))); |
| 276 | await tester.pumpAndSettle(); |
| 277 | expect(find.text('نسخه قدیمی'), findsNothing); |
| 278 | expect(find.textContaining('سرور پاسخ معتبر نداد'), findsOneWidget); |
| 279 | expect(find.textContaining('نسخه ذخیرهشده'), findsNothing); |
| 280 | await tester.pumpWidget(const SizedBox()); |
| 281 | }); |
| 282 | testWidgets( |
| 283 | 'homework sharing requires explicit family and sends planned time', |
| 284 | (tester) async { |
| 285 | final api = _PlannerWritesApi(); |
| 286 | await tester |
| 287 | .pumpWidget(MaterialApp(home: Scaffold(body: PlannerPage(api: api)))); |
| 288 | await tester.pumpAndSettle(); |
| 289 | await tester.tap(find.text('جدید')); |
| 290 | await tester.pumpAndSettle(); |
| 291 | await tester.enterText(find.widgetWithText(TextField, 'عنوان'), 'ریاضی'); |
| 292 | await tester.tap(find.byType(DropdownButtonFormField<String>).first); |
| 293 | await tester.pumpAndSettle(); |
| 294 | await tester.tap(find.text('تکلیف').last); |
| 295 | await tester.pumpAndSettle(); |
| 296 | await tester.ensureVisible(find.byType(SwitchListTile)); |
| 297 | await tester.tap(find.byType(SwitchListTile)); |
| 298 | await tester.pumpAndSettle(); |
| 299 | await tester.tap(find.text('ذخیره')); |
| 300 | await tester.pumpAndSettle(); |
| 301 | expect(find.textContaining('خانوادهای را'), findsOneWidget); |
| 302 | expect(api.mutations, isEmpty); |
| 303 | await tester |
| 304 | .ensureVisible(find.byType(DropdownButtonFormField<String>).last); |
| 305 | await tester.tap(find.byType(DropdownButtonFormField<String>).last); |
| 306 | await tester.pumpAndSettle(); |
| 307 | await tester.tap(find.text('خانواده').last); |
| 308 | await tester.pumpAndSettle(); |
| 309 | await tester.tap(find.text('ذخیره')); |
| 310 | await tester.pumpAndSettle(); |
| 311 | final payload = api.mutations.single['payload'] as Map; |
| 312 | expect(payload['kind'], 'assignment'); |
| 313 | expect(payload['familyId'], 'family-1'); |
| 314 | expect(payload['visibility'], 'parent_guardian'); |
| 315 | expect(payload['plannedDurationSeconds'], 1800); |
| 316 | await tester.pumpWidget(const SizedBox()); |
| 317 | }); |
| 318 | testWidgets( |
| 319 | 'phone registration exposes actual unavailable SMS error without delivery claim', |
| 320 | (tester) async { |
| 321 | Map? payload; |
| 322 | final api = HttpIdentityApi( |
| 323 | baseUrl: 'https://synthetic.example.test', |
| 324 | client: MockClient((req) async { |
| 325 | payload = jsonDecode(req.body) as Map; |
| 326 | return http.Response('{"error":"sms_unavailable"}', 503, |
| 327 | headers: {'content-type': 'application/json'}); |
| 328 | })); |
| 329 | await tester.pumpWidget(MaterialApp(home: RegistrationPage(api: api))); |
| 330 | await tester.pumpAndSettle(); |
| 331 | await tester.tap(find.text('تلفن')); |
| 332 | await tester.pumpAndSettle(); |
| 333 | await tester.enterText(find.widgetWithText(TextField, 'نام'), 'فرزند'); |
| 334 | await tester.enterText( |
| 335 | find.widgetWithText(TextField, 'شماره تلفن'), '+989123456789'); |
| 336 | await tester.enterText( |
| 337 | find.widgetWithText(TextField, 'رمز عبور'), 'synthetic-password'); |
| 338 | await tester.enterText( |
| 339 | find.widgetWithText(TextField, 'تکرار رمز عبور'), 'synthetic-password'); |
| 340 | await tester.tap(find.text('ثبت درخواست')); |
| 341 | await tester.pumpAndSettle(); |
| 342 | expect(payload!['phone'], '+989123456789'); |
| 343 | expect(payload!.containsKey('email'), isFalse); |
| 344 | expect(find.textContaining('سرویس پیامک فعال نیست'), findsOneWidget); |
| 345 | expect(find.textContaining('درخواست بررسی شد'), findsNothing); |
| 346 | await tester.pumpWidget(const SizedBox()); |
| 347 | api.close(); |
| 348 | }); |
| 349 | testWidgets('profile category controls shell without granting family role', |
| 350 | (tester) async { |
| 351 | await tester.pumpWidget(MaterialApp( |
| 352 | home: HomeShell(api: _CategoryApi('adult', role: 'teen_minor')))); |
| 353 | await tester.pumpAndSettle(); |
| 354 | expect(find.text('برنامهریز'), findsOneWidget); |
| 355 | await tester.pumpWidget(const SizedBox()); |
| 356 | }); |
| 357 | testWidgets( |
| 358 | 'offline homework can be edited while its create awaits acknowledgement', |
| 359 | (tester) async { |
| 360 | final api = _OfflinePlannerApi(); |
| 361 | await tester |
| 362 | .pumpWidget(MaterialApp(home: Scaffold(body: PlannerPage(api: api)))); |
| 363 | await tester.pumpAndSettle(); |
| 364 | await tester.tap(find.text('جدید')); |
| 365 | await tester.pumpAndSettle(); |
| 366 | await tester.enterText(find.widgetWithText(TextField, 'عنوان'), 'ریاضی'); |
| 367 | await tester.tap(find.text('ذخیره')); |
| 368 | await tester.pumpAndSettle(); |
| 369 | await tester.tap(find.byType(PopupMenuButton<String>)); |
| 370 | await tester.pumpAndSettle(); |
| 371 | await tester.tap(find.text('ویرایش')); |
| 372 | await tester.pumpAndSettle(); |
| 373 | await tester.enterText( |
| 374 | find.widgetWithText(TextField, 'عنوان'), 'ریاضی جدید'); |
| 375 | await tester.tap(find.text('ذخیره')); |
| 376 | await tester.pumpAndSettle(); |
| 377 | expect(find.byType(AlertDialog), findsNothing); |
| 378 | expect(find.text('ریاضی جدید'), findsOneWidget); |
| 379 | expect(await OfflineStore.forApi(api).pendingCount(), 2); |
| 380 | await tester.pumpWidget(const SizedBox()); |
| 381 | }); |
| 382 | testWidgets( |
| 383 | 'email ownership verification requires explicit new password submission', |
| 384 | (tester) async { |
| 385 | Map? submitted; |
| 386 | final api = HttpIdentityApi( |
| 387 | baseUrl: 'https://synthetic.example.test', |
| 388 | client: MockClient((req) async { |
| 389 | submitted = jsonDecode(req.body) as Map; |
| 390 | return http.Response('{}', 200, |
| 391 | headers: {'content-type': 'application/json'}); |
| 392 | })); |
| 393 | await tester.pumpWidget( |
| 394 | MaterialApp(home: VerifyEmailPage(api: api, token: 'synthetic-proof'))); |
| 395 | await tester.pumpAndSettle(); |
| 396 | expect(submitted, isNull); |
| 397 | await tester.enterText( |
| 398 | find.widgetWithText(TextField, 'رمز عبور جدید'), 'synthetic-password'); |
| 399 | await tester.enterText( |
| 400 | find.widgetWithText(TextField, 'تکرار رمز عبور'), 'synthetic-password'); |
| 401 | await tester.tap(find.text('تأیید و تعیین رمز عبور')); |
| 402 | await tester.pumpAndSettle(); |
| 403 | expect(submitted, |
| 404 | {'token': 'synthetic-proof', 'newPassword': 'synthetic-password'}); |
| 405 | expect(find.text('ایمیل تأیید شد'), findsOneWidget); |
| 406 | await tester.pumpWidget(const SizedBox()); |
| 407 | api.close(); |
| 408 | }); |
| 409 | testWidgets('editing a private task can explicitly change guardian sharing', |
| 410 | (tester) async { |
| 411 | final api = _PlannerWritesApi(); |
| 412 | await tester.pumpWidget(MaterialApp( |
| 413 | home: Scaffold( |
| 414 | body: Builder( |
| 415 | builder: (context) => TextButton( |
| 416 | onPressed: () => showDialog( |
| 417 | context: context, |
| 418 | builder: (_) => PlanItemDialog(api: api, item: { |
| 419 | 'id': 'saved-plan', |
| 420 | 'title': 'ریاضی', |
| 421 | 'kind': 'assignment', |
| 422 | 'status': 'planned', |
| 423 | 'visibility': 'private', |
| 424 | 'version': 1 |
| 425 | })), |
| 426 | child: const Text('باز کردن')))))); |
| 427 | await tester.tap(find.text('باز کردن')); |
| 428 | await tester.pumpAndSettle(); |
| 429 | await tester.ensureVisible(find.byType(SwitchListTile)); |
| 430 | await tester.tap(find.byType(SwitchListTile)); |
| 431 | await tester.pumpAndSettle(); |
| 432 | await tester |
| 433 | .ensureVisible(find.byType(DropdownButtonFormField<String>).last); |
| 434 | await tester.tap(find.byType(DropdownButtonFormField<String>).last); |
| 435 | await tester.pumpAndSettle(); |
| 436 | await tester.tap(find.text('خانواده').last); |
| 437 | await tester.pumpAndSettle(); |
| 438 | await tester.tap(find.text('ذخیره')); |
| 439 | await tester.pumpAndSettle(); |
| 440 | expect((api.mutations.single['payload'] as Map)['visibility'], |
| 441 | 'parent_guardian'); |
| 442 | expect(find.byType(AlertDialog), findsNothing); |
| 443 | await tester.pumpWidget(const SizedBox()); |
| 444 | }); |
| 445 | testWidgets('profile category routing does not depend on family availability', |
| 446 | (tester) async { |
| 447 | await tester.pumpWidget( |
| 448 | MaterialApp(home: HomeShell(api: _CategoryWithoutFamiliesApi()))); |
| 449 | await tester.pumpAndSettle(); |
| 450 | expect(find.text('برنامهریز'), findsOneWidget); |
| 451 | await tester.pumpWidget(const SizedBox()); |
| 452 | }); |
| 453 | testWidgets( |
| 454 | 'logout warns before discarding an unsent change and cancel preserves identity', |
| 455 | (tester) async { |
| 456 | final session = _MemorySession(); |
| 457 | final api = HttpIdentityApi( |
| 458 | baseUrl: 'https://synthetic.example.test', |
| 459 | sessionStore: session, |
| 460 | client: MockClient((req) async => http.Response( |
| 461 | jsonEncode(req.url.path.endsWith('/profile') |
| 462 | ? { |
| 463 | 'profile_category': 'adult', |
| 464 | 'theme_preference': 'adult_blue' |
| 465 | } |
| 466 | : req.url.path.endsWith('/families') |
| 467 | ? {'families': []} |
| 468 | : {'items': []}), |
| 469 | 200, |
| 470 | headers: {'content-type': 'application/json'}))) |
| 471 | ..accessToken = 'synthetic-token' |
| 472 | ..currentUserId = 'child'; |
| 473 | final runtime = RuntimeController(sessionStore: session) |
| 474 | ..initialized = true |
| 475 | ..config = RuntimeConfig(apiBaseUrl: api.baseUrl) |
| 476 | ..api = api; |
| 477 | await OfflineStore.forApi(api).enqueue({ |
| 478 | 'id': '9f7b762c-905f-48a1-81b3-ded3ed72d3c0', |
| 479 | 'entityId': '3a940940-f482-4b5a-937a-d74cc9ef9daf', |
| 480 | 'entityType': 'plan_item', |
| 481 | 'operation': 'create', |
| 482 | 'expectedVersion': 0, |
| 483 | 'payload': {'kind': 'task', 'title': 'معلق'} |
| 484 | }); |
| 485 | await tester.pumpWidget(LifeMateApp(runtime: runtime)); |
| 486 | await tester.pumpAndSettle(); |
| 487 | tester.firstState<ScaffoldState>(find.byType(Scaffold)).openDrawer(); |
| 488 | await tester.pumpAndSettle(); |
| 489 | await tester.scrollUntilVisible(find.text('خروج از حساب'), 100, |
| 490 | scrollable: find |
| 491 | .descendant( |
| 492 | of: find.byType(Drawer), matching: find.byType(Scrollable)) |
| 493 | .first); |
| 494 | await tester.pumpAndSettle(); |
| 495 | await tester.tap(find.text('خروج از حساب')); |
| 496 | await tester.pumpAndSettle(); |
| 497 | expect(find.textContaining('تغییر معلق'), findsOneWidget); |
| 498 | expect(api.accessToken, isNotNull); |
| 499 | await tester.tap(find.text('انصراف')); |
| 500 | await tester.pumpAndSettle(); |
| 501 | expect(api.accessToken, isNotNull); |
| 502 | expect(await OfflineStore.forApi(api).pendingCount(), 1); |
| 503 | await tester.pumpWidget(const SizedBox()); |
| 504 | runtime.dispose(); |
| 505 | }); |
| 506 | testWidgets( |
| 507 | 'endpoint save warns before discard and cancel preserves endpoint and queue', |
| 508 | (tester) async { |
| 509 | final session = _MemorySession(); |
| 510 | final api = HttpIdentityApi( |
| 511 | baseUrl: 'https://synthetic.example.test', |
| 512 | sessionStore: session, |
| 513 | client: MockClient((req) async => http.Response( |
| 514 | jsonEncode(req.url.path.endsWith('/profile') |
| 515 | ? { |
| 516 | 'profile_category': 'adult', |
| 517 | 'theme_preference': 'adult_blue' |
| 518 | } |
| 519 | : req.url.path.endsWith('/families') |
| 520 | ? {'families': []} |
| 521 | : {'items': []}), |
| 522 | 200, |
| 523 | headers: {'content-type': 'application/json'}))) |
| 524 | ..accessToken = 'synthetic-token' |
| 525 | ..currentUserId = 'child'; |
| 526 | final runtime = RuntimeController(sessionStore: session) |
| 527 | ..initialized = true |
| 528 | ..config = RuntimeConfig(apiBaseUrl: api.baseUrl) |
| 529 | ..api = api; |
| 530 | await OfflineStore.forApi(api).enqueue({ |
| 531 | 'id': '9f7b762c-905f-48a1-81b3-ded3ed72d3c0', |
| 532 | 'entityId': '3a940940-f482-4b5a-937a-d74cc9ef9daf', |
| 533 | 'entityType': 'plan_item', |
| 534 | 'operation': 'create', |
| 535 | 'expectedVersion': 0, |
| 536 | 'payload': {'kind': 'task', 'title': 'معلق'} |
| 537 | }); |
| 538 | await tester.pumpWidget(LifeMateApp(runtime: runtime)); |
| 539 | await tester.pumpAndSettle(); |
| 540 | tester.firstState<ScaffoldState>(find.byType(Scaffold)).openDrawer(); |
| 541 | await tester.pumpAndSettle(); |
| 542 | await tester.scrollUntilVisible(find.text('تنظیم اتصال'), 100, |
| 543 | scrollable: find |
| 544 | .descendant( |
| 545 | of: find.byType(Drawer), matching: find.byType(Scrollable)) |
| 546 | .first); |
| 547 | await tester.pumpAndSettle(); |
| 548 | await tester.tap(find.text('تنظیم اتصال')); |
| 549 | await tester.pumpAndSettle(); |
| 550 | await tester.enterText(find.widgetWithText(TextField, 'نشانی اتصال API'), |
| 551 | 'https://other.synthetic.example.test'); |
| 552 | await tester.ensureVisible(find.text('ذخیره و ادامه')); |
| 553 | await tester.pumpAndSettle(); |
| 554 | await tester.tap(find.text('ذخیره و ادامه')); |
| 555 | await tester.pumpAndSettle(); |
| 556 | expect(find.textContaining('تغییر معلق'), findsOneWidget); |
| 557 | await tester.tap(find.text('انصراف')); |
| 558 | await tester.pumpAndSettle(); |
| 559 | expect(runtime.config.apiBaseUrl, 'https://synthetic.example.test'); |
| 560 | expect(api.accessToken, isNotNull); |
| 561 | expect(await OfflineStore.forApi(api).pendingCount(), 1); |
| 562 | await tester.pumpWidget(const SizedBox()); |
| 563 | runtime.dispose(); |
| 564 | }); |
| 565 | testWidgets( |
| 566 | 'offline planner fallback shows owned rows and hides shared child rows', |
| 567 | (tester) async { |
| 568 | final api = HttpIdentityApi( |
| 569 | baseUrl: 'https://synthetic.example.test', |
| 570 | client: MockClient((_) async => |
| 571 | throw http.ClientException('synthetic transport failure'))) |
| 572 | ..accessToken = 'synthetic-token' |
| 573 | ..currentUserId = 'parent'; |
| 574 | await OfflineStore.forApi(api).cachePlanner([ |
| 575 | { |
| 576 | 'id': 'own', |
| 577 | 'owner_user_id': 'parent', |
| 578 | 'title': 'کار خودم', |
| 579 | 'kind': 'task', |
| 580 | 'status': 'planned' |
| 581 | }, |
| 582 | { |
| 583 | 'id': 'shared-child', |
| 584 | 'owner_user_id': 'child', |
| 585 | 'title': 'داده فرزند', |
| 586 | 'kind': 'assignment', |
| 587 | 'status': 'planned' |
| 588 | } |
| 589 | ]); |
| 590 | await tester |
| 591 | .pumpWidget(MaterialApp(home: Scaffold(body: PlannerPage(api: api)))); |
| 592 | await tester.pumpAndSettle(); |
| 593 | expect(find.text('کار خودم'), findsOneWidget); |
| 594 | expect(find.text('داده فرزند'), findsNothing); |
| 595 | expect(find.textContaining('داده زنده نیست'), findsOneWidget); |
| 596 | await tester.pumpWidget(const SizedBox()); |
| 597 | api.close(); |
| 598 | }); |
| 599 | testWidgets( |
| 600 | 'offline owned pending create remains visible without a server owner field', |
| 601 | (tester) async { |
| 602 | final api = HttpIdentityApi( |
| 603 | baseUrl: 'https://synthetic.example.test', |
| 604 | client: MockClient((_) async => |
| 605 | throw http.ClientException('synthetic transport failure'))) |
| 606 | ..accessToken = 'synthetic-token' |
| 607 | ..currentUserId = 'child'; |
| 608 | await PlanSync(api).create( |
| 609 | {'kind': 'assignment', 'title': 'تکلیف خودم', 'visibility': 'private'}); |
| 610 | await tester |
| 611 | .pumpWidget(MaterialApp(home: Scaffold(body: PlannerPage(api: api)))); |
| 612 | await tester.pumpAndSettle(); |
| 613 | expect(find.text('تکلیف خودم'), findsOneWidget); |
| 614 | expect(find.textContaining('در انتظار تأیید سرور'), findsWidgets); |
| 615 | await tester.pumpWidget(const SizedBox()); |
| 616 | api.close(); |
| 617 | }); |
| 618 | testWidgets( |
| 619 | 'owner exam grade edit sends normalized points and current version', |
| 620 | (tester) async { |
| 621 | final api = _PlannerWritesApi(); |
| 622 | await tester.pumpWidget(MaterialApp( |
| 623 | home: Scaffold( |
| 624 | body: Builder( |
| 625 | builder: (context) => TextButton( |
| 626 | onPressed: () => showDialog( |
| 627 | context: context, |
| 628 | builder: (_) => PlanItemDialog(api: api, item: { |
| 629 | 'id': 'saved-exam', |
| 630 | 'title': 'امتحان ریاضی', |
| 631 | 'kind': 'exam', |
| 632 | 'status': 'planned', |
| 633 | 'visibility': 'private', |
| 634 | 'version': 4 |
| 635 | })), |
| 636 | child: const Text('باز کردن')))))); |
| 637 | await tester.tap(find.text('باز کردن')); |
| 638 | await tester.pumpAndSettle(); |
| 639 | await tester |
| 640 | .ensureVisible(find.widgetWithText(TextField, 'نمره (اختیاری)')); |
| 641 | await tester.enterText( |
| 642 | find.widgetWithText(TextField, 'نمره (اختیاری)'), '۱۵٫۵'); |
| 643 | await tester |
| 644 | .ensureVisible(find.widgetWithText(TextField, 'سقف نمره (اختیاری)')); |
| 645 | await tester.enterText( |
| 646 | find.widgetWithText(TextField, 'سقف نمره (اختیاری)'), '۲۰'); |
| 647 | await tester.tap(find.text('ذخیره')); |
| 648 | await tester.pumpAndSettle(); |
| 649 | expect(api.mutations.single['expectedVersion'], 4); |
| 650 | expect((api.mutations.single['payload'] as Map)['gradePoints'], 15.5); |
| 651 | expect((api.mutations.single['payload'] as Map)['gradeOutOf'], 20); |
| 652 | expect(find.byType(AlertDialog), findsNothing); |
| 653 | await tester.pumpWidget(const SizedBox()); |
| 654 | }); |
| 655 | } |