LifeMate — Personal, Family & Learning Companion
fix/backend-request-safety
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 'package:flutter_test/flutter_test.dart'; |
| 2 | import 'dart:convert'; |
| 3 | import 'local_test_preferences.dart'; |
| 4 | import 'package:lifemate/local_only_api.dart'; |
| 5 | import 'package:lifemate/api.dart'; |
| 6 | import 'package:lifemate/local_data_store.dart'; |
| 7 | import 'package:shared_preferences/shared_preferences.dart'; |
| 8 | import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; |
| 9 | import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; |
| 10 | import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; |
| 11 | |
| 12 | final class FailedWrites extends InMemorySharedPreferencesAsync { |
| 13 | FailedWrites(super.data) : super.withData(); |
| 14 | @override |
| 15 | Future<bool> setString( |
| 16 | String key, String value, SharedPreferencesOptions options) async { |
| 17 | await super.setString(key, value, options); |
| 18 | throw StateError('Synthetic native cache publication then rename failure'); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | class FailedNativeCommit extends InMemorySharedPreferencesStore { |
| 23 | FailedNativeCommit(super.data) : super.withData(); |
| 24 | @override |
| 25 | Future<bool> setValue(String type, String key, Object value) async { |
| 26 | await super.setValue(type, key, value); |
| 27 | return false; // Android legacy commit can publish memory before disk fails. |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void main() { |
| 32 | late LocalDataSession session; |
| 33 | setUp(() { |
| 34 | resetLocalPreferences(); |
| 35 | session = LocalDataSession(); |
| 36 | }); |
| 37 | |
| 38 | LocalOnlyApi open() => LocalOnlyApi( |
| 39 | displayName: 'آرام', |
| 40 | category: 'girl_minor', |
| 41 | store: LocalDataStore( |
| 42 | category: 'girl_minor', displayName: 'آرام', session: session)); |
| 43 | |
| 44 | test('assignment survives recreation, update and safe archive', () async { |
| 45 | final created = await open().createPlanItem({ |
| 46 | 'kind': 'assignment', |
| 47 | 'title': 'تمرین ریاضی', |
| 48 | 'dueAt': DateTime.now().toUtc().toIso8601String(), |
| 49 | 'visibility': 'private', |
| 50 | }); |
| 51 | final reopened = open(); |
| 52 | final records = await reopened.listPlanItems(); |
| 53 | expect(records, hasLength(1)); |
| 54 | expect(records.single['id'], created['id']); |
| 55 | expect(records.single['title'], 'تمرین ریاضی'); |
| 56 | await reopened |
| 57 | .updatePlanItem(created['id'].toString(), {'title': 'تمرین علوم'}); |
| 58 | expect((await open().listPlanItems()).single['title'], 'تمرین علوم'); |
| 59 | await open() |
| 60 | .updatePlanItem(created['id'].toString(), {'status': 'cancelled'}); |
| 61 | expect(await open().listPlanItems(), isEmpty); |
| 62 | }); |
| 63 | |
| 64 | test('edited profile is restored by a new API', () async { |
| 65 | await open() |
| 66 | .updateProfile(displayName: 'سارا', themePreference: 'boy_blue'); |
| 67 | final restored = await open().getProfile(); |
| 68 | expect(restored['display_name'], 'سارا'); |
| 69 | expect(restored['theme_preference'], 'boy_blue'); |
| 70 | }); |
| 71 | |
| 72 | test('concurrent writers retain every item with unique identifiers', |
| 73 | () async { |
| 74 | final records = await Future.wait(List.generate( |
| 75 | 20, |
| 76 | (i) => open().createPlanItem({ |
| 77 | 'kind': 'task', |
| 78 | 'title': 'کار $i', |
| 79 | }))); |
| 80 | expect(records.map((item) => item['id']).toSet(), hasLength(20)); |
| 81 | expect(await open().listPlanItems(), hasLength(20)); |
| 82 | }); |
| 83 | |
| 84 | test('invalid changes do not replace persisted records', () async { |
| 85 | final item = |
| 86 | await open().createPlanItem({'kind': 'assignment', 'title': 'علوم'}); |
| 87 | for (final change in <Map<String, dynamic>>[ |
| 88 | {'title': ' '}, |
| 89 | {'status': 'wrong'}, |
| 90 | {'visibility': 'family'}, |
| 91 | {'dueAt': 'not-a-date'}, |
| 92 | {'id': 'spoofed'}, |
| 93 | ]) { |
| 94 | await expectLater(open().updatePlanItem(item['id'] as String, change), |
| 95 | throwsA(isA<ApiException>())); |
| 96 | } |
| 97 | expect((await open().listPlanItems()).single['title'], 'علوم'); |
| 98 | await expectLater(open().updatePlanItem('missing', {'title': 'x'}), |
| 99 | throwsA(isA<ApiException>())); |
| 100 | await expectLater( |
| 101 | open().updateProfile(displayName: ' '), throwsA(isA<ApiException>())); |
| 102 | }); |
| 103 | |
| 104 | test('date window includes due boundary and excludes upper boundary', |
| 105 | () async { |
| 106 | final from = DateTime.utc(2026, 10, 8); |
| 107 | final to = from.add(const Duration(days: 1)); |
| 108 | await open().createPlanItem({ |
| 109 | 'kind': 'assignment', |
| 110 | 'title': 'داخل', |
| 111 | 'dueAt': from.toIso8601String() |
| 112 | }); |
| 113 | await open().createPlanItem( |
| 114 | {'kind': 'task', 'title': 'بعد', 'dueAt': to.toIso8601String()}); |
| 115 | final result = await open().getToday(from: from, to: to); |
| 116 | expect(result.map((item) => item['title']), ['داخل']); |
| 117 | await open() |
| 118 | .updatePlanItem(result.single['id'] as String, {'status': 'completed'}); |
| 119 | expect(await open().getToday(from: from, to: to), isEmpty); |
| 120 | }); |
| 121 | |
| 122 | test('today uses start time when a deadline is on another day', () async { |
| 123 | final from = DateTime.utc(2026, 10, 8); |
| 124 | await open().createPlanItem({ |
| 125 | 'kind': 'assignment', |
| 126 | 'title': 'امروز', |
| 127 | 'startsAt': from.add(const Duration(hours: 10)).toIso8601String(), |
| 128 | 'dueAt': from.add(const Duration(days: 1)).toIso8601String(), |
| 129 | }); |
| 130 | expect( |
| 131 | await open() |
| 132 | .getToday(from: from, to: from.add(const Duration(days: 1))), |
| 133 | hasLength(1)); |
| 134 | }); |
| 135 | |
| 136 | test('numeric dates are rejected instead of coerced into strings', () async { |
| 137 | await expectLater( |
| 138 | open().createPlanItem({ |
| 139 | 'kind': 'task', |
| 140 | 'title': 'زمان نامعتبر', |
| 141 | 'dueAt': 20261008, |
| 142 | }), |
| 143 | throwsA(isA<ApiException>())); |
| 144 | }); |
| 145 | |
| 146 | test('valid JSON with corrupt fields cannot be read or overwritten', |
| 147 | () async { |
| 148 | await open().createPlanItem({'kind': 'task', 'title': 'اول'}); |
| 149 | final original = await open().store.read(); |
| 150 | for (final corrupt in <Map<String, dynamic>>[ |
| 151 | {'notes': <String, dynamic>{}}, |
| 152 | {'priority': 'wrong'}, |
| 153 | {'due_at': 20261008}, |
| 154 | {'starts_at': '2026-10-09T10:00:00Z', 'due_at': '2026-10-08T10:00:00Z'}, |
| 155 | ]) { |
| 156 | final data = jsonDecode(jsonEncode(original)) as Map<String, dynamic>; |
| 157 | (data['items'] as List).first.addAll(corrupt); |
| 158 | final raw = jsonEncode(data); |
| 159 | resetLocalPreferences({LocalDataStore.key: raw}); |
| 160 | await expectLater( |
| 161 | open().listPlanItems(), |
| 162 | throwsA(isA<ApiException>() |
| 163 | .having((e) => e.code, 'code', 'local_data_corrupt'))); |
| 164 | await expectLater(open().updateProfile(displayName: 'دوم'), |
| 165 | throwsA(isA<ApiException>())); |
| 166 | expect(await SharedPreferencesAsync().getString(LocalDataStore.key), raw); |
| 167 | } |
| 168 | }); |
| 169 | |
| 170 | test('corrupt data raises error and is never overwritten', () async { |
| 171 | const bad = '{invalid'; |
| 172 | resetLocalPreferences({LocalDataStore.key: bad}); |
| 173 | await expectLater( |
| 174 | open().listPlanItems(), |
| 175 | throwsA(isA<ApiException>() |
| 176 | .having((e) => e.code, 'code', 'local_data_corrupt'))); |
| 177 | await expectLater(open().createPlanItem({'kind': 'task', 'title': 'new'}), |
| 178 | throwsA(isA<ApiException>())); |
| 179 | expect(await SharedPreferencesAsync().getString(LocalDataStore.key), bad); |
| 180 | }); |
| 181 | |
| 182 | test('failed native write requires restart and blocks further operations', |
| 183 | () async { |
| 184 | final item = await open().createPlanItem({'kind': 'task', 'title': 'اول'}); |
| 185 | final raw = (await SharedPreferencesAsync().getString(LocalDataStore.key))!; |
| 186 | SharedPreferencesAsyncPlatform.instance = |
| 187 | FailedWrites({LocalDataStore.key: raw}); |
| 188 | await expectLater( |
| 189 | open().updatePlanItem(item['id'] as String, {'title': 'دوم'}), |
| 190 | throwsA(isA<ApiException>() |
| 191 | .having((e) => e.code, 'code', 'local_storage_restart_required'))); |
| 192 | await expectLater( |
| 193 | open().listPlanItems(), |
| 194 | throwsA(isA<ApiException>() |
| 195 | .having((e) => e.code, 'code', 'local_storage_restart_required'))); |
| 196 | final uncertain = jsonDecode( |
| 197 | (await SharedPreferencesAsync().getString(LocalDataStore.key))!) as Map; |
| 198 | expect((uncertain['items'] as List).first['title'], 'دوم'); |
| 199 | resetLocalPreferences({ |
| 200 | LocalDataStore.key: raw |
| 201 | }); // simulate native process restart + disk read |
| 202 | await expectLater(open().listPlanItems(), throwsA(isA<ApiException>())); |
| 203 | session = LocalDataSession(); |
| 204 | expect((await open().listPlanItems()).single['title'], 'اول'); |
| 205 | }); |
| 206 | |
| 207 | test('reads never trust legacy memory published by a failed commit', |
| 208 | () async { |
| 209 | await open().createPlanItem({'kind': 'task', 'title': 'اول'}); |
| 210 | final original = await open().store.read(); |
| 211 | final raw = jsonEncode(original); |
| 212 | SharedPreferencesAsyncPlatform.instance = |
| 213 | InMemorySharedPreferencesAsync.withData({LocalDataStore.key: raw}); |
| 214 | SharedPreferencesStorePlatform.instance = |
| 215 | FailedNativeCommit({'flutter.${LocalDataStore.key}': raw}); |
| 216 | (original['items'] as List).first['title'] = 'ذخیره ناموفق'; |
| 217 | final prefs = await SharedPreferences.getInstance(); |
| 218 | expect(await prefs.setString(LocalDataStore.key, jsonEncode(original)), |
| 219 | isFalse); |
| 220 | expect((await open().listPlanItems()).single['title'], 'اول'); |
| 221 | }); |
| 222 | |
| 223 | test('unimplemented server features cannot fabricate success', () async { |
| 224 | expect(open().accessToken, isNull); |
| 225 | expect( |
| 226 | () => open().createAiSession('study'), |
| 227 | throwsA(isA<ApiException>() |
| 228 | .having((e) => e.code, 'code', 'local_feature_unavailable'))); |
| 229 | expect(() => open().createFamily('خانواده'), throwsA(isA<ApiException>())); |
| 230 | }); |
| 231 | } |