| 1 | """Tests for the automated GitHub star history SVG generator.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import importlib.util |
| 6 | import json |
| 7 | import sys |
| 8 | from pathlib import Path |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parent.parent |
| 11 | SCRIPT_PATH = ROOT / ".github" / "scripts" / "update_star_history.py" |
| 12 | SEED_PATH = ROOT / ".github" / "data" / "star-history-seed.json" |
| 13 | |
| 14 | |
| 15 | def _load_module(): |
| 16 | spec = importlib.util.spec_from_file_location("update_star_history", SCRIPT_PATH) |
| 17 | assert spec and spec.loader |
| 18 | mod = importlib.util.module_from_spec(spec) |
| 19 | sys.modules["update_star_history"] = mod |
| 20 | spec.loader.exec_module(mod) |
| 21 | return mod |
| 22 | |
| 23 | |
| 24 | def test_seed_is_sorted_and_monotonic(): |
| 25 | data = json.loads(SEED_PATH.read_text(encoding="utf-8")) |
| 26 | pts = data["points"] |
| 27 | assert len(pts) >= 50 |
| 28 | for prev, cur in zip(pts, pts[1:]): |
| 29 | assert cur[0] > prev[0], f"Dates not strictly increasing: {prev} -> {cur}" |
| 30 | assert cur[1] >= prev[1], f"Stars decreased: {prev} -> {cur}" |
| 31 | |
| 32 | |
| 33 | def test_merge_point_is_monotonic_and_idempotent(): |
| 34 | mod = _load_module() |
| 35 | pts = [["2026-01-25", 1], ["2026-11-24", 74000]] |
| 36 | # Same day with lower count never regresses |
| 37 | updated = mod.merge_point(pts, "2026-11-24", 73500) |
| 38 | assert updated[-1] == ["2026-11-24", 74000] |
| 39 | # Same day with higher count updates in place |
| 40 | updated = mod.merge_point(updated, "2026-11-24", 74500) |
| 41 | assert updated[-1] == ["2026-11-24", 74500] |
| 42 | # New day appends in sorted order |
| 43 | updated = mod.merge_point(updated, "2026-11-25", 75200) |
| 44 | assert updated[-1] == ["2026-11-25", 75200] |
| 45 | # Next-day dip carries highest count forward instead of regressing |
| 46 | updated = mod.merge_point(updated, "2026-11-26", 75100) |
| 47 | assert updated[-1] == ["2026-11-26", 75200] |
| 48 | assert len(updated) == 4 |
| 49 | |
| 50 | |
| 51 | def test_invalid_date_rejected_before_saving(tmp_path, monkeypatch): |
| 52 | import pytest |
| 53 | |
| 54 | mod = _load_module() |
| 55 | data_file = tmp_path / "star-history.json" |
| 56 | monkeypatch.setattr( |
| 57 | sys, |
| 58 | "argv", |
| 59 | [ |
| 60 | "update_star_history.py", |
| 61 | "--seed", |
| 62 | str(SEED_PATH), |
| 63 | "--data", |
| 64 | str(data_file), |
| 65 | "--out-light", |
| 66 | str(tmp_path / "light.svg"), |
| 67 | "--out-dark", |
| 68 | str(tmp_path / "dark.svg"), |
| 69 | "--stars", |
| 70 | "75000", |
| 71 | "--date", |
| 72 | "2026-13-45", |
| 73 | ], |
| 74 | ) |
| 75 | with pytest.raises(SystemExit) as exc: |
| 76 | mod.main() |
| 77 | assert exc.value.code == 2 |
| 78 | assert not data_file.exists() |
| 79 | |
| 80 | |
| 81 | def test_render_svg_light_and_dark(tmp_path): |
| 82 | mod = _load_module() |
| 83 | data = json.loads(SEED_PATH.read_text(encoding="utf-8")) |
| 84 | for theme in ("light", "dark"): |
| 85 | svg = mod.render_svg(data, theme) |
| 86 | assert svg.startswith("<svg ") |
| 87 | assert svg.strip().endswith("</svg>") |
| 88 | assert "mvanhorn/last30days-skill" in svg |
| 89 | assert "74,042" in svg |
| 90 | assert "v4 Launch" in svg |
| 91 | |
| 92 | |
| 93 | def test_nice_y_scale_grows_cleanly(): |
| 94 | mod = _load_module() |
| 95 | y_max, ticks = mod.nice_y_scale(74057) |
| 96 | assert y_max == 80000 |
| 97 | assert ticks == [0, 20000, 40000, 60000, 80000] |
| 98 | |
| 99 | y_max_big, ticks_big = mod.nice_y_scale(105000) |
| 100 | assert y_max_big >= 110000 |
| 101 | assert ticks_big[0] == 0 and ticks_big[-1] == y_max_big |
| 102 |