|
| 1 | +package staker |
| 2 | + |
| 3 | +import ( |
| 4 | + "std" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gno.land/p/demo/avl" |
| 8 | + "gno.land/p/demo/testutils" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDelegate(t *testing.T) { |
| 12 | + t.Skip("Must running separately. because this test depends on TestUndelegate's state") |
| 13 | + std.TestSetOrigCaller(testAddr1) |
| 14 | + resetState() |
| 15 | + |
| 16 | + addr1 := testAddr1 |
| 17 | + |
| 18 | + t.Run("success - first delegation", func(t *testing.T) { |
| 19 | + delegate(addr1, 100) |
| 20 | + |
| 21 | + if totalDelegated != 100 { |
| 22 | + t.Errorf("expected totalDelegated 100, got %d", totalDelegated) |
| 23 | + } |
| 24 | + |
| 25 | + value, exists := delegatorAmount.Get(addr1.String()) |
| 26 | + if !exists || value.(uint64) != 100 { |
| 27 | + t.Error("delegator amount not updated correctly") |
| 28 | + } |
| 29 | + |
| 30 | + innerTree, exists := delegatedFromTo.Get(addr1.String()) |
| 31 | + if !exists { |
| 32 | + t.Error("delegatedFromTo not updated") |
| 33 | + } |
| 34 | + |
| 35 | + inner := innerTree.(*avl.Tree) |
| 36 | + delegatedAmount, exists := inner.Get(addr1.String()) |
| 37 | + if !exists { |
| 38 | + t.Error("delegatedFromTo amount incorrect") |
| 39 | + } |
| 40 | + |
| 41 | + if delegatedAmount.(uint64) != 100 { |
| 42 | + t.Error("delegatedFromTo amount incorrect") |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("success - additional delegation", func(t *testing.T) { |
| 47 | + resetState() |
| 48 | + delegate(addr1, 100) |
| 49 | + delegate(addr1, 50) |
| 50 | + |
| 51 | + if totalDelegated != 150 { |
| 52 | + t.Errorf("expected totalDelegated 150, got %d", totalDelegated) |
| 53 | + } |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func TestUndelegate(t *testing.T) { |
| 58 | + t.Skip("Must running separately. because this test depends on TestUndelegate's state") |
| 59 | + addr1 := testAddr1 |
| 60 | + std.TestSetOrigCaller(addr1) |
| 61 | + resetState() |
| 62 | + |
| 63 | + t.Run("fail - no delegation", func(t *testing.T) { |
| 64 | + defer func() { |
| 65 | + if r := recover(); r == nil { |
| 66 | + t.Error("expected panic for no delegation") |
| 67 | + } |
| 68 | + }() |
| 69 | + undelegate(addr1, 100) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("fail - insufficient amount", func(t *testing.T) { |
| 73 | + delegate(addr1, 50) |
| 74 | + |
| 75 | + defer func() { |
| 76 | + if r := recover(); r == nil { |
| 77 | + t.Error("expected panic for insufficient amount") |
| 78 | + } |
| 79 | + }() |
| 80 | + undelegate(addr1, 100) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("success - partial undelegate", func(t *testing.T) { |
| 84 | + resetState() |
| 85 | + delegate(addr1, 100) |
| 86 | + undelegate(addr1, 30) |
| 87 | + |
| 88 | + if totalDelegated != 70 { |
| 89 | + t.Errorf("expected totalDelegated 70, got %d", totalDelegated) |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("success - full undelegate", func(t *testing.T) { |
| 94 | + resetState() |
| 95 | + delegate(addr1, 100) |
| 96 | + undelegate(addr1, 100) |
| 97 | + |
| 98 | + if totalDelegated != 0 { |
| 99 | + t.Errorf("expected totalDelegated 0, got %d", totalDelegated) |
| 100 | + } |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +func resetState() { |
| 105 | + totalDelegated = 0 |
| 106 | + delegatorAmount = avl.NewTree() |
| 107 | + delegatedFromTo = avl.NewTree() |
| 108 | + delegatedTo = avl.NewTree() |
| 109 | + delegationHistory = avl.NewTree() |
| 110 | + delegationSnapShotHistory = avl.NewTree() |
| 111 | +} |
0 commit comments