Files
github-readme-stats/generator/tests/test_attribution.py
T
shayandClaude Opus 5 a25ae33f29 fix(frontend): give the embed snippet somewhere to paste.
The result screen ended at the snippet and a copy button. GitHub only
renders a profile README from the repo named exactly after the account,
and 15 of 23 arrivals did not have that repo, so the copy button was a
dead end for most people. Add two links under the snippet: open the
README, or create the repo.

Also serve a placeholder card instead of a JSON 400 when someone pastes
the README snippet without swapping YOUR_USERNAME for their own name,
and show a live example card in the README.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BwMoSbmK77Ph7XaV5YnAdS
2026-08-25 08:10:32 -07:00

191 lines
7.1 KiB
Python

"""Per-widget attribution behaves correctly in both contexts.
Standalone widget SVGs (served at /api/<u>/<widget>.svg) must show the
"Generated with gh-stats.com" footer; the composite (served at /api/<u>)
must strip the per-widget footer and only show its own bottom one.
"""
import xml.etree.ElementTree as ET
import pytest
from src import placeholder
from src.models import (
AchievementData,
CollaboratorData,
FocusCategory,
GradeData,
ImpactWeek,
LanguageData,
StreakData,
TagData,
)
from src.widgets import (
render_achievements_widget,
render_collaborators_widget,
render_focus_widget,
render_grade_widget,
render_impact_widget,
render_languages_widget,
render_streaks_widget,
compose_widget,
)
def _grade_fixture() -> GradeData:
return GradeData(
grade="A", score=80,
stats={"commits": 100, "prs": 10, "stars": 5, "repos": 3, "followers": 1},
tags=[TagData(tag="builder", source="earned")],
breakdown={"commits": 0.8, "consistency": 0.6, "stars": 0.4},
)
def _languages_fixture() -> list[LanguageData]:
return [
LanguageData(language="Python", percentage=60.0, loc=0),
LanguageData(language="TypeScript", percentage=40.0, loc=0),
]
# Every widget reachable at /api/<u>/<widget>.svg, rendered from a minimal
# fixture. Parametrised so a new standalone widget can't ship without a footer.
_STANDALONE = {
"grade": lambda: render_grade_widget(_grade_fixture(), theme_name="dark"),
"languages": lambda: render_languages_widget(_languages_fixture(), theme_name="dark"),
"streaks": lambda: render_streaks_widget(
StreakData(current=5, max=12, current_start="2026-08-18",
last_active_date="2026-08-22", max_start="2026-01-01",
max_end="2026-01-12"),
theme_name="dark",
),
"impact": lambda: render_impact_widget(
[ImpactWeek(week_start="2026-01-05", commits=7),
ImpactWeek(week_start="2026-01-12", commits=3)],
theme_name="dark",
),
"impact_empty": lambda: render_impact_widget([], theme_name="dark"),
"focus": lambda: render_focus_widget(
[FocusCategory(category="Backend", percentage=70.0, commit_count=70),
FocusCategory(category="Docs", percentage=30.0, commit_count=30)],
theme_name="dark",
),
"collaborators": lambda: render_collaborators_widget(
[CollaboratorData(username="bob", shared_repos=2, shared_commits=30)],
theme_name="dark",
),
"achievements": lambda: render_achievements_widget(
[AchievementData(title="Hackathon win", subtitle="MIT", event_date="2026-03")],
theme_name="dark",
),
}
@pytest.mark.parametrize("name", sorted(_STANDALONE))
def test_standalone_widget_has_attribution(name):
svg = _STANDALONE[name]()
assert "Generated with gh-stats.com" in svg
assert 'data-gh-attribution="1"' in svg
@pytest.mark.parametrize("name", sorted(_STANDALONE))
def test_standalone_widget_is_well_formed_svg(name):
ET.fromstring(_STANDALONE[name]())
@pytest.mark.parametrize("name", sorted(_STANDALONE))
def test_attribution_is_legible_and_consistent(name):
"""The footer is a credit line, not a disclaimer: big enough to read,
faint enough to stay subordinate, and identical across every widget."""
root = ET.fromstring(_STANDALONE[name]())
nodes = [e for e in root.iter() if e.get("data-gh-attribution") == "1"]
assert len(nodes) == 1
node = nodes[0]
assert node.get("font-size") == "10"
assert node.get("opacity") == "0.75"
assert node.get("text-anchor") == "middle"
@pytest.mark.parametrize("variant", ["building", "rate_limited", "not_found", "unsubstituted"])
def test_placeholder_cards_have_attribution(variant):
svg = placeholder.render(variant, "alice", theme="dark")
assert "Generated with gh-stats.com" in svg
ET.fromstring(svg)
def test_attribution_uses_theme_colors_not_hardcoded_hex():
"""The footer must pick up each theme's secondary text token."""
from src.themes.themes import THEMES
for theme_name in ("dark", "light"):
root = ET.fromstring(render_grade_widget(_grade_fixture(), theme_name=theme_name))
node = next(e for e in root.iter() if e.get("data-gh-attribution") == "1")
assert node.get("fill") == THEMES[theme_name]["text_secondary"]
def test_composite_strips_per_widget_attribution_from_grade():
"""Embedded inside the composite, the grade widget's per-widget
attribution must be removed — the composite has its own footer at
the bottom, showing both would be visually redundant."""
grade_svg = render_grade_widget(_grade_fixture(), theme_name="dark")
composite = compose_widget(
widgets={"grade": grade_svg},
enabled=["grade"],
theme_name="dark",
username="alice",
avatar_b64="",
show_name=False,
)
# The composite has exactly ONE attribution (its own). The widget's
# footer attribution should be gone.
assert composite.count("Generated with gh-stats.com") == 1
# No leftover attribution markers leaked through.
assert "data-gh-attribution" not in composite
def test_composite_strips_per_widget_attribution_from_languages():
langs_svg = render_languages_widget(_languages_fixture(), theme_name="dark")
composite = compose_widget(
widgets={"languages": langs_svg},
enabled=["languages"],
theme_name="dark",
username="alice",
avatar_b64="",
show_name=False,
)
assert composite.count("Generated with gh-stats.com") == 1
assert "data-gh-attribution" not in composite
def test_composite_strips_attribution_from_multiple_widgets():
"""When several attribution-bearing widgets are inlined together,
the composite still shows only its own single footer line."""
grade_svg = render_grade_widget(_grade_fixture(), theme_name="dark")
langs_svg = render_languages_widget(_languages_fixture(), theme_name="dark")
composite = compose_widget(
widgets={"grade": grade_svg, "languages": langs_svg},
enabled=["grade", "languages"],
theme_name="dark",
username="alice",
avatar_b64="",
show_name=False,
)
# Each widget contributed an attribution; the composite contributes
# one of its own — total visible attributions must be exactly 1.
assert composite.count("Generated with gh-stats.com") == 1
def test_composite_reclaims_attribution_height():
"""Stripping the attribution from a per-widget SVG must also reduce
the slot height the composite reserves — otherwise we'd leave 16px
of empty space at the bottom of every embedded widget."""
from src.widgets import composite as composite_mod
grade_svg = render_grade_widget(_grade_fixture(), theme_name="dark")
inner_with_attr, h_with_attr = composite_mod._extract_inner(grade_svg, "grade")
# The standalone grade SVG declares its full height including the
# 16px footer; the embed height should be 16 less.
import re
full_h = int(re.search(r'height="(\d+)"', grade_svg).group(1))
assert h_with_attr == full_h - 16
assert "data-gh-attribution" not in inner_with_attr