import os import unittest from ai_planner import AIPlanner class TestAIPlannerParseJson(unittest.TestCase): def setUp(self): # AIPlanner 생성 시 ZAI_API_KEY가 필요하므로 테스트에서는 더미를 주입한다. os.environ.setdefault("ZAI_API_KEY", "dummy") self.planner = AIPlanner() def test_parse_json_object(self): raw = ( '{"thinking":"t","current_goal":"g",' '"actions":[{"action":"explore","params":{"direction":"east","max_steps":1},"reason":"x"}],' '"after_this":"a"}' ) plan = self.planner._parse_json(raw) self.assertEqual(plan["current_goal"], "g") self.assertEqual(len(plan["actions"]), 1) self.assertEqual(plan["actions"][0]["action"], "explore") def test_parse_json_array_top_level(self): raw = '[{"action":"explore","params":{"direction":"east","max_steps":1},"reason":"x"}]' plan = self.planner._parse_json(raw) self.assertEqual(len(plan["actions"]), 1) self.assertEqual(plan["actions"][0]["action"], "explore") self.assertIn("after_this", plan) def test_parse_json_array_with_code_fence(self): raw = ( "```json\n" '[{"action":"explore","params":{"direction":"east","max_steps":1},"reason":"x"}]\n' "```" ) plan = self.planner._parse_json(raw) self.assertEqual(len(plan["actions"]), 1) self.assertEqual(plan["actions"][0]["action"], "explore")