Files
MichilisandClaude Opus 5.5 6843053582 Phase 1: quick flow fixes
- One message per job: the status message is deleted once the file is
  sent, or shortened to "Sent." when Telegram keeps it.
- No reply quotes in private chats; groups keep them.
- Queue status only mentions the position when the wait is above
  QUEUE_NOTICE_THRESHOLD_SECONDS or somebody is ahead. Status edits are
  throttled to one per 2.5 s per message; a state that arrives too soon
  is shown later instead of dropped. The wait estimate is 0 with a free
  slot.
- Caption with title (uploader and date when generic), uploader,
  duration, a short source link without tracking parameters and an
  optional "via @bot" line (CAPTION_ATTRIBUTION). Uploaded videos get a
  320 px JPEG thumbnail; merged MP4s are written with faststart.
- Buttons under the result: Audio (MP3), Other quality, Thumbnail,
  Delete. Requester only in groups.
- Language resolved once per update and passed in the context. A
  fallback to English is logged as a bug; a test walks every screen in
  Spanish and Dutch. Language picker on the first /start.
- Sizes without a zero decimal ("50 MB").
- First link of a new user shows the card once and sets the default to
  "best that fits my limit".
- Errors offer the next step (Try again, Smaller quality, Upgrade, Open
  app); passing lookup failures are retried once.
- Up to MULTI_LINK_MAX links per message; a link sent a moment ago gets
  "already sent" with a Send again button.
- Mini App menu button, labelled per private chat in the user's
  language.
- Personal /settings in private chats with an "Add me to a group"
  button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 20:08:58 +00:00

191 lines
7.1 KiB
Go

