"""The first screen: what the top of the trail page and the home page hold, the home page's lists (read from data), the header's Lists item, the tables that stack on a phone, and the maps' static drawings (olt/sketch.py) with the script's handover from them. Pages are rendered one at a time; the site is not built. The sketch reads the track files of a site directory, so those are written to a temporary one here. The behaviour of the script in a browser was checked by driving the pages (see the changelog); these pin what its source promises. """ import json import os import re import sys import tempfile import unittest WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT = os.path.dirname(WEB) sys.path.insert(0, WEB) sys.path.insert(0, os.path.join(ROOT, "tools")) from olt import browse, common, datafiles, home, layout, pagekit, records, site, sketch, trail # noqa: E402 SITE = site.load() def read(*parts): with open(os.path.join(*parts), encoding="utf-8") as f: return f.read() def load(tid): return records.load_record(ROOT, tid) class HomePageTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.recs = records.load_records(ROOT) cls.body = home.render(cls.recs, SITE, ROOT) def test_the_hero_no_longer_repeats_the_site_mark_or_the_strip_of_words(self): hero = self.body[:self.body.index('class="container home"')] self.assertNotIn("lockup", hero) self.assertNotIn("hero__tags", hero) self.assertNotIn("(.*?) ', row) self.assertEqual(titles, [browse.by_key(k).title for k in keys]) self.assertTrue(row.rstrip().endswith("")) self.assertIn('All lists', row) self.assertNotIn("Hiking in", row) # a different file gives a different row: the set is not a constant of the generator other = home.home_picks([{"key": "loops", "home": 2}, {"key": "routes", "home": 1}, {"key": "canada"}]) self.assertEqual(other, ["routes", "loops"]) self.assertEqual(re.findall(r'
  • (.*?) ', html)}, html def current(self, path): html = str(layout.header(path, SITE)) return re.findall(r']*>]*>(?:)?([A-Za-z]+)", html) self.assertEqual(names[:5], ["Trails", "Places", "Lists", "About", "Saved"]) self.assertIn('Lists', html) def test_lists_is_current_on_the_index_lists_tables_and_views(self): for path in ("/lists/", "/lists/loops/", "/routes/", "/numbers/", "/numbers/highest-points/", "/authors/", "/authors/andrew-skurka/", "/recent/", "/connected/"): self.assertEqual(self.current(path), ["/lists/"], path) layout.UNDER_LISTS.add("/triple-crown/") try: self.assertEqual(self.current("/triple-crown/"), ["/lists/"]) finally: layout.UNDER_LISTS.discard("/triple-crown/") def test_the_other_items_keep_their_pages(self): self.assertEqual(self.current("/"), ["/"]) self.assertEqual(self.current("/trails/tonto-trail/"), ["/"]) self.assertEqual(self.current("/regions/us-co/"), ["/regions/"]) self.assertEqual(self.current("/regions/us-co/loops/"), ["/regions/"]) self.assertEqual(self.current("/about/"), ["/about/"]) self.assertEqual(self.current("/sources/"), ["/about/"]) self.assertEqual(self.current("/glossary/"), ["/about/"]) def test_the_triple_crown_page_is_listed_under_lists_by_the_page_builder(self): self.assertIn("layout.UNDER_LISTS.add(path)", read(WEB, "olt", "pages.py")) class TablesTest(unittest.TestCase): def test_every_cell_carries_its_label_and_a_wide_table_is_set_by_row(self): t = str(pagekit.table(["Trail", "Length", "States", "Time"], [["A", "1", "X", "2"]], caption="c")) self.assertIn('', t) self.assertEqual(re.findall(r'data-label="([^"]+)"', t), ["Trail", "Length", "States", "Time"]) small = str(pagekit.table(["A", "B", "C"], [["1", "2", "3"]])) self.assertNotIn("data-stack", small) def test_a_compare_table_labels_each_cell_with_its_row_and_stacks_by_column(self): t = str(pagekit.table(["Figure", "AT", "PCT"], [["Length", "2,198", "2,650"], ["States", "14", "3"]], row_heads=True, compare=True)) self.assertIn('
    ', t) self.assertEqual(re.findall(r'
    ', src) self.assertEqual(src.count("data-label="), 6) class SketchTest(unittest.TestCase): def setUp(self): self.dir = tempfile.TemporaryDirectory() os.makedirs(os.path.join(self.dir.name, "trails", "x")) coords = [[-112.0 + i * 0.001, 36.0 + (i % 7) * 0.0004 + i * 0.0005] for i in range(400)] fc = {"type": "FeatureCollection", "features": [ {"type": "Feature", "properties": {"role": "main"}, "geometry": {"type": "MultiLineString", "coordinates": [coords]}}]} with open(os.path.join(self.dir.name, "trails", "x", "map.json"), "w") as f: json.dump(fc, f) sketch.use(self.dir.name) self.box = [-112.0, 36.0, -111.6, 36.2] def tearDown(self): sketch.use("") self.dir.cleanup() def test_the_drawing_is_light_and_in_the_site_s_track_classes(self): fr = sketch.frame(self.box) svg = str(sketch.svg(sketch.trail_features("x"), fr)) self.assertLess(len(svg), 4000) self.assertIn('class="map__sketch"', svg) self.assertIn('class="fallback__casing"', svg) self.assertIn('class="fallback__line"', svg) self.assertIn('preserveAspectRatio="xMidYMid meet"', svg) self.assertNotRegex(svg, r"\d\.\d{3,}") # whole numbers, relative steps self.assertRegex(svg, r'd="M\d+ \d+l') def test_the_frame_is_the_extent_widened_on_every_side(self): w, s, e, n = sketch.frame(self.box) self.assertLess(w, self.box[0]) self.assertLess(s, self.box[1]) self.assertGreater(e, self.box[2]) self.assertGreater(n, self.box[3]) def test_the_viewbox_has_the_frames_proportions_so_both_fit_a_container_the_same_way(self): import math fr = sketch.frame(self.box) svg = str(sketch.svg(sketch.trail_features("x"), fr)) vw, vh = map(float, re.search(r'viewBox="0 0 ([\d.]+) ([\d.]+)"', svg).groups()) merc = lambda lat: math.log(math.tan(math.pi / 4 + math.radians(lat) / 2)) want = (math.radians(fr[2] - fr[0])) / (merc(fr[3]) - merc(fr[1])) self.assertAlmostEqual(vw / vh, want, places=3) def test_a_trail_page_carries_the_frame_and_the_drawing(self): rec = load("tonto-trail") rec["id"] = "x" rec["derived"]["bbox"] = self.box rec["derived"]["files"] = {"line": "x.gpx"} fig = trail.map_figure(rec) self.assertRegex(fig, r'data-frame="-?[\d.]+,-?[\d.]+,-?[\d.]+,-?[\d.]+"') self.assertIn('
    ', trail.map_figure(rec)) class HandoverTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.js = read(WEB, "assets", "app.js") cls.css = read(WEB, "assets", "style.css") def test_the_live_map_opens_on_the_drawings_ground_with_no_padding(self): self.assertIn("var frame = (fig.dataset.frame || '').split(',').map(Number);", self.js) self.assertIn("options.bounds = [[frame[0], frame[1]], [frame[2], frame[3]]];", self.js) self.assertIn("options.fitBoundsOptions = { padding: 0 };", self.js) def test_nothing_is_drawn_over_the_page_s_own_drawing_and_no_placeholder_text_is_written(self): self.assertIn("if (hasSketch()) { sketchCorners(); return; }", self.js) self.assertNotIn("The map loads when you scroll to it.", self.js) self.assertNotIn("Loading the track", self.js) self.assertNotIn("Loading the base map", self.js) self.assertIn("html.js .map__status:not([data-say]) { display: none; }", self.css) def test_the_drawing_gives_way_after_the_first_frame_with_the_tracks_and_a_fade(self): self.assertIn("map.once('render', reveal);", self.js) self.assertIn("map.isSourceLoaded('olt')", self.js) self.assertIn(".map[data-state=\"ready\"] .map__fallback { visibility: hidden; transition: visibility 0s 0.4s; }", self.css) self.assertIn(".map__canvas { opacity: 0; transition: opacity 0.4s ease; }", self.css) def test_every_frame_starts_when_the_page_is_idle_whatever_its_place(self): self.assertIn("requestIdleCallback(begin, { timeout: 500 })", self.js) self.assertNotIn("IntersectionObserver", self.js) self.assertNotIn("MAP_AHEAD", self.js) def test_the_first_view_of_an_overview_needs_no_move(self): self.assertIn("if (entry.frame && same) { target = entry.frame; padding = 0; }", self.js) class PageTopsTest(unittest.TestCase): def test_a_place_or_list_page_puts_its_list_before_the_text_and_the_map_in_the_narrow_order(self): grid = home.place_grid("TEXT", "LIST", "MAP") self.assertLess(grid.index("TEXT"), grid.index("LIST")) # the text is first in the page css = read(WEB, "assets", "style.css") self.assertIn('grid-template-areas: "list" "map" "text";', css) self.assertIn('grid-template-areas: "list text" "list map";', css) def test_every_page_head_is_compact(self): css = read(WEB, "assets", "style.css") self.assertRegex(css, r"\.page-head \.titleblock h1 \{ font-size: var\(--step-3\)") if __name__ == "__main__": unittest.main()