Skip to content

Commit 129d7e3

Browse files
committed
Added option to filter tests by test mode as it is defined in the resource
1 parent 0c21758 commit 129d7e3

File tree

7 files changed

+54
-37
lines changed

7 files changed

+54
-37
lines changed

Editor/Resources/GetTestsResource.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ namespace McpUnity.Resources
1414
/// </summary>
1515
public class GetTestsResource : McpResourceBase
1616
{
17-
private readonly TestRunnerService _testRunnerService;
17+
private readonly ITestRunnerService _testRunnerService;
1818

1919
/// <summary>
2020
/// Constructor
2121
/// </summary>
22-
public GetTestsResource(TestRunnerService testRunnerService)
22+
public GetTestsResource(ITestRunnerService testRunnerService)
2323
{
2424
Name = "get_tests";
2525
Description = "Gets available tests from Unity Test Runner";
@@ -38,15 +38,7 @@ public override JObject Fetch(JObject parameters)
3838
string testModeFilter = parameters["testMode"]?.ToObject<string>();
3939

4040
// Get all tests from the service
41-
var allTests = _testRunnerService.GetAllTests();
42-
43-
// Apply test mode filter if provided
44-
if (!string.IsNullOrEmpty(testModeFilter))
45-
{
46-
allTests = allTests.Where(t =>
47-
t.TestMode.Equals(testModeFilter, StringComparison.OrdinalIgnoreCase)
48-
).ToList();
49-
}
41+
var allTests = _testRunnerService.GetAllTests(testModeFilter);
5042

5143
// Create the results array
5244
var results = new JArray();
@@ -67,9 +59,7 @@ public override JObject Fetch(JObject parameters)
6759
{
6860
["success"] = true,
6961
["message"] = $"Retrieved {allTests.Count} tests",
70-
["tests"] = results,
71-
["editModeCount"] = allTests.Count(t => t.TestMode.Equals("EditMode", StringComparison.OrdinalIgnoreCase)),
72-
["playModeCount"] = allTests.Count(t => t.TestMode.Equals("PlayMode", StringComparison.OrdinalIgnoreCase))
62+
["tests"] = results
7363
};
7464
}
7565
}

