LifeMate — Personal, Family & Learning Companion

fix/study-duration-display
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 87 lines 3.03 KB
Raw
1 // Browser-only focused tests of the actual Dart access paths. The test runner
2 // supplies controlled ownership; production ownership comes from Web Locks.
3 import 'dart:js_interop';
4
5 import 'package:flutter_test/flutter_test.dart';
6 import 'package:http/http.dart' as http;
7 import 'package:http/testing.dart';
8 import 'package:lifemate/api.dart';
9 import 'package:lifemate/local_write_guard.dart';
10 import 'package:lifemate/offline_store.dart';
11 import 'package:lifemate/session_store.dart';
12
13 @JS('lifeguideInstanceOwned')
14 external set _owned(bool value);
15
16 @JS('localStorage')
17 external _Storage get _storage;
18 extension type _Storage(JSObject _) implements JSObject {
19 external String? getItem(String key);
20 external void setItem(String key, String value);
21 external void removeItem(String key);
22 }
23
24 void main() {
25 setUp(() {
26 _owned = false;
27 _storage.setItem(sessionStorageKey, 'synthetic-existing-session');
28 });
29 tearDown(() {
30 _storage.removeItem(sessionStorageKey);
31 _owned = false;
32 });
33
34 test('unowned web session cannot read, replace or erase owner credentials',
35 () async {
36 final store = createSessionStore();
37 await expectLater(store.read(), throwsStateError);
38 await expectLater(store.write('synthetic-replacement'), throwsStateError);
39 await expectLater(store.clear(), throwsStateError);
40 expect(_storage.getItem(sessionStorageKey), 'synthetic-existing-session');
41 _owned = true;
42 expect(await store.read(), 'synthetic-existing-session');
43 });
44
45 test(
46 'unowned web API never sends a request; late response cannot be accepted',
47 () async {
48 var requests = 0;
49 final api = HttpIdentityApi(
50 baseUrl: 'https://family.example.test/api',
51 client: MockClient((request) async {
52 requests++;
53 // Simulate document suspension while an owned request was in flight.
54 _owned = false;
55 return http.Response('{"synthetic":true}', 200);
56 }));
57 addTearDown(api.close);
58 await expectLater(
59 api.requestJson('GET', '/v1/profile', auth: false), throwsStateError);
60 expect(requests, 0);
61 _owned = true;
62 await expectLater(
63 api.requestJson('GET', '/v1/profile', auth: false), throwsStateError);
64 expect(requests, 1);
65 });
66
67 test('unowned personal-cache read and write fail before storage access',
68 () async {
69 const guard = LocalWriteGuard();
70 await expectLater(guard.getBlocked(), throwsStateError);
71 await expectLater(guard.beginWrite(), throwsStateError);
72 final store = OfflineStore.forNamespace('synthetic-owner-cache',
73 session: OfflineSession());
74 final blocked = isA<ApiException>().having(
75 (error) => error.code, 'code', 'local_storage_restart_required');
76 await expectLater(store.readList('planner'), throwsA(blocked));
77 await expectLater(
78 store.writeList('planner', [
79 {'id': 'synthetic-item', 'title': 'synthetic'}
80 ]),
81 throwsA(blocked));
82 expect(
83 isOfflineFailure(
84 const ApiException(500, 'local_storage_restart_required')),
85 false);
86 });
87 }