"""utils.py 单元测试: parse_repo, compute_issue_stats, render_template_placeholders""" from __future__ import annotations import pytest from pydantic import SecretStr from tjhlp.config import GiteaConfig from tjhlp.utils import ( compute_issue_stats, parse_repo, render_template_placeholders, web_base_url, ) # typer.Exit 在非 CLI 上下文中抛出 click.exceptions.Exit _EXIT_EXCEPTIONS = (SystemExit, Exception) class TestParseRepo: def test_valid(self): assert parse_repo("org/repo") == ("org", "repo") def test_valid_with_hyphens(self): assert parse_repo("tjhlp-2025f/hw01") == ("tjhlp-2025f", "hw01") def test_no_slash(self): with pytest.raises(_EXIT_EXCEPTIONS): parse_repo("noslash") def test_multiple_slashes(self): with pytest.raises(_EXIT_EXCEPTIONS): parse_repo("a/b/c") def test_empty_org(self): with pytest.raises(_EXIT_EXCEPTIONS): parse_repo("/repo") def test_empty_repo(self): with pytest.raises(_EXIT_EXCEPTIONS): parse_repo("org/") def test_empty_string(self): with pytest.raises(_EXIT_EXCEPTIONS): parse_repo("") class TestComputeIssueStats: def test_empty_list(self): stats = compute_issue_stats([]) assert stats["total"] == 0 assert stats["open"] == 0 assert stats["closed"] == 0 assert stats["no_reply"] == 0 def test_basic_counts(self): issues = [ {"state": "open", "comments": 0}, {"state": "open", "comments": 2}, {"state": "closed", "comments": 1}, ] stats = compute_issue_stats(issues) assert stats["total"] == 3 assert stats["open"] == 2 assert stats["closed"] == 1 assert stats["with_comments"] == 2 assert stats["no_reply"] == 1 def test_label_counts(self): issues = [ {"state": "open", "comments": 0, "labels": [{"name": "bug"}, {"name": "紧急"}]}, {"state": "open", "comments": 0, "labels": [{"name": "bug"}]}, {"state": "closed", "comments": 1, "labels": []}, ] stats = compute_issue_stats(issues) assert stats["label_counts"]["bug"] == 2 assert stats["label_counts"]["紧急"] == 1 def test_user_counts(self): issues = [ {"state": "open", "comments": 0, "user": {"login": "student1"}}, {"state": "open", "comments": 0, "user": {"login": "student1"}}, {"state": "open", "comments": 0, "user": {"login": "student2"}}, ] stats = compute_issue_stats(issues) assert stats["user_counts"]["student1"] == 2 assert stats["user_counts"]["student2"] == 1 def test_missing_user(self): issues = [ {"state": "open", "comments": 0, "user": None}, {"state": "open", "comments": 0}, # no user key at all ] stats = compute_issue_stats(issues) assert stats["total"] == 2 assert len(stats["user_counts"]) == 0 def test_missing_labels(self): issues = [ {"state": "open", "comments": 0}, # no labels key ] stats = compute_issue_stats(issues) assert len(stats["label_counts"]) == 0 def test_no_reply_only_counts_open(self): """closed issues with 0 comments should NOT count as no_reply""" issues = [ {"state": "closed", "comments": 0}, {"state": "open", "comments": 0}, ] stats = compute_issue_stats(issues) assert stats["no_reply"] == 1 class TestRenderTemplatePlaceholders: def test_replaces_all_placeholders(self): out = render_template_placeholders( 'url: "{{BASE_URL}}/{{ORG}}/{{REPO}}/issues"', org="tjhlp-2025f", repo="hw01", web_base="http://10.80.42.185/QA/", ) assert out == 'url: "http://10.80.42.185/QA/tjhlp-2025f/hw01/issues"' def test_strips_trailing_slash_of_web_base(self): out = render_template_placeholders( "{{BASE_URL}}/assets/x.html", "org", "repo", "http://h/QA//" ) assert out == "http://h/QA/assets/x.html" def test_leaves_base_url_when_not_configured(self): # public_url 没配时应保留占位符(由 push_templates 打警告), 而不是生成坏链接 out = render_template_placeholders("{{BASE_URL}}/x", "org", "repo") assert "{{BASE_URL}}" in out def test_text_without_placeholders_is_unchanged(self): text = "blank_issues_enabled: false\n" assert render_template_placeholders(text, "org", "repo", "http://h") == text class TestWebBaseUrl: def _cfg(self, **kw): base = {"url": "http://10.80.42.185:3000", "token": SecretStr("x")} return GiteaConfig(**{**base, **kw}) def test_prefers_public_url(self): assert ( web_base_url(self._cfg(public_url="http://10.80.42.185/QA/")) == "http://10.80.42.185/QA" ) def test_falls_back_to_url(self): assert web_base_url(self._cfg()) == "http://10.80.42.185:3000"