File size: 4,967 Bytes
53b2ac9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# SPDX-License-Identifier: MIT
"""
Tests for hash, base64, and json_format plugins.
"""

import sys
import os
import json
import unittest
import pytest

sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), "plugins"))

from plugins.hash import run as hash_run, TOOL_DEF as HASH_DEF
from plugins.base64_tool import run as b64_run, TOOL_DEF as B64_DEF
from plugins.json_format import run as json_run, TOOL_DEF as JSON_DEF


# ── hash ──

class TestHash:
    def test_tool_def(self):
        assert HASH_DEF["name"] == "hash"
        assert "algorithm" in HASH_DEF["parameters"]["properties"]

    def test_sha256(self):
        result = hash_run(text="hello", algorithm="sha256")
        assert "2cf24dba" in result

    def test_md5(self):
        result = hash_run(text="hello", algorithm="md5")
        assert "5d41402a" in result

    def test_sha1(self):
        result = hash_run(text="hello", algorithm="sha1")
        assert isinstance(result, str)
        assert "SHA1" in result

    def test_sha512(self):
        result = hash_run(text="hello", algorithm="sha512")
        assert isinstance(result, str)
        assert "SHA512" in result

    def test_default_algorithm_is_sha256(self):
        result = hash_run(text="hello")
        assert "SHA256" in result

    def test_unknown_algorithm(self):
        result = hash_run(text="hello", algorithm="fakehash")
        assert "Unsupported" in result

    def test_no_input(self):
        result = hash_run()
        assert "Error" in result

    def test_file_not_found(self):
        result = hash_run(file="/nonexistent/file.txt")
        assert "Error" in result

    def test_file_hash(self, tmp_path):
        f = tmp_path / "test.txt"
        f.write_text("hello")
        result = hash_run(file=str(f))
        assert "SHA256" in result
        assert len(result) > 10


# ── base64 ──

class TestBase64:
    def test_tool_def(self):
        assert B64_DEF["name"] == "base64"
        assert "action" in B64_DEF["parameters"]["properties"]

    def test_encode(self):
        result = b64_run(text="Hello", action="encode")
        assert "SGVsbG8=" in result

    def test_decode(self):
        result = b64_run(text="SGVsbG8=", action="decode")
        assert "Hello" in result

    def test_roundtrip(self):
        original = "Hello Nexus!"
        encoded = b64_run(text=original, action="encode").split("\n")[1]
        decoded = b64_run(text=encoded, action="decode").split("\n")[1]
        assert decoded == original

    def test_default_action_is_encode(self):
        result = b64_run(text="test")
        assert "encoded" in result

    def test_empty_text(self):
        result = b64_run(text="")
        assert "Error" in result

    def test_unknown_action(self):
        result = b64_run(text="hello", action="compress")
        assert "Unknown" in result

    def test_invalid_base64_decode(self):
        result = b64_run(text="not-valid-base64!!!", action="decode")
        assert "Error" in result


# ── json_format ──

class TestJsonFormat:
    SAMPLE = '{"name": "Nexus", "version": 1, "active": true}'

    def test_tool_def(self):
        assert JSON_DEF["name"] == "json_format"
        assert "action" in JSON_DEF["parameters"]["properties"]

    def test_pretty(self):
        result = json_run(text=self.SAMPLE, action="pretty")
        assert '"name"' in result
        assert "\n" in result

    def test_pretty_is_valid_json(self):
        result = json_run(text=self.SAMPLE, action="pretty")
        parsed = json.loads(result)
        assert parsed["name"] == "Nexus"

    def test_minify(self):
        result = json_run(text=self.SAMPLE, action="minify")
        assert "\n" not in result
        assert " " not in result

    def test_validate(self):
        result = json_run(text=self.SAMPLE, action="validate")
        assert "Valid" in result
        assert "3 keys" in result

    def test_keys(self):
        result = json_run(text=self.SAMPLE, action="keys")
        assert "name" in result
        assert "version" in result

    def test_invalid_json(self):
        result = json_run(text="{bad json}")
        assert "Invalid" in result

    def test_empty_text(self):
        result = json_run(text="")
        assert "Error" in result

    def test_unknown_action(self):
        result = json_run(text=self.SAMPLE, action="explode")
        assert "Unknown" in result

    def test_array_validate(self):
        result = json_run(text="[1,2,3]", action="validate")
        assert "array" in result
        assert "3" in result

    def test_custom_indent(self):
        result = json_run(text=self.SAMPLE, action="pretty", indent=4)
        assert "    " in result

class TestUntestedTool(unittest.TestCase):
    def test_untested_tool(self):
        # Implement test cases here
        pass

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