feat: JSON 배열 처리 및 오류 메시지 개선
- `_parse_json()` 메서드에서 최상위 JSON 배열을 허용하고, 이를 `actions` 리스트로 래핑하여 정상적인 파이프라인으로 이어지도록 개선 - JSON 파싱 실패 시 원인 파악을 돕기 위해 오류 메시지에 첫 비공백 문자를 포함 - 새로운 단위 테스트를 추가하여 JSON 객체 및 배열 파서의 회귀 방지 - README.md 및 문서에 변경 사항 반영
This commit is contained in:
40
tests/test_ai_planner_parse_json.py
Normal file
40
tests/test_ai_planner_parse_json.py
Normal file
@@ -0,0 +1,40 @@
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user