Skip to content

Commit 0b8dc72

Browse files
committed
test(chat): cover text option reuse
1 parent 5592f9f commit 0b8dc72

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

‎chat_test.go‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,61 @@ import (
99
"net/url"
1010
"reflect"
1111
"regexp"
12+
"sync"
1213
"testing"
1314

1415
"github.com/stretchr/testify/require"
1516
)
1617

18+
func TestMsgOptionTextReuse(t *testing.T) {
19+
tests := []struct {
20+
name string
21+
text string
22+
escape bool
23+
want string
24+
}{
25+
{name: "escape special characters", text: "A & B < C > D", escape: true, want: "A &amp; B &lt; C &gt; D"},
26+
{name: "keep literal text", text: "A & B < C > D", escape: false, want: "A & B < C > D"},
27+
{name: "plain text", text: "hello", escape: true, want: "hello"},
28+
{name: "empty text", text: "", escape: true, want: ""},
29+
{name: "escape existing entity once", text: "&amp;", escape: true, want: "&amp;amp;"},
30+
}
31+
32+
for _, test := range tests {
33+
t.Run(test.name, func(t *testing.T) {
34+
option := MsgOptionText(test.text, test.escape)
35+
for i := range 3 {
36+
config, err := applyMsgOptions("testing-token", "CXXX", "https://example.com/", option)
37+
require.NoError(t, err)
38+
require.Equal(t, []string{test.want}, config.values["text"], "use %d", i+1)
39+
}
40+
})
41+
}
42+
}
43+
44+
func TestMsgOptionTextConcurrentReuse(t *testing.T) {
45+
option := MsgOptionText("A & B < C > D", true)
46+
const workers = 32
47+
configs := make([]sendConfig, workers)
48+
errors := make([]error, workers)
49+
start := make(chan struct{})
50+
var wg sync.WaitGroup
51+
52+
for i := range workers {
53+
wg.Go(func() {
54+
<-start
55+
configs[i], errors[i] = applyMsgOptions("testing-token", "CXXX", "https://example.com/", option)
56+
})
57+
}
58+
close(start)
59+
wg.Wait()
60+
61+
for i, config := range configs {
62+
require.NoError(t, errors[i], "worker %d", i)
63+
require.Equal(t, []string{"A &amp; B &lt; C &gt; D"}, config.values["text"], "worker %d", i)
64+
}
65+
}
66+
1767
func postMessageInvalidChannelHandler(rw http.ResponseWriter, r *http.Request) {
1868
rw.Header().Set("Content-Type", "application/json")
1969
response, _ := json.Marshal(chatResponseFull{

0 commit comments

Comments
 (0)