|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. |
2 | 2 |
|
3 | 3 | #pragma warning disable CS0618 // ITextSearch is obsolete |
| 4 | +#pragma warning disable CS8602 // Dereference of a possibly null reference - for LINQ expression properties |
4 | 5 |
|
5 | 6 | using System; |
6 | 7 | using System.IO; |
@@ -99,18 +100,18 @@ public async Task GetSearchResultsReturnsSuccessfullyAsync() |
99 | 100 | this._messageHandlerStub.AddJsonResponse(File.ReadAllText(WhatIsTheSkResponseJson)); |
100 | 101 |
|
101 | 102 | // Create an ITextSearch instance using Brave search |
102 | | - var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient }); |
| 103 | + ITextSearch<BraveWebPage> textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient }); |
103 | 104 |
|
104 | 105 | // Act |
105 | | - KernelSearchResults<object> result = await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", new() { Top = 10, Skip = 0 }); |
| 106 | + KernelSearchResults<BraveWebPage> result = await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", new() { Top = 10, Skip = 0 }); |
106 | 107 |
|
107 | 108 | // Assert |
108 | 109 | Assert.NotNull(result); |
109 | 110 | Assert.NotNull(result.Results); |
110 | 111 | var resultList = await result.Results.ToListAsync(); |
111 | 112 | Assert.NotNull(resultList); |
112 | 113 | Assert.Equal(10, resultList.Count); |
113 | | - foreach (BraveWebResult webPage in resultList) |
| 114 | + foreach (BraveWebPage webPage in resultList) |
114 | 115 | { |
115 | 116 | Assert.NotNull(webPage.Title); |
116 | 117 | Assert.NotNull(webPage.Description); |
@@ -190,12 +191,12 @@ public async Task BuildsCorrectUriForEqualityFilterAsync(string paramName, objec |
190 | 191 | // Arrange |
191 | 192 | this._messageHandlerStub.AddJsonResponse(File.ReadAllText(SiteFilterSkResponseJson)); |
192 | 193 |
|
193 | | - // Create an ITextSearch instance using Brave search |
| 194 | + // Create an ITextSearch instance using Brave search |
194 | 195 | var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient }); |
195 | 196 |
|
196 | 197 | // Act |
197 | 198 | TextSearchOptions searchOptions = new() { Top = 5, Skip = 0, Filter = new TextSearchFilter().Equality(paramName, paramValue) }; |
198 | | - KernelSearchResults<object> result = await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", searchOptions); |
| 199 | + var result = await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", searchOptions); |
199 | 200 |
|
200 | 201 | // Assert |
201 | 202 | var requestUris = this._messageHandlerStub.RequestUris; |
@@ -243,6 +244,151 @@ public void Dispose() |
243 | 244 | GC.SuppressFinalize(this); |
244 | 245 | } |
245 | 246 |
|
| 247 | + #region Generic ITextSearch<BraveWebPage> Interface Tests |
| 248 | + |
| 249 | + [Fact] |
| 250 | + public async Task GenericSearchAsyncReturnsResultsSuccessfullyAsync() |
| 251 | + { |
| 252 | + // Arrange |
| 253 | + this._messageHandlerStub.AddJsonResponse(File.ReadAllText(WhatIsTheSkResponseJson)); |
| 254 | + ITextSearch<BraveWebPage> textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient }); |
| 255 | + |
| 256 | + // Act |
| 257 | + var searchOptions = new TextSearchOptions<BraveWebPage> |
| 258 | + { |
| 259 | + Top = 4, |
| 260 | + Skip = 0 |
| 261 | + }; |
| 262 | + KernelSearchResults<string> result = await textSearch.SearchAsync("What is the Semantic Kernel?", searchOptions); |
| 263 | + |
| 264 | + // Assert - Verify basic generic interface functionality |
| 265 | + Assert.NotNull(result); |
| 266 | + Assert.NotNull(result.Results); |
| 267 | + var resultList = await result.Results.ToListAsync(); |
| 268 | + Assert.NotEmpty(resultList); |
| 269 | + |
| 270 | + // Verify the request was made correctly |
| 271 | + var requestUris = this._messageHandlerStub.RequestUris; |
| 272 | + Assert.Single(requestUris); |
| 273 | + Assert.NotNull(requestUris[0]); |
| 274 | + Assert.Contains("count=4", requestUris[0].AbsoluteUri); |
| 275 | + } |
| 276 | + |
| 277 | + [Fact] |
| 278 | + public async Task GenericGetSearchResultsAsyncReturnsResultsSuccessfullyAsync() |
| 279 | + { |
| 280 | + // Arrange |
| 281 | + this._messageHandlerStub.AddJsonResponse(File.ReadAllText(WhatIsTheSkResponseJson)); |
| 282 | + ITextSearch<BraveWebPage> textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient }); |
| 283 | + |
| 284 | + // Act |
| 285 | + var searchOptions = new TextSearchOptions<BraveWebPage> |
| 286 | + { |
| 287 | + Top = 3, |
| 288 | + Skip = 0 |
| 289 | + }; |
| 290 | + KernelSearchResults<BraveWebPage> result = await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", searchOptions); |
| 291 | + |
| 292 | + // Assert - Verify generic interface returns results |
| 293 | + Assert.NotNull(result); |
| 294 | + Assert.NotNull(result.Results); |
| 295 | + var resultList = await result.Results.ToListAsync(); |
| 296 | + Assert.NotEmpty(resultList); |
| 297 | + // Results are now strongly typed as BraveWebPage |
| 298 | + |
| 299 | + // Verify the request was made correctly |
| 300 | + var requestUris = this._messageHandlerStub.RequestUris; |
| 301 | + Assert.Single(requestUris); |
| 302 | + Assert.NotNull(requestUris[0]); |
| 303 | + Assert.Contains("count=3", requestUris[0].AbsoluteUri); |
| 304 | + } |
| 305 | + |
| 306 | + [Fact] |
| 307 | + public async Task GenericGetTextSearchResultsAsyncReturnsResultsSuccessfullyAsync() |
| 308 | + { |
| 309 | + // Arrange |
| 310 | + this._messageHandlerStub.AddJsonResponse(File.ReadAllText(WhatIsTheSkResponseJson)); |
| 311 | + ITextSearch<BraveWebPage> textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient }); |
| 312 | + |
| 313 | + // Act |
| 314 | + var searchOptions = new TextSearchOptions<BraveWebPage> |
| 315 | + { |
| 316 | + Top = 5, |
| 317 | + Skip = 0 |
| 318 | + }; |
| 319 | + KernelSearchResults<TextSearchResult> result = await textSearch.GetTextSearchResultsAsync("What is the Semantic Kernel?", searchOptions); |
| 320 | + |
| 321 | + // Assert - Verify generic interface returns TextSearchResult objects |
| 322 | + Assert.NotNull(result); |
| 323 | + Assert.NotNull(result.Results); |
| 324 | + var resultList = await result.Results.ToListAsync(); |
| 325 | + Assert.NotEmpty(resultList); |
| 326 | + Assert.All(resultList, item => Assert.IsType<TextSearchResult>(item)); |
| 327 | + |
| 328 | + // Verify the request was made correctly |
| 329 | + var requestUris = this._messageHandlerStub.RequestUris; |
| 330 | + Assert.Single(requestUris); |
| 331 | + Assert.NotNull(requestUris[0]); |
| 332 | + Assert.Contains("count=5", requestUris[0].AbsoluteUri); |
| 333 | + } |
| 334 | + |
| 335 | + [Fact] |
| 336 | + public async Task CollectionContainsFilterThrowsNotSupportedExceptionAsync() |
| 337 | + { |
| 338 | + // Arrange - Tests both Enumerable.Contains (C# 13-) and MemoryExtensions.Contains (C# 14+) |
| 339 | + // The same code array.Contains() resolves differently based on C# language version: |
| 340 | + // - C# 13 and earlier: Enumerable.Contains (LINQ extension method) |
| 341 | + // - C# 14 and later: MemoryExtensions.Contains (span-based optimization due to "first-class spans") |
| 342 | + // Our implementation handles both identically since Brave API has limited query operators |
| 343 | + this._messageHandlerStub.AddJsonResponse(File.ReadAllText(WhatIsTheSkResponseJson)); |
| 344 | + ITextSearch<BraveWebPage> textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient }); |
| 345 | + string[] sites = ["microsoft.com", "github.com"]; |
| 346 | + |
| 347 | + // Act & Assert - Verify that collection Contains pattern throws clear exception |
| 348 | + var searchOptions = new TextSearchOptions<BraveWebPage> |
| 349 | + { |
| 350 | + Top = 5, |
| 351 | + Skip = 0, |
| 352 | + Filter = page => sites.Contains(page.Url!.ToString()) // Enumerable.Contains (C# 13-) or MemoryExtensions.Contains (C# 14+) |
| 353 | + }; |
| 354 | + |
| 355 | + var exception = await Assert.ThrowsAsync<NotSupportedException>(async () => |
| 356 | + { |
| 357 | + await textSearch.SearchAsync("test", searchOptions); |
| 358 | + }); |
| 359 | + |
| 360 | + // Assert - Verify error message explains the limitation clearly |
| 361 | + Assert.Contains("Collection Contains filters", exception.Message); |
| 362 | + Assert.Contains("not supported", exception.Message); |
| 363 | + } |
| 364 | + |
| 365 | + [Fact] |
| 366 | + public async Task StringContainsStillWorksWithLINQFiltersAsync() |
| 367 | + { |
| 368 | + // Arrange - Verify that String.Contains (instance method) still works |
| 369 | + // String.Contains is NOT affected by C# 14 "first-class spans" - only arrays are |
| 370 | + this._messageHandlerStub.AddJsonResponse(File.ReadAllText(WhatIsTheSkResponseJson)); |
| 371 | + ITextSearch<BraveWebPage> textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient }); |
| 372 | + |
| 373 | + // Act - String.Contains should continue to work |
| 374 | + var searchOptions = new TextSearchOptions<BraveWebPage> |
| 375 | + { |
| 376 | + Top = 5, |
| 377 | + Skip = 0, |
| 378 | + Filter = page => page.Title.Contains("Kernel") // String.Contains - instance method |
| 379 | + }; |
| 380 | + KernelSearchResults<string> result = await textSearch.SearchAsync("Semantic Kernel tutorial", searchOptions); |
| 381 | + |
| 382 | + // Assert - Verify String.Contains works correctly |
| 383 | + var requestUris = this._messageHandlerStub.RequestUris; |
| 384 | + Assert.Single(requestUris); |
| 385 | + Assert.NotNull(requestUris[0]); |
| 386 | + Assert.Contains("Kernel", requestUris[0].AbsoluteUri); |
| 387 | + Assert.Contains("count=5", requestUris[0].AbsoluteUri); |
| 388 | + } |
| 389 | + |
| 390 | + #endregion |
| 391 | + |
246 | 392 | #region private |
247 | 393 | private const string WhatIsTheSkResponseJson = "./TestData/brave_what_is_the_semantic_kernel.json"; |
248 | 394 | private const string SiteFilterSkResponseJson = "./TestData/brave_site_filter_what_is_the_semantic_kernel.json"; |
@@ -273,7 +419,7 @@ public TextSearchResult MapFromResultToTextSearchResult(object result) |
273 | 419 | { |
274 | 420 | if (result is not BraveWebResult webPage) |
275 | 421 | { |
276 | | - throw new ArgumentException("Result must be a BraveWebPage", nameof(result)); |
| 422 | + throw new ArgumentException("Result must be a BraveWebResult", nameof(result)); |
277 | 423 | } |
278 | 424 |
|
279 | 425 | return new TextSearchResult(webPage.Description?.ToUpperInvariant() ?? string.Empty) |
|
0 commit comments