rss-tools/sources/telegram/telegram_test.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com telegram: turn links into links; return youtube links in youtube official feed format, 1 month ago
olexsmir@gmail.com telegram: turn links into links; return youtube links in youtube official feed format, 1 month ago
| 1 | package telegram |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "olexsmir.xyz/x/is" |
| 9 | ) |
| 10 | |
| 11 | func TestFeedEntryFromMessageWithImage(t *testing.T) { |
| 12 | msg := &Message{ |
| 13 | MessageID: 42, |
| 14 | Caption: "hello <world>", |
| 15 | Date: time.Date(2026, 4, 22, 19, 38, 0, 0, time.UTC).Unix(), |
| 16 | PhotoBase64: "YWJj", |
| 17 | PhotoMIMEType: "image/png", |
| 18 | } |
| 19 | |
| 20 | entry := feedEntryFromMessage(msg) |
| 21 | is.Equal(t, "🖼️ [2026-04-22]", entry.Title) |
| 22 | is.Equal(t, "html", entry.ContentType) |
| 23 | if !strings.Contains(entry.Content, "<p>hello <world></p>") { |
| 24 | t.Fatalf("expected escaped text in image entry: %s", entry.Content) |
| 25 | } |
| 26 | if !strings.Contains(entry.Content, `src="data:image/png;base64,YWJj"`) { |
| 27 | t.Fatalf("expected image data URI in image entry: %s", entry.Content) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestFeedEntryFromMessageTextOnly(t *testing.T) { |
| 32 | msg := &Message{ |
| 33 | MessageID: 11, |
| 34 | Text: "plain text", |
| 35 | Date: time.Date(2026, 4, 22, 19, 38, 0, 0, time.UTC).Unix(), |
| 36 | } |
| 37 | |
| 38 | entry := feedEntryFromMessage(msg) |
| 39 | is.Equal(t, "plain text", entry.Title) |
| 40 | is.Equal(t, "", entry.ContentType) |
| 41 | is.Equal(t, "plain text", entry.Content) |
| 42 | } |
| 43 | |
| 44 | func TestFeedEntryFromMessageLinkifiesAndAddsAtomLinks(t *testing.T) { |
| 45 | msg := &Message{ |
| 46 | MessageID: 15, |
| 47 | Text: "watch https://example.com and https://youtu.be/dQw4w9WgXcQ.", |
| 48 | Date: time.Date(2026, 4, 23, 11, 0, 0, 0, time.UTC).Unix(), |
| 49 | } |
| 50 | |
| 51 | entry := feedEntryFromMessage(msg) |
| 52 | is.Equal(t, "html", entry.ContentType) |
| 53 | if !strings.Contains(entry.Content, `<a href="https://example.com">https://example.com</a>`) { |
| 54 | t.Fatalf("expected generic link in content: %s", entry.Content) |
| 55 | } |
| 56 | if !strings.Contains(entry.Content, `<a href="https://youtu.be/dQw4w9WgXcQ">https://youtu.be/dQw4w9WgXcQ</a>`) { |
| 57 | t.Fatalf("expected youtube link in content: %s", entry.Content) |
| 58 | } |
| 59 | |
| 60 | is.Equal(t, 2, len(entry.Links)) |
| 61 | is.Equal(t, "https://example.com", entry.Links[0].Href) |
| 62 | is.Equal(t, "https://www.youtube.com/watch?v=dQw4w9WgXcQ", entry.Links[1].Href) |
| 63 | is.Equal(t, "yt:video:dQw4w9WgXcQ", entry.ID) |
| 64 | } |