-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
328 lines (292 loc) · 9.49 KB
/
main_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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"net/url"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestUnmarshal_RDF(t *testing.T) {
byt, err := os.ReadFile("test-data/slashdotMain.xml")
require.Nil(t, err)
f, err := unmarshal(byt)
require.Nil(t, err)
require.Equal(t, "Slashdot", f.Title)
require.Equal(t, "https://slashdot.org/", f.Link)
require.Len(t, f.Entries, 15)
require.Equal(t, time.Date(2022, 7, 28, 10, 52, 17, 0, time.UTC).Unix(), f.Updated.Unix())
fst := f.Entries[0]
require.Equal(t, "Charter Told To Pay $7.3 Billion In Damages After Cable Installer Murders Grandmother", fst.Title)
require.Equal(t, "https://yro.slashdot.org/story/22/07/27/2124200/charter-told-to-pay-73-billion-in-damages-after-cable-installer-murders-grandmother?utm_source=rss1.0mainlinkanon&utm_medium=feed", fst.Link)
require.Equal(t, "https://yro.slashdot.org/story/22/07/27/2124200/charter-told-to-pay-73-billion-in-damages-after-cable-installer-murders-grandmother?utm_source=rss1.0mainlinkanon&utm_medium=feed", fst.ID)
require.Equal(t, time.Date(2022, 7, 28, 10, 0, 0, 0, time.UTC).Unix(), fst.Updated.Unix())
}
func TestTakeOnRules(t *testing.T) {
byt, err := os.ReadFile("test-data/take-on-rules.atom")
require.Nil(t, err)
f, err := unmarshal(byt)
require.Nil(t, err)
require.Equal(t, "https://takeonrules.com/", f.Link)
}
func TestGarrit(t *testing.T) {
byt, err := os.ReadFile("test-data/garrit.xml")
require.Nil(t, err)
f, err := unmarshal(byt)
require.Nil(t, err)
require.Equal(t, "https://garrit.xyz", f.Link)
}
func TestReddit(t *testing.T) {
byt, err := os.ReadFile("test-data/rprogramming.atom")
require.Nil(t, err)
feed, err := unmarshal(byt)
require.Nil(t, err)
require.Equal(t, "programming", feed.Title)
require.Equal(t, "https://www.reddit.com/r/programming/", feed.Link)
require.Len(t, feed.Entries, 25)
first := feed.Entries[0]
require.Equal(t, first.Title, "Dark Mode Coming to GitHub After 7 Years")
}
func TestYouTube(t *testing.T) {
byt, err := os.ReadFile("test-data/wandel.xml")
require.Nil(t, err)
feed, err := unmarshal(byt)
require.Nil(t, err)
require.Equal(t, "Matthias Wandel", feed.Title)
require.Equal(t, "https://www.youtube.com/channel/UCckETVOT59aYw80B36aP9vw", feed.Link)
require.Len(t, feed.Entries, 15)
first := feed.Entries[0]
require.Equal(t, "26\" bandsaw sawdust drawer and bottom enclosure", first.Title)
require.Equal(t, "<div>Working on finishing up my 26\" bandsaw. In this eposode, making the bottom enclosure and the sawdust drawer. This directs nearly all the sawdust into the drawer, making for passive dust collection.\n\n\nhttp://woodgears.ca/big_bandsaw/bottom_enclosure.html</div><div><a href=\"https://www.youtube.com/v/9eRIUV94kgQ?version=3\"><img src=\"https://i2.ytimg.com/vi/9eRIUV94kgQ/hqdefault.jpg\" width=\"480\" height=\"360\" /></a></div>", string(first.Content))
}
func TestNotUtf8(t *testing.T) {
byt, err := os.ReadFile("test-data/not-utf8.rss")
require.Nil(t, err)
_, err = unmarshal(byt)
require.Nil(t, err)
}
func TestParseDateNoTime(t *testing.T) {
byt, err := os.ReadFile("test-data/date-no-time.rss")
require.Nil(t, err)
feed, err := unmarshal(byt)
require.Nil(t, err)
require.Equal(t, len(feed.Entries), 1)
updated := feed.Entries[0].Updated
require.Equal(t, updated, time.Date(2020, 11, 23, 0, 0, 0, 0, time.UTC))
}
func TestParseTime(t *testing.T) {
data := []struct {
raw string
expected time.Time
err error
}{
{
raw: "Mon, 2 March 2020 12:00:00 CET",
expected: time.Date(2020, 3, 2, 12, 0, 0, 0, time.UTC),
err: nil,
},
{
raw: "Tue, 2 Mar 2021 06:50:00 +1300",
expected: time.Date(2021, 3, 1, 17, 50, 0, 0, time.UTC),
err: nil,
},
}
for _, d := range data {
actual, err := parseTime(d.raw)
require.Equal(t, d.err, err)
require.Equal(t, int64(0), d.expected.Unix()-actual.Unix())
}
}
func TestSubstituteRelativeImageSrc(t *testing.T) {
orig := `src="/plus/misc/images/her-soundtrack.jpg"`
expected := `src="http://kottke.org/plus/misc/images/her-soundtrack.jpg"`
byt, err := os.ReadFile("test-data/kottke-entry.html")
require.Nil(t, err)
require.Contains(t, string(byt), orig, "entry should contain relative url")
bu, err := url.Parse("http://kottke.org/")
res, err := absolutifyHTML(string(byt), bu)
require.Contains(t, string(res), expected, "relative url should be replaced by absolute one")
}
func TestSubstituteRelativeAHref(t *testing.T) {
orig := `href="/bradfitz"`
expected := `href="https://github.com/bradfitz"`
byt, err := os.ReadFile("test-data/github-entry.html")
require.Nil(t, err)
require.Contains(t, string(byt), orig, "entry should contain relative url")
require.NotContains(t, string(byt), expected, "absolute url should not yet be present")
bu, err := url.Parse("https://github.com/")
res, err := absolutifyHTML(string(byt), bu)
require.Contains(t, string(res), expected, "relative url should be replaced by absolute one")
require.NotContains(t, string(res), orig, "relative url should not be present anymore")
}
func TestFileExists(t *testing.T) {
exists := "readme.md"
doesNotExist := "does-not-exist"
require.True(t, fileExists(exists))
require.False(t, fileExists(doesNotExist))
}
func TestPickNewData(t *testing.T) {
td := map[string]struct {
feeds []*Feed
limitPerFeed int
timestamps map[string]time.Time
expected []*Feed
}{
"one new entry": {
feeds: []*Feed{
{
Title: "Test Feed",
ID: "5db01937",
Link: "http://example.com",
Updated: time.Date(2022, 7, 23, 1, 2, 3, 0, time.UTC),
Entries: []*FeedEntry{
{
Title: "Old Entry",
Link: "http://example.com/old",
ID: "5db01937-old",
Updated: time.Date(2022, 7, 22, 1, 2, 3, 0, time.UTC),
},
{
Title: "Latest Hidden Entry",
Link: "http://example.com/new",
ID: "5db01937-new",
Updated: time.Date(2022, 7, 23, 1, 2, 3, 0, time.UTC),
},
},
},
},
limitPerFeed: 1,
timestamps: map[string]time.Time{
"5db01937": time.Date(2022, 7, 22, 1, 2, 3, 0, time.UTC),
},
expected: []*Feed{
{
Title: "Test Feed",
ID: "5db01937",
Link: "http://example.com",
Updated: time.Date(2022, 7, 23, 1, 2, 3, 0, time.UTC),
Entries: []*FeedEntry{
{
Title: "Latest Hidden Entry",
Link: "http://example.com/new",
ID: "5db01937-new",
Updated: time.Date(2022, 7, 23, 1, 2, 3, 0, time.UTC),
},
},
},
},
},
"initial pick is latest entry": {
feeds: []*Feed{
{
Title: "Test Feed",
ID: "5db01937",
Link: "http://example.com",
Updated: time.Date(2022, 7, 23, 1, 2, 3, 0, time.UTC),
Entries: []*FeedEntry{
{
Title: "Old Entry",
Link: "http://example.com/old",
ID: "5db01937-old",
Updated: time.Date(2022, 7, 22, 1, 2, 3, 0, time.UTC),
},
{
Title: "Latest Hidden Entry",
Link: "http://example.com/new",
ID: "5db01937-new",
Updated: time.Date(2022, 7, 23, 1, 2, 3, 0, time.UTC),
},
},
},
},
limitPerFeed: 1,
timestamps: map[string]time.Time{},
expected: []*Feed{
{
Title: "Test Feed",
ID: "5db01937",
Link: "http://example.com",
Updated: time.Date(2022, 7, 23, 1, 2, 3, 0, time.UTC),
Entries: []*FeedEntry{
{
Title: "Latest Hidden Entry",
Link: "http://example.com/new",
ID: "5db01937-new",
Updated: time.Date(2022, 7, 23, 1, 2, 3, 0, time.UTC),
},
},
},
},
},
"oldest entry first": {
feeds: []*Feed{
{
Title: "Test Feed",
ID: "5db01937",
Link: "http://example.com",
Updated: time.Date(2022, 7, 23, 3, 2, 3, 0, time.UTC),
Entries: []*FeedEntry{
{
Title: "Entry 3",
Link: "http://example.com/3",
ID: "5db01937-3",
Updated: time.Date(2022, 7, 22, 3, 2, 3, 0, time.UTC),
},
{
Title: "Entry 1",
Link: "http://example.com/1",
ID: "5db01937-1",
Updated: time.Date(2022, 7, 22, 1, 2, 3, 0, time.UTC),
},
{
Title: "Entry 2",
Link: "http://example.com/2",
ID: "5db01937-2",
Updated: time.Date(2022, 7, 22, 2, 2, 3, 0, time.UTC),
},
},
},
},
limitPerFeed: 3,
timestamps: map[string]time.Time{},
expected: []*Feed{
{
Title: "Test Feed",
ID: "5db01937",
Link: "http://example.com",
Updated: time.Date(2022, 7, 23, 3, 2, 3, 0, time.UTC),
Entries: []*FeedEntry{
{
Title: "Entry 1",
Link: "http://example.com/1",
ID: "5db01937-1",
Updated: time.Date(2022, 7, 22, 1, 2, 3, 0, time.UTC),
},
{
Title: "Entry 2",
Link: "http://example.com/2",
ID: "5db01937-2",
Updated: time.Date(2022, 7, 22, 2, 2, 3, 0, time.UTC),
},
{
Title: "Entry 3",
Link: "http://example.com/3",
ID: "5db01937-3",
Updated: time.Date(2022, 7, 22, 3, 2, 3, 0, time.UTC),
},
},
},
},
},
}
for tn, tc := range td {
actual := pickNewData(tc.feeds, tc.limitPerFeed, tc.timestamps)
require.Equal(t, tc.expected, actual, tn)
}
}
func TestFeedInfo(t *testing.T) {
byt, err := os.ReadFile("test-data/sample_head.html")
require.Nil(t, err)
gotTitle, gotLink := findFeedInfo(byt)
require.Equal(t, "Sample Title", gotTitle)
require.Equal(t, "https://example.com/atom.xml", gotLink)
}