"""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 shared layout of every page that shows a set of trails, the rows that take the place of tables, 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) self.assertNotIn("chips", row) # one quiet line of links, not pills # 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'
  • (.*?)'):] self.assertLess(grid.index('class="browse__map" id="map"'), grid.index('class="browse__list"')) self.assertLess(grid.index('class="browse__list"'), grid.index('id="finder-form"')) self.assertLess(grid.index('id="finder-form"'), grid.index('id="entries"')) self.assertNotIn("filters__maplink", self.body) def test_the_open_filters_lie_over_the_rows_and_can_be_closed(self): css = read(WEB, "assets", "style.css") self.assertRegex(css, r"html\.js \.filters__panels \{[^}]*position: absolute;") self.assertIn('class="button filters__done"', self.body) js = read(WEB, "assets", "app.js") self.assertIn("e.key === 'Escape'", js) class ListEntriesCheckTest(unittest.TestCase): def check(self, entries, keys=("a", "b", "c")): import check_datafiles errors = [] check_datafiles.check_list_entries(entries, set(keys), errors) return errors def test_the_real_entries_pass(self): import check_datafiles errors = [] import tomllib with open(os.path.join(ROOT, "collections.toml"), "rb") as f: doc = tomllib.load(f) check_datafiles.check_list_entries(doc["list"], check_datafiles.built_in_list_keys(), errors) self.assertEqual(errors, []) def test_unknown_keys_repeats_and_bad_positions_are_reported(self): self.assertEqual(self.check([{"key": "a", "home": 1}, {"key": "b", "home": 2}]), []) self.assertTrue(any("not the key of a list" in e for e in self.check([{"key": "zzz", "home": 1}]))) self.assertTrue(any("appears twice" in e for e in self.check([{"key": "a", "home": 1}, {"key": "a", "home": 2}]))) self.assertTrue(any("also the place of" in e for e in self.check([{"key": "a", "home": 1}, {"key": "b", "home": 1}]))) self.assertTrue(any("whole number" in e for e in self.check([{"key": "a", "home": 0}]))) self.assertTrue(any("whole number" in e for e in self.check([{"key": "a", "home": "1"}]))) self.assertTrue(any("unknown field" in e for e in self.check([{"key": "a", "shown": 1}]))) class HeaderTest(unittest.TestCase): def nav(self, path): html = str(layout.header(path, SITE)) return {m.group(2): m.group(1) for m in re.finditer(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 NoTablesTest(unittest.TestCase): """The site sets no tables. A set of trails is the site's trail rows; a set of other things is rows in the same language (pagekit.rows_list); a table written in Markdown is set the same way.""" def test_a_set_of_things_is_rows_with_a_name_a_sentence_and_a_line_of_facts(self): t = str(pagekit.table(["File", "What it holds", "Size"], [["a.json", "Every trail with all its fields, written out for the programs that read it", "3 MB"]], caption="c")) self.assertNotIn("', t) self.assertLess(t.index('class="entry__name">a.json'), t.index('class="entry__desc">Every trail')) self.assertIn('', t) def test_a_table_written_in_markdown_or_by_hand_is_set_as_rows(self): html = ('

    x

    ' '
    TierRecords
    Measured12
    Stated3

    y

    ') out = pagekit.untable(html) self.assertNotIn("(\w+)<', out), ["Measured", "Stated"]) self.assertTrue(out.startswith("

    x

    ") and out.endswith("

    y

    ")) def test_the_triple_crown_is_three_trail_rows_with_their_map_and_the_compared_figures_in_each_row(self): from olt import standing recs = records.load_records(ROOT) c = datafiles.collections(ROOT)[0] title, desc, body, members = standing.collection_page(c, recs, SITE) body = str(body) self.assertNotIn("High point", body) self.assertIn("
    Designated
    ", body) def test_a_ranking_is_the_trail_rows_in_rank_order_led_by_the_rank_and_the_figure(self): from olt import standing recs = records.load_records(ROOT) title, desc, body, have = standing.ranking_page(standing.RANKINGS[0], recs, SITE) body = str(body) self.assertNotIn("No. 1', first) self.assertIn('