aboutsummaryrefslogtreecommitdiffstats
path: root/internal/crawler/crawler_test.go
blob: a8a9c9c3ede84a84ed5041a27d677ab0b6efc825 (plain) (blame)
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
package crawler

import (
	"log"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"adammathes.com/neko/config"
	"adammathes.com/neko/internal/safehttp"
	"adammathes.com/neko/models"
	"adammathes.com/neko/models/feed"
)

func init() {
	safehttp.AllowLocal = true
}

func setupTestDB(t *testing.T) {
	t.Helper()
	config.Config.DBFile = ":memory:"
	models.InitDB()
	t.Cleanup(func() {
		if models.DB != nil {
			models.DB.Close()
		}
	})
}

func TestGetFeedContentSuccess(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ua := r.Header.Get("User-Agent")
		if ua == "" {
			t.Error("Request should include User-Agent")
		}
		w.WriteHeader(200)
		w.Write([]byte("<rss><channel><title>Test</title></channel></rss>"))
	}))
	defer ts.Close()

	content := GetFeedContent(ts.URL)
	if content == "" {
		t.Error("GetFeedContent should return content for valid URL")
	}
	if content != "<rss><channel><title>Test</title></channel></rss>" {
		t.Errorf("Unexpected content: %q", content)
	}
}

func TestGetFeedContentBadURL(t *testing.T) {
	content := GetFeedContent("http://invalid.invalid.invalid:99999/feed")
	if content != "" {
		t.Errorf("GetFeedContent should return empty string for bad URL, got %q", content)
	}
}

func TestGetFeedContent404(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(404)
	}))
	defer ts.Close()

	content := GetFeedContent(ts.URL)
	if content != "" {
		t.Errorf("GetFeedContent should return empty for 404, got %q", content)
	}
}

func TestGetFeedContent500(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
	}))
	defer ts.Close()

	content := GetFeedContent(ts.URL)
	if content != "" {
		t.Errorf("GetFeedContent should return empty for 500, got %q", content)
	}
}

func TestGetFeedContentUserAgent(t *testing.T) {
	var receivedUA string
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		receivedUA = r.Header.Get("User-Agent")
		w.WriteHeader(200)
		w.Write([]byte("ok"))
	}))
	defer ts.Close()

	GetFeedContent(ts.URL)
	expected := "neko RSS Crawler +https://github.com/adammathes/neko"
	if receivedUA != expected {
		t.Errorf("Expected UA %q, got %q", expected, receivedUA)
	}
}

func TestCrawlFeedWithTestServer(t *testing.T) {
	setupTestDB(t)

	rssContent := `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Test Feed</title>
    <link>https://example.com</link>
    <item>
      <title>Article 1</title>
      <link>https://example.com/article1</link>
      <description>First article</description>
      <pubDate>Mon, 01 Jan 2024 00:00:00 GMT</pubDate>
    </item>
    <item>
      <title>Article 2</title>
      <link>https://example.com/article2</link>
      <description>Second article</description>
      <pubDate>Tue, 02 Jan 2024 00:00:00 GMT</pubDate>
    </item>
  </channel>
</rss>`

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/rss+xml")
		w.WriteHeader(200)
		w.Write([]byte(rssContent))
	}))
	defer ts.Close()

	// Create a feed pointing to the test server
	f := &feed.Feed{Url: ts.URL, Title: "Test"}
	f.Create()

	ch := make(chan string, 1)
	CrawlFeed(f, ch)
	result := <-ch

	if result == "" {
		t.Error("CrawlFeed should send a result")
	}

	// Verify items were created
	var count int
	models.DB.QueryRow("SELECT COUNT(*) FROM item").Scan(&count)
	if count != 2 {
		t.Errorf("Expected 2 items, got %d", count)
	}
}

func TestCrawlFeedBadContent(t *testing.T) {
	setupTestDB(t)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte("not xml at all"))
	}))
	defer ts.Close()

	f := &feed.Feed{Url: ts.URL, Title: "Bad"}
	f.Create()

	ch := make(chan string, 1)
	CrawlFeed(f, ch)
	result := <-ch

	if result == "" {
		t.Error("CrawlFeed should send a result even on failure")
	}
}

func TestCrawlWorker(t *testing.T) {
	setupTestDB(t)

	rssContent := `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Worker Feed</title>
    <link>https://example.com</link>
    <item>
      <title>Worker Article</title>
      <link>https://example.com/worker-article</link>
      <description>An article</description>
    </item>
  </channel>
</rss>`

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte(rssContent))
	}))
	defer ts.Close()

	f := &feed.Feed{Url: ts.URL, Title: "Worker Test"}
	f.Create()

	feeds := make(chan *feed.Feed, 1)
	results := make(chan string, 1)

	feeds <- f
	close(feeds)

	CrawlWorker(feeds, results)
	result := <-results

	if result == "" {
		t.Error("CrawlWorker should produce a result")
	}
}

func TestCrawl(t *testing.T) {
	setupTestDB(t)

	rssContent := `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Crawl Feed</title>
    <link>https://example.com</link>
    <item>
      <title>Crawl Article</title>
      <link>https://example.com/crawl-article</link>
      <description>Article for crawl test</description>
    </item>
  </channel>
</rss>`
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte(rssContent))
	}))
	defer ts.Close()

	f := &feed.Feed{Url: ts.URL, Title: "Full Crawl"}
	f.Create()

	// Should not panic
	Crawl()

	var count int
	models.DB.QueryRow("SELECT COUNT(*) FROM item").Scan(&count)
	if count != 1 {
		t.Errorf("Expected 1 item after crawl, got %d", count)
	}
}

func TestCrawlFeedWithExtensions(t *testing.T) {
	setupTestDB(t)

	rssContent := `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Extension Feed</title>
    <item>
      <title>Extension Article</title>
      <link>https://example.com/ext</link>
      <description>Short description</description>
      <content:encoded><![CDATA[Much longer content that should be used as description]]></content:encoded>
    </item>
  </channel>
</rss>`

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte(rssContent))
	}))
	defer ts.Close()

	f := &feed.Feed{Url: ts.URL, Title: "Extension Test"}
	f.Create()

	ch := make(chan string, 1)
	CrawlFeed(f, ch)
	<-ch

	var itemTitle, itemDesc string
	err := models.DB.QueryRow("SELECT title, description FROM item WHERE feed_id = ?", f.Id).Scan(&itemTitle, &itemDesc)
	if err != nil {
		log.Fatal(err)
	}

	if itemTitle != "Extension Article" {
		t.Errorf("Expected title 'Extension Article', got %q", itemTitle)
	}
	if !strings.Contains(itemDesc, "Much longer content") {
		t.Errorf("Expected description to contain encoded content, got %q", itemDesc)
	}
}