Files
sovereign_browser/tests/local-site/storage-test.html
T

198 lines
5.5 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Local Site — Storage Test</title>
<link rel="stylesheet" href="assets/site.css">
</head>
<body>
<header>
<h1>Storage Test Page</h1>
<nav><a href="index.html">Home</a></nav>
</header>
<main>
<h2>Results</h2>
<pre id="results">Running tests...</pre>
</main>
<script>
(function () {
var log = [];
function flush() {
var el = document.getElementById('results');
if (el) el.textContent = log.join('\n');
}
function add(msg) {
log.push(msg);
flush();
}
add('=== Storage Test Start ===');
add('URL: ' + location.href);
add('');
// 1. localStorage
add('--- localStorage ---');
try {
localStorage.setItem('ls_test', 'ls_value');
add('SET: ok');
add('GET: ' + localStorage.getItem('ls_test'));
add('LENGTH: ' + localStorage.length);
localStorage.removeItem('ls_test');
add('REMOVE: ok, length now ' + localStorage.length);
} catch (e) {
add('ERROR: ' + e.message);
}
add('');
// 2. sessionStorage
add('--- sessionStorage ---');
try {
sessionStorage.setItem('ss_test', 'ss_value');
add('SET: ok');
add('GET: ' + sessionStorage.getItem('ss_test'));
add('LENGTH: ' + sessionStorage.length);
sessionStorage.removeItem('ss_test');
add('REMOVE: ok, length now ' + sessionStorage.length);
} catch (e) {
add('ERROR: ' + e.message);
}
add('');
// 3. Cookies
add('--- Cookies (document.cookie) ---');
try {
document.cookie = 'sb_test=cookie_value; path=/';
add('SET: ok');
var match = document.cookie.match(/sb_test=([^;]+)/);
add('GET: ' + (match ? match[1] : 'NOT FOUND'));
add('ALL: ' + document.cookie);
} catch (e) {
add('ERROR: ' + e.message);
}
add('');
// 4. IndexedDB
add('--- IndexedDB ---');
add('available: ' + ('indexedDB' in window));
try {
var req = indexedDB.open('sb_test_db', 1);
req.onupgradeneeded = function (e) {
var db = e.target.result;
if (!db.objectStoreNames.contains('kv')) {
db.createObjectStore('kv');
add('UPGRADE: created object store "kv"');
}
};
req.onsuccess = function (e) {
var db = e.target.result;
add('OPEN: ok (name=' + db.name + ', version=' + db.version + ')');
add('STORES: ' + Array.from(db.objectStoreNames).join(', '));
var tx = db.transaction('kv', 'readwrite');
tx.objectStore('kv').put('idb_test_value', 'idb_test_key');
tx.oncomplete = function () {
add('PUT: ok (key=idb_test_key, value=idb_test_value)');
var tx2 = db.transaction('kv', 'readonly');
var r = tx2.objectStore('kv').get('idb_test_key');
r.onsuccess = function () {
add('GET: ' + r.result);
// count entries
var tx3 = db.transaction('kv', 'readonly');
var cr = tx3.objectStore('kv').count();
cr.onsuccess = function () {
add('COUNT: ' + cr.result);
add('INDEXEDDB: ALL PASS');
testCacheAPI();
};
cr.onerror = function () { add('COUNT: error'); testCacheAPI(); };
};
r.onerror = function () { add('GET: error'); testCacheAPI(); };
};
tx.onerror = function () { add('PUT: tx error'); testCacheAPI(); };
};
req.onerror = function (e) {
add('OPEN: error - ' + e.target.error);
testCacheAPI();
};
req.onblocked = function () {
add('OPEN: blocked');
testCacheAPI();
};
} catch (e) {
add('INDEXEDDB EXCEPTION: ' + e.message);
testCacheAPI();
}
// 5. Cache API (CacheStorage)
function testCacheAPI() {
add('');
add('--- Cache API (CacheStorage) ---');
add('available: ' + ('caches' in window));
if (!('caches' in window)) {
add('CACHE: not available');
finishAll();
return;
}
try {
caches.open('sb_test_cache').then(function (cache) {
add('OPEN: ok (cache name=sb_test_cache)');
var testUrl = 'local://test-entry';
var resp = new Response('cached body text', {
headers: { 'Content-Type': 'text/plain' }
});
cache.put(new Request(testUrl), resp).then(function () {
add('PUT: ok (url=' + testUrl + ')');
cache.match(new Request(testUrl)).then(function (matched) {
if (matched) {
matched.text().then(function (body) {
add('MATCH: ok, body="' + body + '"');
cache.keys().then(function (keys) {
add('KEYS: ' + keys.length + ' entries');
add('CACHE: ALL PASS');
finishAll();
}).catch(function (e) {
add('KEYS: error - ' + e.message);
finishAll();
});
}).catch(function (e) {
add('MATCH BODY: error - ' + e.message);
finishAll();
});
} else {
add('MATCH: no match found');
finishAll();
}
}).catch(function (e) {
add('MATCH: error - ' + e.message);
finishAll();
});
}).catch(function (e) {
add('PUT: error - ' + e.message);
finishAll();
});
}).catch(function (e) {
add('OPEN: error - ' + e.message);
finishAll();
});
} catch (e) {
add('CACHE EXCEPTION: ' + e.message);
finishAll();
}
}
function finishAll() {
add('');
add('=== Storage Test Complete ===');
flush();
}
})();
</script>
</body>
</html>