using System.IO; using System.Reflection; using NUnit.Framework; using UnityEditor; public sealed class XWorldJsonPrefabAutoConvertTests { private const string JsonPrefKey = "XWorld.UI.AutoConvertJsonOnFocus"; private const string HtmlPrefKey = "XWorld.UI.AutoConvertHtmlOnFocus"; [TearDown] public void TearDown() { EditorPrefs.DeleteKey(JsonPrefKey); EditorPrefs.DeleteKey(HtmlPrefKey); } [Test] public void JsonAutoConvertDefaultsToEnabledIndependentlyOfHtmlPreference() { EditorPrefs.DeleteKey(JsonPrefKey); EditorPrefs.SetBool(HtmlPrefKey, false); Assert.That(ReadJsonAutoConvertEnabled(), Is.True); } private static bool ReadJsonAutoConvertEnabled() { MethodInfo method = typeof(XWorldUtil).GetMethod( "IsAutoConvertJsonOnFocusEnabled", BindingFlags.Static | BindingFlags.NonPublic); Assert.That(method, Is.Not.Null); return (bool)method.Invoke(null, null); } } public sealed class XWorldLocalServerBuildTests { [Test] public void LocalServerMenuUsesGatewayProjectInsteadOfPrebuiltDll() { string gatewayProjectPath = InvokeStringPath("GetMiniGameGatewayRunnerProjectPath"); Assert.That(gatewayProjectPath.Replace('\\', '/'), Does.EndWith("/Server/Gateway.Runner/Gateway.Runner.csproj")); Assert.That(File.Exists(gatewayProjectPath), Is.True); Assert.That(XWorldUtil.CanOpenLocalServer(), Is.True); } [Test] public void CdnRunnerBuildMenuUsesCdnProject() { string cdnProjectPath = InvokeStringPath("GetMiniGameCdnRunnerProjectPath"); Assert.That(cdnProjectPath.Replace('\\', '/'), Does.EndWith("/Server/Cdn.Runner/Cdn.Runner.csproj")); Assert.That(File.Exists(cdnProjectPath), Is.True); } [Test] public void LocalServerBuildIsSkippedWhenGatewayIsAlreadyRunning() { Assert.That(InvokeShouldBuildLocalServer(false), Is.True); Assert.That(InvokeShouldBuildLocalServer(true), Is.False); } [Test] public void DotnetRunnerLaunchableRequiresRuntimeConfigNextToDll() { string tempDir = Path.Combine(Path.GetTempPath(), "XWorldRunnerTest-" + System.Guid.NewGuid().ToString("N")); Directory.CreateDirectory(tempDir); try { string runnerPath = Path.Combine(tempDir, "Example.Runner.dll"); File.WriteAllText(runnerPath, string.Empty); Assert.That(InvokeLaunchable(runnerPath), Is.False); File.WriteAllText(Path.Combine(tempDir, "Example.Runner.runtimeconfig.json"), "{}"); Assert.That(InvokeLaunchable(runnerPath), Is.True); } finally { Directory.Delete(tempDir, true); } } private static string InvokeStringPath(string methodName) { MethodInfo method = typeof(XWorldUtil).GetMethod( methodName, BindingFlags.Static | BindingFlags.NonPublic); Assert.That(method, Is.Not.Null); return (string)method.Invoke(null, null); } private static bool InvokeLaunchable(string runnerPath) { MethodInfo method = typeof(XWorldUtil).GetMethod( "IsDotnetRunnerLaunchable", BindingFlags.Static | BindingFlags.NonPublic); Assert.That(method, Is.Not.Null); return (bool)method.Invoke(null, new object[] { runnerPath }); } private static bool InvokeShouldBuildLocalServer(bool isGatewayRunning) { MethodInfo method = typeof(XWorldUtil).GetMethod( "ShouldBuildMiniGameLocalServer", BindingFlags.Static | BindingFlags.NonPublic); Assert.That(method, Is.Not.Null); return (bool)method.Invoke(null, new object[] { isGatewayRunning }); } }