| 1 | // El service worker (web/sw.js) al pulsar una notificación, en Node: se carga |
| 2 | // en una sandbox con self, clients y caches de mentira y se le dispara el |
| 3 | // evento `notificationclick`. |
| 4 | // |
| 5 | // 1. «▶ Ejecutar» con la app cerrada: guarda la propuesta en Cache Storage y |
| 6 | // abre el IDE con su id en la URL (antes abría ?notif=execute sin la |
| 7 | // propuesta, y no se ejecutaba nada). |
| 8 | // 2. Con el IDE abierto: le manda la propuesta a esa ventana, sin abrir otra. |
| 9 | // 3. Con solo el Lab abierto: el Lab no es el IDE, se abre el IDE. |
| 10 | // 4. «Ver»: abre el IDE para ver la Mente, sin guardar nada. |
| 11 | // |
| 12 | // node tests/sw-notif.mjs |
| 13 | import { readFileSync } from 'node:fs'; |
| 14 | import { join, dirname } from 'node:path'; |
| 15 | import { fileURLToPath } from 'node:url'; |
| 16 | import vm from 'node:vm'; |
| 17 | const SW = readFileSync(join(dirname(fileURLToPath(import.meta.url)), '..', 'web', 'sw.js'), 'utf8'); |
| 18 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 19 | |
| 20 | function sandbox(ventanas = []) { |
| 21 | const handlers = {}; |
| 22 | const guardado = new Map(); |
| 23 | const abiertas = []; |
| 24 | const self = { |
| 25 | addEventListener: (tipo, fn) => { handlers[tipo] = fn; }, |
| 26 | skipWaiting() {}, clients: { |
| 27 | async matchAll() { return ventanas; }, |
| 28 | async openWindow(u) { abiertas.push(u); return {}; }, |
| 29 | claim() {}, |
| 30 | }, |
| 31 | }; |
| 32 | const caches = { async open(nombre) { return { async put(k, r) { guardado.set(nombre + '|' + k, await r.text()); } }; }, async delete() {} }; |
| 33 | const ctx = vm.createContext({ self, caches, Response: globalThis.Response, URL, console }); |
| 34 | vm.runInContext(SW, ctx); |
| 35 | const clic = async (action, data) => { |
| 36 | let espera = null; |
| 37 | handlers.notificationclick({ action, notification: { data, close() {} }, waitUntil(p) { espera = p; } }); |
| 38 | await espera; |
| 39 | }; |
| 40 | return { clic, guardado, abiertas }; |
| 41 | } |
| 42 | const ventana = url => { const v = { url, mensajes: [], enfocada: false, postMessage(m) { v.mensajes.push(m); }, focus() { v.enfocada = true; } }; return v; }; |
| 43 | |
| 44 | // 1 · ▶ Ejecutar con la app cerrada |
| 45 | { |
| 46 | const s = sandbox([]); |
| 47 | await s.clic('execute', { md: '# Propuesta\nhaz pruebas' }); |
| 48 | const url = s.abiertas[0] || ''; |
| 49 | const id = (/[?&]id=([a-z0-9]+)/.exec(url) || [])[1]; |
| 50 | ok('1 · ▶ Ejecutar con la app cerrada abre el IDE con la acción y un id', /^\.\/\?notif=execute&id=[a-z0-9]+$/.test(url), url); |
| 51 | ok('1 · …y la propuesta queda guardada bajo ese id', s.guardado.get('elffuss-notif|./notif/' + id) === '# Propuesta\nhaz pruebas', JSON.stringify([...s.guardado])); |
| 52 | ok('1 · …y no va en la URL', !/Propuesta|pruebas/.test(url), url); |
| 53 | } |
| 54 | // 2 · con el IDE abierto |
| 55 | { |
| 56 | const ide = ventana('https://code.example/?x'); |
| 57 | const s = sandbox([ide]); |
| 58 | await s.clic('execute', { md: '# P' }); |
| 59 | ok('2 · con el IDE abierto le manda la propuesta a esa ventana y la enfoca', ide.mensajes[0]?.type === 'notif-action' && ide.mensajes[0]?.md === '# P' && ide.enfocada, JSON.stringify(ide.mensajes)); |
| 60 | ok('2 · …sin abrir otra ni guardar nada', s.abiertas.length === 0 && s.guardado.size === 0, JSON.stringify(s.abiertas)); |
| 61 | } |
| 62 | // 3 · solo el Lab abierto |
| 63 | { |
| 64 | const lab = ventana('https://code.example/lab.html'); |
| 65 | const s = sandbox([lab]); |
| 66 | await s.clic('execute', { md: '# P' }); |
| 67 | ok('3 · con solo el Lab abierto, abre el IDE (el Lab no lo ejecutaría)', lab.mensajes.length === 0 && /^\.\/\?notif=execute&id=/.test(s.abiertas[0] || ''), JSON.stringify(s.abiertas)); |
| 68 | } |
| 69 | // 4 · Ver |
| 70 | { |
| 71 | const s = sandbox([]); |
| 72 | await s.clic('open', { md: '# P' }); |
| 73 | ok('4 · «Ver» abre el IDE para la Mente y no guarda nada', s.abiertas[0] === './?notif=open' && s.guardado.size === 0, JSON.stringify(s.abiertas)); |
| 74 | } |
| 75 | |
| 76 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ NOTIFICACIONES: ▶ Ejecutar llega con la propuesta, también con la app cerrada'); |
| 77 | process.exit(fails ? 1 : 0); |