code.vegaprotocol.io/vega@v0.79.0/core/volumerebate/helpers_for_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package volumerebate_test 17 18 import ( 19 "context" 20 "fmt" 21 "testing" 22 "time" 23 24 "code.vegaprotocol.io/vega/core/events" 25 "code.vegaprotocol.io/vega/core/types" 26 "code.vegaprotocol.io/vega/core/volumerebate" 27 "code.vegaprotocol.io/vega/core/volumerebate/mocks" 28 vegapb "code.vegaprotocol.io/vega/protos/vega" 29 30 "github.com/stretchr/testify/require" 31 ) 32 33 // event matchers for relevant events. 34 var ( 35 endedEvt = evtMatcher[events.VolumeRebateProgramEnded]{} 36 startedEvt = evtMatcher[events.VolumeRebateProgramStarted]{} 37 statsEvt = evtMatcher[events.VolumeRebateStatsUpdated]{} 38 updatedEvt = evtMatcher[events.VolumeRebateProgramUpdated]{} 39 ) 40 41 type evts interface { 42 events.VolumeRebateStatsUpdated | events.VolumeRebateProgramStarted | events.VolumeRebateProgramUpdated | events.VolumeRebateProgramEnded 43 } 44 45 type evtMatcher[T evts] struct{} 46 47 func (_ evtMatcher[T]) String() string { 48 var e *T 49 return fmt.Sprintf("matches %T", e) 50 } 51 52 func (_ evtMatcher[T]) Matches(x any) bool { 53 _, ok := x.(*T) 54 return ok 55 } 56 57 // cast uses the matcher for the type assertions in the callbacks, returns nil if the input is incompatible, using the correct matcher should make that impossible. 58 func (_ evtMatcher[T]) cast(v any) *T { 59 e, ok := v.(*T) 60 if !ok { 61 return nil 62 } 63 return e 64 } 65 66 func endEpoch(t *testing.T, engine *volumerebate.SnapshottedEngine, seq uint64, endTime time.Time) { 67 t.Helper() 68 69 engine.OnEpoch(context.Background(), types.Epoch{ 70 Seq: seq, 71 EndTime: endTime, 72 Action: vegapb.EpochAction_EPOCH_ACTION_END, 73 }) 74 } 75 76 func startEpoch(t *testing.T, engine *volumerebate.SnapshottedEngine, seq uint64, startTime time.Time) { 77 t.Helper() 78 79 engine.OnEpoch(context.Background(), types.Epoch{ 80 Seq: seq, 81 StartTime: startTime, 82 Action: vegapb.EpochAction_EPOCH_ACTION_START, 83 }) 84 } 85 86 func expectProgramEnded(t *testing.T, broker *mocks.MockBroker, p1 *types.VolumeRebateProgram) { 87 t.Helper() 88 89 broker.EXPECT().Send(endedEvt).DoAndReturn(func(evt events.Event) { 90 e := endedEvt.cast(evt) 91 require.Equal(t, p1.Version, e.GetVolumeRebateProgramEnded().Version) 92 }).Times(1) 93 } 94 95 func expectStatsUpdated(t *testing.T, broker *mocks.MockBroker) { 96 t.Helper() 97 98 broker.EXPECT().Send(statsEvt).Do(func(evt events.Event) { 99 e := statsEvt.cast(evt) 100 require.NotNil(t, e, "expecting non-nil event of type %s but got %T (nil)", statsEvt, evt) 101 }).Times(1) 102 } 103 104 func expectStatsUpdatedWithUnqualifiedParties(t *testing.T, broker *mocks.MockBroker) { 105 t.Helper() 106 107 broker.EXPECT().Send(statsEvt).Do(func(evt events.Event) { 108 update := statsEvt.cast(evt) 109 require.NotNil(t, update, "expecting event of type %s but got %T (nil)", statsEvt, evt) 110 stats := update.VolumeRebateStatsUpdated() 111 foundUnqualifiedParty := false 112 for _, s := range stats.Stats { 113 if s.PartyId == "p1" { 114 foundUnqualifiedParty = true 115 require.Equal(t, "0", s.AdditionalRebate) 116 require.Equal(t, "900", s.MakerFeesReceived) 117 } 118 } 119 require.True(t, foundUnqualifiedParty) 120 }).Times(1) 121 } 122 123 func expectProgramStarted(t *testing.T, broker *mocks.MockBroker, p1 *types.VolumeRebateProgram) { 124 t.Helper() 125 126 broker.EXPECT().Send(startedEvt).Do(func(evt events.Event) { 127 e := startedEvt.cast(evt) 128 require.NotNil(t, e, "expecting event of type %s but got %T (nil)", startedEvt, evt) 129 require.Equal(t, p1.IntoProto(), e.GetVolumeRebateProgramStarted().Program) 130 }).Times(1) 131 }