import json import tempfile import unittest from datetime import datetime, timezone from pathlib import Path from backend.catalogue_builder import ( ApiResult, CatalogueError, GitHubClient, build_catalogue, discover_candidates, popularity_score, validate_projects, ) NOW = datetime(2026, 12, 1, 12, 0, tzinfo=timezone.utc) class FakeClient: def __init__(self, responses): self.responses = responses def get(self, endpoint, optional=False): if endpoint in self.responses: return ApiResult(self.responses[endpoint]) if optional: return ApiResult(None) raise CatalogueError(f"Missing fake response: {endpoint}") def project(): return { "id": "example", "name": "Example", "github": "example/project", "description": "Example project", "tasks": ["docs"], "deployment": ["local"], "languages": ["multi"], "assurance": ["standard"], "steward": { "name": "Example GmbH", "country": "Germany", "country_code": "DE", "evidence_url": "https://example.test/imprint", }, } class CatalogueBuilderTests(unittest.TestCase): def test_popularity_is_bounded_and_monotonic(self): samples = [0, 10, 100, 1_000, 10_000, 100_000, 10_000_000] scores = [popularity_score(value) for value in samples] self.assertEqual(scores, sorted(scores)) self.assertEqual(scores[0], 0) self.assertLessEqual(scores[-1], 30) def test_invalid_steward_country_is_rejected(self): candidate = project() candidate["steward"]["country_code"] = "US" with self.assertRaisesRegex(CatalogueError, "EU/EEA"): validate_projects([candidate]) def test_builds_verified_project_and_source_backed_champion(self): responses = { "/repos/example/project": { "html_url": "https://github.com/example/project", "stargazers_count": 12_000, "forks_count": 400, "open_issues_count": 12, "pushed_at": "2026-11-29T12:00:00Z", "archived": False, "license": {"spdx_id": "Apache-2.0"}, }, "/repos/example/project/community/profile": { "health_percentage": 90, "files": {"security": {"url": "https://example.test/security"}}, }, "/repos/example/project/releases/latest": { "tag_name": "v2.0.0", "published_at": "2026-11-20T12:00:00Z", }, "/repos/example/project/contents/SECURITY.md": { "html_url": "https://github.com/example/project/blob/main/SECURITY.md", }, } champions = [{ "id": "example-user", "company": "Example User", "country": "Germany", "sectors": ["public"], "tasks": ["docs"], "tools": ["example"], "implementation": "Published implementation.", "outcome": "No measured outcome published", "evidence": { "level": "case_study", "publisher": "Example", "title": "Example case study", "url": "https://example.test/case-study", "published_at": "2026-01-01", }, }] catalogue = build_catalogue([project()], champions, FakeClient(responses), NOW) self.assertEqual(catalogue["summary"]["verified_projects"], 1) self.assertEqual(catalogue["projects"][0]["licence"], "Apache-2.0") self.assertEqual(catalogue["projects"][0]["verification"]["status"], "verified") self.assertEqual(catalogue["champions"][0]["verification"]["status"], "source-backed") self.assertEqual(catalogue["errors"], []) def test_network_failure_is_isolated_per_project(self): catalogue = build_catalogue([project()], [], FakeClient({}), NOW) self.assertEqual(catalogue["projects"], []) self.assertEqual(catalogue["summary"]["failed_projects"], 1) def test_discovery_excludes_existing_project(self): search_path = "/search/repositories?q=org%3Aexample%20archived%3Afalse%20fork%3Afalse&sort=stars&order=desc&per_page=5" client = FakeClient({search_path: {"items": [ {"full_name": "example/project", "name": "project", "stargazers_count": 999}, { "full_name": "example/another", "name": "another", "description": "Another tool", "html_url": "https://github.com/example/another", "stargazers_count": 500, "pushed_at": "2026-11-01T00:00:00Z", "license": {"spdx_id": "MIT"}, }, ]}}) candidates, errors = discover_candidates([project()], client) self.assertEqual(errors, []) self.assertEqual([item["github"] for item in candidates], ["example/another"]) self.assertEqual(candidates[0]["status"], "editorial-review") def test_github_client_reads_cache_offline(self): with tempfile.TemporaryDirectory() as directory: client = GitHubClient(Path(directory), offline=True) endpoint = "/repos/example/project" cache_path = client._cache_path(endpoint) cache_path.write_text(json.dumps({"data": {"name": "project"}}), encoding="utf-8") result = client.get(endpoint) self.assertEqual(result.data["name"], "project") self.assertTrue(result.stale) if __name__ == "__main__": unittest.main()