okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-web: the served web chat page for 0.2.75d9f47922

main
3.0 KiB68 linesraw
1"""serve/test_web_load_unload.py - the web page's header carries the Load / Unload control (#3550, by sakuumomo).
2
3Pins, as text: index.html holds the header button and the Monitor's "Unloaded" badge; app.js wires the button to POST /load
4and POST /unload, holds the POST's state until a later poll agrees, answers a 401 with the key copy, and refits the header
5row only when its text changed (the per-second poll does not force a layout). The behaviour itself, in a real browser, is in
6tools/webtest/browser_test.mjs ("load/unload: ...").
7
8    python -m unittest serve.test_web_load_unload -v
9"""
10from __future__ import annotations
11
12import shutil
13import subprocess
14import unittest
15from pathlib import Path
16
17WEB = Path(__file__).resolve().parent / "web"
18
19
20class WebLoadUnloadMarkup(unittest.TestCase):
21    @classmethod
22    def setUpClass(cls):
23        cls.html = (WEB / "index.html").read_text(encoding="utf-8")
24        cls.js = (WEB / "app.js").read_text(encoding="utf-8")
25        cls.css = (WEB / "app.css").read_text(encoding="utf-8")
26
27    def test_header_button_and_monitor_badge(self):
28        self.assertIn('id="unload-btn"', self.html)
29        self.assertIn('data-s="unloaded"', self.html)
30        self.assertLess(self.html.index('id="unload-btn"'), self.html.index('id="theme-btn"'))   # in the header row
31
32    def test_button_posts_load_and_unload(self):
33        self.assertIn('$("unload-btn").onclick', self.js)
34        self.assertIn('fetch(loadingNow ? "load" : "unload", {method: "POST", headers: headers(true), body: "{}"})', self.js)
35
36    def test_unload_asks_first_and_never_interrupts_a_turn(self):
37        self.assertIn("confirm(tr(\"model.confirmUnload\"", self.js)
38        self.assertIn("turnBlocksUnload(live)", self.js)
39        self.assertIn("function turnBlocksUnload(live)", self.js)
40
41    def test_401_gets_the_key_copy(self):
42        # The control POST answers a 401 the way poll and send do, not with a bare HTTP line.
43        self.assertIn('r.status === 401 ? tr("toast.keyAbout2"', self.js)
44
45    def test_poll_hold_until_a_poll_agrees(self):
46        self.assertIn("metricsHold = {state, after: metricsEpoch};", self.js)
47        self.assertIn("function metricsAgree(live, state)", self.js)
48
49    def test_header_refits_only_on_changed_text(self):
50        # The per-second poll re-sets the pill; the fit runs when its text actually changed.
51        self.assertIn("if (text !== lastPillText) { lastPillText = text; fitHeader(); }", self.js)
52
53    def test_the_header_row_keeps_the_buttons_on_a_phone(self):
54        self.assertIn("#unload-btn", self.css)
55        self.assertIn(".st-header--clip #pill-text", self.css)
56        for width in ("955px", "457px", "382px"):
57            self.assertIn(f"@media (max-width: {width})", self.css)
58
59
60@unittest.skipUnless(shutil.which("node"), "node is not installed")
61class WebLoadUnloadParses(unittest.TestCase):
62    def test_app_js_parses(self):
63        r = subprocess.run(["node", "--check", str(WEB / "app.js")], capture_output=True, text=True)
64        self.assertEqual(r.returncode, 0, r.stderr)
65
66
67if __name__ == "__main__":
68    unittest.main()