LifeMate — Personal, Family & Learning Companion
docs/offline-data-rescue-options
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 from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { createKavenegarSmsProvider } from '../src/kavenegar_sms.js'; |
| 4 | import { createSmsProvider } from '../src/auth_delivery.js'; |
| 5 | |
| 6 | const apiKey = 'SyntheticKavenegarApiKey123456789'; |
| 7 | const template = 'lifeguideotp'; |
| 8 | // Fixture approval only: fetch is always injected; these are not live settings. |
| 9 | const env = { SMS_PROVIDER: 'kavenegar', SMS_LIVE_ENABLED: 'true', KAVENEGAR_TEMPLATE_APPROVED: 'true', KAVENEGAR_API_KEY: apiKey, KAVENEGAR_TEMPLATE: template }; |
| 10 | const request = { phone: '+989121234567', code: '082735', expiresIn: 300, |
| 11 | idempotencyKey: '11111111-2222-4333-8444-555555555555' }; |
| 12 | const ok = () => new Response(JSON.stringify({ |
| 13 | return: { status: 200, message: 'Accepted' }, |
| 14 | entries: { messageid: 123, status: 1 }, |
| 15 | }), { status: 200, headers: { 'content-type': 'application/json' } }); |
| 16 | const safeError = (error, expected) => { |
| 17 | assert.equal(error.message, expected); |
| 18 | assert.equal(error.retryable, false); |
| 19 | assert.equal(error.cause, undefined); |
| 20 | for (const value of [apiKey, request.phone, request.code]) { |
| 21 | assert.equal(String(error.stack).includes(value), false); |
| 22 | } |
| 23 | return true; |
| 24 | }; |
| 25 | |
| 26 | test('Kavenegar is opt-in and existing SMS provider selection remains intact', () => { |
| 27 | const defaultProvider = createSmsProvider({ env: {} }); |
| 28 | assert.equal(defaultProvider.configured, false); |
| 29 | assert.equal(createSmsProvider({ env: { SMS_PROVIDER: 'disabled' } }).configured, false); |
| 30 | const kavenegar = createSmsProvider({ env, fetchImpl: async () => ok() }); |
| 31 | assert.equal(kavenegar.configured, true); |
| 32 | assert.equal(kavenegar.status, 'CONFIGURED_UNVERIFIED'); |
| 33 | assert.equal(JSON.stringify(kavenegar).includes(apiKey), false); |
| 34 | assert.equal(JSON.stringify(kavenegar).includes(template), false); |
| 35 | }); |
| 36 | |
| 37 | test('Kavenegar VerifyLookup uses fixed HTTPS host, Iranian recipient, approved template and one token', async () => { |
| 38 | const calls = []; |
| 39 | const provider = createKavenegarSmsProvider({ env, fetchImpl: async (url, options) => { |
| 40 | calls.push({ url, options }); |
| 41 | return ok(); |
| 42 | } }); |
| 43 | assert.deepEqual(await provider.sendOtp({ ...request, ignoredPrivateNotes: 'do-not-send' }), { delivered: true }); |
| 44 | assert.equal(calls.length, 1); |
| 45 | const target = new URL(calls[0].url); |
| 46 | assert.equal(target.protocol, 'https:'); |
| 47 | assert.equal(target.host, 'api.kavenegar.com'); |
| 48 | assert.equal(target.pathname, `/v1/${apiKey}/verify/lookup.json`); |
| 49 | assert.equal(target.searchParams.get('receptor'), '09121234567'); |
| 50 | assert.equal(target.searchParams.get('token'), '082735'); |
| 51 | assert.equal(target.searchParams.get('template'), 'lifeguideotp'); |
| 52 | assert.equal(target.searchParams.get('type'), 'sms'); |
| 53 | assert.equal([...target.searchParams.keys()].length, 4); |
| 54 | const options = calls[0].options; |
| 55 | assert.equal(options.method, 'GET'); |
| 56 | assert.equal(options.redirect, 'error'); |
| 57 | assert.ok(options.signal instanceof AbortSignal); |
| 58 | assert.equal(options.headers.Accept, 'application/json'); |
| 59 | assert.equal(options.body, undefined); |
| 60 | }); |
| 61 | |
| 62 | test('Kavenegar rejects invalid environment without any outbound call or secret echo', () => { |
| 63 | for (const config of [ |
| 64 | { KAVENEGAR_API_KEY: '' }, |
| 65 | { KAVENEGAR_API_KEY: 'short' }, |
| 66 | { KAVENEGAR_API_KEY: apiKey + '\\n' }, |
| 67 | { KAVENEGAR_TEMPLATE: '' }, |
| 68 | { KAVENEGAR_TEMPLATE: '../password' }, |
| 69 | { KAVENEGAR_TEMPLATE: 'LifeGuide template' }, |
| 70 | { KAVENEGAR_TEMPLATE: '%token' }, |
| 71 | ]) { |
| 72 | assert.throws(() => createKavenegarSmsProvider({ env: { ...env, ...config } }), error => { |
| 73 | assert.match(error.message, /^sms_provider_(token|template)_invalid$/); |
| 74 | assert.equal(error.retryable, false); |
| 75 | assert.equal(error.stack.includes(apiKey), false); |
| 76 | return true; |
| 77 | }); |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | test('Kavenegar rejects unapproved phone shape, token and overlong lifespan before calling fetch', async () => { |
| 82 | let calls = 0; |
| 83 | const provider = createKavenegarSmsProvider({ env, fetchImpl: async () => { calls++; return ok(); } }); |
| 84 | for (const message of [ |
| 85 | { ...request, phone: '+15550000001' }, |
| 86 | { ...request, phone: '09121234567' }, |
| 87 | { ...request, code: '12345' }, |
| 88 | { ...request, code: '12&456' }, |
| 89 | { ...request, expiresIn: 301 }, |
| 90 | { ...request, idempotencyKey: 'bad' }, |
| 91 | ]) { |
| 92 | await assert.rejects(provider.sendOtp(message), error => safeError(error, 'sms_provider_input_invalid')); |
| 93 | } |
| 94 | assert.equal(calls, 0); |
| 95 | }); |
| 96 | |
| 97 | test('Kavenegar provider failures never cause automatic non-idempotent SMS retries', async () => { |
| 98 | for (const status of [301, 400, 401, 403, 429, 500, 503]) { |
| 99 | let readBody = false; |
| 100 | const provider = createKavenegarSmsProvider({ env, fetchImpl: async () => ({ |
| 101 | status, |
| 102 | body: { async cancel() { readBody = true; } }, |
| 103 | }) }); |
| 104 | await assert.rejects(provider.sendOtp(request), error => safeError(error, 'sms_provider_rejected')); |
| 105 | assert.equal(readBody, true); |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | test('Kavenegar treats HTTP200 provider rejection, malformed JSON or missing entries as failures', async () => { |
| 110 | const cases = [ |
| 111 | [JSON.stringify({ return: { status: 411 }, entries: { messageid: 2 } }), 'sms_provider_rejected'], |
| 112 | [JSON.stringify({ return: { status: 200 } }), 'sms_provider_rejected'], |
| 113 | [JSON.stringify({ return: { status: 200 }, entries: [] }), 'sms_provider_rejected'], |
| 114 | ['not json', 'sms_provider_malformed_response'], |
| 115 | ['x'.repeat(8193), 'sms_provider_malformed_response'], |
| 116 | ]; |
| 117 | for (const [body, code] of cases) { |
| 118 | const provider = createKavenegarSmsProvider({ env, fetchImpl: async () => new Response(body, { status: 200 }) }); |
| 119 | await assert.rejects(provider.sendOtp(request), error => safeError(error, code)); |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | test('Kavenegar transport exception never exposes API key, phone or code', async () => { |
| 124 | const provider = createKavenegarSmsProvider({ env, |
| 125 | fetchImpl: async () => { throw new Error(`failed ${apiKey} ${request.phone} ${request.code}`); } }); |
| 126 | await assert.rejects(provider.sendOtp(request), error => safeError(error, 'sms_provider_unavailable')); |
| 127 | }); |
| 128 | |
| 129 | test('Kavenegar bounded timeout aborts fetch with sanitized error', async () => { |
| 130 | let aborted = false; |
| 131 | const provider = createKavenegarSmsProvider({ env, timeoutMs: 20, fetchImpl: async (_url, { signal }) => new Promise((_resolve, reject) => { |
| 132 | const timer = setTimeout(() => reject(new Error('fixture ignored abort')), 400); |
| 133 | signal.addEventListener('abort', () => { aborted = true; clearTimeout(timer); reject(new Error(apiKey)); }, { once: true }); |
| 134 | }) }); |
| 135 | await assert.rejects(provider.sendOtp(request), error => safeError(error, 'sms_provider_timeout')); |
| 136 | assert.equal(aborted, true); |
| 137 | }); |