This repository was archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add periodic dungeon conquest rewards tests
- Loading branch information
Showing
2 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
backend/app/Savor22b.Tests/Action/PeriodicDungeonRewardActionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
) | ||
); | ||
} | ||
} |