"""Tests for the computed elevation facts (olt/elevation.py): the At a glance rows, the profile and its part breaks, the caveats, unit switching, the climb sort, the model credit, and the Routes list dropping its repetition. These render fixtures; they do not need the built site. """ import copy import html 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) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from olt import docs, elevation, home, records, regions, site, trail # noqa: E402 from test_pages import sample_record # noqa: E402 GH = site.Site("N", "T", repo_url="https://github.com/o/r") SOURCE = { "model": "Example Model", "url": "https://model.example/", "attribution": "Example Model credit, as it gives it.", "zoom": 12, "ground_m_per_pixel_at_40n": 29.2, "sample_spacing_m": 30.0, "smoothing": "Moving average over 90 m of track; ascent and descent counted with a 5 m reversal threshold", "limits": "Computed from a public elevation model, not measured on the ground.", "method_version": 1, "computed": "2026-11-13", } def elev(**kw): """A small elevation.json in memory: one part, full coverage, south to north.""" data = { "highest": {"m": 2000, "lat": 40.0, "lon": -105.0}, "lowest": {"m": 500, "lat": 39.0, "lon": -105.5}, "ascent_m": 3000, "descent_m": 2900, "track_km": 100.0, "published_km": 100, "coverage": 1.0, "parts": 1, "orientation": "south-to-north", "profile": [[0, 500], [50, 2000], [100, 800]], "source": copy.deepcopy(SOURCE), } data.update(kw) return data def rec_with(data=None, hike_table=None): rec = sample_record("X Trail") rec["derived"]["elevation"] = data rec["derived"]["has_line"] = data is not None if data is not None: rec["source"]["line_file"] = "line.geojson" rec["derived"]["files"] = {"line": "x/line.geojson"} if hike_table: rec["hike"] = hike_table return rec def render(rec): return trail.render(rec, GH)[2] def text(fragment): return re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " ", fragment))) def glance_of(body): return body[body.index('
", body.index('
'):svg.index('')] mi = svg[svg.index(''):] self.assertIn("Distance along the track (km)", km) self.assertIn("Elevation (m)", km) self.assertIn("2,000", km) # a metre tick reaches the high point self.assertIn("Distance along the track (mi)", mi) self.assertIn("Elevation (ft)", mi) self.assertNotIn("style=", svg) # CSS shows one system, by the same data-units preference as lengths with open(os.path.join(WEB, "assets", "style.css"), encoding="utf-8") as f: css = f.read() self.assertIn(".elev-profile .len__mi { display: none; }", css) self.assertIn(':root[data-units="mi"] .elev-profile .len__mi { display: inline; }', css) self.assertIn(':root[data-units="mi"] .elev-profile .len__km { display: none; }', css) def test_text_alternative_states_the_facts(self): svg = str(elevation.profile_svg(elev())) label = html.unescape(re.search(r'aria-label="([^"]*)"', svg).group(1)) for want in ("3,000 m (9,843 ft)", "100.0 km (62 mi)", "500 m (1,640 ft)", "2,000 m (6,562 ft)", "walking south to north"): self.assertIn(want, label, want) # the same sentence is the visible caption self.assertIn(label, text(render(rec_with(elev())))) def test_no_elevation_file_shows_nothing(self): body = render(rec_with(None)) for absent in ("Elevation profile", "Low point", "Climb", 'id="elevation-data-title"', 'id="elevation-title"', "detail--with-profile"): self.assertNotIn(absent, body, absent) self.assertIn("At a glance", body) # and the page is otherwise whole class AboutTest(unittest.TestCase): def test_method_credit_and_every_value(self): data = elev(parts=2, part_starts_km=[50], profile=ProfileTest.PROFILE) body = render(rec_with(data)) about = body[body.index('
Climb", row) self.assertIn("3,000 m", text(row)) self.assertIn("computed", text(row)) row_none = home.entry(rec_with(None)) self.assertIn('data-climb=""', row_none) self.assertNotIn("
Climb
", row_none) self.assertIn('', home.SORT_SELECT) self.assertIn('', home.SORT_SELECT) with open(os.path.join(WEB, "assets", "app.js"), encoding="utf-8") as f: js = f.read() self.assertIn("'climb-desc'", js) # a trail with no computed climb sorts last in both climb orders self.assertIn("ac === null ? 1 : -1", js) class RoutesListTest(unittest.TestCase): def _route(self): rec = rec_with(None) rec["derived"]["kind"] = "route" rec["kind"] = "route" rec["route"] = {"creator": "A Person", "year": 1970, "line_policy": "sold"} rec["guide"] = [] return rec def test_no_repetition_under_by_author_headings(self): rec = self._route() plain = home.entry(rec) self.assertIn("Route by A Person, 1970", text(plain)) self.assertIn('Route', plain) _, _, body, _ = regions.routes_page([rec], GH) self.assertIn("By A Person", text(body)) self.assertNotIn("entry__by", body) self.assertNotIn("tag--route", body) class LoaderTest(unittest.TestCase): def test_real_records(self): pct = records.load_record(ROOT, "pacific-crest") data = pct["derived"]["elevation"] self.assertIsNotNone(data) self.assertGreater(data["ascent_m"], 0) self.assertIn("Terrain Tiles", data["source"]["attribution"]) none_rec = records.load_record(ROOT, "sierra-high-route") self.assertIsNone(none_rec["derived"]["elevation"]) # elevation facts are derived, so they never appear as record fields self.assertNotIn("ascent_m", text(trail.raw_record(pct))) if __name__ == "__main__": unittest.main()