okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1"""serve/test_web_follow.py - the web chat follows the answer only while the reader has not left (#4014). 2 3The page used to pull the view back to the bottom on every frame when it was within 120 px of it, so one wheel notch 4(about 100 px) could not scroll up while an answer streamed. Pins, with node, the page's own rule and that the wheel, 5touch and keyboard listeners are wired. Skipped without node. 6 7python -m unittest serve.test_web_follow -v 8""" 9from __future__import annotations 10 11import json 12import shutil 13import subprocess 14import unittest 15from pathlib import Path 16 17APP = Path (__file__ ).resolve ().parent / "web" / "app.js" 18 19 20def between (src :str ,start :str ,end :str )-> str : 21a = src .index (start ) 22return src [a :src .index (end ,a )] 23 24 25@ unittest . skipUnless ( shutil . which ( "node" ), "node is not installed" ) 26class WebFollow (unittest .TestCase ): 27@classmethod 28def setUpClass (cls ): 29cls .src = APP .read_text (encoding = "utf-8" ) 30cls .rule = between (cls .src ,"const FOLLOW_SLACK" ,"function scrollDown" ) 31 32def follow (self ,distance ,was ,ms ): 33js = self .rule + f"\nconsole.log(JSON.stringify(followAfterScroll( { distance } , { json . dumps ( was ) } , { ms } )));" 34out = subprocess .run (["node" ,"-e" ,js ],capture_output = True ,text = True ,timeout = 30 ) 35self .assertEqual (out .returncode ,0 ,out .stderr ) 36return json .loads (out .stdout ) 37 38def test_a_wheel_notch_inside_the_old_band_is_not_pulled_back (self ): 39self .assertFalse (self .follow (100 ,True ,20 )) 40self .assertFalse (self .follow (40 ,True ,20 )) 41 42def test_back_at_the_bottom_it_follows_again (self ): 43self .assertTrue (self .follow (0 ,False ,10_000 )) 44self .assertTrue (self .follow (24 ,False ,10_000 )) 45self .assertFalse (self .follow (25 ,False ,10_000 )) 46 47def test_the_page_growing_under_a_following_reader_keeps_following (self ): 48self .assertTrue (self .follow (80 ,True ,5_000 )) 49self .assertFalse (self .follow (80 ,False ,5_000 )) 50self .assertFalse (self .follow (401 ,True ,5_000 )) 51 52def test_the_old_120_px_rule_is_gone_and_the_reader_input_is_wired (self ): 53self .assertNotIn ("< 120" ,between (self .src ,"function renderChat" ,"$(\"chat\").addEventListener(\"click\"" )) 54watch = between (self .src ,"(function watchReader()" ,"})();" ) 55for event in ("wheel" ,"touchmove" ,"pointerdown" ,"keydown" ,"scroll" ): 56self .assertIn (f'" { event } "' ,watch ) 57# a wheel up on a chat that cannot scroll must not switch following off 58self .assertIn ("e.deltaY < 0 && s.scrollTop > 0" ,watch ) 59 60 61if __name__ == "__main__" : 62unittest .main ()