Repository navigation
fix: store HTTP URLs per project so editors don't overwrite each other #1514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sssooonnnggg
wants to merge
2
commits into
CoplayDev:beta
Choose a base branch
from
sssooonnnggg:feat/project-scoped-http-url
base: beta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
TestProjects/UnityMCPTests/Assets/Tests/EditMode/Helpers/HttpEndpointProjectScopeTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| using System.Collections.Generic; | ||
| using MCPForUnity.Editor.Constants; | ||
| using MCPForUnity.Editor.Helpers; | ||
| using MCPForUnity.Editor.Services; | ||
| using NUnit.Framework; | ||
| using UnityEditor; | ||
|
|
||
| namespace MCPForUnityTests.Editor.Helpers | ||
| { | ||
| /// <summary> | ||
| /// The HTTP Local / HTTP Remote URLs are stored per project so two editors on the same | ||
| /// machine can use different servers without overwriting each other's URL. | ||
| /// </summary> | ||
| public class HttpEndpointProjectScopeTests | ||
| { | ||
| private const string OtherProjectHash = "0123456789abcdef"; | ||
|
|
||
| private static string LocalKey => ProjectIdentityUtility.GetProjectScopedKey(EditorPrefKeys.HttpBaseUrl); | ||
| private static string RemoteKey => ProjectIdentityUtility.GetProjectScopedKey(EditorPrefKeys.HttpRemoteBaseUrl); | ||
| private static string OtherProjectLocalKey => $"{EditorPrefKeys.HttpBaseUrl}_{OtherProjectHash}"; | ||
|
|
||
| private readonly Dictionary<string, string> _savedPrefs = new Dictionary<string, string>(); | ||
|
|
||
| private static IEnumerable<string> TouchedKeys() | ||
| { | ||
| yield return EditorPrefKeys.HttpBaseUrl; | ||
| yield return EditorPrefKeys.HttpRemoteBaseUrl; | ||
| yield return LocalKey; | ||
| yield return RemoteKey; | ||
| yield return OtherProjectLocalKey; | ||
| } | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| _savedPrefs.Clear(); | ||
| foreach (string key in TouchedKeys()) | ||
| { | ||
| _savedPrefs[key] = EditorPrefs.HasKey(key) ? EditorPrefs.GetString(key) : null; | ||
| EditorPrefs.DeleteKey(key); | ||
| } | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown() | ||
| { | ||
| foreach (var pair in _savedPrefs) | ||
| { | ||
| if (pair.Value == null) | ||
| EditorPrefs.DeleteKey(pair.Key); | ||
| else | ||
| EditorPrefs.SetString(pair.Key, pair.Value); | ||
| } | ||
| EditorConfigurationCache.Instance.Refresh(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ProjectScopedKey_IsBaseKeyPlusProjectHash() | ||
| { | ||
| Assert.AreEqual( | ||
| $"{EditorPrefKeys.HttpBaseUrl}_{ProjectIdentityUtility.GetProjectHash()}", | ||
| LocalKey); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetLocalBaseUrl_NothingStored_ReturnsDefault() | ||
| { | ||
| Assert.AreEqual("http://127.0.0.1:8080", HttpEndpointUtility.GetLocalBaseUrl()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetLocalBaseUrl_NoProjectValue_FallsBackToMachineWideValue() | ||
| { | ||
| EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9001"); | ||
|
|
||
| Assert.AreEqual("http://127.0.0.1:9001", HttpEndpointUtility.GetLocalBaseUrl()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void SaveLocalBaseUrl_WritesProjectKeyOnly() | ||
| { | ||
| EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9001"); | ||
|
|
||
| HttpEndpointUtility.SaveLocalBaseUrl("http://127.0.0.1:9002"); | ||
|
|
||
| Assert.AreEqual("http://127.0.0.1:9002", EditorPrefs.GetString(LocalKey)); | ||
| Assert.AreEqual("http://127.0.0.1:9001", EditorPrefs.GetString(EditorPrefKeys.HttpBaseUrl), | ||
| "Machine-wide value must stay as-is for projects that never saved their own URL"); | ||
| Assert.AreEqual("http://127.0.0.1:9002", HttpEndpointUtility.GetLocalBaseUrl()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetLocalBaseUrl_IgnoresOtherProjectsValue() | ||
| { | ||
| EditorPrefs.SetString(OtherProjectLocalKey, "http://127.0.0.1:9003"); | ||
|
|
||
| Assert.AreEqual("http://127.0.0.1:8080", HttpEndpointUtility.GetLocalBaseUrl()); | ||
|
|
||
| HttpEndpointUtility.SaveLocalBaseUrl("http://127.0.0.1:9002"); | ||
|
|
||
| Assert.AreEqual("http://127.0.0.1:9002", HttpEndpointUtility.GetLocalBaseUrl()); | ||
| Assert.AreEqual("http://127.0.0.1:9003", EditorPrefs.GetString(OtherProjectLocalKey), | ||
| "Saving in this project must not change another project's URL"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void SaveRemoteBaseUrl_WritesProjectKeyOnly() | ||
| { | ||
| EditorPrefs.SetString(EditorPrefKeys.HttpRemoteBaseUrl, "https://a.example.com"); | ||
|
|
||
| Assert.AreEqual("https://a.example.com", HttpEndpointUtility.GetRemoteBaseUrl()); | ||
|
|
||
| HttpEndpointUtility.SaveRemoteBaseUrl("b.example.com"); | ||
|
|
||
| Assert.AreEqual("https://b.example.com", HttpEndpointUtility.GetRemoteBaseUrl()); | ||
| Assert.AreEqual("https://a.example.com", EditorPrefs.GetString(EditorPrefKeys.HttpRemoteBaseUrl)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void SaveRemoteBaseUrl_Empty_ClearsThisProjectWithoutFallingBack() | ||
| { | ||
| EditorPrefs.SetString(EditorPrefKeys.HttpRemoteBaseUrl, "https://a.example.com"); | ||
|
|
||
| HttpEndpointUtility.SaveRemoteBaseUrl(string.Empty); | ||
|
|
||
| Assert.AreEqual(string.Empty, HttpEndpointUtility.GetRemoteBaseUrl()); | ||
| Assert.AreEqual("https://a.example.com", EditorPrefs.GetString(EditorPrefKeys.HttpRemoteBaseUrl)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConfigurationCache_ReadsAndWritesProjectValue() | ||
| { | ||
| EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9001"); | ||
| HttpEndpointUtility.SaveLocalBaseUrl("http://127.0.0.1:9002"); | ||
| EditorConfigurationCache.Instance.Refresh(); | ||
|
|
||
| Assert.AreEqual("http://127.0.0.1:9002", EditorConfigurationCache.Instance.HttpBaseUrl); | ||
|
|
||
| EditorConfigurationCache.Instance.SetHttpRemoteBaseUrl("https://c.example.com"); | ||
|
|
||
| Assert.AreEqual("https://c.example.com", EditorPrefs.GetString(RemoteKey)); | ||
| Assert.IsFalse(EditorPrefs.HasKey(EditorPrefKeys.HttpRemoteBaseUrl)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConfigurationCache_SetSameValueAsFallback_PinsItToProject() | ||
| { | ||
| EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9001"); | ||
| EditorConfigurationCache.Instance.Refresh(); | ||
| Assert.AreEqual("http://127.0.0.1:9001", EditorConfigurationCache.Instance.HttpBaseUrl); | ||
|
|
||
| EditorConfigurationCache.Instance.SetHttpBaseUrl("http://127.0.0.1:9001"); | ||
| EditorPrefs.SetString(EditorPrefKeys.HttpBaseUrl, "http://127.0.0.1:9005"); | ||
|
|
||
| Assert.AreEqual("http://127.0.0.1:9001", EditorPrefs.GetString(LocalKey)); | ||
| Assert.AreEqual("http://127.0.0.1:9001", HttpEndpointUtility.GetLocalBaseUrl(), | ||
| "A URL set for this project must not follow later machine-wide changes"); | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...rojects/UnityMCPTests/Assets/Tests/EditMode/Helpers/HttpEndpointProjectScopeTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.