70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
"""
|
|
Step 29 — Persistent storage request
|
|
|
|
Without navigator.storage.persist(), the origin's storage (OPFS database,
|
|
PWA precache — including the Bootstrap Icons font) is "best-effort" and the
|
|
browser may evict it after long periods of inactivity or under disk
|
|
pressure. On startup the app now requests persistent storage once,
|
|
fire-and-forget.
|
|
|
|
The real persist() outcome depends on browser heuristics / a permission
|
|
prompt, so these tests stub StorageManager.prototype.persist via an init
|
|
script and verify the app's behaviour around it:
|
|
|
|
- test_persist_requested_on_startup: startup calls persist() exactly once
|
|
and the app boots normally when the request is granted
|
|
- test_app_boots_when_persist_denied: a denied request does not block
|
|
startup (red path)
|
|
- test_app_boots_without_persist_api: browsers without the API still boot
|
|
and no call is attempted (red path)
|
|
"""
|
|
|
|
from playwright.sync_api import Page
|
|
|
|
_DB_READY = "document.documentElement.getAttribute('data-db-ready') === 'true'"
|
|
|
|
# Stub persist() to count calls and resolve with a fixed grant result.
|
|
_STUB_PERSIST_TEMPLATE = """
|
|
(() => {
|
|
window.__persistCalls = 0;
|
|
StorageManager.prototype.persist = function () {
|
|
window.__persistCalls += 1;
|
|
return Promise.resolve(%s);
|
|
};
|
|
})();
|
|
"""
|
|
|
|
_REMOVE_PERSIST = """
|
|
(() => {
|
|
window.__persistCalls = 0;
|
|
StorageManager.prototype.persist = function () {
|
|
window.__persistCalls += 1;
|
|
};
|
|
delete StorageManager.prototype.persist;
|
|
})();
|
|
"""
|
|
|
|
|
|
def test_persist_requested_on_startup(page: Page, app_url: str):
|
|
"""Startup requests persistent storage exactly once and the app boots."""
|
|
page.add_init_script(_STUB_PERSIST_TEMPLATE % "true")
|
|
page.goto(app_url)
|
|
page.wait_for_function(_DB_READY, timeout=30000)
|
|
assert page.evaluate("window.__persistCalls") == 1
|
|
|
|
|
|
def test_app_boots_when_persist_denied(page: Page, app_url: str):
|
|
"""A denied persistent-storage request does not block startup."""
|
|
page.add_init_script(_STUB_PERSIST_TEMPLATE % "false")
|
|
page.goto(app_url)
|
|
page.wait_for_function(_DB_READY, timeout=30000)
|
|
assert page.evaluate("window.__persistCalls") == 1
|
|
|
|
|
|
def test_app_boots_without_persist_api(page: Page, app_url: str):
|
|
"""Browsers without navigator.storage.persist still boot normally."""
|
|
page.add_init_script(_REMOVE_PERSIST)
|
|
page.goto(app_url)
|
|
page.wait_for_function(_DB_READY, timeout=30000)
|
|
assert page.evaluate("window.__persistCalls") == 0
|