- `_parse_json()` 메서드에서 응답 내 여러 JSON 객체 중 `actions`를 포함한 계획 객체를 우선 선택하도록 로직 개선 - JSON 파싱 실패 시 잘림 복구 로직을 추가하여 안정성 향상 - 관련 단위 테스트 추가 및 README.md에 변경 사항 반영
88 lines
3.6 KiB
Python
88 lines
3.6 KiB
Python
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")
|
|
|
|
def test_extract_glm_text_prefers_content_then_reasoning(self):
|
|
# content가 비어있고 reasoning_content에 JSON이 들어있는 케이스
|
|
fake = {
|
|
"choices": [
|
|
{
|
|
"finish_reason": "length",
|
|
"message": {
|
|
"content": "",
|
|
"reasoning_content": '{"thinking":"t","current_goal":"g","actions":[],"after_this":"a"}',
|
|
},
|
|
}
|
|
]
|
|
}
|
|
extracted = self.planner._extract_glm_assistant_text(fake)
|
|
self.assertIn('"current_goal":"g"', extracted)
|
|
|
|
def test_extract_glm_text_uses_reasoning_when_content_has_no_json(self):
|
|
fake = {
|
|
"choices": [
|
|
{
|
|
"finish_reason": "length",
|
|
"message": {
|
|
"content": "1. **Current State Analysis:**\n- Location: (0, 0)\n- Inventory: {...}",
|
|
"reasoning_content": '{"thinking":"t","current_goal":"g","actions":[{"action":"explore","params":{"direction":"east","max_steps":1},"reason":"x"}],"after_this":"a"}',
|
|
},
|
|
}
|
|
]
|
|
}
|
|
extracted = self.planner._extract_glm_assistant_text(fake)
|
|
self.assertIn('"current_goal":"g"', extracted)
|
|
|
|
def test_parse_json_selects_actions_object_when_multiple_json_objects_exist(self):
|
|
# 분석 텍스트 안에 먼저 나오는 하위 JSON({ "foo": 1 })이 있고,
|
|
# 뒤에 실제 계획 JSON({ "actions": [...] })이 있는 경우를 검증한다.
|
|
raw = (
|
|
"1. Analyze...\n"
|
|
'{"foo": 1}\n'
|
|
"2. Continue...\n"
|
|
'{"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")
|
|
|