|
| 1 | +package mock |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stackrox/stackrox-mcp/internal/toolsets" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNewTool(t *testing.T) { |
| 12 | + t.Run("creates tool with provided values", func(t *testing.T) { |
| 13 | + name := "test-tool" |
| 14 | + readOnly := true |
| 15 | + |
| 16 | + tool := NewTool(name, readOnly) |
| 17 | + |
| 18 | + assert.Equal(t, name, tool.NameValue) |
| 19 | + assert.Equal(t, readOnly, tool.ReadOnlyValue) |
| 20 | + assert.False(t, tool.RegisterCalled) |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +func TestTool_IsReadOnly(t *testing.T) { |
| 25 | + t.Run("returns true when read-only", func(t *testing.T) { |
| 26 | + tool := NewTool("readonly", true) |
| 27 | + |
| 28 | + assert.True(t, tool.IsReadOnly()) |
| 29 | + }) |
| 30 | + |
| 31 | + t.Run("returns false when writable", func(t *testing.T) { |
| 32 | + tool := NewTool("writable", false) |
| 33 | + |
| 34 | + assert.False(t, tool.IsReadOnly()) |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +func TestTool_GetTool(t *testing.T) { |
| 39 | + t.Run("returns MCP tool definition", func(t *testing.T) { |
| 40 | + name := "test-tool" |
| 41 | + tool := NewTool(name, true) |
| 42 | + |
| 43 | + mcpTool := tool.GetTool() |
| 44 | + |
| 45 | + require.NotNil(t, mcpTool) |
| 46 | + assert.Equal(t, name, mcpTool.Name) |
| 47 | + assert.NotEmpty(t, mcpTool.Description) |
| 48 | + |
| 49 | + expectedDesc := "Mock tool for testing" |
| 50 | + assert.Equal(t, expectedDesc, mcpTool.Description) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("returns new tool instance each time", func(t *testing.T) { |
| 54 | + tool := NewTool("test", true) |
| 55 | + |
| 56 | + mcpTool1 := tool.GetTool() |
| 57 | + mcpTool2 := tool.GetTool() |
| 58 | + |
| 59 | + // Should be different instances |
| 60 | + assert.NotSame(t, mcpTool1, mcpTool2) |
| 61 | + |
| 62 | + // But with same values |
| 63 | + assert.Equal(t, mcpTool1.Name, mcpTool2.Name) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func TestTool_RegisterWith(t *testing.T) { |
| 68 | + t.Run("sets RegisterCalled flag", func(t *testing.T) { |
| 69 | + tool := NewTool("register-test", true) |
| 70 | + |
| 71 | + assert.False(t, tool.RegisterCalled) |
| 72 | + |
| 73 | + tool.RegisterWith(nil) |
| 74 | + |
| 75 | + assert.True(t, tool.RegisterCalled) |
| 76 | + |
| 77 | + // Can be called multiple times |
| 78 | + tool.RegisterWith(nil) |
| 79 | + |
| 80 | + assert.True(t, tool.RegisterCalled) |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +func TestTool_InterfaceCompliance(t *testing.T) { |
| 85 | + t.Run("implements toolsets.Tool interface", func(*testing.T) { |
| 86 | + var _ toolsets.Tool = (*Tool)(nil) |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +func TestTool_AsInterface(t *testing.T) { |
| 91 | + var toolInstance toolsets.Tool = NewTool("interface-test", true) |
| 92 | + |
| 93 | + assert.Equal(t, "interface-test", toolInstance.GetName()) |
| 94 | + assert.True(t, toolInstance.IsReadOnly()) |
| 95 | + |
| 96 | + mcpTool := toolInstance.GetTool() |
| 97 | + assert.NotNil(t, mcpTool) |
| 98 | + |
| 99 | + toolInstance.RegisterWith(nil) |
| 100 | +} |
| 101 | + |
| 102 | +func TestTool_ReadOnlyWorkflow(t *testing.T) { |
| 103 | + tool := NewTool("read-tool", true) |
| 104 | + |
| 105 | + assert.True(t, tool.IsReadOnly()) |
| 106 | + assert.False(t, tool.RegisterCalled) |
| 107 | + |
| 108 | + mcpTool := tool.GetTool() |
| 109 | + assert.Equal(t, "read-tool", mcpTool.Name) |
| 110 | + |
| 111 | + tool.RegisterWith(nil) |
| 112 | + |
| 113 | + assert.True(t, tool.RegisterCalled) |
| 114 | +} |
| 115 | + |
| 116 | +func TestTool_WritableWorkflow(t *testing.T) { |
| 117 | + tool := NewTool("write-tool", false) |
| 118 | + |
| 119 | + assert.False(t, tool.IsReadOnly()) |
| 120 | + |
| 121 | + _ = tool.GetTool() |
| 122 | + tool.RegisterWith(nil) |
| 123 | + |
| 124 | + assert.True(t, tool.RegisterCalled) |
| 125 | +} |
| 126 | + |
| 127 | +func TestTool_InToolset(t *testing.T) { |
| 128 | + tool1 := NewTool("tool1", true) |
| 129 | + tool2 := NewTool("tool2", false) |
| 130 | + |
| 131 | + tools := []toolsets.Tool{tool1, tool2} |
| 132 | + |
| 133 | + for _, toolInstance := range tools { |
| 134 | + assert.NotEmpty(t, toolInstance.GetName()) |
| 135 | + |
| 136 | + _ = toolInstance.GetTool() |
| 137 | + toolInstance.RegisterWith(nil) |
| 138 | + } |
| 139 | + |
| 140 | + assert.True(t, tool1.RegisterCalled) |
| 141 | + assert.True(t, tool2.RegisterCalled) |
| 142 | +} |
0 commit comments