okami.mom/strata

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

Niko1221Merge f-027-gh-app: GitHub app and Terminal items for 0.2.73cabb9e3f

main
2.6 KiB62 linesraw
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
7    python -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:
21    a = src.index(start)
22    return src[a:src.index(end, a)]
23
24
25@unittest.skipUnless(shutil.which("node"), "node is not installed")
26class WebFollow(unittest.TestCase):
27    @classmethod
28    def setUpClass(cls):
29        cls.src = APP.read_text(encoding="utf-8")
30        cls.rule = between(cls.src, "const FOLLOW_SLACK", "function scrollDown")
31
32    def follow(self, distance, was, ms):
33        js = self.rule + f"\nconsole.log(JSON.stringify(followAfterScroll({distance}, {json.dumps(was)}, {ms})));"
34        out = subprocess.run(["node", "-e", js], capture_output=True, text=True, timeout=30)
35        self.assertEqual(out.returncode, 0, out.stderr)
36        return json.loads(out.stdout)
37
38    def test_a_wheel_notch_inside_the_old_band_is_not_pulled_back(self):
39        self.assertFalse(self.follow(100, True, 20))
40        self.assertFalse(self.follow(40, True, 20))
41
42    def test_back_at_the_bottom_it_follows_again(self):
43        self.assertTrue(self.follow(0, False, 10_000))
44        self.assertTrue(self.follow(24, False, 10_000))
45        self.assertFalse(self.follow(25, False, 10_000))
46
47    def test_the_page_growing_under_a_following_reader_keeps_following(self):
48        self.assertTrue(self.follow(80, True, 5_000))
49        self.assertFalse(self.follow(80, False, 5_000))
50        self.assertFalse(self.follow(401, True, 5_000))
51
52    def test_the_old_120_px_rule_is_gone_and_the_reader_input_is_wired(self):
53        self.assertNotIn("< 120", between(self.src, "function renderChat", "$(\"chat\").addEventListener(\"click\""))
54        watch = between(self.src, "(function watchReader()", "})();")
55        for event in ("wheel", "touchmove", "pointerdown", "keydown", "scroll"):
56            self.assertIn(f'"{event}"', watch)
57        # a wheel up on a chat that cannot scroll must not switch following off
58        self.assertIn("e.deltaY < 0 && s.scrollTop > 0", watch)
59
60
61if __name__ == "__main__":
62    unittest.main()