File size: 4,981 Bytes
4f6bb9e
 
 
 
59a8d26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f6bb9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
from unittest.mock import patch

from core.problem_utils import enrich_problem_examples_and_constraints, extract_starter_code


class TestExtractStarterCode:
    def test_extract_starter_code_single_method(self):
        ref = """class Solution:
    def containsDuplicate(self, nums: list[int]) -> bool:
        seen = set()
        for n in nums:
            if n in seen:
                return True
            seen.add(n)
        return False
"""
        starter = extract_starter_code(ref)
        assert "class Solution:" in starter
        assert "def containsDuplicate(self, nums: list[int]) -> bool:" in starter
        assert "pass" in starter
        assert "seen" not in starter
        assert "return True" not in starter

    def test_extract_starter_code_multi_method(self):
        ref = """class MinStack:
    def __init__(self):
        self.stack = []
        self.min_stack = []

    def push(self, val: int) -> None:
        self.stack.append(val)

    def pop(self) -> None:
        self.stack.pop()

    def top(self) -> int:
        return self.stack[-1]

    def getMin(self) -> int:
        return self.min_stack[-1]
"""
        starter = extract_starter_code(ref)
        assert "class MinStack:" in starter
        assert "def __init__(self):" in starter
        assert "def push(self, val: int) -> None:" in starter
        assert "def pop(self, val" not in starter
        assert "def pop(self) -> None:" in starter
        assert "def top(self) -> int:" in starter
        assert "def getMin(self) -> int:" in starter
        assert "self.stack.append" not in starter
        assert "self.min_stack" not in starter

    def test_extract_starter_code_definition_comment(self):
        ref = """# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None
        return prev
"""
        starter = extract_starter_code(ref)
        assert "# Definition for singly-linked list." in starter
        assert "# class ListNode:" in starter
        assert "def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:" in starter
        assert "pass" in starter
        assert "prev = None" not in starter

    def test_extract_starter_code_standalone_function(self):
        ref = """def two_sum(nums, target):
    seen = {}
    return []
"""
        starter = extract_starter_code(ref)
        assert "def two_sum(nums, target):" in starter
        assert "pass" in starter
        assert "seen = {}" not in starter

    def test_extract_starter_code_no_leak_when_no_defs(self):
        ref = """# Solution using dynamic programming
total = sum([1, 2, 3])
print(total)
"""
        starter = extract_starter_code(ref)
        assert "def solve():" in starter
        assert "pass" in starter
        assert "print(total)" not in starter
        assert "total = sum" not in starter

    def test_extract_starter_code_empty(self):
        assert extract_starter_code("") == ""
        assert extract_starter_code("   ") == ""


class TestEnrichProblemExamplesAndConstraints:
    def test_deserializes_json_strings_from_sqlite(self):
        examples_list = [
            {"input": "nums = [2,7,11,15], target = 9", "output": "[0,1]", "explanation": ""}
        ]
        constraints_list = ["2 <= nums.length <= 10^4", "-10^9 <= nums[i] <= 10^9"]
        prob = {
            "name": "Two Sum",
            "problem_number": 1,
            "description": "Given an array of integers nums and an integer target...",
            "examples": json.dumps(examples_list),
            "constraints": json.dumps(constraints_list),
        }

        with patch("ai_cache.get_leetcode_problem") as mock_cache:
            result = enrich_problem_examples_and_constraints(prob, check_remote_cache=True)
            # Remote cache must NOT be checked because examples and constraints were parsed from JSON
            mock_cache.assert_not_called()

        assert isinstance(result["examples"], list)
        assert len(result["examples"]) == 1
        assert result["examples"][0]["input"] == "nums = [2,7,11,15], target = 9"
        assert isinstance(result["constraints"], list)
        assert len(result["constraints"]) == 2

    def test_skips_remote_cache_for_sandbox_client(self):
        prob = {
            "name": "Custom Problem Without Examples",
            "problem_number": 999,
            "description": "Some simple description.",
            "examples": None,
            "constraints": None,
        }

        class MockSandboxClient:
            def table(self, name):
                return None

        with patch("ai_cache.get_leetcode_problem") as mock_cache:
            enrich_problem_examples_and_constraints(
                prob, client=MockSandboxClient(), check_remote_cache=True
            )
            mock_cache.assert_not_called()