LifeMate — Personal, Family & Learning Companion

feat/aparat-learning-library
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
dart 236 lines 8.66 KB
Raw
1 import 'dart:convert';
2
3 import 'package:flutter/material.dart';
4 import 'package:flutter/services.dart';
5 import 'package:flutter_test/flutter_test.dart';
6 import 'package:http/http.dart' as http;
7 import 'package:http/testing.dart';
8 import 'package:lifemate/api.dart';
9 import 'package:lifemate/education_ui.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 // Explicit HTTP/widget fixtures, not evidence of a live curriculum or playback.
14 const courseId = '10000000-0000-4000-8000-000000000001';
15 const firstId = '20000000-0000-4000-8000-000000000001';
16 const lastId = '20000000-0000-4000-8000-000000000002';
17
18 void main() {
19 TestWidgetsFlutterBinding.ensureInitialized();
20 setUp(() {
21 SharedPreferencesAsyncPlatform.instance =
22 InMemorySharedPreferencesAsync.withData({});
23 String? token;
24 TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
25 .setMockMethodCallHandler(
26 const MethodChannel('lifeguide/local_write_guard'), (call) async {
27 if (call.method == 'getBlocked') return token != null;
28 if (call.method == 'beginWrite') {
29 return token = 'education-widget-fixture';
30 }
31 if (call.method == 'completeWrite' && call.arguments == token) {
32 token = null;
33 return true;
34 }
35 throw PlatformException(code: 'local_storage_restart_required');
36 });
37 });
38
39 Widget screen(Widget child) => MaterialApp(
40 home: Scaffold(
41 body:
42 Directionality(textDirection: TextDirection.rtl, child: child)));
43 http.Response response(Object data) => http.Response(jsonEncode(data), 200,
44 headers: {'content-type': 'application/json; charset=utf-8'});
45 HttpIdentityApi makeApi(
46 Future<http.Response> Function(http.Request) handler) =>
47 HttpIdentityApi(
48 baseUrl: 'https://education.example.test/api',
49 client: MockClient(handler))
50 ..accessToken = 'synthetic-fixture-token'
51 ..currentUserId = 'synthetic-learner';
52
53 testWidgets(
54 'no education profile asks selection without silently choosing grade7',
55 (tester) async {
56 final api = makeApi((request) async => response({
57 'education': null,
58 'courses': [],
59 'fetchedAt': '2026-10-09T10:00:00Z'
60 }));
61 await tester.pumpWidget(screen(EducationLibraryPage(api: api)));
62 await tester.pumpAndSettle();
63 expect(find.textContaining('پایه تحصیلی را انتخاب کن'), findsOneWidget);
64 expect(find.textContaining('پایه 7'), findsNothing);
65 expect(tester.takeException(), isNull);
66 api.close();
67 });
68
69 testWidgets(
70 'grade and upper track are saved through real API contract, not a display-only preference',
71 (tester) async {
72 Map<String, dynamic>? saved;
73 final api = makeApi((request) async {
74 if (request.method == 'PUT') {
75 expect(request.url.path, '/api/v1/me/education');
76 saved = jsonDecode(request.body) as Map<String, dynamic>;
77 return response(saved!);
78 }
79 return response({
80 'education': {
81 'nationalGrade': 10,
82 'trackCode': 'math',
83 'schoolYear': '1405-1406'
84 },
85 'courses': []
86 });
87 });
88 await tester.pumpWidget(screen(EducationLibraryPage(api: api)));
89 await tester.pumpAndSettle();
90 await tester.tap(find.text('انتخاب پایه و رشته'));
91 await tester.pumpAndSettle();
92 await tester.tap(find.byKey(const ValueKey('education-track')));
93 await tester.pumpAndSettle();
94 await tester.tap(find.text('علوم تجربی').last);
95 await tester.pumpAndSettle();
96 await tester.tap(find.text('ذخیره'));
97 await tester.pumpAndSettle();
98 expect(saved, {
99 'nationalGrade': 10,
100 'trackCode': 'experimental',
101 'schoolYear': '1405-1406'
102 });
103 expect(tester.takeException(), isNull);
104 api.close();
105 });
106
107 testWidgets(
108 'midyear last lesson opens in same app with no video load or automatic completion',
109 (tester) async {
110 var writes = 0;
111 final api = makeApi((request) async {
112 if (request.method != 'GET') writes++;
113 expect(request.url.path, '/api/v1/education/courses/$courseId');
114 return response({
115 'id': courseId,
116 'titleFa': 'کتاب آزمایشی',
117 'schoolYear': null,
118 'alignmentNotesFa': 'تطابق نسخه تأیید نشده',
119 'fetchedAt': '2026-10-09T10:00:00Z',
120 'units': [
121 {
122 'id': firstId,
123 'titleFa': 'بخش آغاز',
124 'orderIndex': 1,
125 'videos': [],
126 'progress': null
127 },
128 {
129 'id': lastId,
130 'titleFa': 'بخش میانه سال',
131 'orderIndex': 2,
132 'progress': null,
133 'videos': [
134 {
135 'aparatId': 'rod03i6',
136 'titleFa': 'منبع آزمایشی قاب',
137 'creatorName': 'مدرس نمونه',
138 'creatorUrl': 'https://www.aparat.com/example',
139 'freeAccess': true
140 }
141 ]
142 }
143 ]
144 });
145 });
146 await tester
147 .pumpWidget(screen(LearningCoursePage(api: api, courseId: courseId)));
148 await tester.pumpAndSettle();
149 await tester.tap(find.text('بخش میانه سال'));
150 await tester.pumpAndSettle();
151 expect(find.text('بارگذاری ویدئو از آپارات'), findsOneWidget);
152 await tester.scrollUntilVisible(find.textContaining('پیشرفت شخصی:'), 100,
153 scrollable: find.byType(Scrollable).first);
154 expect(find.textContaining('پیشرفت شخصی: شروع نشده'), findsOneWidget);
155 await tester.scrollUntilVisible(find.text('بخش قبلی'), 300,
156 scrollable: find.byType(Scrollable).first);
157 expect(find.text('بخش قبلی'), findsOneWidget);
158 expect(writes, 0);
159 expect(tester.takeException(), isNull);
160 await tester.pumpWidget(const SizedBox());
161 api.close();
162 });
163
164 testWidgets(
165 'permission denial removes previously displayed personal progress',
166 (tester) async {
167 var denied = false;
168 final api = makeApi((request) async {
169 if (denied) return http.Response('{"error":"forbidden"}', 403);
170 return response({
171 'id': courseId,
172 'titleFa': 'کتاب خصوصی آزمایشی',
173 'units': [
174 {
175 'id': firstId,
176 'titleFa': 'درس خصوصی آزمایشی',
177 'videos': [],
178 'progress': {
179 'unitId': firstId,
180 'status': 'completed',
181 'source': 'self_reported',
182 'version': 1,
183 'updatedAt': '2026-10-09T10:00:00Z'
184 }
185 }
186 ]
187 });
188 });
189 await tester.pumpWidget(screen(
190 LearningLessonPage(api: api, courseId: courseId, unitId: firstId)));
191 await tester.pumpAndSettle();
192 expect(find.textContaining('تکمیل طبق گزارش من'), findsOneWidget);
193 denied = true;
194 await tester.scrollUntilVisible(find.text('همگام‌سازی و تازه‌سازی'), 250,
195 scrollable: find.byType(Scrollable).first);
196 await tester.tap(find.text('همگام‌سازی و تازه‌سازی'));
197 await tester.pumpAndSettle();
198 expect(find.textContaining('تکمیل طبق گزارش من'), findsNothing);
199 expect(find.text('درس خصوصی آزمایشی'), findsNothing);
200 expect(find.textContaining('نشست معتبر لازم است'), findsOneWidget);
201 expect(tester.takeException(), isNull);
202 await tester.pumpWidget(const SizedBox());
203 api.close();
204 });
205
206 testWidgets('rejected progress remains visible after a successful read',
207 (tester) async {
208 final api = makeApi((request) async {
209 if (request.method == 'PUT') {
210 return http.Response('{"error":"version_conflict"}', 409);
211 }
212 return response({
213 'id': courseId,
214 'titleFa': 'کتاب آزمایشی',
215 'units': [
216 {
217 'id': firstId,
218 'titleFa': 'درس آزمایشی',
219 'videos': [],
220 'progress': null
221 }
222 ]
223 });
224 });
225 await tester.pumpWidget(screen(
226 LearningLessonPage(api: api, courseId: courseId, unitId: firstId)));
227 await tester.pumpAndSettle();
228 await tester.tap(find.text('در حال یادگیری'));
229 await tester.pumpAndSettle();
230 expect(find.textContaining('تغییر معلق یا تعارض'), findsOneWidget);
231 expect(find.text('حل تعارض با پیشرفت سرور'), findsOneWidget);
232 expect(tester.takeException(), isNull);
233 await tester.pumpWidget(const SizedBox());
234 api.close();
235 });
236 }