from __future__ import annotations import pytest from leanbench.llm import LLMError, ScriptedClient, extract_code, parse_json def test_parse_json_plain() -> None: assert parse_json('{"a": 1}') == {"a": 1} def test_parse_json_through_a_fence() -> None: assert parse_json('```json\n{"a": [1, 2]}\n```') == {"a": [1, 2]} def test_parse_json_with_chatter_around_it() -> None: assert parse_json('Sure!\n{"ok": true}\nHope that helps.') == {"ok": True} def test_parse_json_raises_when_there_is_none() -> None: with pytest.raises(ValueError): parse_json("no json here") def test_extract_code_pulls_the_lean_block() -> None: raw = "Here you go:\n```lean4\ntheorem t : True := trivial\n```\nDone." assert extract_code(raw) == "theorem t : True := trivial" def test_extract_code_passes_bare_code_through() -> None: assert extract_code("theorem t : True := trivial") == "theorem t : True := trivial" def test_scripted_client_runs_out_loudly() -> None: client = ScriptedClient(["one"]) assert client.complete([]) == "one" with pytest.raises(LLMError): client.complete([])