Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SparrowBrain committed Sep 16, 2024
1 parent 4296c31 commit b193ce9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
21 changes: 21 additions & 0 deletions BuddySave.UnitTests/Core/ClientSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ public void StartClient_ThrowsArgumentException_WhenClientPathIsNull(
Assert.Equal("No client path provided. Cannot start a client session.", exception.Message);
}

[Theory]
[InlineAutoMoqData("")]
[InlineAutoMoqData((string)null)]
public void StartClient_ThrowsArgumentException_WhenClientArgumentsIsNull(
string arguments,
Session session,
ClientParameters clientParameters,
ClientSession sut)
{
// Arrange
clientParameters.Arguments = arguments;

// Act
var exception = Record.Exception(() => sut.StartClient(session, clientParameters));

// Assert
Assert.NotNull(exception);
Assert.IsType<ArgumentException>(exception);
Assert.Equal("No launch arguments provided for client. Cannot start a client session.", exception.Message);
}

[Theory]
[AutoMoqData]
public void StartClient_StartsClient(
Expand Down
4 changes: 4 additions & 0 deletions BuddySave.UnitTests/Core/GamingSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class GamingSessionTests
public async Task Play_StartsClient_WhenLockExists(
[Frozen] Mock<ILockManager> lockManagerMock,
[Frozen] Mock<IClientSession> clientSessionMock,
[Frozen] Mock<IServerSession> serverSessionMock,
GameSave gameSave,
Session ourSession,
Session lockSession,
Expand All @@ -31,6 +32,9 @@ public async Task Play_StartsClient_WhenLockExists(

// Assert
clientSessionMock.Verify(x => x.StartClient(lockSession, clientParameters), Times.Once());
serverSessionMock.Verify(
x => x.RunServerWithAutoSave(It.IsAny<GameSave>(), It.IsAny<Session>(), It.IsAny<ServerParameters>()),
Times.Never);
}

[Theory]
Expand Down
5 changes: 5 additions & 0 deletions BuddySave/Core/ClientSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public void StartClient(Session session, ClientParameters clientParameters)
throw new ArgumentException("No client path provided. Cannot start a client session.");
}

if(string.IsNullOrEmpty(clientParameters.Arguments))
{
throw new ArgumentException("No launch arguments provided for client. Cannot start a client session.");
}

var arguments = clientParameters.Arguments.Replace("{{Ip}}", session.Ip).Replace("{{Port}}", session.Port);
var startInfo = new ProcessStartInfo
{
Expand Down
10 changes: 6 additions & 4 deletions BuddySave/Core/GamingSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ namespace BuddySave.Core;

public class GamingSession(ILockManager lockManager, IServerSession serverSession, IClientSession clientSession) : IGamingSession
{
private const string LocalhostIp = "127.0.0.1";

public async Task Play(GameSave gameSave, Session session, ServerParameters serverParameters, ClientParameters clientParameters)
{
var lockExists = lockManager.LockExists(gameSave);
var sessionToConnectTo = lockExists ? await lockManager.GetLockedSession(gameSave) : GetLocalSession(session);

var serverTask = Task.CompletedTask;
var runServerWithAutoSave = Task.CompletedTask;
if (!lockExists)
{
serverTask = serverSession.RunServerWithAutoSave(gameSave, session, serverParameters);
runServerWithAutoSave = serverSession.RunServerWithAutoSave(gameSave, session, serverParameters);
}

clientSession.StartClient(sessionToConnectTo, clientParameters);

await serverTask;
await runServerWithAutoSave;
}

private static Session GetLocalSession(Session session)
{
return new Session(session.UserName, "127.0.0.1", session.Port);
return new Session(session.UserName, LocalhostIp, session.Port);
}
}

0 comments on commit b193ce9

Please sign in to comment.