-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,179 additions
and
364 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package staker | ||
|
||
import ( | ||
"testing" | ||
|
||
u256 "gno.land/p/gnoswap/uint256" | ||
) | ||
|
||
func TestRemoveInRangePosition(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
poolPath string | ||
tokenId uint64 | ||
setup func() *InternalEmissionReward | ||
verify func(*InternalEmissionReward) | ||
}{ | ||
{ | ||
name: "Normal position removal", | ||
poolPath: "test/pool/path", | ||
tokenId: 1, | ||
setup: func() *InternalEmissionReward { | ||
r := NewInternalEmissionReward() | ||
recipientsMap := NewRewardRecipientMap() | ||
poolLiquidity := NewPoolLiquidity() | ||
|
||
// Initial liquidity setup | ||
inRangeLiquidity := NewInRangeLiquidity() | ||
inRangeLiquidity.SetLiquidity(u256.NewUint(1000)) | ||
poolLiquidity.AddInRangePosition(1, inRangeLiquidity) | ||
|
||
recipientsMap.SetPoolLiquidity("test/pool/path", poolLiquidity) | ||
r.SetRewardRecipientsMap(recipientsMap) | ||
return r | ||
}, | ||
verify: func(r *InternalEmissionReward) { | ||
recipientsMap := r.GetRewardRecipientsMap() | ||
poolLiquidity := recipientsMap.GetPoolLiquidity("test/pool/path") | ||
|
||
// Check if position is removed | ||
position := poolLiquidity.GetInRangeLiquidity(1) | ||
if !position.GetLiquidity().IsZero() { | ||
t.Errorf("Liquidity should be 0, but it is %s", position.GetLiquidity().ToString()) | ||
} | ||
if !position.GetLiquidityRatio().IsZero() { | ||
t.Errorf("Liquidity ratio should be 0, but it is %s", position.GetLiquidityRatio().ToString()) | ||
} | ||
if position.GetStakedHeight() != 0 { | ||
t.Errorf("Staking height should be 0, but it is %d", position.GetStakedHeight()) | ||
} | ||
}, | ||
}, | ||
{ | ||
name: "Non-existent pool path", | ||
poolPath: "non/existent/path", | ||
tokenId: 1, | ||
setup: func() *InternalEmissionReward { | ||
return NewInternalEmissionReward() | ||
}, | ||
verify: func(r *InternalEmissionReward) { | ||
// No Error | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := tt.setup() | ||
r.RemoveInRangePosition(tt.poolPath, tt.tokenId) | ||
tt.verify(r) | ||
}) | ||
} | ||
} | ||
|
||
func TestSelectRewardPools(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
pools map[string]InternalTier | ||
setup func() *InternalEmissionReward | ||
verify func(*InternalEmissionReward) | ||
}{ | ||
{ | ||
name: "Normal pool selection", | ||
pools: map[string]InternalTier{ | ||
"pool/1": {tier: TIER1_INDEX}, | ||
"pool/2": {tier: TIER2_INDEX}, | ||
"pool/3": {tier: TIER3_INDEX}, | ||
"pool/4": {tier: 4}, // Invalid tier | ||
}, | ||
setup: func() *InternalEmissionReward { | ||
return NewInternalEmissionReward() | ||
}, | ||
verify: func(r *InternalEmissionReward) { | ||
rewardPool := r.GetRewardPoolsMap() | ||
|
||
// TIER1 pool verification | ||
pool1 := rewardPool.GetRewardPoolByPoolPath("pool/1") | ||
if pool1.GetTier() != TIER1_INDEX { | ||
t.Errorf("pool/1 should be TIER1, but got %d", pool1.GetTier()) | ||
} | ||
|
||
// TIER2 pool verification | ||
pool2 := rewardPool.GetRewardPoolByPoolPath("pool/2") | ||
if pool2.GetTier() != TIER2_INDEX { | ||
t.Errorf("pool/2 should be TIER2, but got %d", pool2.GetTier()) | ||
} | ||
|
||
// TIER3 pool verification | ||
pool3 := rewardPool.GetRewardPoolByPoolPath("pool/3") | ||
if pool3.GetTier() != TIER3_INDEX { | ||
t.Errorf("pool/3 should be TIER3, but got %d", pool3.GetTier()) | ||
} | ||
|
||
// Invalid tier pool verification | ||
pool4 := rewardPool.GetRewardPoolByPoolPath("pool/4") | ||
if pool4.GetTier() != 0 { | ||
t.Errorf("pool/4 should not be selected, but got tier %d", pool4.GetTier()) | ||
} | ||
}, | ||
}, | ||
{ | ||
name: "Empty pool map", | ||
pools: map[string]InternalTier{}, | ||
setup: func() *InternalEmissionReward { | ||
return NewInternalEmissionReward() | ||
}, | ||
verify: func(r *InternalEmissionReward) { | ||
rewardPool := r.GetRewardPoolsMap() | ||
if len(rewardPool.GetRewardPools()) != 0 { | ||
t.Errorf("reward pools should be empty, but got %d pools", len(rewardPool.GetRewardPools())) | ||
} | ||
}, | ||
}, | ||
{ | ||
name: "Invalid tier only", | ||
pools: map[string]InternalTier{ | ||
"pool/1": {tier: 4}, | ||
"pool/2": {tier: 5}, | ||
}, | ||
setup: func() *InternalEmissionReward { | ||
return NewInternalEmissionReward() | ||
}, | ||
verify: func(r *InternalEmissionReward) { | ||
rewardPool := r.GetRewardPoolsMap() | ||
if len(rewardPool.GetRewardPools()) != 0 { | ||
t.Errorf("no pools should be selected, but got %d pools", len(rewardPool.GetRewardPools())) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := tt.setup() | ||
r.SelectRewardPools(tt.pools) | ||
tt.verify(r) | ||
}) | ||
} | ||
} |
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,191 @@ | ||
package staker | ||
|
||
import ( | ||
"testing" | ||
|
||
u256 "gno.land/p/gnoswap/uint256" | ||
) | ||
|
||
func TestRewardManager_Internal(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
setup func() *RewardManager | ||
testFunc func(*RewardManager) | ||
verify func(*RewardManager) bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "create new RewardManager instance", | ||
setup: func() *RewardManager { | ||
return NewRewardManager() | ||
}, | ||
verify: func(rm *RewardManager) bool { | ||
return rm.internalReward != nil && rm.externalReward != nil | ||
}, | ||
}, | ||
{ | ||
name: "set and get InternalEmissionReward", | ||
setup: func() *RewardManager { | ||
return NewRewardManager() | ||
}, | ||
testFunc: func(rm *RewardManager) { | ||
newInternalReward := NewInternalEmissionReward() | ||
recipientsMap := NewRewardRecipientMap() | ||
poolLiquidity := NewPoolLiquidity() | ||
inRangeLiquidity := NewInRangeLiquidity() | ||
inRangeLiquidity.SetLiquidity(u256.NewUint(1000)) | ||
poolLiquidity.AddInRangePosition(1, inRangeLiquidity) | ||
recipientsMap.SetPoolLiquidity("test/pool", poolLiquidity) | ||
newInternalReward.SetRewardRecipientsMap(recipientsMap) | ||
|
||
rm.SetInternalEmissionReward(newInternalReward) | ||
}, | ||
verify: func(rm *RewardManager) bool { | ||
internalReward := rm.GetInternalEmissionReward() | ||
if internalReward == nil { | ||
return false | ||
} | ||
|
||
recipientsMap := internalReward.GetRewardRecipientsMap() | ||
if recipientsMap == nil { | ||
return false | ||
} | ||
|
||
poolLiquidity := recipientsMap.GetPoolLiquidity("test/pool") | ||
if poolLiquidity == nil { | ||
return false | ||
} | ||
|
||
position := poolLiquidity.GetInRangeLiquidity(1) | ||
if position == nil { | ||
return false | ||
} | ||
|
||
return position.GetLiquidity().Eq(u256.NewUint(1000)) | ||
}, | ||
}, | ||
{ | ||
name: "set nil InternalEmissionReward", | ||
setup: func() *RewardManager { | ||
return NewRewardManager() | ||
}, | ||
testFunc: func(rm *RewardManager) { | ||
rm.SetInternalEmissionReward(nil) | ||
}, | ||
verify: func(rm *RewardManager) bool { | ||
return rm.GetInternalEmissionReward() == nil | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rm := tt.setup() | ||
|
||
if tt.testFunc != nil { | ||
tt.testFunc(rm) | ||
} | ||
|
||
if result := tt.verify(rm); !result { | ||
t.Errorf("%s: verification failed", tt.name) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRewardManager_External(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
setup func() *RewardManager | ||
testFunc func(*RewardManager) | ||
verify func(*RewardManager) bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "create new RewardManager instance for external reward", | ||
setup: func() *RewardManager { | ||
return NewRewardManager() | ||
}, | ||
verify: func(rm *RewardManager) bool { | ||
return rm.externalReward != nil | ||
}, | ||
}, | ||
{ | ||
name: "set and get ExternalIncentiveReward", | ||
setup: func() *RewardManager { | ||
return NewRewardManager() | ||
}, | ||
testFunc: func(rm *RewardManager) { | ||
newExternalReward := NewExternalIncentiveReward() | ||
externalCalculator := NewExternalCalculator(100) | ||
newExternalReward.SetExternalCalculator(externalCalculator) | ||
|
||
rm.SetExternalIncentiveReward(newExternalReward) | ||
}, | ||
verify: func(rm *RewardManager) bool { | ||
externalReward := rm.GetExternalIncentiveReward() | ||
if externalReward == nil { | ||
return false | ||
} | ||
|
||
calculator := externalReward.GetExternalCalculator() | ||
if calculator == nil { | ||
return false | ||
} | ||
|
||
return true | ||
}, | ||
}, | ||
{ | ||
name: "set nil ExternalIncentiveReward", | ||
setup: func() *RewardManager { | ||
return NewRewardManager() | ||
}, | ||
testFunc: func(rm *RewardManager) { | ||
rm.SetExternalIncentiveReward(nil) | ||
}, | ||
verify: func(rm *RewardManager) bool { | ||
return rm.GetExternalIncentiveReward() == nil | ||
}, | ||
}, | ||
{ | ||
name: "get or create ExternalCalculator", | ||
setup: func() *RewardManager { | ||
rm := NewRewardManager() | ||
newExternalReward := NewExternalIncentiveReward() | ||
rm.SetExternalIncentiveReward(newExternalReward) | ||
return rm | ||
}, | ||
testFunc: func(rm *RewardManager) { | ||
externalReward := rm.GetExternalIncentiveReward() | ||
calculator := externalReward.GetOrCreateExternalCalculator(200) // 높이 200으로 설정 | ||
if calculator == nil { | ||
t.Error("calculator should not be nil") | ||
} | ||
}, | ||
verify: func(rm *RewardManager) bool { | ||
externalReward := rm.GetExternalIncentiveReward() | ||
if externalReward == nil { | ||
return false | ||
} | ||
|
||
calculator := externalReward.GetExternalCalculator() | ||
return calculator != nil | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rm := tt.setup() | ||
|
||
if tt.testFunc != nil { | ||
tt.testFunc(rm) | ||
} | ||
|
||
if result := tt.verify(rm); !result { | ||
t.Errorf("%s: verification failed", tt.name) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.