Files
leleka/engine/strata/serve/test_web_api_key.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

100 lines
4.0 KiB
Python

"""The web app uses an edited API key immediately, even when site storage is blocked.
Runs the page's storage and server-access code with Node, without a GPU or server:
python -m unittest serve.test_web_api_key -v
"""
import json
from pathlib import Path
import shutil
import subprocess
import unittest
APP = Path(__file__).with_name("web") / "app.js"
@unittest.skipUnless(shutil.which("node"), "node is not installed")
class WebApiKey(unittest.TestCase):
def run_js(self, actions, *, stored=None, blocked=False, quota=False):
src = APP.read_text(encoding="utf-8")
store = src[src.index("const store ="):src.index("// ------------------------------------------------------------------ toasts")]
access = src[src.index("// ------------------------------------------------------------------ server access"):src.index("let health =")]
js = f"""
const data = new Map(Object.entries({json.dumps(stored or {})}));
const field = {{value: ""}};
const notices = [];
const $ = () => field;
const toast = (...args) => notices.push(args);
const localStorage = {{
getItem(k) {{
if ({json.dumps(blocked)}) throw new Error("Storage blocked");
return data.has(k) ? data.get(k) : null;
}},
setItem(k, v) {{
if ({json.dumps(blocked or quota)}) throw new Error("Storage unavailable");
data.set(k, v);
}},
}};
function edit(value, event = "input") {{
field.value = value;
const handler = field["on" + event];
if (handler) handler();
}}
{store}
{access}
{actions}
"""
result = subprocess.run(["node", "-e", js], capture_output=True, text=True, timeout=30)
self.assertEqual(result.returncode, 0, result.stderr)
return json.loads(result.stdout)
def test_input_is_used_and_saved_without_blur(self):
got = self.run_js('''
edit(" test-key ");
console.log(JSON.stringify({headers: headers(true), saved: data.get("strata.apikey")}));
''')
self.assertEqual(got, {"headers": {"Authorization": "Bearer test-key", "Content-Type": "application/json"},
"saved": '"test-key"'})
def test_saved_key_is_restored_on_reload(self):
got = self.run_js('console.log(JSON.stringify({value: field.value, headers: headers()}));',
stored={"strata.apikey": '"saved-key"'})
self.assertEqual(got, {"value": "saved-key", "headers": {"Authorization": "Bearer saved-key"}})
def test_blocked_storage_still_uses_key_in_memory(self):
got = self.run_js('edit("test-key", "change"); console.log(JSON.stringify(headers()));', blocked=True)
self.assertEqual(got, {"Authorization": "Bearer test-key"})
def test_failed_write_does_not_reuse_old_key(self):
got = self.run_js('edit("new-key", "change"); console.log(JSON.stringify(headers()));',
stored={"strata.apikey": '"old-key"'}, quota=True)
self.assertEqual(got, {"Authorization": "Bearer new-key"})
def test_clearing_key_removes_authorization(self):
for quota in (False, True):
with self.subTest(quota=quota):
got = self.run_js('edit(" "); console.log(JSON.stringify(headers()));',
stored={"strata.apikey": '"old-key"'}, quota=quota)
self.assertEqual(got, {})
def test_successful_save_notifies_only_on_change(self):
got = self.run_js('''
edit("test-key");
const before = notices.length;
edit("test-key", "change");
console.log(JSON.stringify({before, kinds: notices.map(n => n[0])}));
''')
self.assertEqual(got, {"before": 0, "kinds": ["success"]})
def test_failed_save_warns_instead_of_claiming_success(self):
got = self.run_js('edit("test-key", "change"); console.log(JSON.stringify(notices.map(n => n[0])));',
blocked=True)
self.assertEqual(got, ["warn"])
def test_server_without_key_keeps_json_header_only(self):
got = self.run_js('console.log(JSON.stringify(headers(true)));')
self.assertEqual(got, {"Content-Type": "application/json"})
if __name__ == "__main__":
unittest.main()