LifeMate — Personal, Family & Learning Companion

infra/lifeguide-github-pages-qa
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 214 lines 8.47 KB
Raw
1 import 'dart:convert';
2 import 'package:flutter/material.dart';
3 import 'package:flutter/services.dart';
4 import 'package:flutter_test/flutter_test.dart';
5 import 'package:http/http.dart' as http;
6 import 'package:http/testing.dart';
7 import 'package:lifemate/api.dart';
8 import 'package:lifemate/phase3_ui.dart';
9 import 'package:lifemate/school_management_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 void main() {
14 TestWidgetsFlutterBinding.ensureInitialized();
15 setUp(() {
16 SharedPreferencesAsyncPlatform.instance =
17 InMemorySharedPreferencesAsync.withData({});
18 String? guard;
19 TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
20 .setMockMethodCallHandler(
21 const MethodChannel('lifeguide/local_write_guard'), (call) async {
22 if (call.method == 'getBlocked') return guard != null;
23 if (call.method == 'beginWrite') return guard = 'school-test-guard';
24 if (call.method == 'completeWrite' && call.arguments == guard) {
25 guard = null;
26 return true;
27 }
28 throw PlatformException(code: 'local_storage_restart_required');
29 });
30 });
31 testWidgets(
32 'school entry replaces hardcoded quick setup with explicit management',
33 (tester) async {
34 final paths = <String>[];
35 final api = HttpIdentityApi(
36 baseUrl: 'https://school.example.test/api',
37 client: MockClient((request) async {
38 paths.add('${request.method} ${request.url.path}');
39 return http.Response(
40 jsonEncode(request.url.path.endsWith('/profile')
41 ? {'user_id': '10000000-0000-4000-8000-000000000001'}
42 : {
43 'years': [],
44 'subjects': [],
45 'workload': [],
46 'grades': []
47 }),
48 200,
49 headers: {'content-type': 'application/json; charset=utf-8'});
50 }))
51 ..accessToken = 'synthetic'
52 ..currentUserId = '10000000-0000-4000-8000-000000000001';
53 await tester
54 .pumpWidget(MaterialApp(home: Scaffold(body: SchoolPage(api: api))));
55 await tester.pumpAndSettle();
56 expect(find.text('مدیریت مدرسه'), findsOneWidget);
57 expect(find.text('شروع سریع'), findsNothing);
58 expect(paths.where((p) => p.startsWith('POST')), isEmpty);
59 await tester.pumpWidget(const SizedBox());
60 api.close();
61 });
62 testWidgets(
63 'explicit editor has empty user-picked year dates and never sends quick setup writes',
64 (tester) async {
65 final posts = <String>[];
66 final api = HttpIdentityApi(
67 baseUrl: 'https://school-date.test/api',
68 client: MockClient((request) async {
69 if (request.method != 'GET') posts.add(request.body);
70 final data = request.url.path.endsWith('/lifecycle')
71 ? schoolSnapshot()
72 : request.url.path.endsWith('/timetable')
73 ? {'classes': []}
74 : {'subjects': [], 'years': [], 'workload': [], 'grades': []};
75 return jsonResponse(data);
76 }))
77 ..accessToken = 'synthetic'
78 ..currentUserId = schoolOwner;
79 await tester.pumpWidget(MaterialApp(home: SchoolManagementPage(api: api)));
80 await tester.pumpAndSettle();
81 final createYear = find.widgetWithText(TextButton, 'ایجاد').at(1);
82 await tester.ensureVisible(createYear);
83 await tester.tap(createYear);
84 await tester.pumpAndSettle();
85 expect(find.textContaining('تاریخ میلادی: YYYY-MM-DD'), findsOneWidget);
86 final fields =
87 tester.widgetList<TextField>(find.byType(TextField)).toList();
88 expect(
89 fields.map((field) => field.controller!.text), everyElement(isEmpty));
90 expect(find.byIcon(Icons.calendar_month), findsNWidgets(2));
91 expect(posts, isEmpty);
92 await tester.tap(find.text('انصراف'));
93 await tester.pumpAndSettle();
94 await tester.pumpWidget(const SizedBox());
95 api.close();
96 });
97
98 testWidgets(
99 'uncertain non-idempotent subject create refetches once and never auto retries',
100 (tester) async {
101 final subjects = <Map<String, dynamic>>[], writes = <String>[];
102 var readsAfterWrite = 0;
103 final api = HttpIdentityApi(
104 baseUrl: 'https://school-subject.test/api',
105 client: MockClient((request) async {
106 if (request.method == 'POST') {
107 writes.add(request.body);
108 final data = jsonDecode(request.body) as Map;
109 subjects.add({
110 'id': '40000000-0000-4000-8000-000000000001',
111 'name': data['name'],
112 'teacher_name': data['teacherName'],
113 'academic_term_id': schoolTermId
114 });
115 throw http.ClientException('synthetic lost ACK after commit');
116 }
117 if (writes.isNotEmpty) readsAfterWrite++;
118 final data = request.url.path.endsWith('/lifecycle')
119 ? schoolSnapshot()
120 : request.url.path.endsWith('/timetable')
121 ? {'classes': []}
122 : {
123 'subjects': subjects,
124 'years': [],
125 'workload': [],
126 'grades': []
127 };
128 return jsonResponse(data);
129 }))
130 ..accessToken = 'synthetic'
131 ..currentUserId = schoolOwner;
132 await tester.pumpWidget(MaterialApp(home: SchoolManagementPage(api: api)));
133 await tester.pumpAndSettle();
134 await tester.scrollUntilVisible(find.text('درس جدید'), 300,
135 scrollable: find.byType(Scrollable).first);
136 await tester.tap(find.text('درس جدید'));
137 await tester.pumpAndSettle();
138 await tester.enterText(find.byType(TextField).at(0), 'درس انتخابی');
139 await tester.tap(find.text('نیمسال فعال'));
140 await tester.pumpAndSettle();
141 await tester.tap(find.text('نیمسال انتخابی').last);
142 await tester.pumpAndSettle();
143 await tester.tap(find.text('ثبت'));
144 await tester.pumpAndSettle();
145 expect(writes, hasLength(1));
146 expect(subjects, hasLength(1));
147 expect(readsAfterWrite, greaterThanOrEqualTo(3));
148 await tester.drag(find.byType(ListView).first, const Offset(0, 1800));
149 await tester.pumpAndSettle();
150 expect(find.textContaining('نتیجه ارسال نامشخص است'), findsOneWidget);
151 await tester.tap(find.byTooltip('تازه‌کردن'));
152 await tester.pumpAndSettle();
153 expect(writes, hasLength(1));
154 await tester.pumpWidget(const SizedBox());
155 await tester.pumpWidget(MaterialApp(home: SchoolManagementPage(api: api)));
156 await tester.pumpAndSettle();
157 await tester.scrollUntilVisible(find.text('درس انتخابی'), 300,
158 scrollable: find.byType(Scrollable).first);
159 expect(find.text('درس انتخابی'), findsOneWidget);
160 expect(writes, hasLength(1));
161 await tester.pumpWidget(const SizedBox());
162 api.close();
163 });
164 }
165
166 const schoolOwner = '10000000-0000-4000-8000-000000000001';
167 const schoolContextId = '20000000-0000-4000-8000-000000000001';
168 const schoolYearId = '30000000-0000-4000-8000-000000000001';
169 const schoolTermId = '30000000-0000-4000-8000-000000000002';
170 const schoolAt = '2026-10-11T12:00:00Z';
171 http.Response jsonResponse(Object body) => http.Response(jsonEncode(body), 200,
172 headers: {'content-type': 'application/json; charset=utf-8'});
173 Map<String, dynamic> schoolSnapshot() => {
174 'contexts': [
175 {
176 'id': schoolContextId,
177 'user_id': schoolOwner,
178 'kind': 'student',
179 'title': 'مدرسه انتخابی',
180 'active': true,
181 'version': 1,
182 'created_at': schoolAt,
183 'updated_at': schoolAt
184 }
185 ],
186 'years': [
187 {
188 'id': schoolYearId,
189 'student_user_id': schoolOwner,
190 'life_context_id': schoolContextId,
191 'title': 'سال انتخابی',
192 'starts_on': '2026-08-12',
193 'ends_on': '2027-07-05',
194 'active': true,
195 'version': 1,
196 'created_at': schoolAt,
197 'updated_at': schoolAt
198 }
199 ],
200 'terms': [
201 {
202 'id': schoolTermId,
203 'academic_year_id': schoolYearId,
204 'title': 'نیمسال انتخابی',
205 'starts_on': '2026-08-15',
206 'ends_on': '2027-02-10',
207 'active': true,
208 'version': 1,
209 'created_at': schoolAt,
210 'updated_at': schoolAt
211 }
212 ],
213 'fetchedAt': schoolAt
214 };