LifeMate — Personal, Family & Learning Companion

fix/personal-notification-controls
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 486 lines 21.2 KB
Raw
1 import 'dart:io';
2 import 'dart:ui' as ui;
3
4 import 'package:flutter/material.dart';
5 import 'package:flutter/rendering.dart';
6 import 'package:flutter/services.dart';
7 import 'package:flutter_test/flutter_test.dart';
8 import 'package:lifemate/standalone/app.dart';
9 import 'package:lifemate/standalone/model.dart';
10 import 'package:lifemate/standalone/settings_ui.dart';
11 import 'package:lifemate/standalone/store.dart';
12 import 'package:sqflite_common_ffi/sqflite_ffi.dart';
13
14 void main() {
15 late Directory directory;
16 late String path;
17 late StandaloneStore store;
18
19 setUpAll(() async {
20 sqfliteFfiInit();
21 await (FontLoader('LifeGuidePersian')
22 ..addFont(rootBundle.load('assets/fonts/LifeGuidePersian.ttf')))
23 .load();
24 await (FontLoader('MaterialIcons')
25 ..addFont(rootBundle.load('fonts/MaterialIcons-Regular.otf')))
26 .load();
27 });
28 setUp(() async {
29 directory = await Directory.systemTemp.createTemp('lifeguide-ui-');
30 path = '${directory.path}/planner.sqlite';
31 // The same SQLite engine and schema as Android, without a background
32 // isolate so widget-test fake time does not stall a database message.
33 store = await StandaloneStore.open(
34 path: path, factory: databaseFactoryFfiNoIsolate);
35 });
36 tearDown(() async {
37 await store.close();
38 await directory.delete(recursive: true);
39 });
40
41 Future<void> idle(WidgetTester tester) async {
42 var idleFrames = 0;
43 for (var i = 0; i < 500; i++) {
44 await Future<void>.delayed(const Duration(milliseconds: 10));
45 await tester.pumpAndSettle();
46 final busy =
47 find.byType(CircularProgressIndicator).evaluate().isNotEmpty ||
48 find.text('در حال ذخیره…').evaluate().isNotEmpty;
49 idleFrames = busy ? 0 : idleFrames + 1;
50 if (idleFrames >= 3) return;
51 }
52 fail('SQLite-backed UI did not become idle.');
53 }
54
55 Future<void> open(WidgetTester tester,
56 {Future<void> Function(StandaloneStore)? before,
57 Future<void> Function(StandaloneStore)? after}) async {
58 await tester.pumpWidget(RepaintBoundary(
59 key: const Key('personal-test-surface'),
60 child: StandaloneApp(
61 openStore: () async => store,
62 onBeforePlansChange: before,
63 onPlansChanged: after)));
64 await idle(tester);
65 }
66
67 Future<void> tap(WidgetTester tester, Finder target) async {
68 FocusManager.instance.primaryFocus?.unfocus();
69 if (find.byType(SnackBar).evaluate().isNotEmpty) {
70 // Dismiss the prior transient result before the next independent action.
71 ScaffoldMessenger.of(tester.element(find.byType(SnackBar)))
72 .removeCurrentSnackBar();
73 }
74 await tester.pumpAndSettle();
75 if (target.evaluate().isEmpty) {
76 await tester.scrollUntilVisible(target, 200,
77 scrollable: find.byType(Scrollable).first, maxScrolls: 30);
78 }
79 await tester.ensureVisible(target);
80 await tester.pumpAndSettle();
81 await tester.tap(target);
82 await idle(tester);
83 }
84
85 Future<void> chooseRepeat(WidgetTester tester, String label) async {
86 await tap(tester, find.byKey(const Key('plan-recurrence')));
87 await tap(tester, find.text(label).last);
88 }
89
90 Future<void> profile() => store.createProfile(
91 displayName: 'کاربر ساختگی',
92 category: StandaloneProfileCategory.girlMinor);
93
94 Future<void> seedDaily() async {
95 await profile();
96 await store.createPlan(StandalonePlanDraft(
97 title: 'برنامهٔ آزمایشی',
98 kind: StandalonePlanKind.task,
99 recurrence: StandaloneRecurrence.daily,
100 startDate: StandaloneDate.only(DateTime.now()),
101 startMinute: 540,
102 endMinute: 600));
103 }
104
105 testWidgets('onboarding and daily save commit before reminder reconciliation',
106 (tester) async {
107 await tester.runAsync(() async {
108 final events = <String>[];
109 await open(tester, before: (actual) async {
110 events.add('invalidate:${(await actual.listPlans()).length}');
111 }, after: (actual) async {
112 events.add('reconcile:${(await actual.listPlans()).length}');
113 });
114 expect(find.textContaining('بدون حساب و سرور'), findsOneWidget);
115 expect(await store.profile(), isNull);
116 await tester.enterText(find.byType(TextField), 'نام ساختگی');
117 await tap(tester,
118 find.byType(DropdownButtonFormField<StandaloneProfileCategory>));
119 await tap(tester, find.text('دختر').last);
120 await tap(tester, find.text('شروع برنامهٔ من'));
121 expect(find.text('سلام نام ساختگی'), findsOneWidget);
122 expect(await store.listPlans(), isEmpty);
123 await tap(tester, find.text('برنامهٔ تازه'));
124 await tester.enterText(
125 find.byKey(const Key('plan-title')), 'قدم روزانهٔ ساختگی');
126 await chooseRepeat(tester, 'هر روز');
127 await tap(tester, find.text('ذخیرهٔ برنامه'));
128 expect(events, ['invalidate:0', 'reconcile:1']);
129 expect((await store.listPlans()).single.recurrence,
130 StandaloneRecurrence.daily);
131 await tap(tester, find.byType(Checkbox));
132 final today = StandaloneDate.only(DateTime.now());
133 final occurrences = await store.occurrences(
134 from: today, through: StandaloneDate.addDays(today, 1));
135 expect(occurrences.first.completed, isTrue);
136 expect(occurrences.last.completed, isFalse);
137 await tester.pumpWidget(const SizedBox());
138 await store.close();
139 store = await StandaloneStore.open(
140 path: path, factory: databaseFactoryFfiNoIsolate);
141 await open(tester);
142 expect(find.text('سلام نام ساختگی'), findsOneWidget);
143 expect(find.textContaining('۱ از ۱'), findsOneWidget);
144 expect(tester.takeException(), isNull);
145 });
146 });
147
148 testWidgets('one occurrence edit and whole-plan archive preserve other dates',
149 (tester) async {
150 await tester.runAsync(() async {
151 await seedDaily();
152 await open(tester);
153 await tap(tester, find.text('شروع'));
154 await tap(tester, find.text('ویرایش همین نوبت'));
155 await tester.enterText(
156 find.byKey(const Key('occurrence-title')), 'عنوان مستقل');
157 await tap(tester, find.text('ثبت تغییر همین نوبت'));
158 final today = StandaloneDate.only(DateTime.now());
159 var items = await store.occurrences(
160 from: today, through: StandaloneDate.addDays(today, 1));
161 expect(items.first.title, 'عنوان مستقل');
162 expect(items.last.title, 'برنامهٔ آزمایشی');
163 await tap(tester, find.text('انجام شد'));
164 await tap(tester, find.text('بایگانی…'));
165 await tap(tester, find.text('کل برنامه و همهٔ نوبت‌های آینده'));
166 expect(await store.listPlans(), isEmpty);
167 items = await store.occurrences(
168 from: today,
169 through: StandaloneDate.addDays(today, 1),
170 includeArchived: true);
171 expect(items.first.completed, isTrue);
172 expect(items.last.completed, isFalse);
173 expect(
174 find.text('برای این روز هنوز برنامه‌ای ننوشته‌ای.'), findsOneWidget);
175 expect(tester.takeException(), isNull);
176 });
177 });
178
179 testWidgets(
180 'personal week shows Persian calendar without rewriting stored days',
181 (tester) async {
182 await tester.runAsync(() async {
183 await seedDaily();
184 final original = (await store.listPlans()).single;
185 await open(tester);
186 await tap(
187 tester,
188 find.descendant(
189 of: find.byType(NavigationBar), matching: find.text('هفته')));
190 expect(find.text('تاریخ‌ها شمسی‌اند.'), findsOneWidget);
191 expect(find.textContaining('میلادی'), findsNothing);
192 expect((await store.listPlans()).single.startDate, original.startDate);
193 expect(tester.takeException(), isNull);
194 });
195 });
196
197 testWidgets(
198 'Persian picker saves next-year date and keeps ISO SQLite after reopen',
199 (tester) async {
200 tester.view.physicalSize = const Size(390, 844);
201 tester.view.devicePixelRatio = 1;
202 addTearDown(() {
203 tester.view.resetPhysicalSize();
204 tester.view.resetDevicePixelRatio();
205 });
206 await tester.runAsync(() async {
207 await profile();
208 await open(tester);
209 await tap(tester, find.text('برنامهٔ تازه'));
210 await tester.enterText(
211 find.byKey(const Key('plan-title')), 'نوروز ساختگی');
212 await tap(tester, find.textContaining('تاریخ:'));
213 expect(find.text('انتخاب تاریخ شمسی'), findsOneWidget);
214 await tap(tester, find.byKey(const Key('personal-date-year')));
215 await tap(tester, find.text('۱۴۰۶').last);
216 await tap(tester, find.byKey(const Key('personal-date-month')));
217 await tester.scrollUntilVisible(find.text('فروردین'), -150,
218 scrollable: find.byType(Scrollable).last);
219 await tap(tester, find.text('فروردین').last);
220 await tap(tester, find.byKey(const Key('personal-date-day')));
221 await tester.scrollUntilVisible(find.text('۱'), -150,
222 scrollable: find.byType(Scrollable).last);
223 await tap(tester, find.text('۱').last);
224 final evidence = Platform.environment['LIFEGUIDE_UI_EVIDENCE_DIR'];
225 if (evidence != null) {
226 final boundary = tester.renderObject<RenderRepaintBoundary>(
227 find.byKey(const Key('personal-test-surface')));
228 final image = await boundary.toImage(pixelRatio: 2);
229 final bytes = await image.toByteData(format: ui.ImageByteFormat.png);
230 await Directory(evidence).create(recursive: true);
231 await File('$evidence/personal-jalali-picker.png')
232 .writeAsBytes(bytes!.buffer.asUint8List());
233 image.dispose();
234 }
235 await tap(tester, find.text('انتخاب تاریخ'));
236 expect(find.text('تاریخ: ۱ فروردین ۱۴۰۶ شمسی'), findsOneWidget);
237 await tap(tester, find.text('ذخیرهٔ برنامه'));
238 final original = (await store.listPlans()).single;
239 await tester.pumpWidget(const SizedBox());
240 await store.close();
241 final independent = await databaseFactoryFfiNoIsolate.openDatabase(path,
242 options: OpenDatabaseOptions(readOnly: true, singleInstance: false));
243 expect((await independent.query('local_plans')).single['start_date'],
244 '2027-03-21');
245 await independent.close();
246 store = await StandaloneStore.open(
247 path: path, factory: databaseFactoryFfiNoIsolate);
248 await open(tester);
249 expect((await store.listPlans()).single.id, original.id);
250 expect((await store.listPlans()).single.startDate, DateTime(2027, 3, 21));
251 expect(tester.takeException(), isNull);
252 });
253 });
254
255 testWidgets('Persian picker confirms and cancels at320px and200percent text',
256 (tester) async {
257 tester.view.physicalSize = const Size(320, 900);
258 tester.view.devicePixelRatio = 1;
259 tester.platformDispatcher.textScaleFactorTestValue = 2;
260 addTearDown(() {
261 tester.view.resetPhysicalSize();
262 tester.view.resetDevicePixelRatio();
263 tester.platformDispatcher.clearTextScaleFactorTestValue();
264 });
265 await tester.runAsync(() async {
266 await profile();
267 await open(tester);
268 await tap(tester, find.text('برنامهٔ تازه'));
269 await tap(tester, find.textContaining('تاریخ:'));
270 expect(find.text('انتخاب تاریخ شمسی'), findsOneWidget);
271 await tap(tester, find.byKey(const Key('personal-date-year')));
272 await tap(tester, find.text('۱۴۰۶').last);
273 await tap(tester, find.byKey(const Key('personal-date-month')));
274 await tester.scrollUntilVisible(find.text('فروردین'), -150,
275 scrollable: find.byType(Scrollable).last);
276 await tap(tester, find.text('فروردین').last);
277 await tap(tester, find.byKey(const Key('personal-date-day')));
278 await tester.scrollUntilVisible(find.text('۱'), -150,
279 scrollable: find.byType(Scrollable).last);
280 await tap(tester, find.text('۱').last);
281 await tap(tester, find.text('انتخاب تاریخ'));
282 expect(find.text('تاریخ: ۱ فروردین ۱۴۰۶ شمسی'), findsOneWidget);
283 final originalLabel =
284 tester.widget<Text>(find.textContaining('تاریخ:')).data;
285 await tap(tester, find.textContaining('تاریخ:'));
286 await tap(tester, find.byKey(const Key('personal-date-year')));
287 await tap(tester, find.text('۱۴۰۵').last);
288 await tap(tester, find.text('انصراف').last);
289 expect(find.text(originalLabel!), findsOneWidget);
290 expect(await store.listPlans(), isEmpty);
291 expect(tester.takeException(), isNull);
292 });
293 });
294
295 testWidgets('weekly editor persists different hours for selected weekdays',
296 (tester) async {
297 await tester.runAsync(() async {
298 await profile();
299 await open(tester);
300 await tap(tester, find.text('برنامهٔ تازه'));
301 await tester.enterText(
302 find.byKey(const Key('plan-title')), 'هفتهٔ ساختگی');
303 await chooseRepeat(tester, 'روزهای انتخابی هفته');
304 await tap(tester, find.byKey(const Key('plan-weekday-6')));
305 await tap(tester, find.byKey(const Key('plan-weekday-7')));
306 final saturdayHours = find.byKey(const Key('weekly-hours-6'));
307 await tap(
308 tester,
309 find
310 .descendant(
311 of: saturdayHours, matching: find.byType(OutlinedButton))
312 .last);
313 final fields = find.descendant(
314 of: find.byType(TimePickerDialog), matching: find.byType(TextField));
315 await tester.enterText(fields.at(0), '11');
316 await tester.enterText(fields.at(1), '30');
317 await tap(tester, find.widgetWithText(TextButton, 'تأیید'));
318 await tap(tester, find.text('ذخیرهٔ برنامه'));
319 final plan = (await store.listPlans()).single;
320 expect(plan.recurrence, StandaloneRecurrence.weekly);
321 expect(plan.weeklySlots.map((slot) => slot.weekday), [6, 7]);
322 expect(plan.weeklySlots.map((slot) => slot.endMinute), [690, 600]);
323 expect(tester.takeException(), isNull);
324 });
325 });
326
327 testWidgets(
328 'SQLite failure retains editor; committed reminder failure does not',
329 (tester) async {
330 await tester.runAsync(() async {
331 await profile();
332 final db = await databaseFactoryFfiNoIsolate.openDatabase(path);
333 await db.execute('''CREATE TRIGGER synthetic_write_failure
334 BEFORE INSERT ON local_plans BEGIN
335 SELECT RAISE(ABORT, 'synthetic disk-write failure'); END''');
336 await open(tester,
337 after: (_) async => throw StateError('synthetic alarm'));
338 await tap(tester, find.text('برنامهٔ تازه'));
339 await tester.enterText(find.byKey(const Key('plan-title')), 'حفظ فرم');
340 await chooseRepeat(tester, 'یک روز در میان');
341 await tap(tester, find.text('ذخیرهٔ برنامه'));
342 expect(await store.listPlans(), isEmpty);
343 await tester.scrollUntilVisible(find.byKey(const Key('plan-title')), -200,
344 scrollable: find.byType(Scrollable).first);
345 expect(
346 tester
347 .widget<TextField>(find.byKey(const Key('plan-title')))
348 .controller!
349 .text,
350 'حفظ فرم');
351 await tester.scrollUntilVisible(
352 find.text('برنامه ذخیره نشد. می‌توانی دوباره تلاش کنی.'), 200,
353 scrollable: find.byType(Scrollable).first);
354 expect(find.text('برنامه ذخیره نشد. می‌توانی دوباره تلاش کنی.'),
355 findsOneWidget);
356 await db.execute('DROP TRIGGER synthetic_write_failure');
357 await tap(tester, find.text('ذخیرهٔ برنامه'));
358 expect((await store.listPlans()).single.recurrence,
359 StandaloneRecurrence.everyTwoDays);
360 expect(find.byKey(const Key('plan-title')), findsNothing);
361 expect(
362 find.text(
363 'برنامه ذخیره شد؛ آماده‌سازی یادآوری را در تنظیمات بررسی کن.'),
364 findsOneWidget);
365 expect(tester.takeException(), isNull);
366 });
367 });
368
369 testWidgets('onboarding reload failure never recreates a committed profile',
370 (tester) async {
371 await tester.runAsync(() async {
372 var reloads = 0;
373 await tester.pumpWidget(MaterialApp(
374 home: StandaloneOnboarding(
375 store: store,
376 onSaved: () async {
377 reloads++;
378 if (reloads == 1) throw StateError('synthetic read failure');
379 })));
380 await tester.pumpAndSettle();
381 await tester.enterText(find.byType(TextField), 'نام محفوظ');
382 await tap(tester,
383 find.byType(DropdownButtonFormField<StandaloneProfileCategory>));
384 await tap(tester, find.text('بزرگسال').last);
385 await tap(tester, find.text('شروع برنامهٔ من'));
386 final id = (await store.profile())!.id;
387 expect(find.textContaining('نام روی این گوشی ذخیره شد'), findsOneWidget);
388 await tap(tester, find.text('باز کردن برنامهٔ من'));
389 expect((await store.profile())!.id, id);
390 expect(reloads, 2);
391 expect(tester.takeException(), isNull);
392 });
393 });
394
395 testWidgets(
396 '320 wide at 200 percent keeps onboarding, pages and settings usable',
397 (tester) async {
398 await tester.runAsync(() async {
399 tester.view.physicalSize = const Size(320, 740);
400 tester.view.devicePixelRatio = 1;
401 tester.platformDispatcher.textScaleFactorTestValue = 2;
402 addTearDown(tester.view.resetPhysicalSize);
403 addTearDown(tester.view.resetDevicePixelRatio);
404 addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
405 await open(tester);
406 await tester.scrollUntilVisible(find.byType(TextField), 180,
407 scrollable: find.byType(Scrollable).first);
408 await tester.enterText(find.byType(TextField), 'نام ساختگی');
409 await tap(tester,
410 find.byType(DropdownButtonFormField<StandaloneProfileCategory>));
411 await tap(tester, find.text('پسر').last);
412 await tap(tester, find.text('شروع برنامهٔ من'));
413 expect(tester.takeException(), isNull);
414 await tap(
415 tester,
416 find.descendant(
417 of: find.byType(NavigationBar), matching: find.text('هفته')));
418 expect(tester.takeException(), isNull);
419 await tap(
420 tester,
421 find.descendant(
422 of: find.byType(NavigationBar), matching: find.text('تنظیمات')));
423 await tap(tester, find.text('سبز نعنایی'));
424 await tap(tester, find.text('ذخیرهٔ تغییرها'));
425 expect((await store.profile())!.themeKey, 'mint');
426 await tap(tester, find.text('مرجانی گرم'));
427 await tap(tester, find.text('ذخیرهٔ تغییرها'));
428 expect((await store.profile())!.themeKey, 'coral');
429 expect(tester.takeException(), isNull);
430 await tap(
431 tester,
432 find.descendant(
433 of: find.byType(NavigationBar), matching: find.text('امروز')));
434 await tap(tester, find.text('برنامهٔ تازه'));
435 await chooseRepeat(tester, 'روزهای انتخابی هفته');
436 await tap(tester, find.byKey(const Key('plan-weekday-6')));
437 await tap(tester, find.text('ذخیرهٔ برنامه'));
438 expect(find.text('یک عنوان برای برنامه بنویس.'), findsOneWidget);
439 expect(tester.takeException(), isNull);
440 });
441 });
442 testWidgets('resume reads committed plans before reminder reconciliation',
443 (tester) async {
444 await tester.runAsync(() async {
445 await seedDaily();
446 var resumed = 0;
447 await tester.pumpWidget(StandaloneApp(
448 openStore: () async => store,
449 onResumed: (actual) async {
450 expect(await actual.listPlans(), hasLength(1));
451 resumed++;
452 }));
453 await idle(tester);
454 tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
455 tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
456 await idle(tester);
457 expect(resumed, 1);
458 expect(tester.takeException(), isNull);
459 });
460 });
461
462 testWidgets('failed profile write restores reminders from unchanged SQLite',
463 (tester) async {
464 await tester.runAsync(() async {
465 await profile();
466 final db = await databaseFactoryFfiNoIsolate.openDatabase(path);
467 await db.execute(
468 "CREATE TRIGGER reject_profile BEFORE UPDATE ON local_profiles BEGIN SELECT RAISE(ABORT, 'synthetic write failure'); END");
469 final events = <String>[];
470 await open(tester,
471 before: (_) async => events.add('suspend'),
472 after: (actual) async =>
473 events.add('restore:${(await actual.profile())!.displayName}'));
474 await tap(
475 tester,
476 find.descendant(
477 of: find.byType(NavigationBar), matching: find.text('تنظیمات')));
478 await tester.enterText(find.byType(TextField).first, 'نام ذخیره نشده');
479 await tap(tester, find.text('ذخیرهٔ تغییرها'));
480 expect(events, ['suspend', 'restore:کاربر ساختگی']);
481 expect((await store.profile())!.displayName, 'کاربر ساختگی');
482 expect(find.textContaining('تغییرها ذخیره نشد'), findsOneWidget);
483 expect(tester.takeException(), isNull);
484 });
485 });
486 }