Skip to content

Commit

Permalink
Added SessionManagementTests
Browse files Browse the repository at this point in the history
  • Loading branch information
BellringerQuinn committed Dec 1, 2023
1 parent 706aa9d commit b832326
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 6 deletions.
95 changes: 95 additions & 0 deletions Assets/SequenceSDK/WaaS/Tests/SessionManagementTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Threading.Tasks;
using Sequence.WaaS.Authentication;

namespace Sequence.WaaS.Tests
{
public class SessionManagementTests
{
private WaaSWallet _wallet;

public SessionManagementTests(WaaSWallet wallet)
{
_wallet = wallet;
}

public async Task TestSessionManagement()
{
try
{
WaaSTestHarness.TestStarted?.Invoke();
WaaSSession[] sessions = await TestListSessions();
if (sessions == null)
{
return;
}

await TestDropInactiveSession(sessions);

await _wallet.DropThisSession();
}
catch (Exception e)
{
WaaSTestHarness.TestFailed?.Invoke(new WaaSTestFailed(nameof(TestSessionManagement), e.Message));
return;
}

try
{
WaaSSession[] sessions = await _wallet.ListSessions();
WaaSTestHarness.TestFailed?.Invoke(new WaaSTestFailed(nameof(TestSessionManagement), "Expected an exception. Session was likely not dropped."));
}
catch (Exception e)
{
WaaSTestHarness.TestPassed?.Invoke();
}
}

private async Task<WaaSSession[]> TestListSessions()
{
try
{
WaaSSession[] sessions = await _wallet.ListSessions();
if (sessions == null || sessions.Length == 0)
{
throw new Exception("No sessions found.");
}

return sessions;
}
catch (Exception e)
{
WaaSTestHarness.TestFailed?.Invoke(new WaaSTestFailed(nameof(TestListSessions),
e.Message));
return null;
}
}

private async Task TestDropInactiveSession(WaaSSession[] sessions)
{
try
{
int sessionCount = sessions.Length;
for (int i = 0; i < sessionCount; i++)
{
if (sessions[i].id != _wallet.SessionId)
{
await _wallet.DropSession(sessions[i].id);
break;
}
}

if (sessionCount > 1)
{
sessions = await TestListSessions();
int newSessionCount = sessions.Length;
CustomAssert.IsTrue(newSessionCount == sessionCount - 1, nameof(TestSessionManagement));
}
}
catch(Exception e)
{
WaaSTestHarness.TestFailed?.Invoke(new WaaSTestFailed(nameof(TestDropInactiveSession), e.Message));
}
}
}
}
3 changes: 3 additions & 0 deletions Assets/SequenceSDK/WaaS/Tests/SessionManagementTests.cs.meta

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

20 changes: 15 additions & 5 deletions Assets/SequenceSDK/WaaS/Tests/WaaSTestHarness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ private void InitiateTests(WaaSWallet wallet)
TestStarted += () => _testsStarted++;
WaaSWalletTests walletTests = new WaaSWalletTests(wallet);
wallet.OnSendTransactionFailed += OnFailedTransaction;
RunTests(walletTests);
SessionManagementTests sessionManagementTests = new SessionManagementTests(wallet);
RunTests(walletTests, sessionManagementTests);
}

private void OnFailedTransaction(FailedTransactionReturn result)
{
Debug.LogError("Transaction failed: " + result.error);
}

private async Task RunTests(WaaSWalletTests walletTests)
private async Task RunTests(WaaSWalletTests walletTests, SessionManagementTests sessionManagementTests)
{
walletTests.TestMessageSigning("Hello world", Chain.Polygon);
walletTests.TestTransfer();
Expand All @@ -80,13 +81,21 @@ private async Task RunTests(WaaSWalletTests walletTests)
walletTests.TestDelayedEncode(ERC20.Abi);
walletTests.TestSendBatchTransaction_withDelayedEncode("transfer(address,uint256)");
walletTests.TestSendBatchTransaction_withDelayedEncode(ERC20.Abi);
await WaitForTestsToComplete();

sessionManagementTests.TestSessionManagement();
await WaitForTestsToComplete();

Debug.LogError($"Tests run: {_testsStarted} | Tests passed: {_passedTests} | Tests failed: {_failedTests.Count}");
}

private async Task WaitForTestsToComplete()
{
await Task.Delay(100);
while (_testsStarted > _failedTests.Count + _passedTests)
{
await Task.Delay(100);
}

Debug.LogError($"Tests run: {_testsStarted} | Tests passed: {_passedTests} | Tests failed: {_failedTests.Count}");
}
}

Expand All @@ -100,11 +109,12 @@ public WaaSTestFailed(string name, string reason, params object[] args)
{
Name = name;
Args = args;
Reason = reason;
}

public override string ToString()
{
return $"Test {Name} failed with args: {string.Join(", ", Args)}";
return $"Test {Name} failed with args: {string.Join(", ", Args)} | reason: {Reason}";
}
}
}
4 changes: 3 additions & 1 deletion Assets/SequenceSDK/WaaS/WaaSWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Sequence.WaaS
{
public class WaaSWallet : IWallet
{
public string SessionId { get; private set; }
private Address _address;
private HttpClient _httpClient;
private IntentSender _intentSender;
Expand All @@ -19,6 +20,7 @@ public WaaSWallet(Address address, string sessionId, EthWallet sessionWallet, Da
_address = address;
_httpClient = new HttpClient("https://d14tu8valot5m0.cloudfront.net/rpc/WaasWallet");
_intentSender = new IntentSender(new HttpClient(WaaSLogin.WaaSWithAuthUrl), awsDataKey, sessionWallet, sessionId, waasProjectId, waasVersion);
SessionId = sessionId;
}

public Task<CreatePartnerReturn> CreatePartner(CreatePartnerArgs args, Dictionary<string, string> headers = null)
Expand Down Expand Up @@ -136,7 +138,7 @@ public async Task<bool> DropSession(string dropSessionId)

public Task<bool> DropThisSession()
{
return DropSession(_intentSender.SessionId);
return DropSession(SessionId);
}

public event Action<WaaSSession[]> OnSessionsFound;
Expand Down

0 comments on commit b832326

Please sign in to comment.