|
| 1 | +package blockchainapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/blockchainapi/structs" |
| 8 | + "github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/lib" |
| 9 | +) |
| 10 | + |
| 11 | +func TestComputeSessionTokenAmount(t *testing.T) { |
| 12 | + makeBid := func(pricePerSecond int64) *structs.Bid { |
| 13 | + return &structs.Bid{ |
| 14 | + PricePerSecond: &lib.BigInt{Int: *big.NewInt(pricePerSecond)}, |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + bid *structs.Bid |
| 21 | + duration *big.Int |
| 22 | + supply *big.Int |
| 23 | + budget *big.Int |
| 24 | + directPayment bool |
| 25 | + want *big.Int |
| 26 | + wantErr bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "direct payment", |
| 30 | + bid: makeBid(100), |
| 31 | + duration: big.NewInt(3600), |
| 32 | + supply: big.NewInt(1_000_000), |
| 33 | + budget: big.NewInt(50_000), |
| 34 | + directPayment: true, |
| 35 | + want: big.NewInt(360_000), |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "staked", |
| 39 | + bid: makeBid(100), |
| 40 | + duration: big.NewInt(3600), |
| 41 | + supply: big.NewInt(1_000_000), |
| 42 | + budget: big.NewInt(50_000), |
| 43 | + directPayment: false, |
| 44 | + want: big.NewInt(7_200_000), |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "nil bid", |
| 48 | + bid: nil, |
| 49 | + duration: big.NewInt(3600), |
| 50 | + supply: big.NewInt(1_000_000), |
| 51 | + budget: big.NewInt(50_000), |
| 52 | + directPayment: false, |
| 53 | + wantErr: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "zero budget", |
| 57 | + bid: makeBid(100), |
| 58 | + duration: big.NewInt(3600), |
| 59 | + supply: big.NewInt(1_000_000), |
| 60 | + budget: big.NewInt(0), |
| 61 | + directPayment: false, |
| 62 | + wantErr: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "nil budget", |
| 66 | + bid: makeBid(100), |
| 67 | + duration: big.NewInt(3600), |
| 68 | + supply: big.NewInt(1_000_000), |
| 69 | + budget: nil, |
| 70 | + directPayment: false, |
| 71 | + wantErr: true, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, tt := range tests { |
| 76 | + t.Run(tt.name, func(t *testing.T) { |
| 77 | + got, err := computeSessionTokenAmount(tt.bid, tt.duration, tt.supply, tt.budget, tt.directPayment) |
| 78 | + if tt.wantErr { |
| 79 | + if err == nil { |
| 80 | + t.Fatalf("expected error, got result %s", got) |
| 81 | + } |
| 82 | + return |
| 83 | + } |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("unexpected error: %v", err) |
| 86 | + } |
| 87 | + if got.Cmp(tt.want) != 0 { |
| 88 | + t.Errorf("got %s, want %s", got, tt.want) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments