import os import unittest from unittest.mock import patch, MagicMock from ai_planner import AIPlanner class TestAIPlannerFallback(unittest.TestCase): def setUp(self): self.planner = AIPlanner() def test_fallback_uses_ore_anchor_from_summary(self): summary = "- 위치: (0, 0)\n- iron-ore: 100타일 (앵커: 50, 30)\n" plan = self.planner._fallback_plan_from_summary(summary) actions = plan["actions"] self.assertTrue(any(a["action"] == "mine_resource" for a in actions)) def test_fallback_explore_when_no_anchors(self): summary = "- 위치: (0, 0)\n" plan = self.planner._fallback_plan_from_summary(summary) actions = plan["actions"] self.assertTrue(any(a["action"] == "explore" for a in actions)) def test_decide_returns_actions_from_ollama(self): mock_response = MagicMock() mock_response.message.content = ( '{"thinking":"t","current_goal":"g",' '"actions":[{"action":"explore","params":{"direction":"east","max_steps":1},"reason":"x"}],' '"after_this":"a"}' ) with patch("ai_planner.ollama.Client") as MockClient: MockClient.return_value.chat.return_value = mock_response actions = self.planner.decide("## 스텝 1\n현재 상태: 초기") self.assertIsInstance(actions, list) self.assertTrue(len(actions) >= 1) self.assertEqual(actions[0]["action"], "explore") def test_decide_falls_back_when_ollama_raises(self): summary = "- 위치: (10, 10)\n- iron-ore: 50타일 (앵커: 60, 10)\n" with patch("ai_planner.ollama.Client") as MockClient: MockClient.return_value.chat.side_effect = Exception("connection refused") actions = self.planner.decide(summary) self.assertIsInstance(actions, list) self.assertTrue(len(actions) >= 1) if __name__ == "__main__": unittest.main()