LifeMate — Personal, Family & Learning Companion

docs/owner-guide-and-delivery-plan
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 363 lines 15.5 KB
Raw
1 import 'dart:io';
2
3 import 'package:flutter/material.dart';
4 import 'package:flutter/services.dart';
5 import 'package:flutter_test/flutter_test.dart';
6 import 'package:lifemate/standalone/app.dart';
7 import 'package:lifemate/standalone/model.dart';
8 import 'package:lifemate/standalone/settings_ui.dart';
9 import 'package:lifemate/standalone/store.dart';
10 import 'package:sqflite_common_ffi/sqflite_ffi.dart';
11
12 void main() {
13 late Directory directory;
14 late String path;
15 late StandaloneStore store;
16
17 setUpAll(() async {
18 sqfliteFfiInit();
19 await (FontLoader('LifeGuidePersian')
20 ..addFont(rootBundle.load('assets/fonts/LifeGuidePersian.ttf')))
21 .load();
22 });
23 setUp(() async {
24 directory = await Directory.systemTemp.createTemp('lifeguide-ui-');
25 path = '${directory.path}/planner.sqlite';
26 // The same SQLite engine and schema as Android, without a background
27 // isolate so widget-test fake time does not stall a database message.
28 store = await StandaloneStore.open(
29 path: path, factory: databaseFactoryFfiNoIsolate);
30 });
31 tearDown(() async {
32 await store.close();
33 await directory.delete(recursive: true);
34 });
35
36 Future<void> idle(WidgetTester tester) async {
37 var idleFrames = 0;
38 for (var i = 0; i < 500; i++) {
39 await Future<void>.delayed(const Duration(milliseconds: 10));
40 await tester.pumpAndSettle();
41 final busy =
42 find.byType(CircularProgressIndicator).evaluate().isNotEmpty ||
43 find.text('در حال ذخیره…').evaluate().isNotEmpty;
44 idleFrames = busy ? 0 : idleFrames + 1;
45 if (idleFrames >= 3) return;
46 }
47 fail('SQLite-backed UI did not become idle.');
48 }
49
50 Future<void> open(WidgetTester tester,
51 {Future<void> Function(StandaloneStore)? before,
52 Future<void> Function(StandaloneStore)? after}) async {
53 await tester.pumpWidget(StandaloneApp(
54 openStore: () async => store,
55 onBeforePlansChange: before,
56 onPlansChanged: after));
57 await idle(tester);
58 }
59
60 Future<void> tap(WidgetTester tester, Finder target) async {
61 FocusManager.instance.primaryFocus?.unfocus();
62 if (find.byType(SnackBar).evaluate().isNotEmpty) {
63 // Dismiss the prior transient result before the next independent action.
64 ScaffoldMessenger.of(tester.element(find.byType(SnackBar)))
65 .removeCurrentSnackBar();
66 }
67 await tester.pumpAndSettle();
68 if (target.evaluate().isEmpty) {
69 await tester.scrollUntilVisible(target, 200,
70 scrollable: find.byType(Scrollable).first, maxScrolls: 30);
71 }
72 await tester.ensureVisible(target);
73 await tester.pumpAndSettle();
74 await tester.tap(target);
75 await idle(tester);
76 }
77
78 Future<void> chooseRepeat(WidgetTester tester, String label) async {
79 await tap(tester, find.byKey(const Key('plan-recurrence')));
80 await tap(tester, find.text(label).last);
81 }
82
83 Future<void> profile() => store.createProfile(
84 displayName: 'کاربر ساختگی',
85 category: StandaloneProfileCategory.girlMinor);
86
87 Future<void> seedDaily() async {
88 await profile();
89 await store.createPlan(StandalonePlanDraft(
90 title: 'برنامهٔ آزمایشی',
91 kind: StandalonePlanKind.task,
92 recurrence: StandaloneRecurrence.daily,
93 startDate: StandaloneDate.only(DateTime.now()),
94 startMinute: 540,
95 endMinute: 600));
96 }
97
98 testWidgets('onboarding and daily save commit before reminder reconciliation',
99 (tester) async {
100 await tester.runAsync(() async {
101 final events = <String>[];
102 await open(tester, before: (actual) async {
103 events.add('invalidate:${(await actual.listPlans()).length}');
104 }, after: (actual) async {
105 events.add('reconcile:${(await actual.listPlans()).length}');
106 });
107 expect(find.textContaining('بدون حساب و سرور'), findsOneWidget);
108 expect(await store.profile(), isNull);
109 await tester.enterText(find.byType(TextField), 'نام ساختگی');
110 await tap(tester,
111 find.byType(DropdownButtonFormField<StandaloneProfileCategory>));
112 await tap(tester, find.text('دختر').last);
113 await tap(tester, find.text('شروع برنامهٔ من'));
114 expect(find.text('سلام نام ساختگی'), findsOneWidget);
115 expect(await store.listPlans(), isEmpty);
116 await tap(tester, find.text('برنامهٔ تازه'));
117 await tester.enterText(
118 find.byKey(const Key('plan-title')), 'قدم روزانهٔ ساختگی');
119 await chooseRepeat(tester, 'هر روز');
120 await tap(tester, find.text('ذخیرهٔ برنامه'));
121 expect(events, ['invalidate:0', 'reconcile:1']);
122 expect((await store.listPlans()).single.recurrence,
123 StandaloneRecurrence.daily);
124 await tap(tester, find.byType(Checkbox));
125 final today = StandaloneDate.only(DateTime.now());
126 final occurrences = await store.occurrences(
127 from: today, through: StandaloneDate.addDays(today, 1));
128 expect(occurrences.first.completed, isTrue);
129 expect(occurrences.last.completed, isFalse);
130 await tester.pumpWidget(const SizedBox());
131 await store.close();
132 store = await StandaloneStore.open(
133 path: path, factory: databaseFactoryFfiNoIsolate);
134 await open(tester);
135 expect(find.text('سلام نام ساختگی'), findsOneWidget);
136 expect(find.textContaining('۱ از ۱'), findsOneWidget);
137 expect(tester.takeException(), isNull);
138 });
139 });
140
141 testWidgets('one occurrence edit and whole-plan archive preserve other dates',
142 (tester) async {
143 await tester.runAsync(() async {
144 await seedDaily();
145 await open(tester);
146 await tap(tester, find.text('شروع'));
147 await tap(tester, find.text('ویرایش همین نوبت'));
148 await tester.enterText(
149 find.byKey(const Key('occurrence-title')), 'عنوان مستقل');
150 await tap(tester, find.text('ثبت تغییر همین نوبت'));
151 final today = StandaloneDate.only(DateTime.now());
152 var items = await store.occurrences(
153 from: today, through: StandaloneDate.addDays(today, 1));
154 expect(items.first.title, 'عنوان مستقل');
155 expect(items.last.title, 'برنامهٔ آزمایشی');
156 await tap(tester, find.text('انجام شد'));
157 await tap(tester, find.text('بایگانی…'));
158 await tap(tester, find.text('کل برنامه و همهٔ نوبت‌های آینده'));
159 expect(await store.listPlans(), isEmpty);
160 items = await store.occurrences(
161 from: today,
162 through: StandaloneDate.addDays(today, 1),
163 includeArchived: true);
164 expect(items.first.completed, isTrue);
165 expect(items.last.completed, isFalse);
166 expect(
167 find.text('برای این روز هنوز برنامه‌ای ننوشته‌ای.'), findsOneWidget);
168 expect(tester.takeException(), isNull);
169 });
170 });
171
172 testWidgets('weekly editor persists different hours for selected weekdays',
173 (tester) async {
174 await tester.runAsync(() async {
175 await profile();
176 await open(tester);
177 await tap(tester, find.text('برنامهٔ تازه'));
178 await tester.enterText(
179 find.byKey(const Key('plan-title')), 'هفتهٔ ساختگی');
180 await chooseRepeat(tester, 'روزهای انتخابی هفته');
181 await tap(tester, find.byKey(const Key('plan-weekday-6')));
182 await tap(tester, find.byKey(const Key('plan-weekday-7')));
183 final saturdayHours = find.byKey(const Key('weekly-hours-6'));
184 await tap(
185 tester,
186 find
187 .descendant(
188 of: saturdayHours, matching: find.byType(OutlinedButton))
189 .last);
190 final fields = find.descendant(
191 of: find.byType(TimePickerDialog), matching: find.byType(TextField));
192 await tester.enterText(fields.at(0), '11');
193 await tester.enterText(fields.at(1), '30');
194 await tap(tester, find.widgetWithText(TextButton, 'تأیید'));
195 await tap(tester, find.text('ذخیرهٔ برنامه'));
196 final plan = (await store.listPlans()).single;
197 expect(plan.recurrence, StandaloneRecurrence.weekly);
198 expect(plan.weeklySlots.map((slot) => slot.weekday), [6, 7]);
199 expect(plan.weeklySlots.map((slot) => slot.endMinute), [690, 600]);
200 expect(tester.takeException(), isNull);
201 });
202 });
203
204 testWidgets(
205 'SQLite failure retains editor; committed reminder failure does not',
206 (tester) async {
207 await tester.runAsync(() async {
208 await profile();
209 final db = await databaseFactoryFfiNoIsolate.openDatabase(path);
210 await db.execute('''CREATE TRIGGER synthetic_write_failure
211 BEFORE INSERT ON local_plans BEGIN
212 SELECT RAISE(ABORT, 'synthetic disk-write failure'); END''');
213 await open(tester,
214 after: (_) async => throw StateError('synthetic alarm'));
215 await tap(tester, find.text('برنامهٔ تازه'));
216 await tester.enterText(find.byKey(const Key('plan-title')), 'حفظ فرم');
217 await chooseRepeat(tester, 'یک روز در میان');
218 await tap(tester, find.text('ذخیرهٔ برنامه'));
219 expect(await store.listPlans(), isEmpty);
220 await tester.scrollUntilVisible(find.byKey(const Key('plan-title')), -200,
221 scrollable: find.byType(Scrollable).first);
222 expect(
223 tester
224 .widget<TextField>(find.byKey(const Key('plan-title')))
225 .controller!
226 .text,
227 'حفظ فرم');
228 await tester.scrollUntilVisible(
229 find.text('برنامه ذخیره نشد. می‌توانی دوباره تلاش کنی.'), 200,
230 scrollable: find.byType(Scrollable).first);
231 expect(find.text('برنامه ذخیره نشد. می‌توانی دوباره تلاش کنی.'),
232 findsOneWidget);
233 await db.execute('DROP TRIGGER synthetic_write_failure');
234 await tap(tester, find.text('ذخیرهٔ برنامه'));
235 expect((await store.listPlans()).single.recurrence,
236 StandaloneRecurrence.everyTwoDays);
237 expect(find.byKey(const Key('plan-title')), findsNothing);
238 expect(
239 find.text(
240 'برنامه ذخیره شد؛ آماده‌سازی یادآوری را در تنظیمات بررسی کن.'),
241 findsOneWidget);
242 expect(tester.takeException(), isNull);
243 });
244 });
245
246 testWidgets('onboarding reload failure never recreates a committed profile',
247 (tester) async {
248 await tester.runAsync(() async {
249 var reloads = 0;
250 await tester.pumpWidget(MaterialApp(
251 home: StandaloneOnboarding(
252 store: store,
253 onSaved: () async {
254 reloads++;
255 if (reloads == 1) throw StateError('synthetic read failure');
256 })));
257 await tester.pumpAndSettle();
258 await tester.enterText(find.byType(TextField), 'نام محفوظ');
259 await tap(tester,
260 find.byType(DropdownButtonFormField<StandaloneProfileCategory>));
261 await tap(tester, find.text('بزرگسال').last);
262 await tap(tester, find.text('شروع برنامهٔ من'));
263 final id = (await store.profile())!.id;
264 expect(find.textContaining('نام روی این گوشی ذخیره شد'), findsOneWidget);
265 await tap(tester, find.text('باز کردن برنامهٔ من'));
266 expect((await store.profile())!.id, id);
267 expect(reloads, 2);
268 expect(tester.takeException(), isNull);
269 });
270 });
271
272 testWidgets(
273 '320 wide at 200 percent keeps onboarding, pages and settings usable',
274 (tester) async {
275 await tester.runAsync(() async {
276 tester.view.physicalSize = const Size(320, 740);
277 tester.view.devicePixelRatio = 1;
278 tester.platformDispatcher.textScaleFactorTestValue = 2;
279 addTearDown(tester.view.resetPhysicalSize);
280 addTearDown(tester.view.resetDevicePixelRatio);
281 addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
282 await open(tester);
283 await tester.scrollUntilVisible(find.byType(TextField), 180,
284 scrollable: find.byType(Scrollable).first);
285 await tester.enterText(find.byType(TextField), 'نام ساختگی');
286 await tap(tester,
287 find.byType(DropdownButtonFormField<StandaloneProfileCategory>));
288 await tap(tester, find.text('پسر').last);
289 await tap(tester, find.text('شروع برنامهٔ من'));
290 expect(tester.takeException(), isNull);
291 await tap(
292 tester,
293 find.descendant(
294 of: find.byType(NavigationBar), matching: find.text('هفته')));
295 expect(tester.takeException(), isNull);
296 await tap(
297 tester,
298 find.descendant(
299 of: find.byType(NavigationBar), matching: find.text('تنظیمات')));
300 await tap(tester, find.text('سبز نعنایی'));
301 await tap(tester, find.text('ذخیرهٔ تغییرها'));
302 expect((await store.profile())!.themeKey, 'mint');
303 await tap(tester, find.text('مرجانی گرم'));
304 await tap(tester, find.text('ذخیرهٔ تغییرها'));
305 expect((await store.profile())!.themeKey, 'coral');
306 expect(tester.takeException(), isNull);
307 await tap(
308 tester,
309 find.descendant(
310 of: find.byType(NavigationBar), matching: find.text('امروز')));
311 await tap(tester, find.text('برنامهٔ تازه'));
312 await chooseRepeat(tester, 'روزهای انتخابی هفته');
313 await tap(tester, find.byKey(const Key('plan-weekday-6')));
314 await tap(tester, find.text('ذخیرهٔ برنامه'));
315 expect(find.text('یک عنوان برای برنامه بنویس.'), findsOneWidget);
316 expect(tester.takeException(), isNull);
317 });
318 });
319 testWidgets('resume reads committed plans before reminder reconciliation',
320 (tester) async {
321 await tester.runAsync(() async {
322 await seedDaily();
323 var resumed = 0;
324 await tester.pumpWidget(StandaloneApp(
325 openStore: () async => store,
326 onResumed: (actual) async {
327 expect(await actual.listPlans(), hasLength(1));
328 resumed++;
329 }));
330 await idle(tester);
331 tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
332 tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
333 await idle(tester);
334 expect(resumed, 1);
335 expect(tester.takeException(), isNull);
336 });
337 });
338
339 testWidgets('failed profile write restores reminders from unchanged SQLite',
340 (tester) async {
341 await tester.runAsync(() async {
342 await profile();
343 final db = await databaseFactoryFfiNoIsolate.openDatabase(path);
344 await db.execute(
345 "CREATE TRIGGER reject_profile BEFORE UPDATE ON local_profiles BEGIN SELECT RAISE(ABORT, 'synthetic write failure'); END");
346 final events = <String>[];
347 await open(tester,
348 before: (_) async => events.add('suspend'),
349 after: (actual) async =>
350 events.add('restore:${(await actual.profile())!.displayName}'));
351 await tap(
352 tester,
353 find.descendant(
354 of: find.byType(NavigationBar), matching: find.text('تنظیمات')));
355 await tester.enterText(find.byType(TextField).first, 'نام ذخیره نشده');
356 await tap(tester, find.text('ذخیرهٔ تغییرها'));
357 expect(events, ['suspend', 'restore:کاربر ساختگی']);
358 expect((await store.profile())!.displayName, 'کاربر ساختگی');
359 expect(find.textContaining('تغییرها ذخیره نشد'), findsOneWidget);
360 expect(tester.takeException(), isNull);
361 });
362 });
363 }