Debian Codegraph @

pkg/services/chat/telegram/telegram_generator_test.go

raw
1 package telegram_test
2
3 import (
4 "fmt"
5 "io"
6 "strings"
7
8 "github.com/jarcoal/httpmock"
9 "github.com/mattn/go-colorable"
10 "github.com/onsi/ginkgo/v2"
11 "github.com/onsi/gomega"
12 "github.com/onsi/gomega/gbytes"
13
14 "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/telegram"
15 )
16
17 const (
18 mockToken = `0:MockToken`
19 mockAPIBase = "https://api.telegram.org/bot" + mockToken + "/"
20 )
21
22 var (
23 userOut *gbytes.Buffer
24 userIn *gbytes.Buffer
25 userInMono io.Writer
26 )
27
28 var _ = ginkgo.Describe("TelegramGenerator", func() {
29 ginkgo.BeforeEach(func() {
30 userOut = gbytes.NewBuffer()
31 userIn = gbytes.NewBuffer()
32 userInMono = colorable.NewNonColorable(userIn)
33
34 httpmock.Activate()
35 })
36 ginkgo.AfterEach(func() {
37 httpmock.DeactivateAndReset()
38 })
39 ginkgo.It("should return the ", func() {
40 gen := telegram.Generator{
41 Reader: userOut,
42 Writer: userInMono,
43 }
44
45 resultChannel := make(chan string, 1)
46
47 httpmock.RegisterResponder(
48 "GET",
49 mockAPI(`getMe`),
50 httpmock.NewJsonResponderOrPanic(200, &struct {
51 OK bool
52 Result *telegram.User
53 }{
54 true, &telegram.User{
55 ID: 1,
56 IsBot: true,
57 Username: "mockbot",
58 },
59 }),
60 )
61
62 httpmock.RegisterResponder(
63 "POST",
64 mockAPI(`getUpdates`),
65 httpmock.NewJsonResponderOrPanic(200, &struct {
66 OK bool
67 Result []telegram.Update
68 }{
69 true,
70 []telegram.Update{
71 {
72 ChatMemberUpdate: &telegram.ChatMemberUpdate{
73 Chat: &telegram.Chat{Type: `channel`, Title: `mockChannel`},
74 OldChatMember: &telegram.ChatMember{Status: `kicked`},
75 NewChatMember: &telegram.ChatMember{Status: `administrator`},
76 },
77 },
78 {
79 Message: &telegram.Message{
80 Text: "hi!",
81 From: &telegram.User{Username: `mockUser`},
82 Chat: &telegram.Chat{Type: `private`, ID: 667, Username: `mockUser`},
83 },
84 },
85 },
86 }),
87 )
88
89 go func() {
90 defer ginkgo.GinkgoRecover()
91
92 conf, err := gen.Generate(nil, nil, nil)
93
94 gomega.Expect(conf).ToNot(gomega.BeNil())
95 gomega.Expect(err).NotTo(gomega.HaveOccurred())
96
97 resultChannel <- conf.GetURL().String()
98 }()
99
100 defer dumpBuffers()
101
102 mockTyped(mockToken)
103 mockTyped(`no`)
104
105 gomega.Eventually(userIn).
106 Should(gbytes.Say(`Got a bot chat member update for mockChannel, status was changed from kicked to administrator`))
107 gomega.Eventually(userIn).
108 Should(gbytes.Say(`Got 1 chat ID\(s\) so far\. Want to add some more\?`))
109 gomega.Eventually(userIn).Should(gbytes.Say(`Selected chats:`))
110 gomega.Eventually(userIn).Should(gbytes.Say(`667 \(private\) @mockUser`))
111
112 gomega.Eventually(resultChannel).
113 Should(gomega.Receive(gomega.Equal(`telegram://0:MockToken@telegram?chats=667&preview=No`)))
114 })
115 })
116
117 func mockAPI(endpoint string) string {
118 return mockAPIBase + endpoint
119 }
120
121 func mockTyped(a ...any) {
122 _, _ = fmt.Fprintf(userOut, "%v\n", fmt.Sprint(a...))
123 }
124
125 func dumpBuffers() {
126 for line := range strings.SplitSeq(string(userIn.Contents()), "\n") {
127 _, _ = fmt.Fprintf(ginkgo.GinkgoWriter, "> %s\n", line)
128 }
129
130 for line := range strings.SplitSeq(string(userOut.Contents()), "\n") {
131 _, _ = fmt.Fprintf(ginkgo.GinkgoWriter, "< %s\n", line)
132 }
133 }