Debian Codegraph @

pkg/services/chat/zulip/zulip_payload_test.go

raw
1 package zulip
2
3 import (
4 "github.com/onsi/gomega"
5
6 ginkgo "github.com/onsi/ginkgo/v2"
7 )
8
9 var _ = ginkgo.Describe("CreatePayload", func() {
10 ginkgo.It("should create payload with type set to channel by default", func() {
11 cfg := &Config{
12 Stream: "general",
13 }
14
15 payload := CreatePayload(cfg, "test message")
16
17 gomega.Expect(payload.Get("type")).To(gomega.Equal("channel"))
18 })
19
20 ginkgo.It("should set the to field from config stream for channel", func() {
21 cfg := &Config{
22 Stream: "general",
23 }
24
25 payload := CreatePayload(cfg, "test message")
26
27 gomega.Expect(payload.Get("to")).To(gomega.Equal("general"))
28 })
29
30 ginkgo.It("should set the content field from the message", func() {
31 cfg := &Config{
32 Stream: "general",
33 }
34
35 payload := CreatePayload(cfg, "Hello, World!")
36
37 gomega.Expect(payload.Get("content")).To(gomega.Equal("Hello, World!"))
38 })
39
40 ginkgo.It("should include topic when config topic is set", func() {
41 cfg := &Config{
42 Stream: "general",
43 Topic: "announcements",
44 }
45
46 payload := CreatePayload(cfg, "test message")
47
48 gomega.Expect(payload.Get("topic")).To(gomega.Equal("announcements"))
49 })
50
51 ginkgo.It("should omit topic when config topic is empty", func() {
52 cfg := &Config{
53 Stream: "general",
54 }
55
56 payload := CreatePayload(cfg, "test message")
57
58 gomega.Expect(payload.Get("topic")).To(gomega.BeEmpty())
59 })
60
61 ginkgo.It("should handle empty stream name", func() {
62 cfg := &Config{}
63
64 payload := CreatePayload(cfg, "test message")
65
66 gomega.Expect(payload.Get("type")).To(gomega.Equal("channel"))
67 gomega.Expect(payload.Get("to")).To(gomega.BeEmpty())
68 gomega.Expect(payload.Get("content")).To(gomega.Equal("test message"))
69 })
70
71 ginkgo.It("should handle empty message", func() {
72 cfg := &Config{
73 Stream: "general",
74 }
75
76 payload := CreatePayload(cfg, "")
77
78 gomega.Expect(payload.Get("content")).To(gomega.BeEmpty())
79 })
80
81 ginkgo.It("should produce url.Values that can be encoded", func() {
82 cfg := &Config{
83 Stream: "general",
84 Topic: "announcements",
85 }
86
87 payload := CreatePayload(cfg, "test message")
88
89 gomega.Expect(payload.Encode()).To(gomega.ContainSubstring("type=channel"))
90 gomega.Expect(payload.Encode()).To(gomega.ContainSubstring("to=general"))
91 gomega.Expect(payload.Encode()).To(gomega.ContainSubstring("content=test+message"))
92 gomega.Expect(payload.Encode()).To(gomega.ContainSubstring("topic=announcements"))
93 })
94
95 ginkgo.It("should use type=direct and JSON array for to when direct message", func() {
96 cfg := &Config{
97 Type: MessageTypeDirect,
98 To: "[email protected],[email protected]",
99 }
100
101 payload := CreatePayload(cfg, "test message")
102
103 gomega.Expect(payload.Get("type")).To(gomega.Equal("direct"))
104 gomega.Expect(payload.Get("to")).To(gomega.Equal(`["[email protected]","[email protected]"]`))
105 gomega.Expect(payload.Get("content")).To(gomega.Equal("test message"))
106 })
107
108 ginkgo.It("should fall back to stream for direct message recipients if to is empty", func() {
109 cfg := &Config{
110 Type: MessageTypeDirect,
111 Stream: "[email protected]",
112 }
113
114 payload := CreatePayload(cfg, "test message")
115
116 gomega.Expect(payload.Get("type")).To(gomega.Equal("direct"))
117 gomega.Expect(payload.Get("to")).To(gomega.Equal(`["[email protected]"]`))
118 })
119
120 ginkgo.It("should omit topic for direct messages even when set in config", func() {
121 cfg := &Config{
122 Type: MessageTypeDirect,
123 To: "[email protected]",
124 Topic: "should be omitted",
125 }
126
127 payload := CreatePayload(cfg, "test message")
128
129 gomega.Expect(payload.Get("topic")).To(gomega.BeEmpty())
130 })
131
132 ginkgo.It("should include read_by_sender when true", func() {
133 cfg := &Config{
134 Stream: "general",
135 ReadBySender: true,
136 }
137
138 payload := CreatePayload(cfg, "test message")
139
140 gomega.Expect(payload.Get("read_by_sender")).To(gomega.Equal("true"))
141 })
142
143 ginkgo.It("should not include read_by_sender when false", func() {
144 cfg := &Config{
145 Stream: "general",
146 ReadBySender: false,
147 }
148
149 payload := CreatePayload(cfg, "test message")
150
151 gomega.Expect(payload.Get("read_by_sender")).To(gomega.BeEmpty())
152 })
153
154 ginkgo.It("should use explicit channel type", func() {
155 cfg := &Config{
156 Type: MessageTypeChannel,
157 Stream: "foo",
158 }
159
160 payload := CreatePayload(cfg, "test message")
161
162 gomega.Expect(payload.Get("type")).To(gomega.Equal("channel"))
163 gomega.Expect(payload.Get("to")).To(gomega.Equal("foo"))
164 })
165
166 ginkgo.It("should serialize numeric recipient IDs as JSON strings for direct messages", func() {
167 cfg := &Config{
168 Type: MessageTypeDirect,
169 To: "123,456",
170 }
171
172 payload := CreatePayload(cfg, "test message")
173
174 gomega.Expect(payload.Get("type")).To(gomega.Equal("direct"))
175 gomega.Expect(payload.Get("to")).To(gomega.Equal(`["123","456"]`))
176 })
177
178 ginkgo.It("should fall back to stream for numeric recipient IDs when to is empty", func() {
179 cfg := &Config{
180 Type: MessageTypeDirect,
181 Stream: "789",
182 }
183
184 payload := CreatePayload(cfg, "test message")
185
186 gomega.Expect(payload.Get("type")).To(gomega.Equal("direct"))
187 gomega.Expect(payload.Get("to")).To(gomega.Equal(`["789"]`))
188 })
189 })
190
191 var _ = ginkgo.Describe("ValidateMessageType", func() {
192 ginkgo.It("should accept channel type", func() {
193 gomega.Expect(ValidateMessageType(MessageTypeChannel)).To(gomega.Succeed())
194 })
195
196 ginkgo.It("should accept direct type", func() {
197 gomega.Expect(ValidateMessageType(MessageTypeDirect)).To(gomega.Succeed())
198 })
199
200 ginkgo.It("should accept empty type", func() {
201 gomega.Expect(ValidateMessageType(MessageType(""))).To(gomega.Succeed())
202 })
203
204 ginkgo.It("should reject invalid type", func() {
205 err := ValidateMessageType(MessageType("invalid"))
206 gomega.Expect(err).To(gomega.HaveOccurred())
207 gomega.Expect(err.Error()).To(gomega.ContainSubstring("invalid message type"))
208 })
209 })