rss-tools/sources/telegram/telegram_test.go (view raw)
| 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 | if entry.Content == nil { |
| 23 | t.Fatalf("expected content in image entry") |
| 24 | } |
| 25 | is.Equal(t, "html", entry.Content.Type) |
| 26 | if !strings.Contains(entry.Content.Body, "<p>hello <world></p>") { |
| 27 | t.Fatalf("expected escaped text in image entry: %s", entry.Content.Body) |
| 28 | } |
| 29 | if !strings.Contains(entry.Content.Body, `src="data:image/png;base64,YWJj"`) { |
| 30 | t.Fatalf("expected image data URI in image entry: %s", entry.Content.Body) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestFeedEntryFromMessageWithMultipleImages(t *testing.T) { |
| 35 | msg := &Message{ |
| 36 | MessageID: 55, |
| 37 | Caption: "multi image", |
| 38 | Date: time.Date(2026, 4, 23, 12, 15, 0, 0, time.UTC).Unix(), |
| 39 | PhotoAttachments: []PhotoAttachment{ |
| 40 | {Base64: "YWJj", MIMEType: "image/png"}, |
| 41 | {Base64: "ZGVm", MIMEType: "image/jpeg"}, |
| 42 | }, |
| 43 | } |
| 44 | |
| 45 | entry := feedEntryFromMessage(msg) |
| 46 | if entry.Content == nil { |
| 47 | t.Fatalf("expected content in image entry") |
| 48 | } |
| 49 | if !strings.Contains(entry.Content.Body, `src="data:image/png;base64,YWJj"`) { |
| 50 | t.Fatalf("expected first image data URI in image entry: %s", entry.Content.Body) |
| 51 | } |
| 52 | if !strings.Contains(entry.Content.Body, `src="data:image/jpeg;base64,ZGVm"`) { |
| 53 | t.Fatalf("expected second image data URI in image entry: %s", entry.Content.Body) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestFeedEntryFromMessageTextOnly(t *testing.T) { |
| 58 | msg := &Message{ |
| 59 | MessageID: 11, |
| 60 | Text: "plain text", |
| 61 | Date: time.Date(2026, 4, 22, 19, 38, 0, 0, time.UTC).Unix(), |
| 62 | } |
| 63 | |
| 64 | entry := feedEntryFromMessage(msg) |
| 65 | is.Equal(t, "plain text", entry.Title) |
| 66 | if entry.Content == nil { |
| 67 | t.Fatalf("expected content in text entry") |
| 68 | } |
| 69 | is.Equal(t, "text", entry.Content.Type) |
| 70 | is.Equal(t, "plain text", entry.Content.Body) |
| 71 | } |
| 72 | |
| 73 | func TestFeedEntryFromMessagePreservesNewlines(t *testing.T) { |
| 74 | msg := &Message{ |
| 75 | MessageID: 12, |
| 76 | Text: "line 1\nline 2", |
| 77 | Date: time.Date(2026, 4, 22, 19, 38, 0, 0, time.UTC).Unix(), |
| 78 | } |
| 79 | |
| 80 | entry := feedEntryFromMessage(msg) |
| 81 | if entry.Content == nil { |
| 82 | t.Fatalf("expected content in text entry") |
| 83 | } |
| 84 | is.Equal(t, "html", entry.Content.Type) |
| 85 | if !strings.Contains(entry.Content.Body, "line 1<br/>line 2") { |
| 86 | t.Fatalf("expected line breaks preserved in content: %s", entry.Content.Body) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestFeedEntryFromMessageLinkifiesAndAddsAtomLinks(t *testing.T) { |
| 91 | msg := &Message{ |
| 92 | MessageID: 15, |
| 93 | Text: "watch https://example.com and https://youtu.be/dQw4w9WgXcQ.", |
| 94 | Date: time.Date(2026, 4, 23, 11, 0, 0, 0, time.UTC).Unix(), |
| 95 | } |
| 96 | |
| 97 | entry := feedEntryFromMessage(msg) |
| 98 | if entry.Content == nil { |
| 99 | t.Fatalf("expected content in link entry") |
| 100 | } |
| 101 | is.Equal(t, "html", entry.Content.Type) |
| 102 | if !strings.Contains(entry.Content.Body, `<a href="https://example.com">https://example.com</a>`) { |
| 103 | t.Fatalf("expected generic link in content: %s", entry.Content.Body) |
| 104 | } |
| 105 | if !strings.Contains(entry.Content.Body, `<a href="https://youtu.be/dQw4w9WgXcQ">https://youtu.be/dQw4w9WgXcQ</a>`) { |
| 106 | t.Fatalf("expected youtube link in content: %s", entry.Content.Body) |
| 107 | } |
| 108 | |
| 109 | is.Equal(t, 2, len(entry.Link)) |
| 110 | is.Equal(t, "https://example.com", entry.Link[0].Href) |
| 111 | is.Equal(t, "https://www.youtube.com/watch?v=dQw4w9WgXcQ", entry.Link[1].Href) |
| 112 | is.Equal(t, "yt:video:dQw4w9WgXcQ", entry.ID) |
| 113 | } |
| 114 | |
| 115 | func TestFeedEntryFromMessageUsesStoredLinkTitleForSingleLink(t *testing.T) { |
| 116 | msg := &Message{ |
| 117 | MessageID: 16, |
| 118 | Text: "https://example.com/post", |
| 119 | Date: time.Date(2026, 4, 23, 11, 0, 0, 0, time.UTC).Unix(), |
| 120 | LinkTitles: map[string]string{ |
| 121 | "https://example.com/post": "Example Post Title", |
| 122 | }, |
| 123 | } |
| 124 | |
| 125 | entry := feedEntryFromMessage(msg) |
| 126 | is.Equal(t, "Example Post Title", entry.Title) |
| 127 | } |