diff --git a/tests/test_tools.py b/tests/test_tools.py new file mode 100644 index 0000000..e2142d2 --- /dev/null +++ b/tests/test_tools.py @@ -0,0 +1,83 @@ +import pytest +from devterm.tools.json_tool import format_json, validate_json +from devterm.tools.jwt_tool import decode_jwt +from devterm.tools.cron_tool import validate_cron, get_next_run +from devterm.tools.base64_tool import encode_base64, decode_base64 +from devterm.tools.url_tool import encode_url, decode_url + + +class TestJsonTool: + def test_format_json(self): + result = format_json('{"key":"value"}') + assert '{\n "key": "value"\n}' == result + + def test_format_nested_json(self): + result = format_json('{"a":{"b":{"c":1}}}') + assert '"c": 1' in result + + def test_validate_valid_json(self): + assert validate_json('{"key": "value"}') is True + + def test_validate_invalid_json(self): + with pytest.raises(Exception): + validate_json('not json') + + +class TestJwtTool: + def test_decode_jwt(self): + token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" + result = decode_jwt(token) + assert "header" in result + assert "payload" in result + assert result["payload"]["sub"] == "1234567890" + + def test_decode_invalid_jwt(self): + with pytest.raises(ValueError): + decode_jwt("invalid.token") + + +class TestCronTool: + def test_validate_simple_cron(self): + assert validate_cron("* * * * *") is True + + def test_validate_specific_cron(self): + assert validate_cron("0 12 * * *") is True + + def test_validate_invalid_cron(self): + assert validate_cron("invalid") is False + + def test_get_next_run(self): + result = get_next_run("* * * * *") + assert result is not None + + +class TestBase64Tool: + def test_encode_base64(self): + result = encode_base64("hello world") + assert result == "aGVsbG8gd29ybGQ=" + + def test_decode_base64(self): + result = decode_base64("aGVsbG8gd29ybGQ=") + assert result == "hello world" + + def test_roundtrip(self): + original = "Test data with special chars: !@#$%" + encoded = encode_base64(original) + decoded = decode_base64(encoded) + assert original == decoded + + +class TestUrlTool: + def test_encode_url(self): + result = encode_url("hello world") + assert result == "hello%20world" + + def test_decode_url(self): + result = decode_url("hello%20world") + assert result == "hello world" + + def test_roundtrip(self): + original = "https://example.com/path?query=value&other=123" + encoded = encode_url(original) + decoded = decode_url(encoded) + assert original == decoded