all repos

rss-tools @ a5ac527

get rss feed from sources that(i need and) dont provide one

rss-tools/sources/telegram/page_title_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
youtube: fix title fetcher, 10 days ago
1
package telegram
2
3
import (
4
	"context"
5
	"io"
6
	"log/slog"
7
	"net/http"
8
	"strings"
9
	"testing"
10
11
	"olexsmir.xyz/x/is"
12
)
13
14
func TestEnrichMessageWithLinkTitlesStoresFetchedTitle(t *testing.T) {
15
	calls := 0
16
	tg := &telegram{
17
		get: func(_ context.Context, url string) (*http.Response, error) {
18
			calls++
19
			is.Equal(t, "https://example.com/post", url)
20
			return &http.Response{
21
				StatusCode: http.StatusOK,
22
				Header: http.Header{
23
					"Content-Type": []string{"text/html; charset=utf-8"},
24
				},
25
				Body: io.NopCloser(strings.NewReader(`<html><head><title> Example Post Title </title></head></html>`)),
26
			}, nil
27
		},
28
		logger: slog.Default(),
29
	}
30
	msg := &Message{Text: "https://example.com/post"}
31
32
	changed := tg.enrichMessageWithLinkTitles(context.Background(), msg)
33
	is.Equal(t, true, changed)
34
	is.Equal(t, 1, calls)
35
	is.Equal(t, "Example Post Title", msg.LinkTitles["https://example.com/post"])
36
37
	changed = tg.enrichMessageWithLinkTitles(context.Background(), msg)
38
	is.Equal(t, false, changed)
39
	is.Equal(t, 1, calls)
40
}
41
42
func TestEnrichMessageWithLinkTitlesRefreshesPlaceholderCachedTitle(t *testing.T) {
43
	calls := 0
44
	tg := &telegram{
45
		get: func(_ context.Context, _ string) (*http.Response, error) {
46
			calls++
47
			return &http.Response{
48
				StatusCode: http.StatusOK,
49
				Header: http.Header{
50
					"Content-Type": []string{"text/html; charset=utf-8"},
51
				},
52
				Body: io.NopCloser(strings.NewReader(`<html><head><title>Real Video Title</title></head></html>`)),
53
			}, nil
54
		},
55
		logger: slog.Default(),
56
	}
57
	msg := &Message{
58
		Text: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
59
		LinkTitles: map[string]string{
60
			"https://www.youtube.com/watch?v=dQw4w9WgXcQ": " - YouTube ",
61
		},
62
	}
63
64
	changed := tg.enrichMessageWithLinkTitles(context.Background(), msg)
65
	is.Equal(t, true, changed)
66
	is.Equal(t, 1, calls)
67
	is.Equal(t, "Real Video Title", msg.LinkTitles["https://www.youtube.com/watch?v=dQw4w9WgXcQ"])
68
}
69
70
func TestIsSingleLinkMessage(t *testing.T) {
71
	is.Equal(t, true, isSingleLinkMessage(" https://example.com/path. "))
72
	is.Equal(t, false, isSingleLinkMessage("check https://example.com/path"))
73
}
74
75
func TestEnrichMessageWithLinkTitlesIgnoresNonSingleLinkMessages(t *testing.T) {
76
	calls := 0
77
	tg := &telegram{
78
		get: func(_ context.Context, _ string) (*http.Response, error) {
79
			calls++
80
			return nil, nil
81
		},
82
		logger: slog.Default(),
83
	}
84
	msg := &Message{Text: "check this https://example.com/post"}
85
86
	changed := tg.enrichMessageWithLinkTitles(context.Background(), msg)
87
	is.Equal(t, false, changed)
88
	is.Equal(t, 0, calls)
89
}
90
91
func TestFetchPageTitleFallsBackToMetaTitleForYouTubePlaceholder(t *testing.T) {
92
	title, err := fetchPageTitle(context.Background(), func(_ context.Context, _ string) (*http.Response, error) {
93
		return &http.Response{
94
			StatusCode: http.StatusOK,
95
			Header: http.Header{
96
				"Content-Type": []string{"text/html; charset=utf-8"},
97
			},
98
			Body: io.NopCloser(strings.NewReader(`<html><head><title> - YouTube </title><meta property="og:title" content="Real Video Title"></head></html>`)),
99
		}, nil
100
	}, "https://www.youtube.com/watch?v=dQw4w9WgXcQ")
101
	if err != nil {
102
		t.Fatalf("unexpected error: %v", err)
103
	}
104
	is.Equal(t, "Real Video Title", title)
105
}
106
107
func TestFetchPageTitleRejectsYouTubePlaceholderWithoutMetadata(t *testing.T) {
108
	_, err := fetchPageTitle(context.Background(), func(_ context.Context, _ string) (*http.Response, error) {
109
		return &http.Response{
110
			StatusCode: http.StatusOK,
111
			Header: http.Header{
112
				"Content-Type": []string{"text/html; charset=utf-8"},
113
			},
114
			Body: io.NopCloser(strings.NewReader(`<html><head><title> - YouTube </title></head></html>`)),
115
		}, nil
116
	}, "https://www.youtube.com/watch?v=dQw4w9WgXcQ")
117
	if err == nil {
118
		t.Fatalf("expected an error for placeholder title")
119
	}
120
}
121
122
func TestFetchPageTitleFallsBackToYouTubeOEmbed(t *testing.T) {
123
	calls := 0
124
	title, err := fetchPageTitle(context.Background(), func(_ context.Context, url string) (*http.Response, error) {
125
		calls++
126
		if strings.Contains(url, "oembed") {
127
			return &http.Response{
128
				StatusCode: http.StatusOK,
129
				Header: http.Header{
130
					"Content-Type": []string{"application/json; charset=utf-8"},
131
				},
132
				Body: io.NopCloser(strings.NewReader(`{"title":"Rick Astley - Never Gonna Give You Up"}`)),
133
			}, nil
134
		}
135
		return &http.Response{
136
			StatusCode: http.StatusOK,
137
			Header: http.Header{
138
				"Content-Type": []string{"text/html; charset=utf-8"},
139
			},
140
			Body: io.NopCloser(strings.NewReader(`<html><head><title> - YouTube </title></head></html>`)),
141
		}, nil
142
	}, "https://www.youtube.com/watch?v=dQw4w9WgXcQ")
143
	if err != nil {
144
		t.Fatalf("unexpected error: %v", err)
145
	}
146
	is.Equal(t, "Rick Astley - Never Gonna Give You Up", title)
147
	is.Equal(t, 2, calls)
148
}