File size: 2,506 Bytes
af5af97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from unittest.mock import patch

import pytest

from app.core import cache
from app.models.issue import IssueReport
from app.models.report import EngineeringReport
from app.models.repository import RepositoryMetadata
from app.models.review import GeneratedTests, ReviewSuggestions


class FakeRedis:
    """Minimal in-memory stand-in for redis.Redis(decode_responses=True)."""

    def __init__(self) -> None:
        self._store: dict[str, str] = {}

    def get(self, key: str) -> str | None:
        return self._store.get(key)

    def set(self, key: str, value: str, ex: int | None = None) -> None:
        self._store[key] = value


@pytest.fixture(autouse=True)
def clear_cache_client() -> None:
    cache._clear_redis_client_cache()
    yield
    cache._clear_redis_client_cache()


def _fake_report() -> EngineeringReport:
    return EngineeringReport(
        repository=RepositoryMetadata(
            url="https://github.com/owner/repo",
            name="repo",
            local_path="/tmp/fake",
            language="Python",
            frameworks=[],
            architecture="",
            entry_points=[],
            summary="",
        ),
        issues=IssueReport(),
        review=ReviewSuggestions(summary="ok", overall_score=8.0),
        tests=GeneratedTests(),
        full_report="# Report",
        generated_at="2026-06-15T00:00:00Z",
    )


def test_cache_round_trip() -> None:
    fake = FakeRedis()
    with patch("app.core.cache.get_redis_client", return_value=fake):
        cache.set_cached_report("https://github.com/owner/repo", "abc123", _fake_report())
        result = cache.get_cached_report("https://github.com/owner/repo", "abc123")

    assert result is not None
    assert result.full_report == "# Report"


def test_cache_miss_returns_none() -> None:
    fake = FakeRedis()
    with patch("app.core.cache.get_redis_client", return_value=fake):
        result = cache.get_cached_report("https://github.com/owner/repo", "missing-sha")

    assert result is None


def test_cache_read_failure_returns_none() -> None:
    with patch("app.core.cache.get_redis_client", side_effect=ConnectionError("redis down")):
        result = cache.get_cached_report("https://github.com/owner/repo", "abc123")

    assert result is None


def test_cache_write_failure_is_silent() -> None:
    with patch("app.core.cache.get_redis_client", side_effect=ConnectionError("redis down")):
        cache.set_cached_report("https://github.com/owner/repo", "abc123", _fake_report())