LifeMate — Personal, Family & Learning Companion
fix/personal-upgrade-delivery-guard
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:async'; |
| 2 | import 'package:flutter/services.dart'; |
| 3 | import 'package:flutter_test/flutter_test.dart'; |
| 4 | import 'package:http/testing.dart'; |
| 5 | import 'package:lifemate/api.dart'; |
| 6 | import 'package:lifemate/local_reminders.dart'; |
| 7 | import 'package:lifemate/offline_store.dart'; |
| 8 | import 'package:lifemate/plan_sync.dart'; |
| 9 | import 'package:lifemate/session_store.dart'; |
| 10 | import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; |
| 11 | import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; |
| 12 | |
| 13 | class _Session implements SessionStore { |
| 14 | final List<String> events; |
| 15 | _Session(this.events); |
| 16 | @override |
| 17 | Future<String?> read() async => null; |
| 18 | @override |
| 19 | Future<void> write(String value) async {} |
| 20 | @override |
| 21 | Future<void> clear() async { |
| 22 | events.add('credential-clear'); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | class _Port implements LocalReminderPort { |
| 27 | final List<String> events; |
| 28 | final OfflineStore store; |
| 29 | _Port(this.events, this.store); |
| 30 | @override |
| 31 | Future<void> suppressPlanItems(String scope, Set<String> ids) async { |
| 32 | expect(scope, store.scope); |
| 33 | expect(ids, {'22222222-2222-4222-8222-222222222222'}); |
| 34 | expect(await store.pendingCount(), 1); |
| 35 | events.add('suppress'); |
| 36 | } |
| 37 | |
| 38 | @override |
| 39 | Future<void> deactivate() async { |
| 40 | events.add('deactivate'); |
| 41 | } |
| 42 | |
| 43 | @override |
| 44 | dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 45 | } |
| 46 | |
| 47 | class _OfflineApi implements IdentityApi { |
| 48 | final List<String> events; |
| 49 | _OfflineApi(this.events); |
| 50 | @override |
| 51 | Future<Map<String, dynamic>> submitSyncMutations( |
| 52 | List<Map<String, dynamic>> values) async { |
| 53 | events.add('send'); |
| 54 | throw const ApiException(0, 'network_unavailable'); |
| 55 | } |
| 56 | |
| 57 | @override |
| 58 | dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 59 | } |
| 60 | |
| 61 | void main() { |
| 62 | TestWidgetsFlutterBinding.ensureInitialized(); |
| 63 | setUp(() { |
| 64 | SharedPreferencesAsyncPlatform.instance = |
| 65 | InMemorySharedPreferencesAsync.withData({}); |
| 66 | String? pending; |
| 67 | TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger |
| 68 | .setMockMethodCallHandler( |
| 69 | const MethodChannel('lifeguide/local_write_guard'), (call) async { |
| 70 | if (call.method == 'getBlocked') return pending != null; |
| 71 | if (call.method == 'beginWrite') return pending = 'synthetic'; |
| 72 | if (call.method == 'completeWrite' && call.arguments == pending) { |
| 73 | pending = null; |
| 74 | return true; |
| 75 | } |
| 76 | throw PlatformException(code: 'local_storage_restart_required'); |
| 77 | }); |
| 78 | }); |
| 79 | test( |
| 80 | 'offline completion suppresses old alarm after durable enqueue before send', |
| 81 | () async { |
| 82 | final events = <String>[], |
| 83 | store = OfflineStore.forNamespace('synthetic-reminder-scope'); |
| 84 | final result = await PlanSync(_OfflineApi(events), |
| 85 | store: store, reminders: _Port(events, store)) |
| 86 | .complete({ |
| 87 | 'id': '22222222-2222-4222-8222-222222222222', |
| 88 | 'title': 'برنامهٔ ساختگی', |
| 89 | 'status': 'planned', |
| 90 | 'version': 1 |
| 91 | }); |
| 92 | expect(result.queued, isTrue); |
| 93 | expect(events, ['suppress', 'send']); |
| 94 | }); |
| 95 | test('logout starts alarm cancellation before clearing credentials', |
| 96 | () async { |
| 97 | final events = <String>[]; |
| 98 | final gate = Completer<void>(); |
| 99 | final api = HttpIdentityApi( |
| 100 | baseUrl: 'https://fixture.invalid/api', |
| 101 | client: MockClient((_) async => throw StateError('Unexpected network')), |
| 102 | sessionStore: _Session(events), |
| 103 | onSessionDeactivating: () async { |
| 104 | events.add('cancel-start'); |
| 105 | await gate.future; |
| 106 | events.add('cancel-finish'); |
| 107 | }) |
| 108 | ..accessToken = 'synthetic-access' |
| 109 | ..currentUserId = '11111111-1111-4111-8111-111111111111'; |
| 110 | final signingOut = api.logout(revoke: false); |
| 111 | await Future<void>.delayed(Duration.zero); |
| 112 | expect(events.first, 'cancel-start'); |
| 113 | expect(api.accessToken, isNull); |
| 114 | gate.complete(); |
| 115 | await signingOut; |
| 116 | expect(events.indexOf('cancel-finish'), |
| 117 | lessThan(events.indexOf('credential-clear'))); |
| 118 | }); |
| 119 | test( |
| 120 | 'alarm cancellation failure still clears credentials and reports failure', |
| 121 | () async { |
| 122 | final events = <String>[]; |
| 123 | final api = HttpIdentityApi( |
| 124 | baseUrl: 'https://fixture.invalid/api', |
| 125 | client: MockClient((_) async => throw StateError('Unexpected network')), |
| 126 | sessionStore: _Session(events), |
| 127 | onSessionDeactivating: () async { |
| 128 | throw StateError('synthetic device failure'); |
| 129 | }) |
| 130 | ..accessToken = 'synthetic-access' |
| 131 | ..currentUserId = '11111111-1111-4111-8111-111111111111'; |
| 132 | await expectLater(api.logout(revoke: false), throwsStateError); |
| 133 | expect(api.accessToken, isNull); |
| 134 | expect(api.currentUserId, isNull); |
| 135 | expect(events, ['credential-clear']); |
| 136 | }); |
| 137 | } |