Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Immutable.Passport.Runtime.Tests")]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Immutable.Passport.Helpers;
using Immutable.Passport.Model;
using UnityEngine;

namespace Immutable.Passport.Core
{
/// <summary>
/// Handles serialization of outgoing requests and deserialization/validation
/// of incoming responses for the game bridge.
/// </summary>
internal static class BrowserMessageCodec
{
/// <summary>
/// Serializes a request into a JavaScript function call string.
/// </summary>
internal static string BuildJsCall(BrowserRequest request)
{
var escapedJson = JsonUtility.ToJson(request).Replace("\\", "\\\\").Replace("\"", "\\\"");
return $"callFunction(\"{escapedJson}\")";
}

/// <summary>
/// Deserializes and validates a raw game bridge response message.
/// </summary>
internal static BrowserResponse ParseAndValidateResponse(string message)
{
var response = message.OptDeserializeObject<BrowserResponse>();

if (response == null || string.IsNullOrEmpty(response.responseFor) || string.IsNullOrEmpty(response.requestId))
{
throw new PassportException("Response from game bridge is incorrect. Check game bridge file.");
}

return response;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Immutable.Passport.Core.Logging;
using Immutable.Passport.Model;

namespace Immutable.Passport.Core
{
/// <summary>
/// Maps game bridge error responses into typed Passport exceptions.
/// </summary>
internal static class BrowserResponseErrorMapper
{
private const string TAG = "[Browser Response Error Mapper]";

/// <summary>
/// Converts a failed BrowserResponse into the appropriate PassportException.
/// </summary>
internal static PassportException MapToException(BrowserResponse response)
{
try
{
if (!string.IsNullOrEmpty(response.error) && !string.IsNullOrEmpty(response.errorType))
{
var type = (PassportErrorType)Enum.Parse(typeof(PassportErrorType), response.errorType);
return new PassportException(response.error, type);
}

return new PassportException(!string.IsNullOrEmpty(response.error) ? response.error : "Unknown error");
}
catch (Exception ex)
{
PassportLogger.Error($"{TAG} Parse passport type error: {ex.Message}");
}

return new PassportException(response.error ?? "Failed to parse error");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using Cysharp.Threading.Tasks;

namespace Immutable.Passport.Core
{
/// <summary>
/// Tracks in-flight game bridge requests by mapping request IDs to their completion sources.
/// </summary>
internal class PendingRequestRegistry
{
private readonly Dictionary<string, UniTaskCompletionSource<string>> _requests = new Dictionary<string, UniTaskCompletionSource<string>>();

/// <summary>
/// Registers a new pending request and returns its completion source.
/// </summary>
internal UniTaskCompletionSource<string> Register(string requestId)
{
var completion = new UniTaskCompletionSource<string>();
_requests.Add(requestId, completion);
return completion;
}

/// <summary>
/// Returns true if a pending request exists for the given ID.
/// </summary>
internal bool Contains(string requestId)
{
return _requests.ContainsKey(requestId);
}

/// <summary>
/// Retrieves the completion source for a pending request.
/// </summary>
internal UniTaskCompletionSource<string> Get(string requestId)
{
return _requests[requestId];
}

/// <summary>
/// Removes a completed request from the registry.
/// </summary>
internal void Remove(string requestId)
{
_requests.Remove(requestId);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Immutable.Browser.Core;
using Immutable.Passport.Model;
using UnityEngine;
using UnityEngine.TestTools;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Immutable.Passport.Helpers;
Expand Down Expand Up @@ -78,7 +79,7 @@ public async Task CallAndResponse_Failed_NoRequestId()
}

Assert.NotNull(e);
Assert.IsTrue(e.Message.Contains("Response from browser is incorrect") == true);
Assert.IsTrue(e.Message.Contains("Response from game bridge is incorrect") == true);
}

[Test]
Expand Down Expand Up @@ -149,6 +150,91 @@ public void CallAndResponse_Success_BrowserReady()

Assert.True(onReadyCalled);
}

[Test]
public void SetCallTimeout_DoesNotThrow()
{
Assert.DoesNotThrow(() => manager.SetCallTimeout(5000));
}

[Test]
public void LaunchAuthURL_ForwardsUrlAndRedirectUri()
{
manager.LaunchAuthURL("https://auth.example.com", "myapp://callback");

Assert.AreEqual("https://auth.example.com", mockClient.lastLaunchedUrl);
Assert.AreEqual("myapp://callback", mockClient.lastLaunchedRedirectUri);
}

[Test]
public async Task CallAndResponse_Failed_ErrorFieldSet_SuccessTrue_ThrowsException()
{
// success=true but an error field is present — should still be treated as failure
mockClient.browserResponse = new BrowserResponse
{
responseFor = FUNCTION_NAME,
error = ERROR,
success = true
};

PassportException e = null;
try
{
await manager.Call(FUNCTION_NAME);
}
catch (PassportException ex)
{
e = ex;
}

Assert.NotNull(e);
Assert.AreEqual(ERROR, e.Message);
}

[Test]
public void HandleResponse_UnknownRequestId_Throws()
{
// A well-formed response whose requestId was never registered via Call()
var response = new BrowserResponse
{
responseFor = FUNCTION_NAME,
requestId = "unknown-request-id",
success = true
};

LogAssert.Expect(LogType.Error, new Regex("No TaskCompletionSource for request id"));

var ex = Assert.Throws<PassportException>(
() => mockClient.InvokeUnityPostMessage(JsonUtility.ToJson(response))
);

Assert.IsTrue(ex.Message.Contains("No TaskCompletionSource for request id"));
}

[Test]
public async Task CallAndResponse_Failed_ClientError_NoErrorField()
{
// success=false but no error or errorType - should get "Unknown error"
mockClient.browserResponse = new BrowserResponse
{
responseFor = FUNCTION_NAME,
success = false
};

PassportException e = null;
try
{
await manager.Call(FUNCTION_NAME);
}
catch (PassportException ex)
{
e = ex;
}

Assert.NotNull(e);
Assert.Null(e.Type);
Assert.AreEqual("Unknown error", e.Message);
}
}

internal class MockBrowserClient : IWebBrowserClient
Expand Down Expand Up @@ -200,9 +286,13 @@ private string Between(string value, string a, string b)
return value.Substring(adjustedPosA, posB - adjustedPosA);
}

public string lastLaunchedUrl;
public string lastLaunchedRedirectUri;

public void LaunchAuthURL(string url, string redirectUri)
{
throw new NotImplementedException();
lastLaunchedUrl = url;
lastLaunchedRedirectUri = redirectUri;
}

public void Dispose()
Expand Down
Loading
Loading