"""Saved trails and the display switches in the header: what the generator writes. The header carries the units switch and the theme menu on every page (and the footer no longer does); every row of every list of trails, and every trail page, has a Save button; /saved/ holds every row for app.js to show, is not indexed and is not in the sitemap; nothing is inline. The behaviour itself (localStorage, the link, the menu) is in app.js and was checked in browsers. """ import html import os import re import sys import unittest from html.parser import HTMLParser WEB = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ROOT = os.path.dirname(WEB) sys.path.insert(0, WEB) from olt import common, home, layout, records, saved, seo, site # noqa: E402 import builtsite # noqa: E402 OUT = builtsite.OUT def setUpModule(): builtsite.ensure() def read_out(relpath): with open(os.path.join(OUT, *relpath.split("/")), encoding="utf-8") as f: return f.read() def html_files(): for base, _, names in os.walk(OUT): for n in names: if n.endswith(".html"): yield os.path.relpath(os.path.join(base, n), OUT).replace(os.sep, "/") class Tree(HTMLParser): """Start tags with their attributes and the open elements above each one, and the text inside each ", m.start())] visible = re.search(r'', button).group(1) name = html.unescape(re.sub(r"<[^>]+>", "", button[button.index(''):])) self.assertEqual((visible, name), ("Save", f"Save {rec['name']}"), rec["id"]) def test_the_star_follows_the_name_and_is_not_inside_its_link(self): rec = self.records[0] row = home.entry(rec) name_link_end = row.index("", row.index('class="entry__name"')) self.assertLess(name_link_end, row.index('class="save save--row')) self.assertLess(row.index('class="save save--row'), row.index('class="entry__where"')) def test_names_are_escaped(self): rec = dict(self.records[0], id="x", name='A & B ') button = common.save_button(rec) self.assertIn("Save A & B <i>", button) self.assertNotIn("", button) class SavedPageTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.records = records.load_records(ROOT) cls.doc = read_out("saved/index.html") def test_holds_every_trail_hidden_for_the_script_to_show(self): rows = re.findall(r'
  • ]*)>', self.doc) self.assertEqual(len(rows), len(self.records)) self.assertTrue(all(r.endswith(" hidden") for r in rows)) self.assertIn('
      ', self.doc) self.assertIn("data-saved-total", self.doc) self.assertIn("data-saved-page", self.doc) def test_opens_in_the_order_saved(self): self.assertIn('data-default-sort="saved"', self.doc) self.assertIn('', self.doc) # Only this page offers that order. self.assertNotIn('value="saved"', read_out("index.html")) def test_its_map_names_what_it_can_draw(self): fig = re.search(r'
      ]*>', self.doc).group(0) self.assertNotIn("data-ids=", fig) drawable = re.search(r'data-drawable="([^"]*)"', fig).group(1).split() self.assertEqual(drawable, home.drawable_ids(sorted(self.records, key=home.sort_name))) caption = self.doc[self.doc.index('
      '):] self.assertNotRegex(caption[:caption.index("
      ")], r"\d+ tracks") def test_link_controls_and_states_are_written(self): for hook in ("data-saved-share", "data-saved-url", 'data-copy="#saved-url"', 'data-copy-done="Link copied."', "data-saved-from", "data-saved-merge", "data-saved-dropped", "data-saved-empty", "data-saved-none"): self.assertIn(hook, self.doc, hook) self.assertIn(">Copy link", self.doc) self.assertIn("Save these", self.doc) self.assertIn("Bookmark it, or send it to another device or another person", self.doc) self.assertIn('Find a trail', self.doc) def test_says_it_needs_javascript(self): self.assertIn("Saved trails need JavaScript, which is off or blocked in this browser. " 'See every trail.', self.doc) def test_not_indexed_and_not_in_the_sitemap(self): head = self.doc[:self.doc.index("")] self.assertIn('', head) self.assertNotIn('rel="canonical"', head) self.assertNotIn('property="og:url"', head) self.assertNotIn("/saved/", read_out("sitemap.xml")) self.assertNotIn(saved.PATH, seo.all_paths(self.records, [])) # Every other page keeps its canonical link and is indexable. other = read_out("about/index.html") self.assertIn('rel="canonical"', other) self.assertNotIn('name="robots"', other) def test_noindex_page_without_base_url(self): page = layout.layout("T", "", path="/saved/", noindex=True, site=site.Site("N", "T")) self.assertIn('', page) self.assertNotIn("canonical", page) def test_the_words_a_visitor_reads(self): text = re.sub(r"<[^>]+>", " ", self.doc[self.doc.index('
      '):self.doc.index('