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 198 lines 7.67 KB
Raw
1 import 'dart:convert';
2 import 'dart:async';
3
4 import 'package:flutter/material.dart';
5 import 'package:flutter/services.dart';
6 import 'package:flutter_test/flutter_test.dart';
7 import 'package:http/http.dart' as http;
8 import 'package:http/testing.dart';
9 import 'package:lifemate/api.dart';
10 import 'package:lifemate/learning_feedback_ui.dart';
11 import 'package:lifemate/offline_store.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 void main() {
16 TestWidgetsFlutterBinding.ensureInitialized();
17 setUp(() {
18 SharedPreferencesAsyncPlatform.instance =
19 InMemorySharedPreferencesAsync.withData({});
20 String? token;
21 TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
22 .setMockMethodCallHandler(
23 const MethodChannel('lifeguide/local_write_guard'), (call) async {
24 if (call.method == 'getBlocked') return token != null;
25 if (call.method == 'beginWrite') return token = 'feedback-fixture';
26 if (call.method == 'completeWrite' && call.arguments == token) {
27 token = null;
28 return true;
29 }
30 throw PlatformException(code: 'local_storage_restart_required');
31 });
32 });
33 const mutationId = '81111111-1111-4111-8111-111111111111';
34 const body = {
35 'mutationId': mutationId,
36 'unitId': null,
37 'category': 'topic_request',
38 'summary': 'درخواست تمرین بیشتر',
39 'shareWithContentTeam': true
40 };
41
42 test(
43 'explicit feedback survives reopening and retries the same consented summary',
44 () async {
45 var offline = true;
46 final sent = <Map<String, dynamic>>[];
47 final api = HttpIdentityApi(
48 baseUrl: 'https://feedback.example.test/api',
49 client: MockClient((request) async {
50 expect(request.url.path, '/api/v1/education/feedback');
51 if (offline) throw http.ClientException('synthetic offline');
52 sent.add(jsonDecode(request.body) as Map<String, dynamic>);
53 return http.Response(
54 jsonEncode({
55 'feedback': {
56 'id': '91111111-1111-4111-8111-111111111111',
57 'mutationId': mutationId,
58 'unitId': null,
59 'category': 'topic_request',
60 'summary': 'درخواست تمرین بیشتر',
61 'shareWithContentTeam': true,
62 'withdrawnAt': null,
63 'status': 'received',
64 'version': 1,
65 'updatedAt': '2026-10-09T10:00:00Z',
66 'createdAt': '2026-10-09T10:00:00Z'
67 },
68 'acknowledgement': {
69 'mutationId': mutationId,
70 'feedbackId': '91111111-1111-4111-8111-111111111111',
71 'status': 'already_applied'
72 }
73 }),
74 200,
75 headers: {'content-type': 'application/json; charset=utf-8'});
76 }))
77 ..accessToken = 'synthetic-token'
78 ..currentUserId = 'synthetic-owner';
79 final scope = api.cacheNamespace!;
80 expect((await LearningFeedbackSync(api).submit(body))['queued'], true);
81 final saved = await OfflineStore.forNamespace(scope).readQueue();
82 expect(saved.single['payload'], body);
83 offline = false;
84 await LearningFeedbackSync(api, store: OfflineStore.forNamespace(scope))
85 .flush();
86 await LearningFeedbackSync(api).flush();
87 expect(sent, [body]);
88 expect(await OfflineStore.forNamespace(scope).readQueue(), isEmpty);
89 expect(await OfflineStore.forNamespace(scope).readPlanner(), isEmpty);
90 expect(await OfflineStore.forNamespace(scope).readLastAck(), isNotNull);
91 api.close();
92 });
93
94 test('wrong feedback acknowledgement cannot remove an unsent summary',
95 () async {
96 final api = HttpIdentityApi(
97 baseUrl: 'https://feedback.example.test/api',
98 client: MockClient((request) async => http.Response(
99 jsonEncode({
100 'feedback': {
101 'id': '91111111-1111-4111-8111-111111111111',
102 'mutationId': mutationId,
103 'unitId': null,
104 'category': 'topic_request',
105 'summary': 'درخواست تمرین بیشتر',
106 'shareWithContentTeam': true,
107 'withdrawnAt': null,
108 'status': 'received',
109 'version': 1,
110 'updatedAt': '2026-10-09T10:00:00Z',
111 'createdAt': '2026-10-09T10:00:00Z'
112 },
113 'acknowledgement': {
114 'mutationId': mutationId,
115 'feedbackId': 'different-receipt',
116 'status': 'accepted'
117 }
118 }),
119 200,
120 headers: {'content-type': 'application/json; charset=utf-8'})))
121 ..accessToken = 'synthetic-token'
122 ..currentUserId = 'synthetic-owner';
123 await expectLater(
124 LearningFeedbackSync(api).submit(body), throwsA(isA<ApiException>()));
125 expect(
126 (await OfflineStore.forApi(api).readQueue()).single['payload'], body);
127 api.close();
128 });
129
130 testWidgets('a delayed history response cannot restore a withdrawn summary',
131 (tester) async {
132 tester.view.physicalSize = const Size(900, 1400);
133 tester.view.devicePixelRatio = 1;
134 addTearDown(tester.view.resetPhysicalSize);
135 addTearDown(tester.view.resetDevicePixelRatio);
136 final delayed = Completer<http.Response>();
137 var reads = 0, withdrawn = false;
138 final row = <String, dynamic>{
139 'id': '91111111-1111-4111-8111-111111111111',
140 'mutationId': mutationId,
141 'unitId': null,
142 'category': 'topic_request',
143 'summary': body['summary'],
144 'shareWithContentTeam': true,
145 'withdrawnAt': null,
146 'status': 'received',
147 'version': 1,
148 'updatedAt': '2026-10-09T10:00:00Z',
149 'createdAt': '2026-10-09T10:00:00Z'
150 };
151 http.Response response(Object value, [int status = 200]) =>
152 http.Response(jsonEncode(value), status,
153 headers: {'content-type': 'application/json; charset=utf-8'});
154 final api = HttpIdentityApi(
155 baseUrl: 'https://feedback.example.test/api',
156 client: MockClient((request) async {
157 if (request.method == 'GET') {
158 if (++reads == 1) return delayed.future;
159 return response({
160 'feedback': withdrawn ? [] : [row]
161 });
162 }
163 if (request.method == 'DELETE') {
164 withdrawn = true;
165 return http.Response('', 204);
166 }
167 final sent = jsonDecode(request.body) as Map;
168 row['mutationId'] = sent['mutationId'];
169 return response({
170 'feedback': row,
171 'acknowledgement': {
172 'mutationId': sent['mutationId'],
173 'feedbackId': row['id'],
174 'status': 'accepted'
175 }
176 }, 201);
177 }))
178 ..accessToken = 'synthetic-token'
179 ..currentUserId = 'synthetic-owner';
180 await tester.pumpWidget(MaterialApp(home: LearningFeedbackPage(api: api)));
181 await tester.pump();
182 await tester.enterText(find.byType(TextField), body['summary'] as String);
183 await tester.tap(find.byType(Checkbox));
184 await tester.tap(find.text('ارسال بازخورد'));
185 await tester.pumpAndSettle();
186 expect(find.text(body['summary'] as String), findsOneWidget);
187 await tester.tap(find.text('پس‌گرفتن رضایت'));
188 await tester.pumpAndSettle();
189 expect(withdrawn, true);
190 delayed.complete(response({
191 'feedback': [row]
192 }));
193 await tester.pumpAndSettle();
194 expect(find.text(body['summary'] as String), findsNothing);
195 await tester.pumpWidget(const SizedBox());
196 api.close();
197 });
198 }