diff --git a/tests/test_web_companion.py b/tests/test_web_companion.py index 9186e0d..f9ecbce 100644 --- a/tests/test_web_companion.py +++ b/tests/test_web_companion.py @@ -95,7 +95,7 @@ def test_web_companion_node_test_suite_passes(): pytest.skip("Node.js ist in dieser Umgebung nicht installiert") proc = subprocess.run( - [node_bin, "--test", "tests/library.test.mjs", "tests/pwa.test.mjs", "tests/i18n.test.mjs"], + [node_bin, "--test", "tests/library.test.mjs", "tests/pwa.test.mjs", "tests/i18n.test.mjs", "tests/sw_asset_hash.test.mjs"], cwd=str(WEB_DIR), capture_output=True, text=True, diff --git a/web_companion/package.json b/web_companion/package.json index f47bb5a..ac0171a 100644 --- a/web_companion/package.json +++ b/web_companion/package.json @@ -3,7 +3,7 @@ "private": true, "type": "module", "scripts": { - "test": "node --test tests/library.test.mjs tests/pwa.test.mjs tests/i18n.test.mjs", + "test": "node --test tests/library.test.mjs tests/pwa.test.mjs tests/i18n.test.mjs tests/sw_asset_hash.test.mjs", "smoke:pwa": "node --test tests/pwa.test.mjs" } } diff --git a/web_companion/sw.js b/web_companion/sw.js index 65d90cb..1169e96 100644 --- a/web_companion/sw.js +++ b/web_companion/sw.js @@ -1,4 +1,7 @@ const CACHE_NAME = "sqliteviewer-companion-v5"; +// Inhalts-Hash aller OFFLINE_ASSETS-Dateien inkl. Manifest und Icons (web_companion/tests/sw_asset_hash.test.mjs prueft ihn): +// aendert sich eine davon, CACHE_NAME erhoehen, sonst behalten installierte PWAs den alten Code. +const ASSET_HASH = 'ba4caa95b77f'; // eslint-disable-line no-unused-vars const OFFLINE_ASSETS = [ "./", "./index.html", diff --git a/web_companion/tests/sw_asset_hash.test.mjs b/web_companion/tests/sw_asset_hash.test.mjs new file mode 100644 index 0000000..6352422 --- /dev/null +++ b/web_companion/tests/sw_asset_hash.test.mjs @@ -0,0 +1,59 @@ +// Wächter gegen veraltete Service-Worker-Caches (Vorlage: assistassets-ai/mediplaner a0363a2, PR #8). +// Der Service Worker liefert cache-first: ändert sich eine gecachte Datei, ohne dass CACHE_NAME steigt, +// behalten installierte PWAs den alten Code. ASSET_HASH bindet die Version an den Inhalt. +// Der Service Worker selbst zählt nicht mit (ein Eintrag von sich selbst wäre zirkulär; Browser prüfen ihn ohnehin per Byte-Vergleich). +// Die Dateiliste kommt aus dem Asset-Array des Service Workers selbst (inkl. Manifest und Icons); +// eine eigene Liste hier würde still veralten. Schlägt der Test fehl: +// CACHE_NAME in sw.js erhöhen und ASSET_HASH auf den genannten Wert setzen. +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { createHash } from 'node:crypto'; +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); +const SW = 'sw.js'; +const ARRAY = 'OFFLINE_ASSETS'; +// Asset-URL im Service Worker -> Datei relativ zu ROOT ('' = ausgelassen) +const toFile = (asset) => asset.replace(/^\.\//, ''); +const TEXT = /\.(html|js|mjs|css|webmanifest|json|svg|txt|xml)$/i; + +// Exakte Groß-/Kleinschreibung: Windows/macOS verbergen Abweichungen, ein Linux-Server liefert dann 404 +// (und cache.addAll() bricht die gesamte Installation des Service Workers ab). +const exists = (rel) => { + const abs = path.resolve(ROOT, rel); + for (let cur = abs; cur !== ROOT && cur !== path.dirname(cur); cur = path.dirname(cur)) { + let names; + try { names = fs.readdirSync(path.dirname(cur)); } catch { continue; } + if (!names.includes(path.basename(cur))) return false; + } + return fs.existsSync(abs); +}; +const read = (rel) => fs.readFileSync(path.join(ROOT, rel), 'utf-8'); + +describe('Service-Worker-Cache', () => { + it('ASSET_HASH passt zum Inhalt aller gecachten Dateien', () => { + const sw = read(SW); + const block = sw.match(new RegExp(`const ${ARRAY}\\s*=\\s*\\[([\\s\\S]*?)\\];?`)); + assert.ok(block, `${ARRAY} fehlt in ${SW}`); + const assets = [...new Set([...block[1].matchAll(/['"]([^'"]+)['"]/g)] + .map((m) => toFile(m[1])) + .filter((rel) => rel !== '' && rel !== SW))]; + assert.ok(assets.includes('index.html'), `index.html fehlt in ${ARRAY}`); + const hash = createHash('sha256'); + for (const rel of assets) { + assert.ok(exists(rel), `In ${ARRAY} gelistet, aber nicht vorhanden: ${rel}`); + const content = TEXT.test(rel) + ? Buffer.from(read(rel).replace(/\r\n/g, '\n'), 'utf-8') + : fs.readFileSync(path.join(ROOT, rel)); + hash.update(`${rel}\0`); + hash.update(content); + } + const actual = hash.digest('hex').slice(0, 12); + const recorded = sw.match(/ASSET_HASH\s*=\s*['"]([0-9a-f]{12})['"]/); + assert.ok(recorded, `ASSET_HASH fehlt in ${SW}`); + assert.equal(recorded[1], actual, + `Gecachte Dateien geändert: CACHE_NAME in ${SW} erhöhen und ASSET_HASH auf '${actual}' setzen`); + }); +});