import unittest from graph_builder import _json_response class JsonResponseTests(unittest.TestCase): def test_parses_object_with_trailing_text(self): text = '{"entities":[{"name":"Python","type":"language"}],"relations":[]}\nExtra text' result = _json_response(text) self.assertEqual(result["entities"][0]["name"], "Python") def test_parses_object_from_markdown_fence(self): text = '```json\n{"entities":[],"relations":[]}\n```' result = _json_response(text) self.assertEqual(result, {"entities": [], "relations": []}) def test_selects_object_with_graph_schema(self): text = 'Status {"status":"ok"}\nResult: {"entities":[],"relations":[]}' result = _json_response(text) self.assertEqual(result, {"entities": [], "relations": []}) def test_rejects_response_without_schema(self): with self.assertRaises(ValueError): _json_response('{"status":"ok"}') if __name__ == "__main__": unittest.main()