LifeMate — Personal, Family & Learning Companion

feat/local-signing-preflight
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 282 lines 11.7 KB
Raw
1 import 'dart:io';
2
3 import 'package:flutter_test/flutter_test.dart';
4 import 'package:lifemate/standalone/model.dart';
5 import 'package:lifemate/standalone/store.dart';
6 import 'package:sqflite_common_ffi/sqflite_ffi.dart';
7
8 void main() {
9 late Directory directory;
10 late String path;
11 late StandaloneStore store;
12 setUpAll(sqfliteFfiInit);
13 setUp(() async {
14 directory = await Directory.systemTemp.createTemp('lifeguide-local-store-');
15 path = '${directory.path}/planner.sqlite';
16 store = await StandaloneStore.open(path: path, factory: databaseFactoryFfi);
17 });
18 tearDown(() async {
19 await store.close();
20 await directory.delete(recursive: true);
21 });
22 Future<StandaloneProfile> profile() => store.createProfile(
23 displayName: 'نام ساختگی',
24 category: StandaloneProfileCategory.girlMinor,
25 );
26 StandalonePlanDraft daily({
27 DateTime? start,
28 DateTime? until,
29 int? reminder,
30 StandaloneRecurrence recurrence = StandaloneRecurrence.daily,
31 }) =>
32 StandalonePlanDraft(
33 title: 'برنامه ساختگی',
34 kind: StandalonePlanKind.task,
35 recurrence: recurrence,
36 startDate: start ?? DateTime(2026, 10, 19),
37 untilDate: until,
38 startMinute: 480,
39 endMinute: 540,
40 reminderMinutes: reminder,
41 );
42 test('real SQLite file preserves profile and independent edits on reopen',
43 () async {
44 final owner = await profile();
45 expect(
46 owner.id,
47 matches(RegExp(
48 r'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$')));
49 final plan = await store.createPlan(daily());
50 final first = await store.occurrences(
51 from: DateTime(2026, 10, 19), through: DateTime(2026, 10, 20));
52 await store.updateOccurrence(first.first.id,
53 title: 'عنوان مستقل',
54 startMinute: 600,
55 endMinute: 660,
56 completed: true);
57 await store.close();
58 expect(await File(path).length(), greaterThan(0));
59 store = await StandaloneStore.open(path: path, factory: databaseFactoryFfi);
60 expect((await store.profile())!.id, owner.id);
61 final reread = await store.occurrences(
62 from: DateTime(2026, 10, 19), through: DateTime(2026, 10, 20));
63 expect(reread.map((item) => item.id), first.map((item) => item.id));
64 expect(reread.first.title, 'عنوان مستقل');
65 expect(reread.first.completed, isTrue);
66 expect(reread.first.startMinute, 600);
67 expect(reread.last.completed, isFalse);
68 expect(reread.last.title, 'برنامه ساختگی');
69 await store.archivePlan(plan.id);
70 expect(await store.listPlans(), isEmpty);
71 expect((await store.occurrence(first.first.id))!.completed, isTrue);
72 await store.close();
73 store = await StandaloneStore.open(path: path, factory: databaseFactoryFfi);
74 expect(
75 (await store.listPlans(includeArchived: true)).single.archived, isTrue);
76 });
77 test('profile edits retain identity and plans; a second profile is rejected',
78 () async {
79 final owner = await profile();
80 await store.createPlan(daily());
81 await expectLater(profile(), throwsStateError);
82 final updated = await store.updateProfile(
83 displayName: 'نام تازه', companionName: 'آوا', themeKey: 'mint');
84 expect(updated.id, owner.id);
85 expect(updated.displayName, 'نام تازه');
86 expect(updated.companionName, 'آوا');
87 expect(updated.themeKey, 'mint');
88 expect(await store.listPlans(), hasLength(1));
89 });
90 test('every two days uses calendar dates across month and DST', () async {
91 await profile();
92 await store.createPlan(daily(
93 start: DateTime(2026, 10, 30),
94 until: DateTime(2026, 11, 5),
95 recurrence: StandaloneRecurrence.everyTwoDays));
96 final occurrences = await store.occurrences(
97 from: DateTime(2026, 10, 31), through: DateTime(2026, 11, 7));
98 expect(occurrences.map((item) => item.date.day), [1, 3, 5]);
99 expect(occurrences.every((item) => item.startMinute == 480), isTrue);
100 expect(occurrences.every((item) => item.startsAt.hour == 8), isTrue);
101 expect(
102 StandaloneDate.daysBetween(DateTime(2026, 3, 7), DateTime(2026, 3, 10)),
103 3);
104 });
105 test('weekly schedules use selected weekdays and independent hours',
106 () async {
107 await profile();
108 await store.createPlan(StandalonePlanDraft(
109 title: 'هفتگی ساختگی',
110 kind: StandalonePlanKind.routine,
111 recurrence: StandaloneRecurrence.weekly,
112 startDate: DateTime(2026, 10, 19),
113 untilDate: DateTime(2026, 10, 25),
114 startMinute: 480,
115 endMinute: 540,
116 weeklySlots: const [
117 StandaloneWeeklySlot(
118 weekday: DateTime.monday, startMinute: 480, endMinute: 540),
119 StandaloneWeeklySlot(
120 weekday: DateTime.friday, startMinute: 1080, endMinute: 1140),
121 ],
122 ));
123 final occurrences = await store.occurrences(
124 from: DateTime(2026, 10, 18), through: DateTime(2026, 10, 26));
125 expect(occurrences.map((item) => item.date.day), [19, 23]);
126 expect(occurrences.map((item) => item.startMinute), [480, 1080]);
127 expect(occurrences.map((item) => item.endMinute), [540, 1140]);
128 });
129 test(
130 'one-off plans produce only their date and overlapping reads never duplicate',
131 () async {
132 await profile();
133 await store.createPlan(daily(recurrence: StandaloneRecurrence.once));
134 final reads = await Future.wait(List.generate(
135 4,
136 (_) => store.occurrences(
137 from: DateTime(2026, 10, 18), through: DateTime(2026, 10, 21))));
138 expect(reads.every((items) => items.length == 1), isTrue);
139 expect(reads.map((items) => items.single.id).toSet(), hasLength(1));
140 });
141 test('archived occurrence is never regenerated', () async {
142 await profile();
143 await store.createPlan(daily());
144 final initial = await store.occurrences(
145 from: DateTime(2026, 10, 19), through: DateTime(2026, 10, 20));
146 await store.archiveOccurrence(initial.first.id);
147 final active = await store.occurrences(
148 from: DateTime(2026, 10, 19), through: DateTime(2026, 10, 20));
149 expect(active.map((item) => item.id), [initial.last.id]);
150 expect((await store.occurrence(initial.first.id))!.archived, isTrue);
151 });
152 test('reminders use edits and exclude completed and archived series',
153 () async {
154 await profile();
155 final plan = await store.createPlan(daily(reminder: 10));
156 final occurrences = await store.occurrences(
157 from: DateTime(2026, 10, 19), through: DateTime(2026, 10, 21));
158 await store.updateOccurrence(occurrences[0].id, completed: true);
159 await store.updateOccurrence(occurrences[1].id,
160 startMinute: 600, endMinute: 660, reminderMinutes: 30);
161 final pending = await store.pendingReminders(
162 from: DateTime(2026, 10, 19), through: DateTime(2026, 10, 22));
163 expect(
164 pending.map((item) => item.id), [occurrences[1].id, occurrences[2].id]);
165 expect(pending.first.remindAt, DateTime(2026, 10, 20, 9, 30));
166 await store.archivePlan(plan.id);
167 expect(
168 await store.pendingReminders(
169 from: DateTime(2026, 10, 19), through: DateTime(2026, 10, 22)),
170 isEmpty);
171 expect((await store.occurrence(occurrences[0].id))!.completed, isTrue);
172 });
173 test('invalid schedule fails before a plan is saved', () async {
174 await profile();
175 await expectLater(
176 store.createPlan(StandalonePlanDraft(
177 title: '',
178 kind: StandalonePlanKind.task,
179 recurrence: StandaloneRecurrence.daily,
180 startDate: DateTime(2026, 10, 19),
181 startMinute: 600,
182 endMinute: 500,
183 )),
184 throwsArgumentError);
185 await expectLater(
186 store.createPlan(daily(reminder: 7)), throwsArgumentError);
187 expect(await store.listPlans(), isEmpty);
188 });
189 test('SQLite independently rejects duplicates and invalid times', () async {
190 await profile();
191 await store.createPlan(daily(recurrence: StandaloneRecurrence.once));
192 final item = (await store.occurrences(
193 from: DateTime(2026, 10, 19), through: DateTime(2026, 10, 19)))
194 .single;
195 final database = await databaseFactoryFfi.openDatabase(path);
196 await expectLater(
197 database.rawInsert(
198 'INSERT INTO local_occurrences SELECT ? AS id, plan_id, occurrence_date, title, kind, start_minute, end_minute, completed, archived, reminder_minutes FROM local_occurrences WHERE id = ?',
199 ['00000000-0000-4000-8000-000000000000', item.id],
200 ),
201 throwsA(isA<DatabaseException>()));
202 await expectLater(
203 database.rawUpdate(
204 'UPDATE local_occurrences SET end_minute = start_minute WHERE id = ?',
205 [item.id]),
206 throwsA(isA<DatabaseException>()));
207 });
208 test('a storage failure rolls back the complete plan transaction', () async {
209 await profile();
210 final database = await databaseFactoryFfi.openDatabase(path);
211 await database.execute(
212 "CREATE TRIGGER reject_slot BEFORE INSERT ON local_weekly_slots BEGIN SELECT RAISE(ABORT, 'synthetic disk write rejection'); END");
213 await expectLater(
214 store.createPlan(StandalonePlanDraft(
215 title: 'rollback fixture',
216 kind: StandalonePlanKind.routine,
217 recurrence: StandaloneRecurrence.weekly,
218 startDate: DateTime(2026, 10, 19),
219 startMinute: 480,
220 endMinute: 540,
221 weeklySlots: const [
222 StandaloneWeeklySlot(weekday: 1, startMinute: 480, endMinute: 540)
223 ],
224 )),
225 throwsA(isA<DatabaseException>()));
226 expect(await store.listPlans(includeArchived: true), isEmpty);
227 expect(await database.query('local_weekly_slots'), isEmpty);
228 });
229 test('schema version is durable and newer data is refused without resetting',
230 () async {
231 final owner = await profile();
232 final database = await databaseFactoryFfi.openDatabase(path);
233 expect(await database.getVersion(), StandaloneStore.schemaVersion);
234 await database.setVersion(StandaloneStore.schemaVersion + 1);
235 await store.close();
236 await expectLater(
237 StandaloneStore.open(path: path, factory: databaseFactoryFfi),
238 throwsStateError);
239 final preserved = await databaseFactoryFfi.openDatabase(path);
240 expect(await preserved.getVersion(), StandaloneStore.schemaVersion + 1);
241 expect((await preserved.query('local_profiles')).single['id'], owner.id);
242 await preserved.close();
243 });
244 test('reminders can be disabled independently and include next-day lead time',
245 () async {
246 await profile();
247 await store.createPlan(StandalonePlanDraft(
248 title: 'midnight fixture',
249 kind: StandalonePlanKind.event,
250 recurrence: StandaloneRecurrence.daily,
251 startDate: DateTime(2026, 10, 19),
252 startMinute: 10,
253 endMinute: 60,
254 reminderMinutes: 30,
255 ));
256 final pending = await store.pendingReminders(
257 from: DateTime(2026, 10, 19, 23),
258 through: DateTime(2026, 10, 19, 23, 59));
259 expect(pending.single.date, DateTime(2026, 10, 20));
260 expect(pending.single.remindAt, DateTime(2026, 10, 19, 23, 40));
261 await store.updateOccurrence(pending.single.id, removeReminder: true);
262 expect(
263 await store.pendingReminders(
264 from: DateTime(2026, 10, 19, 23),
265 through: DateTime(2026, 10, 19, 23, 59)),
266 isEmpty);
267 });
268 test('closed writes throw and corruption never resets the database',
269 () async {
270 await profile();
271 await store.close();
272 await expectLater(
273 store.updateProfile(displayName: 'not saved'), throwsA(anything));
274 final badPath = '${directory.path}/corrupt.sqlite';
275 final bytes = [1, 7, 9, 2, 5, 3];
276 await File(badPath).writeAsBytes(bytes);
277 await expectLater(
278 StandaloneStore.open(path: badPath, factory: databaseFactoryFfi),
279 throwsA(anything));
280 expect(await File(badPath).readAsBytes(), bytes);
281 });
282 }