okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5d9f47922
main
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 8python -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 22def setUpClass (cls ): 23cls .html = (WEB / "index.html" ).read_text (encoding = "utf-8" ) 24cls .js = (WEB / "app.js" ).read_text (encoding = "utf-8" ) 25cls .css = (WEB / "app.css" ).read_text (encoding = "utf-8" ) 26 27def test_header_button_and_monitor_badge (self ): 28self .assertIn ('id="unload-btn"' ,self .html ) 29self .assertIn ('data-s="unloaded"' ,self .html ) 30self .assertLess (self .html .index ('id="unload-btn"' ),self .html .index ('id="theme-btn"' ))# in the header row 31 32def test_button_posts_load_and_unload (self ): 33self .assertIn ('$("unload-btn").onclick' ,self .js ) 34self .assertIn ('fetch(loadingNow ? "load" : "unload", {method: "POST", headers: headers(true), body: "{}"})' ,self .js ) 35 36def test_unload_asks_first_and_never_interrupts_a_turn (self ): 37self .assertIn ("confirm(tr(\"model.confirmUnload\"" ,self .js ) 38self .assertIn ("turnBlocksUnload(live)" ,self .js ) 39self .assertIn ("function turnBlocksUnload(live)" ,self .js ) 40 41def 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. 43self .assertIn ('r.status === 401 ? tr("toast.keyAbout2"' ,self .js ) 44 45def test_poll_hold_until_a_poll_agrees (self ): 46self .assertIn ("metricsHold = {state, after: metricsEpoch};" ,self .js ) 47self .assertIn ("function metricsAgree(live, state)" ,self .js ) 48 49def 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. 51self .assertIn ("if (text !== lastPillText) { lastPillText = text; fitHeader(); }" ,self .js ) 52 53def test_the_header_row_keeps_the_buttons_on_a_phone (self ): 54self .assertIn ("#unload-btn" ,self .css ) 55self .assertIn (".st-header--clip #pill-text" ,self .css ) 56for width in ("955px" ,"457px" ,"382px" ): 57self .assertIn (f"@media (max-width: { width } )" ,self .css ) 58 59 60@ unittest . skipUnless ( shutil . which ( "node" ), "node is not installed" ) 61class WebLoadUnloadParses (unittest .TestCase ): 62def test_app_js_parses (self ): 63r = subprocess .run (["node" ,"--check" ,str (WEB / "app.js" )],capture_output = True ,text = True ) 64self .assertEqual (r .returncode ,0 ,r .stderr ) 65 66 67if __name__ == "__main__" : 68unittest .main ()