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
dart 361 lines 13.2 KB
Raw
1 import 'dart:convert';
2 import 'dart:ui' show SemanticsRole;
3
4 import 'package:flutter/foundation.dart' show kIsWeb;
5 import 'package:flutter/material.dart';
6 import 'package:flutter/services.dart';
7 import 'package:flutter_test/flutter_test.dart';
8 import 'package:http/http.dart' as http;
9 import 'package:http/testing.dart';
10 import 'package:lifemate/api.dart';
11 import 'package:lifemate/education_ui.dart';
12 import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
13 import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
14
15 // Explicit HTTP/widget fixtures, not evidence of a live curriculum or playback.
16 const courseId = '10000000-0000-4000-8000-000000000001';
17 const firstId = '20000000-0000-4000-8000-000000000001';
18 const lastId = '20000000-0000-4000-8000-000000000002';
19
20 void main() {
21 TestWidgetsFlutterBinding.ensureInitialized();
22 setUp(() {
23 SharedPreferencesAsyncPlatform.instance =
24 InMemorySharedPreferencesAsync.withData({});
25 String? token;
26 TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
27 .setMockMethodCallHandler(
28 const MethodChannel('lifeguide/local_write_guard'), (call) async {
29 if (call.method == 'getBlocked') return token != null;
30 if (call.method == 'beginWrite') {
31 return token = 'education-widget-fixture';
32 }
33 if (call.method == 'completeWrite' && call.arguments == token) {
34 token = null;
35 return true;
36 }
37 throw PlatformException(code: 'local_storage_restart_required');
38 });
39 });
40
41 Widget screen(Widget child) => MaterialApp(
42 home: Scaffold(
43 body:
44 Directionality(textDirection: TextDirection.rtl, child: child)));
45 http.Response response(Object data) => http.Response(jsonEncode(data), 200,
46 headers: {'content-type': 'application/json; charset=utf-8'});
47 HttpIdentityApi makeApi(
48 Future<http.Response> Function(http.Request) handler) =>
49 HttpIdentityApi(
50 baseUrl: 'https://education.example.test/api',
51 client: MockClient(handler))
52 ..accessToken = 'synthetic-fixture-token'
53 ..currentUserId = 'synthetic-learner';
54
55 Future<void> showProgress(WidgetTester tester) async {
56 await tester.scrollUntilVisible(find.textContaining('پیشرفت شخصی:'), 100,
57 scrollable: find.byType(Scrollable).first);
58 await tester.pumpAndSettle();
59 }
60
61 void expectProgressAnnouncement(WidgetTester tester, String label) {
62 final announcement = find.bySemanticsLabel(label);
63 expect(announcement, findsOneWidget);
64 expect(
65 tester.getSemantics(announcement),
66 matchesSemantics(
67 label: label,
68 role: kIsWeb ? SemanticsRole.status : SemanticsRole.none,
69 isLiveRegion: !kIsWeb,
70 textDirection: TextDirection.rtl));
71 }
72
73 testWidgets(
74 'progress live region announces acknowledged status and timestamp after update and reopen',
75 (tester) async {
76 final semantics = tester.ensureSemantics();
77 Map<String, dynamic>? progress;
78 final api = makeApi((request) async {
79 if (request.method == 'PUT') {
80 final submitted = jsonDecode(request.body) as Map<String, dynamic>;
81 progress = {
82 ...submitted,
83 'unitId': firstId,
84 'version': 1,
85 'updatedAt': '2026-10-11T10:00:00Z',
86 'acceptedAt': '2026-10-11T10:00:00Z'
87 };
88 return response(progress!);
89 }
90 return response({
91 'id': courseId,
92 'titleFa': 'کتاب آزمایشی',
93 'units': [
94 {
95 'id': firstId,
96 'titleFa': 'درس آزمایشی',
97 'videos': [],
98 'progress': progress
99 }
100 ]
101 });
102 });
103 Widget lesson() => screen(
104 LearningLessonPage(api: api, courseId: courseId, unitId: firstId));
105 await tester.pumpWidget(lesson());
106 await tester.pumpAndSettle();
107 await showProgress(tester);
108 expectProgressAnnouncement(tester, 'پیشرفت شخصی: شروع نشده');
109 await tester.tap(find.text('در حال یادگیری'));
110 await tester.pumpAndSettle();
111 await showProgress(tester);
112 const accepted =
113 'پیشرفت شخصی: در حال یادگیری\nآخرین ثبت سرور: 2026-10-11T10:00:00Z';
114 expectProgressAnnouncement(tester, accepted);
115 expect(find.bySemanticsLabel('پیشرفت شخصی: شروع نشده'), findsNothing);
116 await tester.pumpWidget(const SizedBox());
117 await tester.pumpAndSettle();
118 await tester.pumpWidget(lesson());
119 await tester.pumpAndSettle();
120 await showProgress(tester);
121 expectProgressAnnouncement(tester, accepted);
122 expect(tester.takeException(), isNull);
123 await tester.pumpWidget(const SizedBox());
124 semantics.dispose();
125 api.close();
126 });
127
128 testWidgets(
129 'pending progress announcement preserves the prior server timestamp',
130 (tester) async {
131 final semantics = tester.ensureSemantics();
132 final api = makeApi((request) async {
133 if (request.method == 'PUT') {
134 throw http.ClientException('synthetic offline');
135 }
136 return response({
137 'id': courseId,
138 'titleFa': 'کتاب آزمایشی',
139 'units': [
140 {
141 'id': firstId,
142 'titleFa': 'درس آزمایشی',
143 'videos': [],
144 'progress': {
145 'unitId': firstId,
146 'status': 'in_progress',
147 'source': 'self_reported',
148 'version': 1,
149 'updatedAt': '2026-10-11T09:00:00Z'
150 }
151 }
152 ]
153 });
154 });
155 await tester.pumpWidget(screen(
156 LearningLessonPage(api: api, courseId: courseId, unitId: firstId)));
157 await tester.pumpAndSettle();
158 await tester.tap(find.text('طبق گزارش من تکمیل شد'));
159 await tester.pumpAndSettle();
160 await showProgress(tester);
161 expectProgressAnnouncement(tester,
162 'پیشرفت شخصی: تکمیل طبق گزارش من\nتغییر روی دستگاه ذخیره شده؛ تأیید سرور دریافت نشده است.\nآخرین ثبت سرور: 2026-10-11T09:00:00Z');
163 expect(tester.takeException(), isNull);
164 await tester.pumpWidget(const SizedBox());
165 semantics.dispose();
166 api.close();
167 });
168
169 testWidgets(
170 'no education profile asks selection without silently choosing grade7',
171 (tester) async {
172 final api = makeApi((request) async => response({
173 'education': null,
174 'courses': [],
175 'fetchedAt': '2026-10-09T10:00:00Z'
176 }));
177 await tester.pumpWidget(screen(EducationLibraryPage(api: api)));
178 await tester.pumpAndSettle();
179 expect(find.textContaining('پایه تحصیلی را انتخاب کن'), findsOneWidget);
180 expect(find.textContaining('پایه 7'), findsNothing);
181 expect(tester.takeException(), isNull);
182 api.close();
183 });
184
185 testWidgets(
186 'grade and upper track are saved through real API contract, not a display-only preference',
187 (tester) async {
188 Map<String, dynamic>? saved;
189 final api = makeApi((request) async {
190 if (request.method == 'PUT') {
191 expect(request.url.path, '/api/v1/me/education');
192 saved = jsonDecode(request.body) as Map<String, dynamic>;
193 return response(saved!);
194 }
195 return response({
196 'education': {
197 'nationalGrade': 10,
198 'trackCode': 'math',
199 'schoolYear': '1405-1406'
200 },
201 'courses': []
202 });
203 });
204 await tester.pumpWidget(screen(EducationLibraryPage(api: api)));
205 await tester.pumpAndSettle();
206 await tester.tap(find.text('انتخاب پایه و رشته'));
207 await tester.pumpAndSettle();
208 await tester.tap(find.byKey(const ValueKey('education-track')));
209 await tester.pumpAndSettle();
210 await tester.tap(find.text('علوم تجربی').last);
211 await tester.pumpAndSettle();
212 await tester.tap(find.text('ذخیره'));
213 await tester.pumpAndSettle();
214 expect(saved, {
215 'nationalGrade': 10,
216 'trackCode': 'experimental',
217 'schoolYear': '1405-1406'
218 });
219 expect(tester.takeException(), isNull);
220 api.close();
221 });
222
223 testWidgets(
224 'midyear last lesson opens in same app with no video load or automatic completion',
225 (tester) async {
226 var writes = 0;
227 final api = makeApi((request) async {
228 if (request.method != 'GET') writes++;
229 expect(request.url.path, '/api/v1/education/courses/$courseId');
230 return response({
231 'id': courseId,
232 'titleFa': 'کتاب آزمایشی',
233 'schoolYear': null,
234 'alignmentNotesFa': 'تطابق نسخه تأیید نشده',
235 'fetchedAt': '2026-10-09T10:00:00Z',
236 'units': [
237 {
238 'id': firstId,
239 'titleFa': 'بخش آغاز',
240 'orderIndex': 1,
241 'videos': [],
242 'progress': null
243 },
244 {
245 'id': lastId,
246 'titleFa': 'بخش میانه سال',
247 'orderIndex': 2,
248 'progress': null,
249 'videos': [
250 {
251 'aparatId': 'rod03i6',
252 'titleFa': 'منبع آزمایشی قاب',
253 'creatorName': 'مدرس نمونه',
254 'creatorUrl': 'https://www.aparat.com/example',
255 'freeAccess': true
256 }
257 ]
258 }
259 ]
260 });
261 });
262 await tester
263 .pumpWidget(screen(LearningCoursePage(api: api, courseId: courseId)));
264 await tester.pumpAndSettle();
265 await tester.tap(find.text('بخش میانه سال'));
266 await tester.pumpAndSettle();
267 expect(find.text('بارگذاری ویدئو از آپارات'), findsOneWidget);
268 await tester.scrollUntilVisible(find.textContaining('پیشرفت شخصی:'), 100,
269 scrollable: find.byType(Scrollable).first);
270 expect(find.textContaining('پیشرفت شخصی: شروع نشده'), findsOneWidget);
271 await tester.scrollUntilVisible(find.text('بخش قبلی'), 300,
272 scrollable: find.byType(Scrollable).first);
273 expect(find.text('بخش قبلی'), findsOneWidget);
274 expect(writes, 0);
275 expect(tester.takeException(), isNull);
276 await tester.pumpWidget(const SizedBox());
277 api.close();
278 });
279
280 testWidgets(
281 'permission denial removes previously displayed personal progress',
282 (tester) async {
283 final semantics = tester.ensureSemantics();
284 var denied = false;
285 final api = makeApi((request) async {
286 if (denied) return http.Response('{"error":"forbidden"}', 403);
287 return response({
288 'id': courseId,
289 'titleFa': 'کتاب خصوصی آزمایشی',
290 'units': [
291 {
292 'id': firstId,
293 'titleFa': 'درس خصوصی آزمایشی',
294 'videos': [],
295 'progress': {
296 'unitId': firstId,
297 'status': 'completed',
298 'source': 'self_reported',
299 'version': 1,
300 'updatedAt': '2026-10-09T10:00:00Z'
301 }
302 }
303 ]
304 });
305 });
306 await tester.pumpWidget(screen(
307 LearningLessonPage(api: api, courseId: courseId, unitId: firstId)));
308 await tester.pumpAndSettle();
309 expect(find.textContaining('تکمیل طبق گزارش من'), findsOneWidget);
310 await showProgress(tester);
311 expectProgressAnnouncement(tester,
312 'پیشرفت شخصی: تکمیل طبق گزارش من\nآخرین ثبت سرور: 2026-10-09T10:00:00Z');
313 denied = true;
314 await tester.scrollUntilVisible(find.text('همگام‌سازی و تازه‌سازی'), 250,
315 scrollable: find.byType(Scrollable).first);
316 await tester.tap(find.text('همگام‌سازی و تازه‌سازی'));
317 await tester.pumpAndSettle();
318 expect(find.textContaining('تکمیل طبق گزارش من'), findsNothing);
319 expect(
320 find.bySemanticsLabel(
321 RegExp('تکمیل طبق گزارش من|2026-10-09T10:00:00Z')),
322 findsNothing);
323 expect(find.text('درس خصوصی آزمایشی'), findsNothing);
324 expect(find.textContaining('نشست معتبر لازم است'), findsOneWidget);
325 expect(tester.takeException(), isNull);
326 await tester.pumpWidget(const SizedBox());
327 semantics.dispose();
328 api.close();
329 });
330
331 testWidgets('rejected progress remains visible after a successful read',
332 (tester) async {
333 final api = makeApi((request) async {
334 if (request.method == 'PUT') {
335 return http.Response('{"error":"version_conflict"}', 409);
336 }
337 return response({
338 'id': courseId,
339 'titleFa': 'کتاب آزمایشی',
340 'units': [
341 {
342 'id': firstId,
343 'titleFa': 'درس آزمایشی',
344 'videos': [],
345 'progress': null
346 }
347 ]
348 });
349 });
350 await tester.pumpWidget(screen(
351 LearningLessonPage(api: api, courseId: courseId, unitId: firstId)));
352 await tester.pumpAndSettle();
353 await tester.tap(find.text('در حال یادگیری'));
354 await tester.pumpAndSettle();
355 expect(find.textContaining('تغییر معلق یا تعارض'), findsOneWidget);
356 expect(find.text('حل تعارض با پیشرفت سرور'), findsOneWidget);
357 expect(tester.takeException(), isNull);
358 await tester.pumpWidget(const SizedBox());
359 api.close();
360 });
361 }