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
mjs 52 lines 2.98 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import fs from 'node:fs/promises';
4 import vm from 'node:vm';
5
6 const source = await fs.readFile(new URL('../web/lifeguide-sw.js', import.meta.url), 'utf8');
7 function worker({offline=false}={}) {
8 const handlers={},stored=new Map(),requests=[];
9 const response=new Response('synthetic public document',{status:200});
10 const cache={async put(key,value){stored.set(typeof key==='string'?key:key.url,value);},
11 async match(key){return stored.get(typeof key==='string'?key:key.url);}};
12 const context={URL,Request,Response,
13 self:{location:{href:'https://offline.example.test/lifeguide-sw.js',origin:'https://offline.example.test'},
14 addEventListener(name,handler){handlers[name]=handler;},async skipWaiting(){},clients:{async claim(){}}},
15 caches:{async open(){return cache;},async keys(){return[];},async delete(){}},
16 async fetch(request,options){requests.push({request,options});if(offline)throw new Error('synthetic network down');return response.clone();}};
17 vm.runInNewContext(source,context);
18 return {handlers,cache,stored,requests,setOffline(value){offline=value;},
19 async install(){let done;handlers.install({waitUntil(value){done=value;}});await done;},
20 async fetch(path,{mode='navigate',authorization=false,method='GET'}={}) {
21 let result;
22 const request={url:'https://offline.example.test'+path,mode,method,
23 headers:new Headers(authorization?{authorization:'synthetic'}:{})};
24 handlers.fetch({request,respondWith(value){result=value;}});
25 return result===undefined?undefined:await result;
26 }};
27 }
28
29 test('installation caches only a credential-free public offline notice and retry script',async()=>{
30 const w=worker();await w.install();
31 assert.equal(w.stored.size,2);
32 for(const call of w.requests){assert.equal(call.request.credentials,'omit');assert.equal(call.options.cache,'no-store');}
33 });
34 test('cold offline navigation receives public notice; online navigation prefers network',async()=>{
35 const w=worker();await w.install();w.setOffline(true);
36 assert.equal((await w.fetch('/')).status,200);
37 w.setOffline(false);const before=w.requests.length;
38 assert.equal((await w.fetch('/')).status,200);
39 assert.equal(w.requests.length,before+1);
40 });
41 test('API, runtime configuration, auth paths and authenticated requests never receive fallback',async()=>{
42 const w=worker();await w.install();w.setOffline(true);
43 for(const path of ['/api/health','/api/v1/profile','/config.json','/verify-email?token=synthetic','/accept-invitation?token=synthetic','/unknown']) {
44 assert.equal(await w.fetch(path),undefined,path);
45 }
46 assert.equal(await w.fetch('/',{authorization:true}),undefined);
47 assert.equal(await w.fetch('/',{method:'POST'}),undefined);
48 });
49 test('uncached private content is never created by offline navigation',async()=>{
50 const w=worker();await w.install();const keys=[...w.stored.keys()];w.setOffline(true);
51 await w.fetch('/index.html');assert.deepEqual([...w.stored.keys()],keys);
52 });