# Copyright (c) Huawei Technologies Co., Ltd. 2026-2026. All rights reserved.
# MindIE 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 unittest
from olc.bean.olc_control_result import OlcControlResult
from olc.bean.match_wrapper import MatchWrapper
from olc.bean.tag_group import TagGroup
from olc.bean.olc_config_rule import FlowPolicy


class TestOlcControlResult(unittest.TestCase):
    def test_initialization(self):
        """测试OlcControlResult的初始化"""
        # 测试默认值
        result1 = OlcControlResult()
        self.assertEqual(result1.block, False)
        self.assertEqual(result1.block_msg, "")
        self.assertIsNone(result1.block_rule)
        self.assertEqual(result1.tokens_to_deduct, 0)

        # 测试带参数初始化
        tag_group = TagGroup(domain="test_domain", name="test_group")
        policy = FlowPolicy(
            name="test_policy",
            category="test_category",
            enabled=True,
            block_msg="test_block_msg",
            policy_type="NODE",
            time_unit="second",
            time_interval=1,
            rate_limit=100,
            burst_limit=200,
            flow_control_mode="qps",
            max_wait_time_ms=500,
            calculate_alg=None,
            assign_alg=None
        )
        match_wrapper = MatchWrapper(tag_group, policy)
        
        result2 = OlcControlResult(
            block=True,
            block_msg="Blocked by rule",
            block_rule=match_wrapper
        )
        self.assertEqual(result2.block, True)
        self.assertEqual(result2.block_msg, "Blocked by rule")
        self.assertEqual(result2.block_rule, match_wrapper)
        self.assertEqual(result2.tokens_to_deduct, 0)

    def test_set_tokens_to_deduct(self):
        """测试set_tokens_to_deduct方法"""
        result = OlcControlResult()
        self.assertEqual(result.tokens_to_deduct, 0)

        # 测试设置正数
        result.set_tokens_to_deduct(10)
        self.assertEqual(result.tokens_to_deduct, 10)

        # 测试设置负数
        result.set_tokens_to_deduct(-5)
        self.assertEqual(result.tokens_to_deduct, -5)

    def test_repr_method(self):
        """测试__repr__方法"""
        result = OlcControlResult(
            block=True,
            block_msg="Blocked by rule",
            block_rule=None
        )
        repr_str = repr(result)
        self.assertIn("OlcControlResult", repr_str)
        self.assertIn("block = True", repr_str)
        self.assertIn("block_mgs = Blocked by rule", repr_str)
        self.assertIn("block_rule = None", repr_str)
        self.assertIn("tokens_to_deduct = 0", repr_str)

    def test_str_method(self):
        """测试__str__方法"""
        result = OlcControlResult(
            block=True,
            block_msg="Blocked by rule",
            block_rule=None
        )
        str_str = str(result)
        self.assertIn("OlcControlResult", str_str)
        self.assertIn("block = True", str_str)
        self.assertIn("block_mgs = Blocked by rule", str_str)
        self.assertIn("block_rule = None", str_str)
        self.assertIn("tokens_to_deduct = 0", str_str)


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