# Copyright (c) Huawei Technologies Co., Ltd. 2026-2026. All rights reserved.
# OpenOLC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         `http://license.coscl.org.cn/MulanPSL2`
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

import os
import tempfile
import unittest
from unittest.mock import patch

from olc.stub.config_stub_manager import JsonFileConfigStub, OlcConfigPluginManager, Stub

class TestConfigStubManager(unittest.TestCase):
    def setUp(self):
        # 创建临时文件用于测试
        self.temp_dir = tempfile.mkdtemp()
        self.json_file = os.path.join(self.temp_dir, "olc.json")
        
        # 创建测试用的JSON文件
        with open(self.json_file, "w", encoding="utf-8") as f:
            f.write('{"rules": ["rule1", "rule2"]}\n')
        
        # 重置OlcConfigPluginManager单例
        OlcConfigPluginManager._instance = None
    
    def tearDown(self):
        # 清理临时文件
        for root, dirs, files in os.walk(self.temp_dir, topdown=False):
            for file in files:
                os.remove(os.path.join(root, file))
            for dir in dirs:
                os.rmdir(os.path.join(root, dir))
        os.rmdir(self.temp_dir)
    
    @patch('olc.utils.path_utils.PathUtils.get_config_path')
    def test_json_file_config_stub_get_olc_config_rules_success(self, mock_get_config_path):
        # 模拟PathUtils.get_config_path返回临时目录
        mock_get_config_path.return_value = self.temp_dir + "/"
        
        # 创建JsonFileConfigStub实例
        stub = JsonFileConfigStub()
        
        # 调用get_olc_config_rules方法
        result = stub.get_olc_config_rules()
        
        # 验证结果
        self.assertIsNotNone(result)
        self.assertIn('"rules": ["rule1", "rule2"]', result)
    
    @patch('olc.utils.path_utils.PathUtils.get_config_path')
    def test_json_file_config_stub_get_olc_config_rules_file_not_found(self, mock_get_config_path):
        # 模拟PathUtils.get_config_path返回一个不存在的目录
        non_existent_dir = os.path.join(self.temp_dir, "non_existent")
        mock_get_config_path.return_value = non_existent_dir + "/"
        
        # 创建JsonFileConfigStub实例
        stub = JsonFileConfigStub()
        
        # 调用get_olc_config_rules方法
        result = stub.get_olc_config_rules()
        
        # 验证结果
        self.assertIsNone(result)
    
    @patch('olc.utils.path_utils.PathUtils.get_config_path')
    def test_json_file_config_stub_get_olc_config_rules_not_json_file(self, mock_get_config_path):
        # 模拟PathUtils.get_config_path返回临时目录
        mock_get_config_path.return_value = self.temp_dir + "/"
        
        # 删除现有的olc.json文件
        os.remove(self.json_file)
        
        # 创建一个非JSON文件
        non_json_file = os.path.join(self.temp_dir, "olc.txt")
        with open(non_json_file, "w", encoding="utf-8") as f:
            f.write("not a json file")
        
        # 创建JsonFileConfigStub实例
        stub = JsonFileConfigStub()
        
        # 调用get_olc_config_rules方法,使用非JSON文件
        result = stub.get_olc_config_rules()
        
        # 验证结果
        self.assertIsNone(result)
        
        # 清理非JSON文件
        os.remove(non_json_file)
    
    def test_olc_config_plugin_manager_register_stub(self):
        # 创建OlcConfigPluginManager实例
        manager = OlcConfigPluginManager.get_instance()
        
        # 定义一个测试用的Stub子类
        class TestStub(Stub):
            def close_stub(self):
                pass
            
            def get_stub_name(self):
                return "test"
            
            def get_olc_config_rules(self):
                return None
        
        # 注册Stub
        manager.register_stub("test", TestStub)
        
        # 验证注册成功
        self.assertIn("test", manager.get_stub_list())
        self.assertEqual(manager.get_stub_plugin("test"), TestStub)
    
    def test_olc_config_plugin_manager_get_stub_plugin_not_found(self):
        # 创建OlcConfigPluginManager实例
        manager = OlcConfigPluginManager.get_instance()
        
        # 尝试获取不存在的Stub
        result = manager.get_stub_plugin("non_existent")
        
        # 验证结果
        self.assertIsNone(result)
    
    def test_olc_config_plugin_manager_register_stub_empty_name(self):
        # 创建OlcConfigPluginManager实例
        manager = OlcConfigPluginManager.get_instance()
        
        # 定义一个测试用的Stub子类
        class TestStub(Stub):
            def close_stub(self):
                pass
            
            def get_stub_name(self):
                return "test"
            
            def get_olc_config_rules(self):
                return None
        
        # 注册Stub时使用空名称
        manager.register_stub("", TestStub)
        
        # 验证注册失败
        self.assertNotIn("", manager.get_stub_list())
    
    def test_olc_config_plugin_manager_register_stub_none_stub(self):
        # 创建OlcConfigPluginManager实例
        manager = OlcConfigPluginManager.get_instance()
        
        # 注册Stub时使用None
        manager.register_stub("test", None)
        
        # 验证注册失败
        self.assertNotIn("test", manager.get_stub_list())


if __name__ == "__main__":
    unittest.main()