Editor/Services/ConsoleLogsService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,18 @@ public void StopListening()
7070
/// Get all logs as a JSON array
7171
/// </summary>
7272
/// <returns>JArray containing all logs</returns>
73-
public JArray GetAllLogsAsJson()
73+
public JArray GetAllLogsAsJson(string logType = "")
7474
{
75-
// Convert log entries to a JSON array
75+
// Convert log entries to a JSON array, filtering by logType if provided
7676
JArray logsArray = new JArray();
77+
bool filter = !string.IsNullOrEmpty(logType);
7778

7879
lock (_logEntries)
7980
{
8081
foreach (var entry in _logEntries)
8182
{
83+
if (filter && !entry.Type.ToString().Equals(logType, System.StringComparison.OrdinalIgnoreCase))
84+
continue;
8285
logsArray.Add(new JObject
8386
{
8487
["message"] = entry.Message,

Editor/Services/IConsoleLogsService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ namespace McpUnity.Services
1111
public interface IConsoleLogsService
1212
{
1313
/// <summary>
14-
/// Get all logs as a JSON array
14+
/// Get all logs as a JSON array, optionally filtered by log type
1515
/// </summary>
16-
/// <returns>JArray containing all logs</returns>
17-
JArray GetAllLogsAsJson();
16+
/// <param name="logType">UnityEngine.LogType as string (e.g. "Error", "Warning", "Log"). Empty string for all logs.</param>
17+
/// <returns>JArray containing filtered logs</returns>
18+
JArray GetAllLogsAsJson(string logType = "");
1819

1920
/// <summary>
2021
/// Start listening for logs

Editor/Services/ITestRunnerService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public interface ITestRunnerService
1616
TestRunnerApi TestRunnerApi { get; }
1717

1818
/// <summary>
19-
/// Get a list of all available tests
19+
/// Get a list of available tests, optionally filtered by test mode
2020
/// </summary>
21-
List<TestItemInfo> GetAllTests();
21+
/// <param name="testMode">Optional test mode filter (EditMode, PlayMode, or empty for all)</param>
22+
/// <returns>List of test items matching the specified test mode, or all tests if no mode specified</returns>
23+
List<TestItemInfo> GetAllTests(string testMode = "");
2224

2325
/// <summary>
2426
/// Execute tests with the provided parameters

Editor/Services/TestRunnerService.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,25 @@ public TestRunnerService()
3030
}
3131

3232
/// <summary>
33-
/// Get a list of all available tests
33+
/// Get a list of available tests, optionally filtered by test mode
3434
/// </summary>
35-
public List<TestItemInfo> GetAllTests()
35+
/// <param name="testMode">Optional test mode filter (EditMode, PlayMode, or empty for all)</param>
36+
/// <returns>List of test items matching the specified test mode, or all tests if no mode specified</returns>
37+
public List<TestItemInfo> GetAllTests(string testMode = "")
3638
{
3739
var tests = new List<TestItemInfo>();
3840

39-
// Get tests for both edit mode and play mode
40-
_testRunnerApi.RetrieveTestList(TestMode.EditMode, adaptor => CollectTestItems(adaptor, tests));
41-
_testRunnerApi.RetrieveTestList(TestMode.PlayMode, adaptor => CollectTestItems(adaptor, tests));
41+
// Check if we need to retrieve EditMode tests
42+
if (string.IsNullOrEmpty(testMode) || testMode.Equals("EditMode", StringComparison.OrdinalIgnoreCase))
43+
{
44+
_testRunnerApi.RetrieveTestList(TestMode.EditMode, adaptor => CollectTestItems(adaptor, tests));
45+
}
46+
47+
// Check if we need to retrieve PlayMode tests
48+
if (string.IsNullOrEmpty(testMode) || testMode.Equals("PlayMode", StringComparison.OrdinalIgnoreCase))
49+
{
50+
_testRunnerApi.RetrieveTestList(TestMode.PlayMode, adaptor => CollectTestItems(adaptor, tests));
51+
}
4252

4353
return tests;
4454
}

Server/build/resources/getTestsResource.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ function listTestModes(resourceMimeType) {
1717
resources: [
1818
{
1919
uri: `unity://tests/EditMode`,
20-
name: "EditMode tests",
21-
description: "List of all EditMode tests in Unity's test runner",
20+
name: "List only 'EditMode' tests",
21+
description: "List only 'EditMode' tests from Unity's test runner",
2222
mimeType: resourceMimeType
2323
},
2424
{
2525
uri: `unity://tests/PlayMode`,
26-
name: "PlayMode tests",
27-
description: "List of all PlayMode tests in Unity's test runner",
26+
name: "List only 'PlayMode' tests",
27+
description: "List only 'PlayMode' tests from Unity's test runner",
28+
mimeType: resourceMimeType
29+
},
30+
{
31+
uri: `unity://tests/`,
32+
name: "List all tests",
33+
description: "List of all tests in Unity's test runner, this includes PlayMode and EditMode tests",
2834
mimeType: resourceMimeType
2935
}
3036
]
@@ -66,7 +72,7 @@ export function createGetTestsResource(server, mcpUnity, logger) {
6672
*/
6773
async function resourceHandler(mcpUnity, uri, variables) {
6874
// Convert the new handler signature to work with our existing code
69-
const testMode = variables["testMode"] || 'EditMode';
75+
const testMode = variables["testMode"];
7076
const response = await mcpUnity.sendRequest({
7177
method: resourceName,
7278
params: {

Server/src/resources/getTestsResource.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ function listTestModes(resourceMimeType: string) {
3131
resources: [
3232
{
3333
uri: `unity://tests/EditMode`,
34-
name: "EditMode tests",
35-
description: "List of all EditMode tests in Unity's test runner",
34+
name: "List only 'EditMode' tests",
35+
description: "List only 'EditMode' tests from Unity's test runner",
3636
mimeType: resourceMimeType
3737
},
3838
{
3939
uri: `unity://tests/PlayMode`,
40-
name: "PlayMode tests",
41-
description: "List of all PlayMode tests in Unity's test runner",
40+
name: "List only 'PlayMode' tests",
41+
description: "List only 'PlayMode' tests from Unity's test runner",
42+
mimeType: resourceMimeType
43+
},
44+
{
45+
uri: `unity://tests/`,
46+
name: "List all tests",
47+
description: "List of all tests in Unity's test runner, this includes PlayMode and EditMode tests",
4248
mimeType: resourceMimeType
4349
}
4450
]
@@ -75,7 +81,6 @@ export function createGetTestsResource(server: McpServer, mcpUnity: McpUnity, lo
7581
);
7682
}
7783

78-
7984
/**
8085
* Handles requests for test information from Unity's Test Runner
8186
* Retrieves tests filtered by test mode (EditMode or PlayMode)
@@ -88,7 +93,7 @@ export function createGetTestsResource(server: McpServer, mcpUnity: McpUnity, lo
8893
*/
8994
async function resourceHandler(mcpUnity: McpUnity, uri: URL, variables: Variables): Promise<ReadResourceResult> {
9095
// Convert the new handler signature to work with our existing code
91-
const testMode = variables["testMode"] || 'EditMode';
96+
const testMode = variables["testMode"];
9297

9398
const response = await mcpUnity.sendRequest({
9499
method: resourceName,

0 commit comments

Comments
 (0)