#!/usr/bin/env python3
# coding: utf-8
# Copyright (c) 2025 CANN community contributors.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# 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 FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------

"""
Unit tests for pypto._error module.

This test suite verifies that error handling works correctly in PyPTO by
testing error scenarios that trigger different error types through the frontend.
"""

import pytest

import pypto
from pypto.error import PyptoError, PyptoGeneralError, _catch_and_wrap_error


def test_python_error_codes_are_bound_from_cpp():
    """Test that Python wrapper error codes are exported from the C++ binding."""
    assert int(pypto.pypto_impl.ExternalError.RUNTIME_ERROR) == 0x00003
    assert int(pypto.pypto_impl.ExternalError.NAME_ERROR) == 0x00004
    assert int(pypto.pypto_impl.ExternalError.NOT_IMPLEMENTED_ERROR) == 0x00005
    assert int(pypto.pypto_impl.ExternalError.KEY_ERROR) == 0x00006
    assert int(pypto.pypto_impl.ExternalError.INVALID_OPERATION) == 0x00007
    assert int(pypto.pypto_impl.ExternalError.INVALID_TYPE) == 0x00001
    assert int(pypto.pypto_impl.ExternalError.INVALID_VAL) == 0x00002
    assert int(pypto.pypto_impl.ExternalError.OUT_OF_RANGE) == 0x00008
    assert int(pypto.pypto_impl.ExternalError.UNKNOWN) == 0x0FFFF


def test_pypto_error_init():
    err = PyptoError(0xF00001, "test error")
    assert "ErrCode: F00001" in str(err)
    assert "test error" in str(err)


def test_pypto_error_no_duplicate_errcode():
    err = PyptoError(0xF00001, "ErrCode: F00003, original error")
    assert "ErrCode: F00003" in str(err)
    assert str(err).count("ErrCode:") == 1


def test_catch_and_wrap_error_normal():
    @_catch_and_wrap_error("test operation")
    def normal_func(x):
        return x * 2

    assert normal_func(5) == 10


def test_catch_and_wrap_error_wraps_exception():
    @_catch_and_wrap_error("test operation")
    def failing_func():
        raise ValueError("test error")

    with pytest.raises(PyptoGeneralError) as exc_info:
        failing_func()

    assert "Failed to test operation" in str(exc_info.value)
    assert "test error" in str(exc_info.value)


def test_catch_and_wrap_error_preserves_errcode():
    @_catch_and_wrap_error("test operation")
    def failing_func():
        raise RuntimeError("ErrCode: F00003, some error")

    with pytest.raises(PyptoGeneralError) as exc_info:
        failing_func()

    assert "ErrCode: F00003" in str(exc_info.value)


if __name__ == "__main__":
    pytest.main([__file__, "-v"])