|
| 1 | +package hash |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestToolHash_Basic(t *testing.T) { |
| 11 | + schema := map[string]interface{}{ |
| 12 | + "type": "object", |
| 13 | + "properties": map[string]interface{}{ |
| 14 | + "param1": map[string]interface{}{ |
| 15 | + "type": "string", |
| 16 | + }, |
| 17 | + }, |
| 18 | + } |
| 19 | + |
| 20 | + hash1, err := ToolHash("server1", "tool1", "A test tool", schema) |
| 21 | + require.NoError(t, err) |
| 22 | + assert.NotEmpty(t, hash1) |
| 23 | + |
| 24 | + // Same inputs should produce same hash |
| 25 | + hash2, err := ToolHash("server1", "tool1", "A test tool", schema) |
| 26 | + require.NoError(t, err) |
| 27 | + assert.Equal(t, hash1, hash2) |
| 28 | +} |
| 29 | + |
| 30 | +func TestToolHash_DifferentServerName(t *testing.T) { |
| 31 | + schema := map[string]interface{}{"type": "object"} |
| 32 | + desc := "Test tool" |
| 33 | + |
| 34 | + hash1, err := ToolHash("server1", "tool1", desc, schema) |
| 35 | + require.NoError(t, err) |
| 36 | + |
| 37 | + hash2, err := ToolHash("server2", "tool1", desc, schema) |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + assert.NotEqual(t, hash1, hash2, "Different server names should produce different hashes") |
| 41 | +} |
| 42 | + |
| 43 | +func TestToolHash_DifferentToolName(t *testing.T) { |
| 44 | + schema := map[string]interface{}{"type": "object"} |
| 45 | + desc := "Test tool" |
| 46 | + |
| 47 | + hash1, err := ToolHash("server1", "tool1", desc, schema) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + hash2, err := ToolHash("server1", "tool2", desc, schema) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + assert.NotEqual(t, hash1, hash2, "Different tool names should produce different hashes") |
| 54 | +} |
| 55 | + |
| 56 | +func TestToolHash_DifferentDescription(t *testing.T) { |
| 57 | + schema := map[string]interface{}{"type": "object"} |
| 58 | + |
| 59 | + hash1, err := ToolHash("server1", "tool1", "Description v1", schema) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + hash2, err := ToolHash("server1", "tool1", "Description v2 - modified", schema) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + assert.NotEqual(t, hash1, hash2, "Different descriptions should produce different hashes") |
| 66 | +} |
| 67 | + |
| 68 | +func TestToolHash_DifferentSchema(t *testing.T) { |
| 69 | + desc := "Test tool" |
| 70 | + schema1 := map[string]interface{}{ |
| 71 | + "type": "object", |
| 72 | + "properties": map[string]interface{}{ |
| 73 | + "param1": map[string]interface{}{"type": "string"}, |
| 74 | + }, |
| 75 | + } |
| 76 | + schema2 := map[string]interface{}{ |
| 77 | + "type": "object", |
| 78 | + "properties": map[string]interface{}{ |
| 79 | + "param1": map[string]interface{}{"type": "string"}, |
| 80 | + "param2": map[string]interface{}{"type": "number"}, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + hash1, err := ToolHash("server1", "tool1", desc, schema1) |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + hash2, err := ToolHash("server1", "tool1", desc, schema2) |
| 88 | + require.NoError(t, err) |
| 89 | + |
| 90 | + assert.NotEqual(t, hash1, hash2, "Different schemas should produce different hashes") |
| 91 | +} |
| 92 | + |
| 93 | +func TestToolHash_NilSchema(t *testing.T) { |
| 94 | + hash1, err := ToolHash("server1", "tool1", "Test tool", nil) |
| 95 | + require.NoError(t, err) |
| 96 | + assert.NotEmpty(t, hash1) |
| 97 | + |
| 98 | + // Empty schema should produce consistent hash |
| 99 | + hash2, err := ToolHash("server1", "tool1", "Test tool", nil) |
| 100 | + require.NoError(t, err) |
| 101 | + assert.Equal(t, hash1, hash2) |
| 102 | +} |
| 103 | + |
| 104 | +func TestToolHash_EmptyDescription(t *testing.T) { |
| 105 | + schema := map[string]interface{}{"type": "object"} |
| 106 | + |
| 107 | + hash1, err := ToolHash("server1", "tool1", "", schema) |
| 108 | + require.NoError(t, err) |
| 109 | + assert.NotEmpty(t, hash1) |
| 110 | + |
| 111 | + hash2, err := ToolHash("server1", "tool1", "non-empty", schema) |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + assert.NotEqual(t, hash1, hash2, "Empty vs non-empty description should differ") |
| 115 | +} |
| 116 | + |
| 117 | +func TestComputeToolHash_Basic(t *testing.T) { |
| 118 | + schema := map[string]interface{}{"type": "object"} |
| 119 | + |
| 120 | + hash := ComputeToolHash("server1", "tool1", "Test tool", schema) |
| 121 | + assert.NotEmpty(t, hash) |
| 122 | + |
| 123 | + // Should be consistent |
| 124 | + hash2 := ComputeToolHash("server1", "tool1", "Test tool", schema) |
| 125 | + assert.Equal(t, hash, hash2) |
| 126 | +} |
| 127 | + |
| 128 | +func TestComputeToolHash_FallbackOnMarshalError(t *testing.T) { |
| 129 | + // Create a value that cannot be marshaled to JSON (channel) |
| 130 | + // Note: Go's json.Marshal actually handles most types gracefully, |
| 131 | + // but functions and channels will fail |
| 132 | + invalidSchema := make(chan int) |
| 133 | + |
| 134 | + // Should not panic, should return fallback hash |
| 135 | + hash := ComputeToolHash("server1", "tool1", "desc", invalidSchema) |
| 136 | + assert.NotEmpty(t, hash, "Should return fallback hash on marshal error") |
| 137 | +} |
| 138 | + |
| 139 | +func TestComputeToolHash_DescriptionOnlyChange(t *testing.T) { |
| 140 | + schema := map[string]interface{}{ |
| 141 | + "type": "object", |
| 142 | + "properties": map[string]interface{}{ |
| 143 | + "arg": map[string]interface{}{"type": "string"}, |
| 144 | + }, |
| 145 | + } |
| 146 | + |
| 147 | + hash1 := ComputeToolHash("myserver", "my_tool", "Original description", schema) |
| 148 | + hash2 := ComputeToolHash("myserver", "my_tool", "Updated description with more details", schema) |
| 149 | + |
| 150 | + assert.NotEqual(t, hash1, hash2, "Description-only changes must produce different hashes") |
| 151 | +} |
| 152 | + |
| 153 | +func TestVerifyToolHash_Match(t *testing.T) { |
| 154 | + schema := map[string]interface{}{"type": "object"} |
| 155 | + desc := "Test tool" |
| 156 | + |
| 157 | + hash, err := ToolHash("server1", "tool1", desc, schema) |
| 158 | + require.NoError(t, err) |
| 159 | + |
| 160 | + matches, err := VerifyToolHash("server1", "tool1", desc, schema, hash) |
| 161 | + require.NoError(t, err) |
| 162 | + assert.True(t, matches, "Hash should match for same inputs") |
| 163 | +} |
| 164 | + |
| 165 | +func TestVerifyToolHash_NoMatch(t *testing.T) { |
| 166 | + schema := map[string]interface{}{"type": "object"} |
| 167 | + |
| 168 | + hash, err := ToolHash("server1", "tool1", "desc v1", schema) |
| 169 | + require.NoError(t, err) |
| 170 | + |
| 171 | + // Different description |
| 172 | + matches, err := VerifyToolHash("server1", "tool1", "desc v2", schema, hash) |
| 173 | + require.NoError(t, err) |
| 174 | + assert.False(t, matches, "Hash should not match for different description") |
| 175 | +} |
| 176 | + |
| 177 | +func TestStringHash(t *testing.T) { |
| 178 | + hash1 := StringHash("hello") |
| 179 | + hash2 := StringHash("hello") |
| 180 | + hash3 := StringHash("world") |
| 181 | + |
| 182 | + assert.Equal(t, hash1, hash2, "Same input should produce same hash") |
| 183 | + assert.NotEqual(t, hash1, hash3, "Different input should produce different hash") |
| 184 | + assert.Len(t, hash1, 64, "SHA-256 hex string should be 64 characters") |
| 185 | +} |
| 186 | + |
| 187 | +func TestBytesHash(t *testing.T) { |
| 188 | + hash1 := BytesHash([]byte("hello")) |
| 189 | + hash2 := BytesHash([]byte("hello")) |
| 190 | + hash3 := BytesHash([]byte("world")) |
| 191 | + |
| 192 | + assert.Equal(t, hash1, hash2, "Same input should produce same hash") |
| 193 | + assert.NotEqual(t, hash1, hash3, "Different input should produce different hash") |
| 194 | + assert.Len(t, hash1, 64, "SHA-256 hex string should be 64 characters") |
| 195 | +} |
0 commit comments