Skip to content

Fix tests-run state being wiped by PlayerPrefs.DeleteAll#870

Open
Abhinav-0311 wants to merge 3 commits into
IvanMurzak:mainfrom
Abhinav-0311:fix-tests-run-session-state
Open

Fix tests-run state being wiped by PlayerPrefs.DeleteAll#870
Abhinav-0311 wants to merge 3 commits into
IvanMurzak:mainfrom
Abhinav-0311:fix-tests-run-session-state

Conversation

@Abhinav-0311

Copy link
Copy Markdown

Fixes #863.\n\n## Summary\n- Moves the ests-run pending request id and response option state from PlayerPrefs to editor SessionState.\n- Keeps the state alive across domain reloads while preventing project/test code that calls PlayerPrefs.DeleteAll() from wiping the routing id mid-run.\n- Logs an explicit error if a completed/deferred test run cannot be routed because the request id is missing.\n\n## Testing\n- git diff --check\n- Not run: Unity editor tests / local compile because Unity and dotnet are not available in this environment.

@Abhinav-0311 Abhinav-0311 requested a review from IvanMurzak as a code owner July 11, 2026 13:54
@IvanMurzak

Copy link
Copy Markdown
Owner

Hi @Abhinav-0311, thanks for the contribution.

Could you please explain, why is it needed for?

PlayerPrefs survives domain reload already.

@Abhinav-0311

Abhinav-0311 commented Jul 12, 2026 via email

Copy link
Copy Markdown
Author

@IvanMurzak

Copy link
Copy Markdown
Owner

@Abhinav-0311 Thanks for clarifying. Now I see the problem you want to fix. Hm... this PR doesn't fix it, it only moves the problem from PlayerPrefs.DeleteAll to EditorPrefs.DeleteAll. The data still could be deleted by tests run.

@IvanMurzak IvanMurzak added the bug Something isn't working label Jul 13, 2026
@Abhinav-0311

Copy link
Copy Markdown
Author

Thanks, that makes sense as a concern. One clarification: this PR is not moving the data to EditorPrefs; it moves the tests-run routing/options from PlayerPrefs to UnityEditor.SessionState.

So PlayerPrefs.DeleteAll() and EditorPrefs.DeleteAll() should not clear these values. They are stored through SessionState.GetString/SetString, GetBool/SetBool, and GetInt/SetInt.

The specific bug I am trying to fix is the common test-fixture cleanup case:

[TearDown]
public void TearDown()
{
    PlayerPrefs.DeleteAll();
}

That can currently wipe TestCallRequestID while the test run is in flight, so RunFinished cannot notify the MCP request. SessionState keeps the existing domain-reload behavior for the deferred compile path, but separates this MCP/editor routing state from project runtime PlayerPrefs cleanup.

It is still true that editor test code could intentionally erase the package's exact SessionState keys if it references them, but that is a different class of issue from broad game-state cleanup. If you would prefer to avoid SessionState for the in-flight run as well, I can rework the PR to keep the active request/options in the registered collector instance and use SessionState only for the pre-run compile/domain-reload handoff.

@IvanMurzak

Copy link
Copy Markdown
Owner

Hey @Abhinav-0311, thanks for clarifying your vision.
It sounds right to me. Session data is a different level, and we may ignore that fact that it could be erased too. Because usually nobody uses the Session data in tests.

Please resolve merge conflicts, and I will run tests on this PR.

@IvanMurzak IvanMurzak added the enhancement New feature or request label Jul 14, 2026
# Conflicts:
#	Unity-MCP-Plugin/Packages/com.ivanmurzak.unity.mcp/Editor/Scripts/API/Tool/Tests.Run.cs
#	Unity-MCP-Plugin/Packages/com.ivanmurzak.unity.mcp/Editor/Scripts/API/Tool/Tests.cs
@Abhinav-0311 Abhinav-0311 force-pushed the fix-tests-run-session-state branch from cb1984d to 207bc1d Compare July 14, 2026 13:15
@Abhinav-0311

Copy link
Copy Markdown
Author

Resolved the merge conflicts and pushed the rebased branch.

While resolving against current main, I kept the newer active-test-run guard and moved its timestamp (ActiveTestRunStartedUtcTicksKey) to SessionState as well. That way a test calling PlayerPrefs.DeleteAll() cannot make the active run look stale and clear the in-flight request id.

Local verification:

  • git diff --check origin/main...HEAD

I did not run Unity editor tests locally from this checkout.

ClearActiveTestRun(requestId);
if (string.IsNullOrEmpty(requestId))
{
Debug.LogError("[TestRunner] Compilation failed after a deferred test run, but the pending MCP request id was missing. The error response cannot be delivered to the client.");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use a proper logging wrapper from Unity-MCP project, that would respect the logging settings in AI Game Developer window.

}
else
{
Debug.LogError("[TestRunner] Test run finished, but the pending MCP request id was missing. The test results cannot be delivered to the client.");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use a proper logging wrapper from Unity-MCP project, that would respect the logging settings in AI Game Developer window.

};
}

public sealed class SessionStateString

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a dedicated file for this new C# class

@Abhinav-0311

Copy link
Copy Markdown
Author

Addressed the requested changes in 52d09e3:

  • Replaced the two new direct Debug.LogError calls with UnityMcpPluginEditor.Instance.LogError(...), so they go through the Unity-MCP logging wrapper and respect the AI Game Developer logging settings.
  • Moved the SessionState wrappers out of TestResultCollector.cs.
  • Put each wrapper in its own file: SessionStateString.cs, SessionStateBool.cs, and SessionStateInt.cs.

Local verification:

  • git diff --cached --check
  • git diff --check

I did not run Unity editor tests locally from this checkout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tests-run hangs forever when a test calls PlayerPrefs.DeleteAll() (request id stored in PlayerPrefs)

2 participants