code.vegaprotocol.io/vega@v0.79.0/core/volumediscount/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 volumediscount_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/volumediscount" 27 "code.vegaprotocol.io/vega/core/volumediscount/mocks" 28 vegapb "code.vegaprotocol.io/vega/protos/vega" 29 30 "github.com/stretchr/testify/require" 31 ) 32 33 type vdEvents interface { 34 *events.VolumeDiscountStatsUpdated | *events.VolumeDiscountProgramStarted | *events.VolumeDiscountProgramUpdated | *events.VolumeDiscountProgramEnded 35 } 36 37 type eventMatcher[T vdEvents] struct{} 38 39 func (_ eventMatcher[T]) Matches(x any) bool { 40 _, ok := x.(T) 41 return ok 42 } 43 44 func (_ eventMatcher[T]) String() string { 45 var e T 46 return fmt.Sprintf("matches %T", e) 47 } 48 49 func endEpoch(t *testing.T, engine *volumediscount.SnapshottedEngine, seq uint64, endTime time.Time) { 50 t.Helper() 51 52 engine.OnEpoch(context.Background(), types.Epoch{ 53 Seq: seq, 54 EndTime: endTime, 55 Action: vegapb.EpochAction_EPOCH_ACTION_END, 56 }) 57 } 58 59 func startEpoch(t *testing.T, engine *volumediscount.SnapshottedEngine, seq uint64, startTime time.Time) { 60 t.Helper() 61 62 engine.OnEpoch(context.Background(), types.Epoch{ 63 Seq: seq, 64 StartTime: startTime, 65 Action: vegapb.EpochAction_EPOCH_ACTION_START, 66 }) 67 } 68 69 func expectProgramEnded(t *testing.T, broker *mocks.MockBroker, p1 *types.VolumeDiscountProgram) { 70 t.Helper() 71 72 broker.EXPECT().Send(eventMatcher[*events.VolumeDiscountProgramEnded]{}).DoAndReturn(func(evt events.Event) { 73 e := evt.(*events.VolumeDiscountProgramEnded) 74 require.Equal(t, p1.Version, e.GetVolumeDiscountProgramEnded().Version) 75 }).Times(1) 76 } 77 78 func expectStatsUpdated(t *testing.T, broker *mocks.MockBroker) { 79 t.Helper() 80 81 broker.EXPECT().Send(eventMatcher[*events.VolumeDiscountStatsUpdated]{}).Do(func(evt events.Event) { 82 _, ok := evt.(*events.VolumeDiscountStatsUpdated) 83 require.Truef(t, ok, "expecting event of type *events.VolumeDiscountStatsUpdated but got %T", evt) 84 }).Times(1) 85 } 86 87 func expectStatsUpdatedWithUnqualifiedParties(t *testing.T, broker *mocks.MockBroker) { 88 t.Helper() 89 90 broker.EXPECT().Send(eventMatcher[*events.VolumeDiscountStatsUpdated]{}).Do(func(evt events.Event) { 91 update, ok := evt.(*events.VolumeDiscountStatsUpdated) 92 require.Truef(t, ok, "expecting event of type *events.VolumeDiscountStatsUpdated but got %T", evt) 93 stats := update.VolumeDiscountStatsUpdated() 94 foundUnqualifiedParty := false 95 for _, s := range stats.Stats { 96 if s.PartyId == "p1" { 97 foundUnqualifiedParty = true 98 require.Equal(t, "", s.DiscountFactor) 99 require.Equal(t, "900", s.RunningVolume) 100 } 101 } 102 require.True(t, foundUnqualifiedParty) 103 }).Times(1) 104 } 105 106 func expectProgramStarted(t *testing.T, broker *mocks.MockBroker, p1 *types.VolumeDiscountProgram) { 107 t.Helper() 108 109 broker.EXPECT().Send(eventMatcher[*events.VolumeDiscountProgramStarted]{}).Do(func(evt events.Event) { 110 e, ok := evt.(*events.VolumeDiscountProgramStarted) 111 require.Truef(t, ok, "expecting event of type *events.VolumeDiscountProgramStarted but got %T", evt) 112 require.Equal(t, p1.IntoProto(), e.GetVolumeDiscountProgramStarted().Program) 113 }).Times(1) 114 }