-
Notifications
You must be signed in to change notification settings - Fork 13
/
content_test.go
307 lines (301 loc) · 6.56 KB
/
content_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package brutalinks
import (
"git.sr.ht/~mariusor/brutalinks/internal/config"
"net/url"
"reflect"
"testing"
)
func Test_replaceTags(t *testing.T) {
type args struct {
cur Item
}
tests := []struct {
name string
args args
want string
}{
{
name: "mention-but-not-in-url",
args: args{
Item{
MimeType: "text/markdown",
Data: `https://todo.sr.ht/~marius/go-activitypub
~marius`,
Metadata: &ItemMetadata{
Mentions: TagCollection{
Tag{
Type: TagMention,
Name: "marius",
URL: "https://brutalinks.git/~marius",
},
},
},
},
},
want: `https://todo.sr.ht/~marius/go-activitypub
<a href="https://brutalinks.git/~marius" rel="mention">marius</a>`,
},
{
name: "mention-and-tag",
args: args{
Item{
MimeType: "text/markdown",
Data: `some #tag
~marius`,
Metadata: &ItemMetadata{
Mentions: TagCollection{
Tag{
Type: TagMention,
Name: "marius",
URL: "https://brutalinks.git/~marius",
},
},
Tags: TagCollection{
Tag{
Type: TagTag,
Name: "tag",
URL: "https://brutalinks.git/t/tag",
},
},
},
},
},
want: `some <a href="https://brutalinks.git/t/tag" rel="tag">tag</a>
<a href="https://brutalinks.git/~marius" rel="mention">marius</a>`,
},
{
name: "tag",
args: args{
Item{
MimeType: "text/markdown",
Data: `some #tag-`,
Metadata: &ItemMetadata{
Tags: TagCollection{
Tag{
Type: TagTag,
Name: "tag",
URL: "https://brutalinks.git/t/tag",
},
},
},
},
},
want: `some <a href="https://brutalinks.git/t/tag" rel="tag">tag</a>-`,
},
}
Instance = new(Application)
brut, _ := url.ParseRequestURI("https://brutalinks.git")
Instance.BaseURL = *brut
Instance.Conf = &config.Configuration{HostName: "brutalinks.git"}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := replaceTags(tt.args.cur.MimeType, tt.args.cur); got != tt.want {
t.Errorf("replaceTags() =\n%v, want\n%v", got, tt.want)
}
})
}
}
func Test_loadTags(t *testing.T) {
tests := []struct {
name string
data string
wantTags TagCollection
wantMentions TagCollection
}{
{
name: "a-tag",
data: "a #tag",
wantTags: TagCollection{
Tag{
Type: TagTag,
Name: "tag",
URL: "/t/tag",
},
},
wantMentions: TagCollection{},
},
{
name: "an-at-mention",
data: "a @mention",
wantTags: TagCollection{},
wantMentions: TagCollection{
Tag{
Type: TagMention,
Name: "mention",
URL: "/~mention",
},
},
},
{
name: "an-at-with-instance-mention",
data: "a @[email protected]",
wantTags: TagCollection{},
wantMentions: TagCollection{
Tag{
Type: TagMention,
Name: "mention",
URL: "https://brutalinks.git/@mention",
},
},
},
{
name: "a-tilde-mention",
data: "another ~mention",
wantTags: TagCollection{},
wantMentions: TagCollection{
Tag{
Type: TagMention,
Name: "mention",
URL: "/~mention",
},
},
},
{
name: "a-tilde-with-instance-mention",
data: "another [email protected]",
wantTags: TagCollection{},
wantMentions: TagCollection{
Tag{
Type: TagMention,
Name: "mention",
URL: "https://brutalinks.git/~mention",
},
},
},
{
name: "a-tag-with-dash",
data: "a #dashed-tag",
wantTags: TagCollection{
Tag{
Type: TagTag,
Name: "dashed-tag",
URL: "/t/dashed-tag",
},
},
wantMentions: TagCollection{},
},
{
name: "a-tag-with-underscore",
data: "a #underscored_tag",
wantTags: TagCollection{
Tag{
Type: TagTag,
Name: "underscored_tag",
URL: "/t/underscored_tag",
},
},
wantMentions: TagCollection{},
},
{
name: "an-UPPERCASE-tag",
data: "a #UPPERTAG",
wantTags: TagCollection{
Tag{
Type: TagTag,
Name: "UPPERTAG",
URL: "/t/UPPERTAG",
},
},
wantMentions: TagCollection{},
},
{
name: "multiple-times-same-tag",
data: "a #tag is just a #tag",
wantTags: TagCollection{
Tag{
Type: TagTag,
Name: "tag",
URL: "/t/tag",
},
},
wantMentions: TagCollection{},
},
{
name: "more-than-six-tag",
data: "a #tag is just a #tag, and #sometimes it's #not. #hello #june",
wantTags: TagCollection{
Tag{
Type: TagTag,
Name: "tag",
URL: "/t/tag",
},
Tag{
Type: TagTag,
Name: "sometimes",
URL: "/t/sometimes",
},
Tag{
Type: TagTag,
Name: "not",
URL: "/t/not",
},
Tag{
Type: TagTag,
Name: "hello",
URL: "/t/hello",
},
Tag{
Type: TagTag,
Name: "june",
URL: "/t/june",
},
},
wantMentions: TagCollection{},
},
{
name: "release-notes-202006",
data: `
In this release I added the capability for users to block other users.
It is not fully up [to spec](https://www.w3.org/TR/activitypub/#block-activity-outbox), as I'm not entirely
sure how to "prevent the blocked user from interacting with any object posted by the actor".
Most of the time, however I spent working on the go-ap packages, because this feature served
as the start point for a large detour in the storage layer of #fedbox, the #activitypub service that
constitutes the backend of littr.me, which resulted in the capability of storing "private" activities in actors'
outboxes.
This is a very important step forward in removing the need for a custom "activities" collection for #fedbox.
#updates #june #new-release`,
wantTags: TagCollection{
Tag{
Type: TagTag,
Name: "fedbox",
URL: "/t/fedbox",
},
Tag{
Type: TagTag,
Name: "activitypub",
URL: "/t/activitypub",
},
Tag{
Type: TagTag,
Name: "updates",
URL: "/t/updates",
},
Tag{
Type: TagTag,
Name: "june",
URL: "/t/june",
},
Tag{
Type: TagTag,
Name: "new-release",
URL: "/t/new-release",
},
},
wantMentions: TagCollection{},
},
}
// TODO(marius): stop relying on global state
Instance.BaseURL = url.URL{}
Instance.Conf = nil
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tags, mentions := loadTags(tt.data)
if !reflect.DeepEqual(tags, tt.wantTags) {
t.Errorf("loadTags() got tags = %v, want %v", tags, tt.wantTags)
}
if !reflect.DeepEqual(mentions, tt.wantMentions) {
t.Errorf("loadTags() got mentions = %v, want %v", mentions, tt.wantMentions)
}
})
}
}