#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Exercise benchmark history selection and safe site rendering.""" import sys from pathlib import Path import unittest sys.path.insert(0, str(Path(__file__).resolve().parent)) from bench_history import previous_measurement, render_html, updated_index class HistoryTests(unittest.TestCase): def test_retry_uses_previous_commit(self): first = "a" * 40 second = "b" * 40 index = {"schema": 1, "entries": [ {"sha": first, "timestamp": "2026-01-01T00:00:00+00:00", "metrics": {"bench_ns_per_op": {"hash": 10.0}}}, {"sha": second, "timestamp": "2026-01-02T00:00:00+00:00", "metrics": {"bench_ns_per_op": {"hash": 8.0}}}]} self.assertEqual(previous_measurement(index, second, lambda ancestor, sha: 1 if ancestor == first else None), {"hash": 10.0}) updated = updated_index(index, second, {"bench_ns_per_op": {"hash": 7.5}}, "2026-01-03T00:00:00+00:00") self.assertEqual(len(updated["entries"]), 2) self.assertEqual(updated["entries"][-1]["metrics"]["bench_ns_per_op"]["hash"], 7.5) def test_invalid_measurement_and_sha_rejected(self): with self.assertRaises(ValueError): updated_index({}, "bad", {"bench_ns_per_op": {"hash": 1}}, "now") with self.assertRaises(ValueError): updated_index({}, "a" * 40, {"bench_ns_per_op": {"hash": -1}}, "now") def test_site_escapes_history_content(self): entry = {"sha": "a" * 40, "timestamp": "", "metrics": {"bench_ns_per_op": {"hash": 10.0}}} page = render_html({"entries": [entry]}) self.assertIn("<script>", page) self.assertNotIn("