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()