|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gotd/td/tg" |
| 8 | +) |
| 9 | + |
| 10 | +func TestConvertGiftToStars(t *testing.T) { |
| 11 | + inv := newMockInvoker() |
| 12 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.BoolBox{Bool: &tg.BoolTrue{}}) |
| 13 | + |
| 14 | + err := newMockBot(inv).ConvertGiftToStars(context.Background(), "bc1", OwnedGiftFromMessage(123)) |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("ConvertGiftToStars: %v", err) |
| 17 | + } |
| 18 | + |
| 19 | + wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.PaymentsConvertStarGiftRequest{}} |
| 20 | + |
| 21 | + inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper) |
| 22 | + |
| 23 | + if wrapper.ConnectionID != "bc1" { |
| 24 | + t.Fatalf("connection id = %q", wrapper.ConnectionID) |
| 25 | + } |
| 26 | + |
| 27 | + req, ok := wrapper.Query.(*tg.PaymentsConvertStarGiftRequest) |
| 28 | + if !ok { |
| 29 | + t.Fatalf("query = %#v", wrapper.Query) |
| 30 | + } |
| 31 | + |
| 32 | + gift, ok := req.Stargift.(*tg.InputSavedStarGiftUser) |
| 33 | + if !ok || gift.MsgID != 123 { |
| 34 | + t.Fatalf("stargift = %#v", req.Stargift) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestUpgradeGift(t *testing.T) { |
| 39 | + inv := newMockInvoker() |
| 40 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.UpdatesBox{Updates: &tg.Updates{}}) |
| 41 | + |
| 42 | + err := newMockBot(inv).UpgradeGift(context.Background(), "bc1", OwnedGiftFromMessage(7), true) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("UpgradeGift: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.PaymentsUpgradeStarGiftRequest{}} |
| 48 | + |
| 49 | + inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper) |
| 50 | + |
| 51 | + req, ok := wrapper.Query.(*tg.PaymentsUpgradeStarGiftRequest) |
| 52 | + if !ok { |
| 53 | + t.Fatalf("query = %#v", wrapper.Query) |
| 54 | + } |
| 55 | + |
| 56 | + if !req.KeepOriginalDetails { |
| 57 | + t.Fatalf("keep original details not set") |
| 58 | + } |
| 59 | + |
| 60 | + if g, ok := req.Stargift.(*tg.InputSavedStarGiftUser); !ok || g.MsgID != 7 { |
| 61 | + t.Fatalf("stargift = %#v", req.Stargift) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestTransferGift(t *testing.T) { |
| 66 | + inv := newMockInvoker() |
| 67 | + inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, &tg.UpdatesBox{Updates: &tg.Updates{}}) |
| 68 | + |
| 69 | + err := newMockBot(inv).TransferGift(context.Background(), "bc1", OwnedGiftFromSlug("rare-gift"), userRef(99, 5)) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("TransferGift: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.PaymentsTransferStarGiftRequest{}} |
| 75 | + |
| 76 | + inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper) |
| 77 | + |
| 78 | + req, ok := wrapper.Query.(*tg.PaymentsTransferStarGiftRequest) |
| 79 | + if !ok { |
| 80 | + t.Fatalf("query = %#v", wrapper.Query) |
| 81 | + } |
| 82 | + |
| 83 | + if g, ok := req.Stargift.(*tg.InputSavedStarGiftSlug); !ok || g.Slug != "rare-gift" { |
| 84 | + t.Fatalf("stargift = %#v", req.Stargift) |
| 85 | + } |
| 86 | + |
| 87 | + if _, ok := req.ToID.(*tg.InputPeerUser); !ok { |
| 88 | + t.Fatalf("to id = %#v, want user", req.ToID) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestConvertGiftInvalidID(t *testing.T) { |
| 93 | + inv := newMockInvoker() |
| 94 | + |
| 95 | + err := newMockBot(inv).ConvertGiftToStars(context.Background(), "bc1", "bogus") |
| 96 | + if err == nil { |
| 97 | + t.Fatal("expected error for invalid owned_gift_id") |
| 98 | + } |
| 99 | + |
| 100 | + if inv.count() != 0 { |
| 101 | + t.Fatalf("made %d RPC calls, want 0", inv.count()) |
| 102 | + } |
| 103 | +} |
0 commit comments