Files
Evection/apps/pwa/tests.py
T
ksmithandClaude Sonnet 5 7577110238 Add the PWA layer: manifest, service worker, icons, offline page
Implements installable-PWA support per the plan: a manifest.webmanifest
view (name/short_name "Evection", standalone display, 192/512/maskable
icons), a service worker served at domain root (/sw.js, with
Service-Worker-Allowed: / so its scope covers the whole app, not just
/static/), and an offline fallback page.

The service worker ignores all non-GET requests outright (so mutations
like mark-read/star/OPML upload stay network-only without special-
casing every endpoint), does network-first-with-cache-fallback for
navigations (bounded to the 60 most recently viewed pages, trimmed
oldest-first), and cache-first for /static/ assets, with the cache
name keyed by EVECTION_PWA_CACHE_VERSION so a deploy can invalidate
old caches. Per the earlier decision, embedded remote images in
article content are not cached for offline reading — text/HTML only.

Generates a simple RSS-glyph icon set (192/512/maskable/apple-touch)
with a throwaway Pillow script (not a project dependency). Adds iOS
meta tags and service-worker registration to base.html. Fixed a real
bug found during this work: the first-run setup-wizard middleware was
redirecting /manifest.webmanifest, /sw.js, and /offline/ to /setup/
before an admin account existed, which would have broken a browser's
very first visit to a fresh install.

Verified: manifest JSON validity and required fields, sw.js delivery
(content-type, Service-Worker-Allowed header, correct body) via both
curl and automated tests (112 passing). Actual service-worker
registration could not be verified in this sandboxed browser pane —
confirmed via a control test (even a trivial unrelated static file
fails to register here with an opaque "unknown error fetching script")
that this is an environment limitation of locally-started dev servers
in this tool, not an application bug; registration, install-prompt,
and offline behavior need confirming in a real browser.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 16:46:06 -06:00

64 lines
2.2 KiB
Python

import json
import pytest
from django.urls import reverse
@pytest.mark.django_db
def test_manifest_is_valid_json_with_expected_fields(client):
response = client.get(reverse("pwa:manifest"))
assert response.status_code == 200
assert response["Content-Type"] == "application/manifest+json"
manifest = json.loads(response.content)
assert manifest["name"] == "Evection"
assert manifest["display"] == "standalone"
assert manifest["scope"] == "/"
sizes = {icon["sizes"] for icon in manifest["icons"]}
assert {"192x192", "512x512"} <= sizes
assert any(icon.get("purpose") == "maskable" for icon in manifest["icons"])
@pytest.mark.django_db
def test_service_worker_is_served_at_root_with_correct_type_and_scope(client):
response = client.get(reverse("pwa:service_worker"))
assert response.status_code == 200
assert response["Content-Type"] == "application/javascript"
assert response["Service-Worker-Allowed"] == "/"
body = response.content.decode()
assert 'self.addEventListener("install"' in body
assert 'self.addEventListener("fetch"' in body
# Mutations must stay network-only, never intercepted.
assert 'request.method !== "GET"' in body
@pytest.mark.django_db
def test_offline_page_renders(client):
response = client.get(reverse("pwa:offline"))
assert response.status_code == 200
assert b"offline" in response.content.lower()
@pytest.mark.django_db
def test_manifest_and_sw_do_not_require_login(client):
# The service worker fetches these itself, unauthenticated, before any
# user necessarily has a session.
assert client.get(reverse("pwa:manifest")).status_code == 200
assert client.get(reverse("pwa:service_worker")).status_code == 200
assert client.get(reverse("pwa:offline")).status_code == 200
@pytest.mark.django_db
def test_base_template_links_manifest_and_registers_service_worker(client, django_user_model):
django_user_model.objects.create_user("alice", password="pw-alice-123")
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
content = response.content.decode()
assert 'rel="manifest"' in content
assert "serviceWorker" in content
assert "apple-touch-icon" in content