package bot
import (
"context"
"io"
"log/slog"
"strings"
"sync"
"testing"
"time"
"github.com/mymmrac/telego"
"github.com/noderunners/dlbot/internal/config"
"github.com/noderunners/dlbot/internal/db"
"github.com/noderunners/dlbot/internal/i18n"
"github.com/noderunners/dlbot/internal/media"
"github.com/noderunners/dlbot/internal/sched"
"github.com/noderunners/dlbot/internal/service"
"github.com/noderunners/dlbot/internal/tg"
"github.com/noderunners/dlbot/locales"
)
// testBot is a Bot with just enough set up for the pure parts: texts,
// captions, keyboards and the status throttle.
func testBot(t *testing.T, api tg.API) *Bot {
t.Helper()
log := slog.New(slog.NewTextHandler(io.Discard, nil))
tr, err := i18n.Load(locales.FS, log)
if err != nil {
t.Fatal(err)
}
return &Bot{
api: api, cfg: config.Default(), tr: tr, log: log, username: "nr_bot",
statuses: map[string]*statusState{}, live: map[string]*liveJob{}, liveTried: map[string]int{},
statusMinGap: 2500 * time.Millisecond,
}
}
func info(title, uploader string) *service.CardInfo {
return &service.CardInfo{
VideoID: "abc123", Title: title, Uploader: uploader, Duration: 75, Site: "instagram.com",
UploadDate: "20250503", HasAudio: true, Thumbnail: "https://img.example.com/t.jpg",
}
}
func TestCaption(t *testing.T) {
b := testBot(t, nil)
url := "https://www.instagram.com/reel/C1x/?igsh=MWQ1&utm_source=ig_web"
got := b.caption("en", info("Sunset over the bay", "nasa"), url, media.Best)
want := "Sunset over the bay\nnasa · 1:15\n" +
`instagram.com/reel/C1x` + "\nvia @nr_bot"
if got != want {
t.Fatalf("caption =\n%s\nwant\n%s", got, want)
}
// A generic title becomes the uploader and the date; the uploader is
// then not repeated on the next line.
got = b.caption("en", info("Video by nasa", "nasa"), url, media.Best)
if !strings.HasPrefix(got, "nasa · 3 May 2025\n1:15\n") {
t.Fatalf("generic title caption = %q", got)
}
got = b.caption("nl", info("", "nasa"), url, media.Best)
if !strings.HasPrefix(got, "nasa · 3 mei 2025") {
t.Fatalf("Dutch generic title caption = %q", got)
}
// Titles from the web cannot inject markup.
got = b.caption("en", info("