-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathSearchToolsIntegrationTests.cs
More file actions
108 lines (91 loc) · 4.37 KB
/
SearchToolsIntegrationTests.cs
File metadata and controls
108 lines (91 loc) · 4.37 KB
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
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.Text.Json;
using AwesomeAssertions;
using Elastic.Documentation.Mcp.Remote.Responses;
using ModelContextProtocol.Protocol;
namespace Mcp.Remote.IntegrationTests;
/// <summary>
/// Integration tests for SearchTools MCP tools.
/// </summary>
public class SearchToolsIntegrationTests(ITestOutputHelper output) : McpToolsIntegrationTestsBase(output)
{
[Fact]
public async Task SemanticSearch_ReturnsResults()
{
// Arrange
var (searchTools, clientAccessor) = CreateSearchTools();
Assert.SkipUnless(searchTools is not null, "Elasticsearch is not configured");
LogDiagnostics(clientAccessor);
var canConnect = await clientAccessor!.CanConnect(TestContext.Current.CancellationToken);
Assert.SkipUnless(canConnect, "Elasticsearch is not connected");
// Act
var result = await searchTools.SemanticSearch(
"elasticsearch getting started",
cancellationToken: TestContext.Current.CancellationToken);
// Assert
result.IsError.Should().BeFalse("SemanticSearch should not return an error result");
var resultJson = ((TextContentBlock)result.Content[0]).Text;
Output.WriteLine($"Result: {resultJson}");
var response = JsonSerializer.Deserialize(resultJson, McpJsonContext.Default.SemanticSearchResponse);
response.Should().NotBeNull();
if (response!.Results.Count == 0)
await LogIndexCount(clientAccessor, TestContext.Current.CancellationToken);
response.Results.Should().NotBeEmpty($"Search for 'elasticsearch getting started' should return results (index: {clientAccessor.SearchIndex})");
response.TotalHits.Should().BeGreaterThan(0);
Output.WriteLine($"Total hits: {response.TotalHits}");
Output.WriteLine($"Results returned: {response.Results.Count}");
}
[Fact]
public async Task SemanticSearch_WithProductFilter()
{
// Arrange
var (searchTools, clientAccessor) = CreateSearchTools();
Assert.SkipUnless(searchTools is not null, "Elasticsearch is not configured");
LogDiagnostics(clientAccessor);
var canConnect = await clientAccessor!.CanConnect(TestContext.Current.CancellationToken);
Assert.SkipUnless(canConnect, "Elasticsearch is not connected");
// Act
var result = await searchTools.SemanticSearch(
"getting started",
productFilter: "elasticsearch",
cancellationToken: TestContext.Current.CancellationToken);
// Assert
result.IsError.Should().BeFalse("SemanticSearch with product filter should not return an error result");
var resultJson = ((TextContentBlock)result.Content[0]).Text;
Output.WriteLine($"Result: {resultJson}");
var response = JsonSerializer.Deserialize(resultJson, McpJsonContext.Default.SemanticSearchResponse);
response.Should().NotBeNull();
if (response!.Results.Count == 0)
await LogIndexCount(clientAccessor!, TestContext.Current.CancellationToken);
response.Results.Should().NotBeEmpty($"Search with product filter should return results (index: {clientAccessor!.SearchIndex})");
Output.WriteLine($"Total hits: {response.TotalHits}");
}
[Fact]
public async Task FindRelatedDocs_ReturnsRelated()
{
// Arrange
var (searchTools, clientAccessor) = CreateSearchTools();
Assert.SkipUnless(searchTools is not null, "Elasticsearch is not configured");
LogDiagnostics(clientAccessor);
var canConnect = await clientAccessor!.CanConnect(TestContext.Current.CancellationToken);
Assert.SkipUnless(canConnect, "Elasticsearch is not connected");
// Act
var result = await searchTools.FindRelatedDocs(
"data streams",
limit: 5,
cancellationToken: TestContext.Current.CancellationToken);
// Assert
result.IsError.Should().BeFalse("FindRelatedDocs should not return an error result");
var resultJson = ((TextContentBlock)result.Content[0]).Text;
Output.WriteLine($"Result: {resultJson}");
var response = JsonSerializer.Deserialize(resultJson, McpJsonContext.Default.RelatedDocsResponse);
response.Should().NotBeNull();
if (response!.RelatedDocs.Count == 0)
await LogIndexCount(clientAccessor!, TestContext.Current.CancellationToken);
response.RelatedDocs.Should().NotBeEmpty($"Finding related docs for 'data streams' should return results (index: {clientAccessor!.SearchIndex})");
response.Count.Should().BeGreaterThan(0);
Output.WriteLine($"Related docs count: {response.Count}");
}
}