LifeMate — Personal, Family & Learning Companion
fix/study-duration-display
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 'package:flutter/services.dart'; |
| 3 | import 'package:lifemate/api.dart'; |
| 4 | import 'package:lifemate/offline_store.dart'; |
| 5 | import 'package:lifemate/plan_sync.dart'; |
| 6 | import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; |
| 7 | import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; |
| 8 | |
| 9 | class _SyncApi implements IdentityApi { |
| 10 | bool offline = true; |
| 11 | bool conflict = false; |
| 12 | String? malformedAck; |
| 13 | final sent = <Map<String, dynamic>>[]; |
| 14 | final items = <String, Map<String, dynamic>>{}; |
| 15 | final applied = <String>{}; |
| 16 | @override |
| 17 | dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 18 | @override |
| 19 | Future<Map<String, dynamic>> submitSyncMutations( |
| 20 | List<Map<String, dynamic>> mutations) async { |
| 21 | if (offline) throw const ApiException(0, 'network_unavailable'); |
| 22 | return { |
| 23 | 'results': mutations.map((m) { |
| 24 | sent.add(Map<String, dynamic>.from(m)); |
| 25 | if (conflict) { |
| 26 | return { |
| 27 | 'id': m['id'], |
| 28 | 'status': 'conflict', |
| 29 | 'error': 'version_conflict' |
| 30 | }; |
| 31 | } |
| 32 | if (m['operation'] != 'create' && !items.containsKey(m['entityId'])) { |
| 33 | return {'id': m['id'], 'status': 'rejected', 'error': 'not_found'}; |
| 34 | } |
| 35 | final item = items.putIfAbsent( |
| 36 | m['entityId'] as String, |
| 37 | () => { |
| 38 | 'id': m['entityId'], |
| 39 | 'title': (m['payload'] as Map)['title'], |
| 40 | 'kind': 'assignment', |
| 41 | 'status': 'planned', |
| 42 | 'visibility': 'private', |
| 43 | 'version': 1, |
| 44 | }); |
| 45 | if (applied.add(m['id'] as String) && m['operation'] != 'create') { |
| 46 | if (item['version'] != m['expectedVersion']) { |
| 47 | return { |
| 48 | 'id': m['id'], |
| 49 | 'status': 'conflict', |
| 50 | 'error': 'version_conflict' |
| 51 | }; |
| 52 | } |
| 53 | item.addAll(Map<String, dynamic>.from(m['payload'] as Map)); |
| 54 | item['version'] = (item['version'] as int) + 1; |
| 55 | } |
| 56 | return { |
| 57 | 'id': m['id'], |
| 58 | 'status': 'accepted', |
| 59 | if (malformedAck != 'missing') |
| 60 | 'item': { |
| 61 | ...item, |
| 62 | if (malformedAck == 'wrong') 'id': 'wrong-entity' |
| 63 | }, |
| 64 | 'acceptedAt': '2026-10-08T10:00:00Z' |
| 65 | }; |
| 66 | }).toList() |
| 67 | }; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void main() { |
| 72 | TestWidgetsFlutterBinding.ensureInitialized(); |
| 73 | setUp(() { |
| 74 | SharedPreferencesAsyncPlatform.instance = |
| 75 | InMemorySharedPreferencesAsync.withData({}); |
| 76 | String? pending; |
| 77 | TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger |
| 78 | .setMockMethodCallHandler( |
| 79 | const MethodChannel('lifeguide/local_write_guard'), (call) async { |
| 80 | if (call.method == 'getBlocked') return pending != null; |
| 81 | if (call.method == 'beginWrite') return pending = 'owned-test-token'; |
| 82 | if (call.method == 'completeWrite' && call.arguments == pending) { |
| 83 | pending = null; |
| 84 | return true; |
| 85 | } |
| 86 | throw PlatformException(code: 'local_storage_restart_required'); |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | test( |
| 91 | 'offline create survives reopening then acknowledges once with stable IDs', |
| 92 | () async { |
| 93 | final api = _SyncApi(); |
| 94 | final store = OfflineStore.forNamespace('test|child'); |
| 95 | final result = await PlanSync(api, store: store) |
| 96 | .create({'kind': 'assignment', 'title': 'ریاضی'}); |
| 97 | expect(result.queued, isTrue); |
| 98 | final reopened = OfflineStore.forNamespace('test|child'); |
| 99 | final queue = await reopened.readQueue(); |
| 100 | expect(queue, hasLength(1)); |
| 101 | expect(queue.single['id'], matches(RegExp(r'^[a-f0-9-]{36}$'))); |
| 102 | expect(queue.single['entityId'], result.item!['id']); |
| 103 | api.offline = false; |
| 104 | await PlanSync(api, store: reopened).flush(); |
| 105 | await PlanSync(api, store: reopened).flush(); |
| 106 | expect(api.sent, hasLength(1)); |
| 107 | expect(api.sent.single['id'], queue.single['id']); |
| 108 | expect(await reopened.pendingCount(), 0); |
| 109 | expect(await reopened.readLastAck(), isNotNull); |
| 110 | }); |
| 111 | |
| 112 | test('separate account/server scopes never share cache or pending changes', |
| 113 | () async { |
| 114 | final first = OfflineStore.forNamespace('https://one.test/api|child'); |
| 115 | final parent = OfflineStore.forNamespace('https://one.test/api|parent'); |
| 116 | final other = OfflineStore.forNamespace('https://two.test/api|child'); |
| 117 | await first.cachePlanner([ |
| 118 | {'id': 'private', 'notes': 'synthetic private note'} |
| 119 | ]); |
| 120 | expect(await parent.readPlanner(), isEmpty); |
| 121 | expect(await other.readPlanner(), isEmpty); |
| 122 | await first.clear(); |
| 123 | expect(await OfflineStore.forNamespace(first.scope).readPlanner(), isEmpty); |
| 124 | await expectLater( |
| 125 | first.cachePlanner([ |
| 126 | {'id': 'stale'} |
| 127 | ]), |
| 128 | throwsA(isA<ApiException>())); |
| 129 | }); |
| 130 | |
| 131 | test('offline create then complete survives restart and both acknowledge', |
| 132 | () async { |
| 133 | final api = _SyncApi(); |
| 134 | final store = OfflineStore.forNamespace('chain|child'); |
| 135 | final created = await PlanSync(api, store: store) |
| 136 | .create({'kind': 'assignment', 'title': 'ریاضی'}); |
| 137 | await PlanSync(api, store: store).complete(created.item!); |
| 138 | api.offline = false; |
| 139 | final reopened = OfflineStore.forNamespace(store.scope); |
| 140 | await PlanSync(api, store: reopened).flush(); |
| 141 | expect(await reopened.pendingCount(), 0); |
| 142 | expect(api.items.values.single['status'], 'completed'); |
| 143 | expect(api.items.values.single['version'], 2); |
| 144 | }); |
| 145 | |
| 146 | test('reconnection drains the pending create before a new completion', |
| 147 | () async { |
| 148 | final api = _SyncApi(); |
| 149 | final store = OfflineStore.forNamespace('reconnect|child'); |
| 150 | final created = |
| 151 | await PlanSync(api, store: store).create({'title': 'ریاضی'}); |
| 152 | api.offline = false; |
| 153 | await PlanSync(api, store: store).complete(created.item!); |
| 154 | expect(api.sent.map((m) => m['operation']), ['create', 'complete']); |
| 155 | expect(await store.pendingCount(), 0); |
| 156 | }); |
| 157 | |
| 158 | test('malformed acknowledgement never removes the pending change', () async { |
| 159 | for (final malformed in ['missing', 'wrong']) { |
| 160 | final api = _SyncApi() |
| 161 | ..offline = false |
| 162 | ..malformedAck = malformed; |
| 163 | final store = OfflineStore.forNamespace('ack-$malformed|child'); |
| 164 | await expectLater( |
| 165 | PlanSync(api, store: store).create({'title': 'ریاضی'}), |
| 166 | throwsA(isA<ApiException>() |
| 167 | .having((e) => e.code, 'code', 'invalid_sync_acknowledgement'))); |
| 168 | expect(await store.pendingCount(), 1); |
| 169 | } |
| 170 | }); |
| 171 | |
| 172 | test('fresh server cache does not erase an unsent optimistic update', |
| 173 | () async { |
| 174 | final api = _SyncApi(); |
| 175 | final store = OfflineStore.forNamespace('overlay|child'); |
| 176 | await store.cachePlanner([ |
| 177 | { |
| 178 | 'id': '00000000-0000-4000-8000-000000000111', |
| 179 | 'title': 'old', |
| 180 | 'version': 1 |
| 181 | } |
| 182 | ]); |
| 183 | await PlanSync(api, store: store) |
| 184 | .update((await store.readPlanner()).single, {'title': 'offline edit'}); |
| 185 | await store.cachePlanner([ |
| 186 | { |
| 187 | 'id': '00000000-0000-4000-8000-000000000111', |
| 188 | 'title': 'old', |
| 189 | 'version': 1 |
| 190 | } |
| 191 | ]); |
| 192 | expect((await store.readPlanner()).single['title'], 'offline edit'); |
| 193 | expect((await store.readPlanner()).single['pending_sync'], isTrue); |
| 194 | }); |
| 195 | |
| 196 | test('conflicts retain the change and never become an offline success', |
| 197 | () async { |
| 198 | final api = _SyncApi(); |
| 199 | final store = OfflineStore.forNamespace('test|child'); |
| 200 | await PlanSync(api, store: store) |
| 201 | .create({'kind': 'assignment', 'title': 'ریاضی'}); |
| 202 | api.offline = false; |
| 203 | api.conflict = true; |
| 204 | await expectLater( |
| 205 | PlanSync(api, store: store).flush(), |
| 206 | throwsA(isA<ApiException>() |
| 207 | .having((e) => e.code, 'code', 'sync_conflict'))); |
| 208 | expect(await store.pendingCount(), 1); |
| 209 | expect((await store.readQueue()).single['queueStatus'], 'conflict'); |
| 210 | }); |
| 211 | |
| 212 | test('HTTP permission, validation, server and storage errors are not offline', |
| 213 | () { |
| 214 | for (final status in [401, 403, 409, 422, 500, 503]) { |
| 215 | expect(isOfflineFailure(ApiException(status, 'failure')), isFalse); |
| 216 | } |
| 217 | expect( |
| 218 | isOfflineFailure(const ApiException(0, 'network_unavailable')), isTrue); |
| 219 | expect(isOfflineFailure(const ApiException(0, 'local_storage_failed')), |
| 220 | isFalse); |
| 221 | }); |
| 222 | } |