Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit

Permalink
test: Add periodic dungeon conquest rewards tests
Browse files Browse the repository at this point in the history
  • Loading branch information
upa-r-upa committed Apr 15, 2024
1 parent dbd58c3 commit 0fe5351
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 2 deletions.
12 changes: 10 additions & 2 deletions backend/app/Savor22b.Tests/Action/ExplorationDungeonActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public void Execute_Success_AlreadyUsedKey()
IAccountStateDelta beforeState = new DummyState();
var state = new RootState(
new InventoryState(),
new UserDungeonState(dungeonHistories, ImmutableList<DungeonConquestHistoryState>.Empty)
new UserDungeonState(
dungeonHistories,
ImmutableList<DungeonConquestHistoryState>.Empty,
ImmutableList<DungeonConquestPeriodicRewardHistoryState>.Empty
)
);
beforeState = beforeState.SetState(SignerAddress(), state.Serialize());

Expand Down Expand Up @@ -123,7 +127,11 @@ public void Execute_Fail_NotHaveRequiredDungeonKey()
IAccountStateDelta beforeState = new DummyState();
var state = new RootState(
new InventoryState(),
new UserDungeonState(dungeonHistories, ImmutableList<DungeonConquestHistoryState>.Empty)
new UserDungeonState(
dungeonHistories,
ImmutableList<DungeonConquestHistoryState>.Empty,
ImmutableList<DungeonConquestPeriodicRewardHistoryState>.Empty
)
);
beforeState = beforeState.SetState(SignerAddress(), state.Serialize());

Expand Down
165 changes: 165 additions & 0 deletions backend/app/Savor22b.Tests/Action/PeriodicDungeonRewardActionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
namespace Savor22b.Tests.Action;

using System;
using System.Collections.Immutable;
using Libplanet;
using Libplanet.State;
using Savor22b.Action;
using Savor22b.Action.Exceptions;
using Savor22b.Constants;
using Savor22b.States;
using Xunit;

public class PeriodicDungeonRewardActionTests : ActionTests
{
public static int TestDungeonId = 0;

public PeriodicDungeonRewardActionTests() { }

[Fact]
public void Execute_Success_Normal()
{
long currentBlock = 402;

IAccountStateDelta beforeState = new DummyState();
var state = new RootState(
new InventoryState(),
new UserDungeonState(
ImmutableList.Create(
new DungeonHistoryState(1, TestDungeonId, 1, ImmutableList<int>.Empty)
),
ImmutableList.Create(new DungeonConquestHistoryState(1, TestDungeonId, 1)),
ImmutableList.Create(
new DungeonConquestPeriodicRewardHistoryState(
105,
TestDungeonId,
ImmutableList.Create(1, 2, 3)
),
new DungeonConquestPeriodicRewardHistoryState(
205,
TestDungeonId,
ImmutableList.Create(1, 2, 3)
)
)
)
);

GlobalDungeonState globalDungeonState = new GlobalDungeonState(
new Dictionary<string, Address> { [TestDungeonId.ToString()] = SignerAddress() }
);

beforeState = beforeState
.SetState(SignerAddress(), state.Serialize())
.SetState(Addresses.DungeonDataAddress, globalDungeonState.Serialize());

PeriodicDungeonRewardAction action = new PeriodicDungeonRewardAction(TestDungeonId);

IAccountStateDelta afterState = action.Execute(
new DummyActionContext
{
PreviousStates = beforeState,
Signer = SignerAddress(),
Random = random,
Rehearsal = false,
BlockIndex = currentBlock,
}
);

Bencodex.Types.IValue? afterRootStateEncoded = afterState.GetState(SignerAddress());
RootState afterRootState = afterRootStateEncoded is Bencodex.Types.Dictionary bdict
? new RootState(bdict)
: throw new Exception();
UserDungeonState userDungeonState = afterRootState.UserDungeonState;

Assert.Equal(
0,
userDungeonState.PresentDungeonPeriodicRewardCount(TestDungeonId, 1, currentBlock)
);
Assert.Equal(4, userDungeonState.DungeonConquestPeriodicRewardHistories.Count);
Assert.Equal(5 * 2, afterRootState.InventoryState.SeedStateList.Count);
}

[Fact]
public void Execute_Fail_NotConquest()
{
IAccountStateDelta beforeState = new DummyState();
var state = new RootState(
new InventoryState(),
new UserDungeonState(
ImmutableList.Create(new DungeonHistoryState(1, 2, 1, ImmutableList<int>.Empty)),
ImmutableList.Create(new DungeonConquestHistoryState(1, 2, 1)),
ImmutableList<DungeonConquestPeriodicRewardHistoryState>.Empty
)
);
beforeState = beforeState.SetState(SignerAddress(), state.Serialize());

PeriodicDungeonRewardAction action = new PeriodicDungeonRewardAction(TestDungeonId);

Assert.Throws<PermissionDeniedException>(
() =>
action.Execute(
new DummyActionContext
{
PreviousStates = beforeState,
Signer = SignerAddress(),
Random = random,
Rehearsal = false,
BlockIndex = 200,
}
)
);
}

[Fact]
public void Execute_Fail_AlreadyReceive()
{
long currentBlock = 299;

IAccountStateDelta beforeState = new DummyState();
var state = new RootState(
new InventoryState(),
new UserDungeonState(
ImmutableList.Create(
new DungeonHistoryState(1, TestDungeonId, 1, ImmutableList<int>.Empty)
),
ImmutableList.Create(new DungeonConquestHistoryState(1, TestDungeonId, 1)),
ImmutableList.Create(
new DungeonConquestPeriodicRewardHistoryState(
105,
TestDungeonId,
ImmutableList.Create(1, 2, 3)
),
new DungeonConquestPeriodicRewardHistoryState(
205,
TestDungeonId,
ImmutableList.Create(1, 2, 3)
)
)
)
);

GlobalDungeonState globalDungeonState = new GlobalDungeonState(
new Dictionary<string, Address> { [TestDungeonId.ToString()] = SignerAddress() }
);

beforeState = beforeState
.SetState(SignerAddress(), state.Serialize())
.SetState(Addresses.DungeonDataAddress, globalDungeonState.Serialize());

PeriodicDungeonRewardAction action = new PeriodicDungeonRewardAction(TestDungeonId);

Assert.Throws<ResourceIsNotReadyException>(
() =>
action.Execute(
new DummyActionContext
{
PreviousStates = beforeState,
Signer = SignerAddress(),
Random = random,
Rehearsal = false,
BlockIndex = currentBlock,
}
)
);
}
}

0 comments on commit 0fe5351

Please sign in to comment.