2025-10-13 23:16:43 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MCPForUnity.Editor.Helpers;
|
|
|
|
|
using UnityEditor;
|
2025-11-26 05:08:33 +08:00
|
|
|
using UnityEditor.SceneManagement;
|
2025-10-13 23:16:43 +08:00
|
|
|
using UnityEditor.TestTools.TestRunner.Api;
|
|
|
|
|
using UnityEngine;
|
2025-11-26 05:08:33 +08:00
|
|
|
using UnityEngine.SceneManagement;
|
2025-10-13 23:16:43 +08:00
|
|
|
|
|
|
|
|
namespace MCPForUnity.Editor.Services
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Concrete implementation of <see cref="ITestRunnerService"/>.
|
|
|
|
|
/// Coordinates Unity Test Runner operations and produces structured results.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal sealed class TestRunnerService : ITestRunnerService, ICallbacks, IDisposable
|
|
|
|
|
{
|
|
|
|
|
private static readonly TestMode[] AllModes = { TestMode.EditMode, TestMode.PlayMode };
|
|
|
|
|
|
|
|
|
|
private readonly TestRunnerApi _testRunnerApi;
|
|
|
|
|
private readonly SemaphoreSlim _operationLock = new SemaphoreSlim(1, 1);
|
|
|
|
|
private readonly List<ITestResultAdaptor> _leafResults = new List<ITestResultAdaptor>();
|
|
|
|
|
private TaskCompletionSource<TestRunResult> _runCompletionSource;
|
|
|
|
|
|
|
|
|
|
public TestRunnerService()
|
|
|
|
|
{
|
|
|
|
|
_testRunnerApi = ScriptableObject.CreateInstance<TestRunnerApi>();
|
|
|
|
|
_testRunnerApi.RegisterCallbacks(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<Dictionary<string, string>>> GetTestsAsync(TestMode? mode)
|
|
|
|
|
{
|
2025-11-26 05:08:33 +08:00
|
|
|
await _operationLock.WaitAsync().ConfigureAwait(true);
|
2025-10-13 23:16:43 +08:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var modes = mode.HasValue ? new[] { mode.Value } : AllModes;
|
|
|
|
|
|
|
|
|
|
var results = new List<Dictionary<string, string>>();
|
|
|
|
|
var seen = new HashSet<string>(StringComparer.Ordinal);
|
|
|
|
|
|
|
|
|
|
foreach (var m in modes)
|
|
|
|
|
{
|
|
|
|
|
var root = await RetrieveTestRootAsync(m).ConfigureAwait(true);
|
|
|
|
|
if (root != null)
|
|
|
|
|
{
|
|
|
|
|
CollectFromNode(root, m, results, seen, new List<string>());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_operationLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 04:59:21 +08:00
|
|
|
public async Task<TestRunResult> RunTestsAsync(TestMode mode, TestFilterOptions filterOptions = null)
|
2025-10-13 23:16:43 +08:00
|
|
|
{
|
2025-11-26 05:08:33 +08:00
|
|
|
await _operationLock.WaitAsync().ConfigureAwait(true);
|
2025-10-13 23:16:43 +08:00
|
|
|
Task<TestRunResult> runTask;
|
2025-11-26 05:08:33 +08:00
|
|
|
bool adjustedPlayModeOptions = false;
|
|
|
|
|
bool originalEnterPlayModeOptionsEnabled = false;
|
|
|
|
|
EnterPlayModeOptions originalEnterPlayModeOptions = EnterPlayModeOptions.None;
|
2025-10-13 23:16:43 +08:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_runCompletionSource != null && !_runCompletionSource.Task.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("A Unity test run is already in progress.");
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 05:08:33 +08:00
|
|
|
if (EditorApplication.isPlaying || EditorApplication.isPlayingOrWillChangePlaymode)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Cannot start a test run while the Editor is in or entering Play Mode. Stop Play Mode and try again.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mode == TestMode.PlayMode)
|
|
|
|
|
{
|
|
|
|
|
// PlayMode runs transition the editor into play across multiple update ticks. Unity's
|
|
|
|
|
// built-in pipeline schedules SaveModifiedSceneTask early, but that task uses
|
|
|
|
|
// EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo which throws once play mode is
|
|
|
|
|
// active. To minimize that window we pre-save dirty scenes and disable domain reload (so the
|
|
|
|
|
// MCP bridge stays alive). We do NOT force runSynchronously here because that can freeze the
|
|
|
|
|
// editor in some projects. If the TestRunner still hits the save task after entering play, the
|
|
|
|
|
// run can fail; in that case, rerun from a clean Edit Mode state.
|
|
|
|
|
adjustedPlayModeOptions = EnsurePlayModeRunsWithoutDomainReload(
|
|
|
|
|
out originalEnterPlayModeOptionsEnabled,
|
|
|
|
|
out originalEnterPlayModeOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 23:16:43 +08:00
|
|
|
_leafResults.Clear();
|
|
|
|
|
_runCompletionSource = new TaskCompletionSource<TestRunResult>(TaskCreationOptions.RunContinuationsAsynchronously);
|
2026-01-04 04:42:32 +08:00
|
|
|
// Mark running immediately so readiness snapshots reflect the busy state even before callbacks fire.
|
|
|
|
|
TestRunStatus.MarkStarted(mode);
|
2025-10-13 23:16:43 +08:00
|
|
|
|
2025-12-18 04:59:21 +08:00
|
|
|
var filter = new Filter
|
|
|
|
|
{
|
|
|
|
|
testMode = mode,
|
|
|
|
|
testNames = filterOptions?.TestNames,
|
|
|
|
|
groupNames = filterOptions?.GroupNames,
|
|
|
|
|
categoryNames = filterOptions?.CategoryNames,
|
|
|
|
|
assemblyNames = filterOptions?.AssemblyNames
|
|
|
|
|
};
|
2025-11-26 05:08:33 +08:00
|
|
|
var settings = new ExecutionSettings(filter);
|
|
|
|
|
|
v9 pre-release pruning (#528)
* refactor: Split ParseColorOrDefault into two overloads and change default to Color.white
* Auto-format Python code
* Remove unused Python module
* Refactored VFX functionality into multiple files
Tested everything, works like a charm
* Rename ManageVfx folder to just Vfx
We know what it's managing
* Clean up whitespace on plugin tools and resources
* Make ManageGameObject less of a monolith by splitting it out into different files
* Remove obsolete FindObjectByInstruction method
We also update the namespace for ManageVFX
* refactor: Consolidate editor state resources into single canonical implementation
Merged EditorStateV2 into EditorState, making get_editor_state the canonical resource. Updated Unity C# to use EditorStateCache directly. Enhanced Python implementation with advice/staleness enrichment, external changes detection, and instance ID inference. Removed duplicate EditorStateV2 resource and legacy fallback mapping.
* Validate editor state with Pydantic models in both C# and Python
Added strongly-typed Pydantic models for EditorStateV2 schema in Python and corresponding C# classes with JsonProperty attributes. Updated C# to serialize using typed classes instead of anonymous objects. Python now validates the editor state payload before returning it, catching schema mismatches early.
* Consolidate run_tests and run_tests_async into single async implementation
Merged run_tests_async into run_tests, making async job-based execution the default behavior. Removed synchronous blocking test execution. Updated RunTests.cs to start test jobs immediately and return job_id for polling. Changed TestJobManager methods to internal visibility. Updated README to reflect single run_tests_async tool. Python implementation now uses async job pattern exclusively.
* Validate test job responses with Pydantic models in Python
* Change resources URI from unity:// to mcpforunity://
It should reduce conflicts with other Unity MCPs that users try, and to comply with Unity's requests regarding use of their company and product name
* Update README with all tools + better listing for resources
* Update other references to resources
* Updated translated doc - unfortunately I cannot verify
* Update the Chinese translation of the dev docks
* Change menu item from Setup Window to Local Setup Window
We now differentiate whether it's HTTP local or remote
* Fix URIs for menu items and tests
* Shouldn't have removed it
* Minor edits from CodeRabbit feedback
* Don't use reflection which takes longer
* Fix failing python tests
* Add serialization helpers for ParticleSystem curves and MinMaxCurve types
Added SerializeAnimationCurve and SerializeMinMaxCurve helper methods to properly serialize Unity's curve types. Updated GetInfo to use these helpers for startLifetime, startSpeed, startSize, gravityModifier, and rateOverTime instead of only reading constant values.
* Use ctx param
* Update Server/src/services/tools/run_tests.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
* Minor fixes
* Rename anything EditorStateV2 to just EditorState
It's the default, there's no old version
* Make infer_single_instance_id public by removing underscore prefix
* Fix Python tests, again
* Replace AI generated .meta files with actual Unity ones
* ## Pre-Launch Enhancements: Testing Infrastructure & Tool Improvements (#8)
* Add local test harness for fast developer iteration
Scripts for running the NL/T/GO test suites locally against a GUI Unity
Editor, complementing the CI workflows in .github/workflows/.
Benefits:
- 10-100x faster than CI (no Docker startup)
- Real-time Unity console debugging
- Single test execution for rapid iteration
- Auto-detects HTTP vs stdio transport
Usage:
./scripts/local-test/setup.sh # One-time setup
./scripts/local-test/quick-test.sh NL-0 # Run single test
./scripts/local-test/run-nl-suite-local.sh # Full suite
See scripts/local-test/README.md for details.
Also updated .gitignore to:
- Allow scripts/local-test/ to be tracked
- Ignore generated artifacts (reports/*.xml, .claude/local/, .unity-mcp/)
* Fix issue #525: Save dirty scenes for all test modes
Move SaveDirtyScenesIfNeeded() call outside the PlayMode conditional
so EditMode tests don't get blocked by Unity's "Save Scene" modal dialog.
This prevents MCP from timing out when running EditMode tests with unsaved
scene changes.
* fix: add missing FAST_FAIL_TIMEOUT constant in PluginHub
The FAST_FAIL_TIMEOUT class attribute was referenced on line 149 but never
defined, causing AttributeError on every ping attempt. This error was silently
caught by the broad 'except Exception' handler, causing all fast-fail commands
(read_console, get_editor_state, ping) to fail after 6 seconds of retries with
'ping not answered' error.
Added FAST_FAIL_TIMEOUT = 10 to define a 10-second timeout for fast-fail
commands, matching the intent of the existing fast-fail infrastructure.
* feat(ScriptableObject): enhance dry-run validation for AnimationCurve and Quaternion
Dry-run validation now validates value formats, not just property existence:
- AnimationCurve: Validates structure ({keys:[...]} or direct array), checks
each keyframe is an object, validates numeric fields (time, value, inSlope,
outSlope, inWeight, outWeight) and integer fields (weightedMode)
- Quaternion: Validates array length (3 for Euler, 4 for raw) or object
structure ({x,y,z,w} or {euler:[x,y,z]}), ensures all components are numeric
Refactored shared validation helpers into appropriate locations:
- ParamCoercion: IsNumericToken, ValidateNumericField, ValidateIntegerField
- VectorParsing: ValidateAnimationCurveFormat, ValidateQuaternionFormat
Added comprehensive XML documentation clarifying keyframe field defaults
(all default to 0 except as noted).
Added 5 new dry-run validation tests covering valid and invalid formats
for both AnimationCurve and Quaternion properties.
* test: fix integration tests after merge
- test_refresh_unity_retry_recovery: Mock now handles both refresh_unity and
get_editor_state commands (refresh_unity internally calls get_editor_state
when wait_for_ready=True)
- test_run_tests_async_forwards_params: Mock response now includes required
'mode' field for RunTestsStartResponse Pydantic validation
- test_get_test_job_forwards_job_id: Updated to handle GetTestJobResponse as
Pydantic model instead of dict (use model_dump() for assertions)
* Update warning message to apply to all test modes
Follow-up to PR #527: Since SaveDirtyScenesIfNeeded() now runs for all test modes, update the warning message to say 'tests' instead of 'PlayMode tests'.
* feat(run_tests): add wait_timeout to get_test_job to avoid client loop detection
When polling for test completion, MCP clients like Cursor can detect the
repeated get_test_job calls as 'looping' and terminate the agent.
Added wait_timeout parameter that makes the server wait internally for tests
to complete (polling Unity every 2s) before returning. This dramatically
reduces client-side tool calls from 10-20 down to 1-2, avoiding loop detection.
Usage: get_test_job(job_id='xxx', wait_timeout=30)
- Returns immediately if tests complete within timeout
- Returns current status if timeout expires (client can call again)
- Recommended: 30-60 seconds
* fix: use Pydantic attribute access in test_run_tests_async for merge compatibility
* revert: remove local test harness - will be submitted in separate PR
---------
Co-authored-by: Scott Jennings <scott.jennings+CIGINT@cloudimperiumgames.com>
---------
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: dsarno <david@lighthaus.us>
Co-authored-by: Scott Jennings <scott.jennings+CIGINT@cloudimperiumgames.com>
2026-01-08 06:51:51 +08:00
|
|
|
// Save dirty scenes for all test modes to prevent modal dialogs blocking MCP
|
|
|
|
|
// (Issue #525: EditMode tests were blocked by save dialog)
|
|
|
|
|
SaveDirtyScenesIfNeeded();
|
2025-11-26 05:08:33 +08:00
|
|
|
|
2026-01-15 07:02:40 +08:00
|
|
|
// Apply no-throttling preemptively for PlayMode tests. This ensures Unity
|
|
|
|
|
// isn't throttled during the Play mode transition (which requires multiple
|
|
|
|
|
// editor frames). Without this, unfocused Unity may never reach RunStarted
|
|
|
|
|
// where throttling would normally be disabled.
|
|
|
|
|
if (mode == TestMode.PlayMode)
|
|
|
|
|
{
|
|
|
|
|
TestRunnerNoThrottle.ApplyNoThrottlingPreemptive();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 05:08:33 +08:00
|
|
|
_testRunnerApi.Execute(settings);
|
2025-10-13 23:16:43 +08:00
|
|
|
|
|
|
|
|
runTask = _runCompletionSource.Task;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2026-01-04 04:42:32 +08:00
|
|
|
// Ensure the status is cleared if we failed to start the run.
|
|
|
|
|
TestRunStatus.MarkFinished();
|
2025-11-26 05:08:33 +08:00
|
|
|
if (adjustedPlayModeOptions)
|
|
|
|
|
{
|
|
|
|
|
RestoreEnterPlayModeOptions(originalEnterPlayModeOptionsEnabled, originalEnterPlayModeOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 23:16:43 +08:00
|
|
|
_operationLock.Release();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await runTask.ConfigureAwait(true);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2025-11-26 05:08:33 +08:00
|
|
|
if (adjustedPlayModeOptions)
|
|
|
|
|
{
|
|
|
|
|
RestoreEnterPlayModeOptions(originalEnterPlayModeOptionsEnabled, originalEnterPlayModeOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 23:16:43 +08:00
|
|
|
_operationLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_testRunnerApi?.UnregisterCallbacks(this);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Ignore cleanup errors
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_testRunnerApi != null)
|
|
|
|
|
{
|
|
|
|
|
ScriptableObject.DestroyImmediate(_testRunnerApi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_operationLock.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region TestRunnerApi callbacks
|
|
|
|
|
|
|
|
|
|
public void RunStarted(ITestAdaptor testsToRun)
|
|
|
|
|
{
|
|
|
|
|
_leafResults.Clear();
|
2026-01-04 04:42:32 +08:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Best-effort progress info for async polling (avoid heavy payloads).
|
|
|
|
|
int? total = null;
|
|
|
|
|
if (testsToRun != null)
|
|
|
|
|
{
|
|
|
|
|
total = CountLeafTests(testsToRun);
|
|
|
|
|
}
|
|
|
|
|
TestJobManager.OnRunStarted(total);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
TestJobManager.OnRunStarted(null);
|
|
|
|
|
}
|
2025-10-13 23:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RunFinished(ITestResultAdaptor result)
|
|
|
|
|
{
|
2026-01-15 07:02:40 +08:00
|
|
|
// Always create payload and clean up job state, even if _runCompletionSource is null.
|
|
|
|
|
// This handles domain reload scenarios (e.g., PlayMode tests) where the TestRunnerService
|
|
|
|
|
// is recreated and _runCompletionSource is lost, but TestJobManager state persists via
|
|
|
|
|
// SessionState and the Test Runner still delivers the RunFinished callback.
|
2025-10-13 23:16:43 +08:00
|
|
|
var payload = TestRunResult.Create(result, _leafResults);
|
2026-01-15 07:02:40 +08:00
|
|
|
|
|
|
|
|
// Clean up state regardless of _runCompletionSource - these methods safely handle
|
|
|
|
|
// the case where no MCP job exists (e.g., manual test runs via Unity UI).
|
2026-01-04 04:42:32 +08:00
|
|
|
TestRunStatus.MarkFinished();
|
|
|
|
|
TestJobManager.OnRunFinished();
|
|
|
|
|
TestJobManager.FinalizeCurrentJobFromRunFinished(payload);
|
2026-01-15 07:02:40 +08:00
|
|
|
|
|
|
|
|
// Report result to awaiting caller if we have a completion source
|
|
|
|
|
if (_runCompletionSource != null)
|
|
|
|
|
{
|
|
|
|
|
_runCompletionSource.TrySetResult(payload);
|
|
|
|
|
_runCompletionSource = null;
|
|
|
|
|
}
|
2025-10-13 23:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TestStarted(ITestAdaptor test)
|
|
|
|
|
{
|
2026-01-04 04:42:32 +08:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Prefer FullName for uniqueness; fall back to Name.
|
|
|
|
|
string fullName = test?.FullName;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(fullName))
|
|
|
|
|
{
|
|
|
|
|
fullName = test?.Name;
|
|
|
|
|
}
|
|
|
|
|
TestJobManager.OnTestStarted(fullName);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
2025-10-13 23:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void TestFinished(ITestResultAdaptor result)
|
|
|
|
|
{
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result.HasChildren)
|
|
|
|
|
{
|
|
|
|
|
_leafResults.Add(result);
|
2026-01-04 04:42:32 +08:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string fullName = result.Test?.FullName;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(fullName))
|
|
|
|
|
{
|
|
|
|
|
fullName = result.Test?.Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isFailure = false;
|
|
|
|
|
string message = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// NUnit outcomes are strings in the adaptor; keep it simple.
|
|
|
|
|
string outcome = result.ResultState;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(outcome))
|
|
|
|
|
{
|
|
|
|
|
var o = outcome.Trim().ToLowerInvariant();
|
|
|
|
|
isFailure = o.Contains("failed") || o.Contains("error");
|
|
|
|
|
}
|
|
|
|
|
message = result.Message;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignore adaptor quirks
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TestJobManager.OnLeafTestFinished(fullName, isFailure, message);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
2025-10-13 23:16:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2026-01-04 04:42:32 +08:00
|
|
|
private static int CountLeafTests(ITestAdaptor node)
|
|
|
|
|
{
|
|
|
|
|
if (node == null)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!node.HasChildren)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int total = 0;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var child in node.Children)
|
|
|
|
|
{
|
|
|
|
|
total += CountLeafTests(child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// If Unity changes the adaptor behavior, treat it as "unknown total".
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 05:08:33 +08:00
|
|
|
private static bool EnsurePlayModeRunsWithoutDomainReload(
|
|
|
|
|
out bool originalEnterPlayModeOptionsEnabled,
|
|
|
|
|
out EnterPlayModeOptions originalEnterPlayModeOptions)
|
|
|
|
|
{
|
|
|
|
|
originalEnterPlayModeOptionsEnabled = EditorSettings.enterPlayModeOptionsEnabled;
|
|
|
|
|
originalEnterPlayModeOptions = EditorSettings.enterPlayModeOptions;
|
|
|
|
|
|
|
|
|
|
// When Play Mode triggers a domain reload, the MCP connection is torn down and the pending
|
|
|
|
|
// test run response never makes it back to the caller. To keep the bridge alive for this
|
|
|
|
|
// invocation, temporarily enable Enter Play Mode Options with domain reload disabled.
|
|
|
|
|
bool domainReloadDisabled = (originalEnterPlayModeOptions & EnterPlayModeOptions.DisableDomainReload) != 0;
|
|
|
|
|
bool needsChange = !originalEnterPlayModeOptionsEnabled || !domainReloadDisabled;
|
|
|
|
|
if (!needsChange)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var desired = originalEnterPlayModeOptions | EnterPlayModeOptions.DisableDomainReload;
|
|
|
|
|
EditorSettings.enterPlayModeOptionsEnabled = true;
|
|
|
|
|
EditorSettings.enterPlayModeOptions = desired;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RestoreEnterPlayModeOptions(bool originalEnabled, EnterPlayModeOptions originalOptions)
|
|
|
|
|
{
|
|
|
|
|
EditorSettings.enterPlayModeOptions = originalOptions;
|
|
|
|
|
EditorSettings.enterPlayModeOptionsEnabled = originalEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SaveDirtyScenesIfNeeded()
|
|
|
|
|
{
|
|
|
|
|
int sceneCount = SceneManager.sceneCount;
|
|
|
|
|
for (int i = 0; i < sceneCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var scene = SceneManager.GetSceneAt(i);
|
|
|
|
|
if (scene.isDirty)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(scene.path))
|
|
|
|
|
{
|
v9 pre-release pruning (#528)
* refactor: Split ParseColorOrDefault into two overloads and change default to Color.white
* Auto-format Python code
* Remove unused Python module
* Refactored VFX functionality into multiple files
Tested everything, works like a charm
* Rename ManageVfx folder to just Vfx
We know what it's managing
* Clean up whitespace on plugin tools and resources
* Make ManageGameObject less of a monolith by splitting it out into different files
* Remove obsolete FindObjectByInstruction method
We also update the namespace for ManageVFX
* refactor: Consolidate editor state resources into single canonical implementation
Merged EditorStateV2 into EditorState, making get_editor_state the canonical resource. Updated Unity C# to use EditorStateCache directly. Enhanced Python implementation with advice/staleness enrichment, external changes detection, and instance ID inference. Removed duplicate EditorStateV2 resource and legacy fallback mapping.
* Validate editor state with Pydantic models in both C# and Python
Added strongly-typed Pydantic models for EditorStateV2 schema in Python and corresponding C# classes with JsonProperty attributes. Updated C# to serialize using typed classes instead of anonymous objects. Python now validates the editor state payload before returning it, catching schema mismatches early.
* Consolidate run_tests and run_tests_async into single async implementation
Merged run_tests_async into run_tests, making async job-based execution the default behavior. Removed synchronous blocking test execution. Updated RunTests.cs to start test jobs immediately and return job_id for polling. Changed TestJobManager methods to internal visibility. Updated README to reflect single run_tests_async tool. Python implementation now uses async job pattern exclusively.
* Validate test job responses with Pydantic models in Python
* Change resources URI from unity:// to mcpforunity://
It should reduce conflicts with other Unity MCPs that users try, and to comply with Unity's requests regarding use of their company and product name
* Update README with all tools + better listing for resources
* Update other references to resources
* Updated translated doc - unfortunately I cannot verify
* Update the Chinese translation of the dev docks
* Change menu item from Setup Window to Local Setup Window
We now differentiate whether it's HTTP local or remote
* Fix URIs for menu items and tests
* Shouldn't have removed it
* Minor edits from CodeRabbit feedback
* Don't use reflection which takes longer
* Fix failing python tests
* Add serialization helpers for ParticleSystem curves and MinMaxCurve types
Added SerializeAnimationCurve and SerializeMinMaxCurve helper methods to properly serialize Unity's curve types. Updated GetInfo to use these helpers for startLifetime, startSpeed, startSize, gravityModifier, and rateOverTime instead of only reading constant values.
* Use ctx param
* Update Server/src/services/tools/run_tests.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
* Minor fixes
* Rename anything EditorStateV2 to just EditorState
It's the default, there's no old version
* Make infer_single_instance_id public by removing underscore prefix
* Fix Python tests, again
* Replace AI generated .meta files with actual Unity ones
* ## Pre-Launch Enhancements: Testing Infrastructure & Tool Improvements (#8)
* Add local test harness for fast developer iteration
Scripts for running the NL/T/GO test suites locally against a GUI Unity
Editor, complementing the CI workflows in .github/workflows/.
Benefits:
- 10-100x faster than CI (no Docker startup)
- Real-time Unity console debugging
- Single test execution for rapid iteration
- Auto-detects HTTP vs stdio transport
Usage:
./scripts/local-test/setup.sh # One-time setup
./scripts/local-test/quick-test.sh NL-0 # Run single test
./scripts/local-test/run-nl-suite-local.sh # Full suite
See scripts/local-test/README.md for details.
Also updated .gitignore to:
- Allow scripts/local-test/ to be tracked
- Ignore generated artifacts (reports/*.xml, .claude/local/, .unity-mcp/)
* Fix issue #525: Save dirty scenes for all test modes
Move SaveDirtyScenesIfNeeded() call outside the PlayMode conditional
so EditMode tests don't get blocked by Unity's "Save Scene" modal dialog.
This prevents MCP from timing out when running EditMode tests with unsaved
scene changes.
* fix: add missing FAST_FAIL_TIMEOUT constant in PluginHub
The FAST_FAIL_TIMEOUT class attribute was referenced on line 149 but never
defined, causing AttributeError on every ping attempt. This error was silently
caught by the broad 'except Exception' handler, causing all fast-fail commands
(read_console, get_editor_state, ping) to fail after 6 seconds of retries with
'ping not answered' error.
Added FAST_FAIL_TIMEOUT = 10 to define a 10-second timeout for fast-fail
commands, matching the intent of the existing fast-fail infrastructure.
* feat(ScriptableObject): enhance dry-run validation for AnimationCurve and Quaternion
Dry-run validation now validates value formats, not just property existence:
- AnimationCurve: Validates structure ({keys:[...]} or direct array), checks
each keyframe is an object, validates numeric fields (time, value, inSlope,
outSlope, inWeight, outWeight) and integer fields (weightedMode)
- Quaternion: Validates array length (3 for Euler, 4 for raw) or object
structure ({x,y,z,w} or {euler:[x,y,z]}), ensures all components are numeric
Refactored shared validation helpers into appropriate locations:
- ParamCoercion: IsNumericToken, ValidateNumericField, ValidateIntegerField
- VectorParsing: ValidateAnimationCurveFormat, ValidateQuaternionFormat
Added comprehensive XML documentation clarifying keyframe field defaults
(all default to 0 except as noted).
Added 5 new dry-run validation tests covering valid and invalid formats
for both AnimationCurve and Quaternion properties.
* test: fix integration tests after merge
- test_refresh_unity_retry_recovery: Mock now handles both refresh_unity and
get_editor_state commands (refresh_unity internally calls get_editor_state
when wait_for_ready=True)
- test_run_tests_async_forwards_params: Mock response now includes required
'mode' field for RunTestsStartResponse Pydantic validation
- test_get_test_job_forwards_job_id: Updated to handle GetTestJobResponse as
Pydantic model instead of dict (use model_dump() for assertions)
* Update warning message to apply to all test modes
Follow-up to PR #527: Since SaveDirtyScenesIfNeeded() now runs for all test modes, update the warning message to say 'tests' instead of 'PlayMode tests'.
* feat(run_tests): add wait_timeout to get_test_job to avoid client loop detection
When polling for test completion, MCP clients like Cursor can detect the
repeated get_test_job calls as 'looping' and terminate the agent.
Added wait_timeout parameter that makes the server wait internally for tests
to complete (polling Unity every 2s) before returning. This dramatically
reduces client-side tool calls from 10-20 down to 1-2, avoiding loop detection.
Usage: get_test_job(job_id='xxx', wait_timeout=30)
- Returns immediately if tests complete within timeout
- Returns current status if timeout expires (client can call again)
- Recommended: 30-60 seconds
* fix: use Pydantic attribute access in test_run_tests_async for merge compatibility
* revert: remove local test harness - will be submitted in separate PR
---------
Co-authored-by: Scott Jennings <scott.jennings+CIGINT@cloudimperiumgames.com>
---------
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: dsarno <david@lighthaus.us>
Co-authored-by: Scott Jennings <scott.jennings+CIGINT@cloudimperiumgames.com>
2026-01-08 06:51:51 +08:00
|
|
|
McpLog.Warn($"[TestRunnerService] Skipping unsaved scene '{scene.name}': save it manually before running tests.");
|
2025-11-26 05:08:33 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
EditorSceneManager.SaveScene(scene);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
McpLog.Warn($"[TestRunnerService] Failed to save dirty scene '{scene.name}': {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 23:16:43 +08:00
|
|
|
#region Test list helpers
|
|
|
|
|
|
|
|
|
|
private async Task<ITestAdaptor> RetrieveTestRootAsync(TestMode mode)
|
|
|
|
|
{
|
|
|
|
|
var tcs = new TaskCompletionSource<ITestAdaptor>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
|
|
|
|
|
|
_testRunnerApi.RetrieveTestList(mode, root =>
|
|
|
|
|
{
|
|
|
|
|
tcs.TrySetResult(root);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Ensure the editor pumps at least one additional update in case the window is unfocused.
|
|
|
|
|
EditorApplication.QueuePlayerLoopUpdate();
|
|
|
|
|
|
|
|
|
|
var completed = await Task.WhenAny(tcs.Task, Task.Delay(TimeSpan.FromSeconds(30))).ConfigureAwait(true);
|
|
|
|
|
if (completed != tcs.Task)
|
|
|
|
|
{
|
|
|
|
|
McpLog.Warn($"[TestRunnerService] Timeout waiting for test retrieval callback for {mode}");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await tcs.Task.ConfigureAwait(true);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
McpLog.Error($"[TestRunnerService] Error retrieving tests for {mode}: {ex.Message}\n{ex.StackTrace}");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CollectFromNode(
|
|
|
|
|
ITestAdaptor node,
|
|
|
|
|
TestMode mode,
|
|
|
|
|
List<Dictionary<string, string>> output,
|
|
|
|
|
HashSet<string> seen,
|
|
|
|
|
List<string> path)
|
|
|
|
|
{
|
|
|
|
|
if (node == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool hasName = !string.IsNullOrEmpty(node.Name);
|
|
|
|
|
if (hasName)
|
|
|
|
|
{
|
|
|
|
|
path.Add(node.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool hasChildren = node.HasChildren && node.Children != null;
|
|
|
|
|
|
|
|
|
|
if (!hasChildren)
|
|
|
|
|
{
|
|
|
|
|
string fullName = string.IsNullOrEmpty(node.FullName) ? node.Name ?? string.Empty : node.FullName;
|
|
|
|
|
string key = $"{mode}:{fullName}";
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(fullName) && seen.Add(key))
|
|
|
|
|
{
|
|
|
|
|
string computedPath = path.Count > 0 ? string.Join("/", path) : fullName;
|
|
|
|
|
output.Add(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
["name"] = node.Name ?? fullName,
|
|
|
|
|
["full_name"] = fullName,
|
|
|
|
|
["path"] = computedPath,
|
|
|
|
|
["mode"] = mode.ToString(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (node.Children != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var child in node.Children)
|
|
|
|
|
{
|
|
|
|
|
CollectFromNode(child, mode, output, seen, path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hasName && path.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
path.RemoveAt(path.Count - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Summary of a Unity test run.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class TestRunResult
|
|
|
|
|
{
|
|
|
|
|
internal TestRunResult(TestRunSummary summary, IReadOnlyList<TestRunTestResult> results)
|
|
|
|
|
{
|
|
|
|
|
Summary = summary;
|
|
|
|
|
Results = results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TestRunSummary Summary { get; }
|
|
|
|
|
public IReadOnlyList<TestRunTestResult> Results { get; }
|
|
|
|
|
|
|
|
|
|
public int Total => Summary.Total;
|
|
|
|
|
public int Passed => Summary.Passed;
|
|
|
|
|
public int Failed => Summary.Failed;
|
|
|
|
|
public int Skipped => Summary.Skipped;
|
|
|
|
|
|
2026-01-02 12:36:45 +08:00
|
|
|
public object ToSerializable(string mode, bool includeDetails = false, bool includeFailedTests = false)
|
2025-10-13 23:16:43 +08:00
|
|
|
{
|
2026-01-02 12:36:45 +08:00
|
|
|
// Determine which results to include
|
|
|
|
|
IEnumerable<object> resultsToSerialize;
|
|
|
|
|
if (includeDetails)
|
|
|
|
|
{
|
|
|
|
|
// Include all test results
|
|
|
|
|
resultsToSerialize = Results.Select(r => r.ToSerializable());
|
|
|
|
|
}
|
|
|
|
|
else if (includeFailedTests)
|
|
|
|
|
{
|
|
|
|
|
// Include only failed and skipped tests
|
|
|
|
|
resultsToSerialize = Results
|
|
|
|
|
.Where(r => !string.Equals(r.State, "Passed", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
.Select(r => r.ToSerializable());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// No individual test results
|
|
|
|
|
resultsToSerialize = null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 23:16:43 +08:00
|
|
|
return new
|
|
|
|
|
{
|
|
|
|
|
mode,
|
|
|
|
|
summary = Summary.ToSerializable(),
|
2026-01-02 12:36:45 +08:00
|
|
|
results = resultsToSerialize?.ToList(),
|
2025-10-13 23:16:43 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static TestRunResult Create(ITestResultAdaptor summary, IReadOnlyList<ITestResultAdaptor> tests)
|
|
|
|
|
{
|
|
|
|
|
var materializedTests = tests.Select(TestRunTestResult.FromAdaptor).ToList();
|
|
|
|
|
|
|
|
|
|
int passed = summary?.PassCount
|
|
|
|
|
?? materializedTests.Count(t => string.Equals(t.State, "Passed", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
int failed = summary?.FailCount
|
|
|
|
|
?? materializedTests.Count(t => string.Equals(t.State, "Failed", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
int skipped = summary?.SkipCount
|
|
|
|
|
?? materializedTests.Count(t => string.Equals(t.State, "Skipped", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
double duration = summary?.Duration
|
|
|
|
|
?? materializedTests.Sum(t => t.DurationSeconds);
|
|
|
|
|
|
|
|
|
|
int total = summary != null ? passed + failed + skipped : materializedTests.Count;
|
|
|
|
|
|
|
|
|
|
var summaryPayload = new TestRunSummary(
|
|
|
|
|
total,
|
|
|
|
|
passed,
|
|
|
|
|
failed,
|
|
|
|
|
skipped,
|
|
|
|
|
duration,
|
|
|
|
|
summary?.ResultState ?? "Unknown");
|
|
|
|
|
|
|
|
|
|
return new TestRunResult(summaryPayload, materializedTests);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class TestRunSummary
|
|
|
|
|
{
|
|
|
|
|
internal TestRunSummary(int total, int passed, int failed, int skipped, double durationSeconds, string resultState)
|
|
|
|
|
{
|
|
|
|
|
Total = total;
|
|
|
|
|
Passed = passed;
|
|
|
|
|
Failed = failed;
|
|
|
|
|
Skipped = skipped;
|
|
|
|
|
DurationSeconds = durationSeconds;
|
|
|
|
|
ResultState = resultState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Total { get; }
|
|
|
|
|
public int Passed { get; }
|
|
|
|
|
public int Failed { get; }
|
|
|
|
|
public int Skipped { get; }
|
|
|
|
|
public double DurationSeconds { get; }
|
|
|
|
|
public string ResultState { get; }
|
|
|
|
|
|
|
|
|
|
internal object ToSerializable()
|
|
|
|
|
{
|
|
|
|
|
return new
|
|
|
|
|
{
|
|
|
|
|
total = Total,
|
|
|
|
|
passed = Passed,
|
|
|
|
|
failed = Failed,
|
|
|
|
|
skipped = Skipped,
|
|
|
|
|
durationSeconds = DurationSeconds,
|
|
|
|
|
resultState = ResultState,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class TestRunTestResult
|
|
|
|
|
{
|
|
|
|
|
internal TestRunTestResult(
|
|
|
|
|
string name,
|
|
|
|
|
string fullName,
|
|
|
|
|
string state,
|
|
|
|
|
double durationSeconds,
|
|
|
|
|
string message,
|
|
|
|
|
string stackTrace,
|
|
|
|
|
string output)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
FullName = fullName;
|
|
|
|
|
State = state;
|
|
|
|
|
DurationSeconds = durationSeconds;
|
|
|
|
|
Message = message;
|
|
|
|
|
StackTrace = stackTrace;
|
|
|
|
|
Output = output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
public string FullName { get; }
|
|
|
|
|
public string State { get; }
|
|
|
|
|
public double DurationSeconds { get; }
|
|
|
|
|
public string Message { get; }
|
|
|
|
|
public string StackTrace { get; }
|
|
|
|
|
public string Output { get; }
|
|
|
|
|
|
|
|
|
|
internal object ToSerializable()
|
|
|
|
|
{
|
|
|
|
|
return new
|
|
|
|
|
{
|
|
|
|
|
name = Name,
|
|
|
|
|
fullName = FullName,
|
|
|
|
|
state = State,
|
|
|
|
|
durationSeconds = DurationSeconds,
|
|
|
|
|
message = Message,
|
|
|
|
|
stackTrace = StackTrace,
|
|
|
|
|
output = Output,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static TestRunTestResult FromAdaptor(ITestResultAdaptor adaptor)
|
|
|
|
|
{
|
|
|
|
|
if (adaptor == null)
|
|
|
|
|
{
|
|
|
|
|
return new TestRunTestResult(string.Empty, string.Empty, "Unknown", 0.0, string.Empty, string.Empty, string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new TestRunTestResult(
|
|
|
|
|
adaptor.Name,
|
|
|
|
|
adaptor.FullName,
|
|
|
|
|
adaptor.ResultState,
|
|
|
|
|
adaptor.Duration,
|
|
|
|
|
adaptor.Message,
|
|
|
|
|
adaptor.StackTrace,
|
|
|
|
|
adaptor.Output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|