package links
import (
"errors"
"reflect"
"strings"
"testing"
)
func TestNormalize(t *testing.T) {
tests := []struct {
name string
in string
want string
wantErr error
}{
{"plain https", "https://example.com/watch?v=1", "https://example.com/watch?v=1", nil},
{"http kept", "http://example.com/a", "http://example.com/a", nil},
{"no scheme", "youtu.be/abc123", "https://youtu.be/abc123", nil},
{"upper host", "HTTPS://Example.COM/Path", "https://example.com/Path", nil},
{"trailing punctuation", "https://example.com/a).", "https://example.com/a", nil},
{"fragment dropped", "https://example.com/a#t=10", "https://example.com/a", nil},
{"userinfo dropped", "https://user:pw@example.com/a", "https://example.com/a", nil},
{"port kept", "https://example.com:8443/a", "https://example.com:8443/a", nil},
{"spaces around", " https://example.com/a ", "https://example.com/a", nil},
{"public ip", "http://8.8.8.8/v.mp4", "http://8.8.8.8/v.mp4", nil},
{"empty", "", "", ErrNotURL},
{"word", "hello", "", ErrNotURL},
{"sentence", "hello there. friend", "", ErrNotURL},
{"ftp", "ftp://example.com/a", "", ErrScheme},
{"file", "file:///etc/passwd", "", ErrScheme},
{"javascript", "javascript://alert(1)", "", ErrScheme},
{"localhost", "http://localhost/a", "", ErrPrivateHost},
{"localhost port", "http://localhost:8080/a", "", ErrPrivateHost},
{"loopback", "http://127.0.0.1/a", "", ErrPrivateHost},
{"private 10", "http://10.1.2.3/a", "", ErrPrivateHost},
{"private 192", "http://192.168.1.1/a", "", ErrPrivateHost},
{"link local metadata", "http://169.254.169.254/latest/meta-data", "", ErrPrivateHost},
{"ipv6 loopback", "http://[::1]/a", "", ErrPrivateHost},
{"dot local", "http://printer.local/a", "", ErrPrivateHost},
{"dot internal", "http://db.internal/a", "", ErrPrivateHost},
{"unspecified", "http://0.0.0.0/a", "", ErrPrivateHost},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := Normalize(tc.in)
if !errors.Is(err, tc.wantErr) {
t.Fatalf("Normalize(%q) error = %v, want %v", tc.in, err, tc.wantErr)
}
if got != tc.want {
t.Fatalf("Normalize(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
func TestExtract(t *testing.T) {
tests := []struct {
name string
text string
entities []Entity
want []string
}{
{
name: "regex fallback",
text: "look https://example.com/a and http://example.org/b.",
want: []string{"https://example.com/a", "http://example.org/b"},
},
{
name: "url entity without scheme",
text: "/dl youtu.be/abc",
entities: []Entity{{Type: "bot_command", Offset: 0, Length: 3}, {Type: "url", Offset: 4, Length: 12}},
want: []string{"https://youtu.be/abc"},
},
{
name: "text link entity",
text: "click here",
entities: []Entity{{Type: "text_link", Offset: 6, Length: 4, URL: "https://example.com/v"}},
want: []string{"https://example.com/v"},
},
{
// The emoji is two UTF-16 units, so the entity offset is 3, not 2.
name: "utf16 offsets",
text: "\U0001F600 https://example.com/x",
entities: []Entity{{Type: "url", Offset: 3, Length: 21}},
want: []string{"https://example.com/x"},
},
{
name: "duplicates removed",
text: "https://example.com/a https://example.com/a",
want: []string{"https://example.com/a"},
},
{
name: "private hosts dropped",
text: "http://127.0.0.1/x https://example.com/ok",
want: []string{"https://example.com/ok"},
},
{
name: "bad entity range ignored, regex used",
text: "https://example.com/a",
entities: []Entity{{Type: "url", Offset: 50, Length: 10}},
want: []string{"https://example.com/a"},
},
{name: "none", text: "just words", want: nil},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := Extract(tc.text, tc.entities)
if !reflect.DeepEqual(got, tc.want) {
t.Fatalf("Extract() = %#v, want %#v", got, tc.want)
}
})
}
}
func TestSite(t *testing.T) {
tests := map[string]string{
"https://www.youtube.com/watch?v=1": "youtube.com",
"https://m.youtube.com/watch?v=1": "youtube.com",
"https://YouTu.be/x": "youtu.be",
"https://sub.example.com:8080/a": "sub.example.com",
"::bad::": "",
}
for in, want := range tests {
if got := Site(in); got != want {
t.Errorf("Site(%q) = %q, want %q", in, got, want)
}
}
}
func TestRedact(t *testing.T) {
got := Redact("https://bob:hunter2@example.com/v?id=7&token=abc&X-Signature=zzz")
for _, leak := range []string{"hunter2", "bob", "abc", "zzz"} {
if strings.Contains(got, leak) {
t.Fatalf("Redact leaked %q in %q", leak, got)
}
}
if !strings.Contains(got, "id=7") {
t.Fatalf("Redact dropped a harmless parameter: %q", got)
}
text := RedactText("ERROR: failed https://a.example/x?sig=SECRET1 then https://u:p@b.example/y")
if strings.Contains(text, "SECRET1") || strings.Contains(text, "u:p@") {
t.Fatalf("RedactText leaked: %q", text)
}
}
func TestCleanRemovesTrackingParameters(t *testing.T) {
cases := []struct{ in, want string }{
// Campaign and share tags go, the video ID stays.
{"https://www.youtube.com/watch?v=abc123&utm_source=x&utm_medium=y&si=SHARE", "https://www.youtube.com/watch?v=abc123"},
{"https://youtu.be/abc123?si=XYZ&t=42", "https://youtu.be/abc123?t=42"},
{"https://www.youtube.com/watch?feature=share&v=abc&list=PL1", "https://www.youtube.com/watch?v=abc&list=PL1"},
{"https://www.instagram.com/reel/C1x/?igsh=MWQ1ZGUxMzBkMA==", "https://www.instagram.com/reel/C1x/"},
{"https://www.instagram.com/p/C1x/?igshid=abc&img_index=2", "https://www.instagram.com/p/C1x/?img_index=2"},
{"https://example.com/v?fbclid=IwAR0&jplx=1&id=9", "https://example.com/v?id=9"},
// "t" is a share token on X and a start time on YouTube.
{"https://x.com/user/status/1?s=20&t=abcdef", "https://x.com/user/status/1"},
{"https://mobile.twitter.com/user/status/1?s=46", "https://mobile.twitter.com/user/status/1"},
{"https://www.youtube.com/watch?v=abc&t=90s", "https://www.youtube.com/watch?v=abc&t=90s"},
{"https://www.tiktok.com/@u/video/7?is_from_webapp=1&sender_device=pc&lang=en", "https://www.tiktok.com/@u/video/7?lang=en"},
// Keys are compared without case; nothing else changes.
{"https://example.com/a?UTM_Campaign=1", "https://example.com/a"},
{"https://example.com/a", "https://example.com/a"},
{"https://example.com/a?", "https://example.com/a"},
{"https://example.com/a?b=1&&c=2", "https://example.com/a?b=1&c=2"},
}
for _, c := range cases {
if got := Clean(c.in); got != c.want {
t.Errorf("Clean(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestShortLink(t *testing.T) {
cases := []struct {
in string
max int
want string
}{
{"https://www.youtube.com/watch?v=abc", 40, "youtube.com/watch?v=abc"},
{"https://example.com/", 40, "example.com"},
{"https://example.com/a/very/long/path/that/goes/on", 20, "example.com/a/very/…"},
{"https://www.instagram.com/reel/C1x/", 40, "instagram.com/reel/C1x"},
}
for _, c := range cases {
if got := Short(c.in, c.max); got != c.want {
t.Errorf("Short(%q, %d) = %q, want %q", c.in, c.max, got, c.want)
}
}
}