"""Small site-wide improvements that change nothing on screen: map click tolerance, cross-origin data files, description endings, guessable-address redirects, the print rules, the extra skip links and the list-size logo thumbnails.""" import os import re import sys import unittest WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT = os.path.dirname(WEB) sys.path.insert(0, WEB) import serve # noqa: E402 from olt import common, records, redirects, thumbs # noqa: E402 import builtsite # noqa: E402 OUT = builtsite.OUT def setUpModule(): builtsite.ensure() def read(relpath): with open(os.path.join(OUT, *relpath.split("/")), encoding="utf-8") as f: return f.read() def asset(name): with open(os.path.join(WEB, "assets", name), encoding="utf-8") as f: return f.read() class MapClickToleranceTest(unittest.TestCase): """The click handler itself needs a browser; these check the logic it is built on is in place.""" @classmethod def setUpClass(cls): js = asset("app.js") cls.src = js[js.index("function wireOverview"):js.index("function fitToVisible")] def test_clicks_and_hovers_look_in_a_box_round_the_pointer_not_at_one_pixel(self): self.assertIn("queryRenderedFeatures([[x - pad, y - pad], [x + pad, y + pad]]", self.src) self.assertNotIn("map.on('click', def.id", self.src) self.assertNotIn("map.on('mousemove', def.id", self.src) self.assertIn("map.on('click', function (e)", self.src) def test_a_finger_gets_a_wider_box_than_a_mouse(self): match = re.search(r"var pad = coarse \? (\d+) : (\d+);", self.src) self.assertTrue(match) self.assertGreater(int(match.group(1)), int(match.group(2))) self.assertGreaterEqual(int(match.group(2)), 4) def test_the_nearest_feature_wins_and_a_miss_does_nothing(self): self.assertIn("d < bestD", self.src) self.assertIn("bestD <= pad * pad", self.src) self.assertIn("if (!hit) return;", self.src) def test_the_tracks_are_drawn_as_before(self): js = asset("app.js") self.assertIn("{ id: 'olt-line', color: '--trail', width: [3, 1.6, 9, 4]", js) self.assertNotIn("olt-hit", js) # no extra hit layer: nothing new is drawn class CrossOriginDataTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.rules = serve.parse_headers(os.path.join(OUT, "_headers")) def headers(self, path): out = {} for pattern, headers in self.rules: if pattern.match(path): for name, value in headers: self.assertNotIn(name.lower(), out, f"{path}: {name} set by two rules") out[name.lower()] = value return out def test_the_public_data_files_allow_any_site_to_fetch_them(self): for path in ("/data/trails.json", "/data/overview.geojson", "/data/index.geojson", "/data/trails.csv", "/data/all-gpx.zip", "/trails/appalachian/appalachian.gpx", "/trails/appalachian/appalachian.geojson"): self.assertEqual(self.headers(path).get("access-control-allow-origin"), "*", path) def test_no_page_and_no_other_file_carries_it(self): for path in ("/", "/index.html", "/data/", "/data/schema/", "/data/changelog/", "/trails/appalachian/", "/trails/appalachian/index.html", "/404.html", "/feed.xml", "/llms.txt", "/trails/appalachian/trail.toml", "/trails/appalachian/release.txt", "/assets/og.png"): self.assertNotIn("access-control-allow-origin", self.headers(path), path) def test_every_built_data_file_and_trail_download_is_covered(self): for base, _, names in os.walk(os.path.join(OUT, "data")): for n in names: if n.endswith((".json", ".geojson", ".csv", ".zip")): path = "/" + os.path.relpath(os.path.join(base, n), OUT).replace(os.sep, "/") self.assertEqual(self.headers(path).get("access-control-allow-origin"), "*", path) def test_the_security_headers_are_still_on_everything(self): for path in ("/", "/trails/appalachian/", "/data/trails.json", "/trails/appalachian/appalachian.gpx"): h = self.headers(path) self.assertEqual(h.get("x-content-type-options"), "nosniff", path) self.assertEqual(h.get("referrer-policy"), "strict-origin-when-cross-origin", path) self.assertIn("frame-ancestors 'none'", h.get("content-security-policy", ""), path) class DescriptionEndingTest(unittest.TestCase): def test_cut_clean_ends_at_a_word_with_a_full_stop_and_no_ellipsis(self): text = "The walk goes " + "on and on and on " * 20 + "to the end." cut = common.cut_clean(text, 100) self.assertLessEqual(len(cut), 100) self.assertTrue(cut.endswith(".")) self.assertNotIn("…", cut) self.assertEqual(common.cut_clean("Short.", 100), "Short.") def test_cut_clean_never_ends_on_a_joining_word(self): cut = common.cut_clean("A long trail through the mountains and valleys of the northern part of the state", 52) self.assertNotRegex(cut, r"\b(and|of|the|through)\.$") def test_the_share_description_is_a_whole_sentence_where_one_fits(self): text = ("A trail of 70 miles (113 km) in Maine, usually 5 days. " "It follows the ridge and crosses several summits before it drops to the lake shore and the road.") self.assertEqual(common.share_description(text), "A trail of 70 miles (113 km) in Maine, usually 5 days.") def test_the_share_description_from_one_long_sentence_ends_at_a_word(self): got = common.share_description("A trail of " + "many long miles and more " * 12 + "end.") self.assertLessEqual(len(got), common.SHARE_DESCRIPTION_LIMIT) self.assertTrue(got.endswith(".")) self.assertNotIn("…", got) def test_every_built_trail_page_describes_itself_in_whole_sentences(self): trails = os.path.join(OUT, "trails") pages = 0 for tid in sorted(os.listdir(trails)): path = os.path.join(trails, tid, "index.html") if not os.path.isfile(path): continue doc = read(f"trails/{tid}/index.html") desc = re.search(r'Skip to content', doc) self.assertIn('', doc) self.assertIn('', doc) for target in ("browse-list", "entries"): self.assertEqual(doc.count(f'id="{target}"'), 1, target) def test_a_page_without_those_targets_gets_only_the_one_skip_link(self): doc = read("trails/appalachian/index.html") self.assertEqual(doc.count('class="skip-link"'), 1) def test_every_skip_link_has_a_target_on_its_page(self): for page in ("index.html", "regions/index.html", "trails/appalachian/index.html", "lists/index.html"): doc = read(page) for target in re.findall(r'class="skip-link" href="#([^"]+)"', doc): self.assertEqual(doc.count(f'id="{target}"'), 1, f"{page}: #{target}") class ListThumbnailTest(unittest.TestCase): def test_a_list_thumbnail_is_at_most_twice_the_largest_box_a_list_draws(self): for name in os.listdir(thumbs.LIST_THUMB_DIR): w, h = thumbs.png_size(os.path.join(thumbs.LIST_THUMB_DIR, name)) self.assertLessEqual(w, 200, name) self.assertLessEqual(h, 124, name) self.assertTrue(os.path.isfile(os.path.join(thumbs.THUMB_DIR, name)), f"{name}: no full thumbnail") def test_the_list_shows_the_small_copy_with_its_own_size_and_the_header_the_larger(self): home_doc = read("index.html") m = re.search(r'