|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Google.Protobuf.WellKnownTypes; |
| 5 | +using Microsoft.Extensions.AI; |
| 6 | +using OpenAI.Responses; |
| 7 | +using xAI.Protocol; |
| 8 | + |
| 9 | +namespace xAI; |
| 10 | + |
| 11 | +public class GrokConversionTests |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public void AsTool_WithWebSearch() |
| 15 | + { |
| 16 | + var webSearch = new HostedWebSearchTool(); |
| 17 | + |
| 18 | + var tool = webSearch.AsProtocolTool(); |
| 19 | + |
| 20 | + Assert.NotNull(tool?.WebSearch); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void AsTool_WithWebSearch_ThrowsIfAllowedAndExcluded() |
| 25 | + { |
| 26 | + var webSearch = new GrokSearchTool |
| 27 | + { |
| 28 | + AllowedDomains = ["Foo"], |
| 29 | + ExcludedDomains = ["Bar"] |
| 30 | + }; |
| 31 | + |
| 32 | + Assert.Throws<NotSupportedException>(() => webSearch.AsProtocolTool()); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void AsTool_WithWebSearch_AllowedDomains() |
| 37 | + { |
| 38 | + var webSearch = new GrokSearchTool |
| 39 | + { |
| 40 | + AllowedDomains = ["foo.com", "bar.com"], |
| 41 | + }; |
| 42 | + |
| 43 | + var tool = webSearch.AsProtocolTool(); |
| 44 | + |
| 45 | + Assert.NotNull(tool?.WebSearch); |
| 46 | + Assert.Equal(["foo.com", "bar.com"], tool.WebSearch.AllowedDomains); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void AsTool_WithWebSearch_ExcludedDomains() |
| 51 | + { |
| 52 | + var webSearch = new GrokSearchTool |
| 53 | + { |
| 54 | + ExcludedDomains = ["foo.com", "bar.com"], |
| 55 | + }; |
| 56 | + |
| 57 | + var tool = webSearch.AsProtocolTool(); |
| 58 | + |
| 59 | + Assert.NotNull(tool?.WebSearch); |
| 60 | + Assert.Equal(["foo.com", "bar.com"], tool.WebSearch.ExcludedDomains); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void AsTool_WithWebSearch_ImageUnderstanding() |
| 65 | + { |
| 66 | + var webSearch = new GrokSearchTool |
| 67 | + { |
| 68 | + EnableImageUnderstanding = true |
| 69 | + }; |
| 70 | + |
| 71 | + var tool = webSearch.AsProtocolTool(); |
| 72 | + |
| 73 | + Assert.NotNull(tool?.WebSearch); |
| 74 | + Assert.True(tool.WebSearch.EnableImageUnderstanding); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void AsTool_WithXSearch_ThrowsIfAllowedAndExcluded() |
| 79 | + { |
| 80 | + var webSearch = new GrokXSearchTool |
| 81 | + { |
| 82 | + AllowedHandles = ["Foo"], |
| 83 | + ExcludedHandles = ["Bar"] |
| 84 | + }; |
| 85 | + |
| 86 | + Assert.Throws<NotSupportedException>(() => webSearch.AsProtocolTool()); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void AsTool_WithXSearch_AllowedHandles() |
| 91 | + { |
| 92 | + var webSearch = new GrokXSearchTool |
| 93 | + { |
| 94 | + AllowedHandles = ["foo", "bar"], |
| 95 | + }; |
| 96 | + |
| 97 | + var tool = webSearch.AsProtocolTool(); |
| 98 | + |
| 99 | + Assert.NotNull(tool?.XSearch); |
| 100 | + Assert.Equal(["foo", "bar"], tool.XSearch.AllowedXHandles); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void AsTool_WithXSearch_ExcludedDomains() |
| 105 | + { |
| 106 | + var webSearch = new GrokXSearchTool |
| 107 | + { |
| 108 | + ExcludedHandles = ["foo", "bar"], |
| 109 | + }; |
| 110 | + |
| 111 | + var tool = webSearch.AsProtocolTool(); |
| 112 | + |
| 113 | + Assert.NotNull(tool?.XSearch); |
| 114 | + Assert.Equal(["foo", "bar"], tool.XSearch.ExcludedXHandles); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void AsTool_WithXSearch_ImageUnderstanding() |
| 119 | + { |
| 120 | + var webSearch = new GrokXSearchTool |
| 121 | + { |
| 122 | + EnableImageUnderstanding = true |
| 123 | + }; |
| 124 | + |
| 125 | + var tool = webSearch.AsProtocolTool(); |
| 126 | + |
| 127 | + Assert.NotNull(tool?.XSearch); |
| 128 | + Assert.True(tool.XSearch.EnableImageUnderstanding); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void AsTool_WithXSearch_VideoUnderstanding() |
| 133 | + { |
| 134 | + var webSearch = new GrokXSearchTool |
| 135 | + { |
| 136 | + EnableVideoUnderstanding = true |
| 137 | + }; |
| 138 | + |
| 139 | + var tool = webSearch.AsProtocolTool(); |
| 140 | + |
| 141 | + Assert.NotNull(tool?.XSearch); |
| 142 | + Assert.True(tool.XSearch.EnableVideoUnderstanding); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact] |
| 146 | + public void AsTool_WithXSearch_FromTo() |
| 147 | + { |
| 148 | + var webSearch = new GrokXSearchTool |
| 149 | + { |
| 150 | + FromDate = DateOnly.FromDateTime(DateTime.UtcNow.Subtract(TimeSpan.FromDays(1))), |
| 151 | + ToDate = DateOnly.FromDateTime(DateTime.UtcNow) |
| 152 | + }; |
| 153 | + |
| 154 | + var tool = webSearch.AsProtocolTool(); |
| 155 | + |
| 156 | + Assert.NotNull(tool?.XSearch); |
| 157 | + Assert.Equal(tool.XSearch.FromDate, Timestamp.FromDateTime(webSearch.FromDate.Value.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc))); |
| 158 | + Assert.Equal(tool.XSearch.ToDate, Timestamp.FromDateTime(webSearch.ToDate.Value.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc))); |
| 159 | + } |
| 160 | + |
| 161 | + [Fact] |
| 162 | + public void AsTool_WithFunctionTool() |
| 163 | + { |
| 164 | + var functionTool = AIFunctionFactory.Create(() => "", "Name", "Description"); |
| 165 | + |
| 166 | + var tool = functionTool.AsProtocolTool(); |
| 167 | + |
| 168 | + Assert.NotNull(tool?.Function); |
| 169 | + Assert.Equal("Name", tool.Function.Name); |
| 170 | + Assert.Equal("Description", tool.Function.Description); |
| 171 | + } |
| 172 | + |
| 173 | + [Fact] |
| 174 | + public void AsTool_WithCodeExecution() |
| 175 | + { |
| 176 | + var codeTool = new HostedCodeInterpreterTool(); |
| 177 | + |
| 178 | + var tool = codeTool.AsProtocolTool(); |
| 179 | + |
| 180 | + Assert.NotNull(tool?.CodeExecution); |
| 181 | + } |
| 182 | + |
| 183 | + [Fact] |
| 184 | + public void AsTool_WithHostedFileSearchTool() |
| 185 | + { |
| 186 | + var collectionId = Guid.NewGuid().ToString(); |
| 187 | + var instructions = "Return N/A if no results found"; |
| 188 | + var fileSearch = new HostedFileSearchTool() |
| 189 | + { |
| 190 | + MaximumResultCount = 50, |
| 191 | + Inputs = [new HostedVectorStoreContent(collectionId)] |
| 192 | + }.WithInstructions(instructions); |
| 193 | + |
| 194 | + var tool = fileSearch.AsProtocolTool(); |
| 195 | + |
| 196 | + Assert.NotNull(tool?.CollectionsSearch); |
| 197 | + Assert.Contains(collectionId, tool.CollectionsSearch.CollectionIds); |
| 198 | + Assert.Equal(50, tool.CollectionsSearch.Limit); |
| 199 | + Assert.Equal(instructions, tool.CollectionsSearch.Instructions); |
| 200 | + } |
| 201 | + |
| 202 | + [Fact] |
| 203 | + public void AsTool_WithHostedMcpTool() |
| 204 | + { |
| 205 | + var accessToken = Guid.NewGuid().ToString(); |
| 206 | + var headers = new Dictionary<string, string> |
| 207 | + { |
| 208 | + ["foo"] = "baz" |
| 209 | + }; |
| 210 | + var mcpTool = new HostedMcpServerTool("foo", "foo.com", new Dictionary<string, object?> |
| 211 | + { |
| 212 | + ["x-extra"] = "bar", |
| 213 | + [nameof(MCP.ExtraHeaders)] = headers |
| 214 | + }) |
| 215 | + { |
| 216 | + AllowedTools = ["list"], |
| 217 | + AuthorizationToken = accessToken, |
| 218 | + }; |
| 219 | + |
| 220 | + var tool = mcpTool.AsProtocolTool(); |
| 221 | + |
| 222 | + Assert.NotNull(tool?.Mcp); |
| 223 | + Assert.Equal("foo", tool.Mcp.ServerLabel); |
| 224 | + Assert.Equal("foo.com", tool.Mcp.ServerUrl); |
| 225 | + Assert.Contains("list", tool.Mcp.AllowedToolNames); |
| 226 | + Assert.Equal(accessToken, tool.Mcp.Authorization); |
| 227 | + Assert.Contains(KeyValuePair.Create("x-extra", "bar"), tool.Mcp.ExtraHeaders); |
| 228 | + Assert.Contains(KeyValuePair.Create("foo", "baz"), tool.Mcp.ExtraHeaders); |
| 229 | + } |
| 230 | +} |
0 commit comments