LifeMate — Personal, Family & Learning Companion
main
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
| 1 | import test, { after } from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { app, pool } from '../src/server.js'; |
| 4 | |
| 5 | const server = app.listen(0); |
| 6 | const address = server.address(); |
| 7 | const base = `http://127.0.0.1:${address.port}`; |
| 8 | |
| 9 | after(async () => { |
| 10 | await new Promise((resolve) => server.close(resolve)); |
| 11 | await pool.end(); |
| 12 | }); |
| 13 | |
| 14 | async function request(path, { method = 'GET', body, token } = {}) { |
| 15 | const response = await fetch(base + path, { |
| 16 | method, |
| 17 | headers: { |
| 18 | 'content-type': 'application/json', |
| 19 | ...(token ? { authorization: `Bearer ${token}` } : {}), |
| 20 | }, |
| 21 | body: body === undefined ? undefined : JSON.stringify(body), |
| 22 | }); |
| 23 | const text = await response.text(); |
| 24 | return { |
| 25 | status: response.status, |
| 26 | body: text ? JSON.parse(text) : null, |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | async function registerVerified(email, displayName) { |
| 31 | const password = 'Phase4Pass!123'; |
| 32 | const registration = await request('/v1/auth/register', { |
| 33 | method: 'POST', |
| 34 | body: { email, displayName, password }, |
| 35 | }); |
| 36 | assert.equal(registration.status, 201); |
| 37 | assert.equal( |
| 38 | ( |
| 39 | await request('/v1/auth/verify-email', { |
| 40 | method: 'POST', |
| 41 | body: { token: registration.body.verificationToken }, |
| 42 | }) |
| 43 | ).status, |
| 44 | 204, |
| 45 | ); |
| 46 | const login = await request('/v1/auth/login', { |
| 47 | method: 'POST', |
| 48 | body: { email, password }, |
| 49 | }); |
| 50 | assert.equal(login.status, 200); |
| 51 | return { token: login.body.accessToken, userId: registration.body.userId }; |
| 52 | } |
| 53 | |
| 54 | test('phase 4 learning wellbeing and guardian safety boundaries', async () => { |
| 55 | const suffix = Date.now().toString(36); |
| 56 | const parentEmail = `p4-parent-${suffix}@example.test`; |
| 57 | const teenEmail = `p4-teen-${suffix}@example.test`; |
| 58 | const outsiderEmail = `p4-outsider-${suffix}@example.test`; |
| 59 | |
| 60 | const parent = await registerVerified(parentEmail, 'Phase4 Parent'); |
| 61 | const teen = await registerVerified(teenEmail, 'Phase4 Teen'); |
| 62 | const outsider = await registerVerified(outsiderEmail, 'Phase4 Outsider'); |
| 63 | |
| 64 | const family = await request('/v1/families', { |
| 65 | method: 'POST', |
| 66 | token: parent.token, |
| 67 | body: { name: 'Phase4 Family', role: 'parent_guardian' }, |
| 68 | }); |
| 69 | assert.equal(family.status, 201); |
| 70 | |
| 71 | const invitation = await request(`/v1/families/${family.body.id}/invitations`, { |
| 72 | method: 'POST', |
| 73 | token: parent.token, |
| 74 | body: { |
| 75 | email: teenEmail, |
| 76 | role: 'teen_minor', |
| 77 | themePreference: 'girl_pink', |
| 78 | }, |
| 79 | }); |
| 80 | assert.equal(invitation.status, 201); |
| 81 | |
| 82 | assert.equal( |
| 83 | ( |
| 84 | await request('/v1/invitations/accept', { |
| 85 | method: 'POST', |
| 86 | token: teen.token, |
| 87 | body: { token: invitation.body.invitationToken }, |
| 88 | }) |
| 89 | ).status, |
| 90 | 204, |
| 91 | ); |
| 92 | |
| 93 | assert.equal( |
| 94 | ( |
| 95 | await request(`/v1/families/${family.body.id}/guardians`, { |
| 96 | method: 'POST', |
| 97 | token: parent.token, |
| 98 | body: { |
| 99 | guardianUserId: parent.userId, |
| 100 | minorUserId: teen.userId, |
| 101 | }, |
| 102 | }) |
| 103 | ).status, |
| 104 | 204, |
| 105 | ); |
| 106 | |
| 107 | const goal = await request('/v1/learning/goals', { |
| 108 | method: 'POST', |
| 109 | token: teen.token, |
| 110 | body: { title: 'تسلط روی جبر', target: 'حل تمرینهای فصل اول' }, |
| 111 | }); |
| 112 | assert.equal(goal.status, 201); |
| 113 | |
| 114 | const learningCheckin = await request('/v1/learning/checkins', { |
| 115 | method: 'POST', |
| 116 | token: teen.token, |
| 117 | body: { |
| 118 | learningGoalId: goal.body.id, |
| 119 | confidence: 3, |
| 120 | difficulty: 4, |
| 121 | note: 'تمرین بیشتری لازم دارم', |
| 122 | }, |
| 123 | }); |
| 124 | assert.equal(learningCheckin.status, 201); |
| 125 | |
| 126 | const privateCheckin = await request('/v1/wellbeing/checkins', { |
| 127 | method: 'POST', |
| 128 | token: teen.token, |
| 129 | body: { |
| 130 | mood: 3, |
| 131 | energy: 2, |
| 132 | stress: 4, |
| 133 | note: 'این متن خصوصی است', |
| 134 | visibility: 'private', |
| 135 | }, |
| 136 | }); |
| 137 | assert.equal(privateCheckin.status, 201); |
| 138 | assert.equal(Object.hasOwn(privateCheckin.body, 'note'), false); |
| 139 | |
| 140 | const sharedCheckin = await request('/v1/wellbeing/checkins', { |
| 141 | method: 'POST', |
| 142 | token: teen.token, |
| 143 | body: { |
| 144 | mood: 4, |
| 145 | energy: 3, |
| 146 | stress: 2, |
| 147 | note: 'این متن هم نباید به والد برگردد', |
| 148 | visibility: 'guardian_summary', |
| 149 | }, |
| 150 | }); |
| 151 | assert.equal(sharedCheckin.status, 201); |
| 152 | assert.equal(Object.hasOwn(sharedCheckin.body, 'note'), false); |
| 153 | |
| 154 | const ownCheckins = await request('/v1/wellbeing/checkins', { |
| 155 | token: teen.token, |
| 156 | }); |
| 157 | assert.equal(ownCheckins.status, 200); |
| 158 | assert.equal(ownCheckins.body.items.length >= 2, true); |
| 159 | assert.equal( |
| 160 | ownCheckins.body.items.some((item) => Object.hasOwn(item, 'note')), |
| 161 | false, |
| 162 | ); |
| 163 | |
| 164 | const guardianSummary = await request( |
| 165 | `/v1/families/${family.body.id}/children/${teen.userId}/wellbeing-summary`, |
| 166 | { token: parent.token }, |
| 167 | ); |
| 168 | assert.equal(guardianSummary.status, 200); |
| 169 | assert.equal(guardianSummary.body.summary.checkin_count, 1); |
| 170 | assert.equal(guardianSummary.body.rawNotesIncluded, false); |
| 171 | assert.equal(guardianSummary.body.rawConversationIncluded, false); |
| 172 | |
| 173 | const outsiderSummary = await request( |
| 174 | `/v1/families/${family.body.id}/children/${teen.userId}/wellbeing-summary`, |
| 175 | { token: outsider.token }, |
| 176 | ); |
| 177 | assert.equal(outsiderSummary.status, 403); |
| 178 | |
| 179 | const familyGuidance = await request( |
| 180 | `/v1/families/${family.body.id}/children/${teen.userId}/family-guidance`, |
| 181 | { |
| 182 | method: 'POST', |
| 183 | token: parent.token, |
| 184 | body: { question: 'چطور بدون فشار حمایتش کنم؟' }, |
| 185 | }, |
| 186 | ); |
| 187 | assert.equal(familyGuidance.status, 200); |
| 188 | assert.equal(familyGuidance.body.advisory, true); |
| 189 | assert.equal(familyGuidance.body.medicalDiagnosis, false); |
| 190 | assert.equal(familyGuidance.body.rawNotesIncluded, false); |
| 191 | assert.equal(familyGuidance.body.rawConversationIncluded, false); |
| 192 | |
| 193 | const outsiderGuidance = await request( |
| 194 | `/v1/families/${family.body.id}/children/${teen.userId}/family-guidance`, |
| 195 | { |
| 196 | method: 'POST', |
| 197 | token: outsider.token, |
| 198 | body: { question: 'وضعیت چیست؟' }, |
| 199 | }, |
| 200 | ); |
| 201 | assert.equal(outsiderGuidance.status, 403); |
| 202 | |
| 203 | const session = await request('/v1/ai/sessions', { |
| 204 | method: 'POST', |
| 205 | token: teen.token, |
| 206 | body: { kind: 'wellbeing' }, |
| 207 | }); |
| 208 | assert.equal(session.status, 201); |
| 209 | |
| 210 | const ordinary = await request(`/v1/ai/sessions/${session.body.id}/messages`, { |
| 211 | method: 'POST', |
| 212 | token: teen.token, |
| 213 | body: { message: 'امروز کمی استرس دارم' }, |
| 214 | }); |
| 215 | assert.equal(ordinary.status, 200); |
| 216 | assert.equal(ordinary.body.advisory, true); |
| 217 | assert.equal(ordinary.body.medicalDiagnosis, false); |
| 218 | assert.equal(ordinary.body.automaticAction, false); |
| 219 | |
| 220 | const urgent = await request(`/v1/ai/sessions/${session.body.id}/messages`, { |
| 221 | method: 'POST', |
| 222 | token: teen.token, |
| 223 | body: { message: 'به خودکشی فکر میکنم' }, |
| 224 | }); |
| 225 | assert.equal(urgent.status, 200); |
| 226 | assert.equal(urgent.body.safety_class, 'urgent_review'); |
| 227 | |
| 228 | const safetySummary = await request( |
| 229 | `/v1/families/${family.body.id}/children/${teen.userId}/wellbeing-summary`, |
| 230 | { token: parent.token }, |
| 231 | ); |
| 232 | assert.equal(safetySummary.status, 200); |
| 233 | assert.equal(safetySummary.body.safety.open_urgent_count >= 1, true); |
| 234 | }); |