LifeMate — Personal, Family & Learning Companion

feat/aparat-learning-library
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
mjs 88 lines 5.76 KB
Raw
1 #!/usr/bin/env node
2 // Node >=22. TLS verification always remains enabled. Never logs credentials.
3 import https from 'node:https';
4 import fs from 'node:fs';
5 import crypto from 'node:crypto';
6
7 const origin = new URL(process.argv[2] ?? process.env.SMOKE_ORIGIN ?? '');
8 if (origin.protocol !== 'https:' || origin.username || origin.password || origin.search || origin.hash || origin.pathname !== '/') {
9 throw new Error('Pass one HTTPS origin without credentials, path, query or fragment');
10 }
11 const ca = process.env.ROOT_CA_FILE ? fs.readFileSync(process.env.ROOT_CA_FILE) : undefined;
12 const results = [];
13 let currentCheck = 'TLS/connection';
14 const record = (name, status, detail) => results.push({ name, status, detail });
15 async function request(path, { method = 'GET', json, token } = {}) {
16 const url = new URL(path, origin);
17 if (url.origin !== origin.origin) throw new Error('Cross-origin request refused');
18 const body = json === undefined ? undefined : JSON.stringify(json);
19 return await new Promise((resolve, reject) => {
20 const req = https.request(url, { method, ca, rejectUnauthorized: true, timeout: 15000, headers: {
21 ...(body ? { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) } : {}),
22 ...(token ? { Authorization: `Bearer ${token}` } : {}),
23 } }, res => {
24 const tls = res.socket?.authorized;
25 const chunks = []; let size = 0;
26 res.on('data', chunk => { size += chunk.length; if (size > 4 * 1024 * 1024) req.destroy(new Error('Oversize response')); else chunks.push(chunk); });
27 res.on('end', () => resolve({ status: res.statusCode, text: Buffer.concat(chunks).toString('utf8'), headers: res.headers, tls }));
28 });
29 req.on('timeout', () => req.destroy(new Error('Timeout')));
30 req.on('error', reject);
31 if (body) req.write(body);
32 req.end();
33 });
34 }
35 function assert(condition, message) { if (!condition) throw new Error(message); }
36 try {
37 const health = await request('/health');
38 assert(health.tls === true, 'TLS certificate chain and hostname are not verified');
39 record('TLS', 'PASS', 'Certificate chain and hostname verified by Node TLS');
40 currentCheck = '/health';
41 assert(health.status === 200 && JSON.parse(health.text).status === 'ok', `health HTTP ${health.status}`);
42 record('/health', 'PASS', 'HTTP 200 and database-backed status ok');
43 currentCheck = 'PWA';
44 const pwa = await request('/');
45 assert(pwa.status === 200 && /LifeGuide|لایف‌گاید/.test(pwa.text), 'PWA title/content missing');
46 const manifest = await request('/manifest.json');
47 assert(manifest.status === 200 && JSON.parse(manifest.text).display === 'standalone', 'PWA manifest missing standalone');
48 record('PWA', 'PASS', 'HTTPS HTML and standalone manifest loaded; Safari installation UNVERIFIED');
49 currentCheck = 'runtime config';
50 const config = await request('/config.json');
51 assert(config.status === 200 && JSON.parse(config.text).apiBaseUrl === '/api', 'Same-origin runtime API config missing');
52 assert(String(config.headers['cache-control']).includes('no-store'), 'Runtime config can become stale');
53 record('runtime config', 'PASS', 'Same-origin /api; no-store');
54 currentCheck = 'API auth boundary';
55 const denied = await request('/api/v1/plan-items');
56 assert(denied.status === 401, `Anonymous planner unexpectedly returned HTTP ${denied.status}`);
57 record('API auth boundary', 'PASS', 'Anonymous planner request rejected with 401');
58 if (process.env.SMOKE_ALLOW_SYNTHETIC_WRITE !== 'yes') {
59 record('API round-trip', 'UNVERIFIED', 'Set SMOKE_ALLOW_SYNTHETIC_WRITE=yes and synthetic SMOKE_EMAIL/SMOKE_PASSWORD to run');
60 } else {
61 currentCheck = 'API round-trip';
62 assert(process.env.SMOKE_EMAIL && process.env.SMOKE_PASSWORD, 'Synthetic account credentials required');
63 const login = await request('/api/v1/auth/login', { method: 'POST', json: { email: process.env.SMOKE_EMAIL, password: process.env.SMOKE_PASSWORD } });
64 assert(login.status === 200, `Login HTTP ${login.status}`);
65 const token = JSON.parse(login.text).accessToken;
66 assert(token, 'Access token missing');
67 const title = `LifeGuide synthetic reachability ${crypto.randomUUID()}`;
68 const created = await request('/api/v1/plan-items', { method: 'POST', token, json: { kind: 'assignment', title, visibility: 'private', priority: 'normal' } });
69 assert(created.status === 201, `Create HTTP ${created.status}`);
70 const createdItem = JSON.parse(created.text);
71 const id = createdItem.id;
72 assert(id, 'Created record ID missing');
73 const read = await request('/api/v1/plan-items?kind=assignment', { token });
74 assert(read.status === 200 && JSON.parse(read.text).items.some(item => item.id === id && item.title === title), 'Independent read did not return created task');
75 const archived = await request(`/api/v1/plan-items/${id}`, { method: 'PATCH', token, json: { status: 'cancelled', expectedVersion: createdItem.version } });
76 assert(archived.status === 200 && JSON.parse(archived.text).status === 'cancelled', 'Safe archive failed');
77 const logout = await request('/api/v1/auth/logout', { method: 'POST', token, json: { refreshToken: JSON.parse(login.text).refreshToken } });
78 assert(logout.status === 204, 'Synthetic session logout failed');
79 const revoked = await request('/api/v1/plan-items', { token });
80 assert(revoked.status === 401, 'Logged-out session remains usable');
81 record('API round-trip', 'PASS', 'Synthetic private homework created, independently read, and archived on server');
82 record('session cleanup', 'PASS', 'Logout acknowledged; old access token rejected');
83 }
84 } catch (error) {
85 record(currentCheck, 'FAIL', `${error.code ?? 'CHECK_FAILED'}: ${String(error.message).slice(0, 240)}`);
86 process.exitCode = 1;
87 }
88 console.log(JSON.stringify({ origin: origin.origin, at: new Date().toISOString(), results }, null, 2));