Files
Media-download-telegram-bot/internal/app/screens_test.go
T
MichilisandClaude Opus 5.5 123cede7f3 Phase 2: command screens
- /help: a short introduction with the user's default format, how to be
  asked every time and the limits on one line, plus buttons Commands,
  Account, Groups, Open app and Upgrade that switch the same message to
  a section. Group commands are hidden in private chats; "Add me to a
  group" takes their place.
- /quality: Ask every time, Best that fits my limit, Best, 1080p, 720p,
  480p and Audio only, with the active one checked. Free users see which
  qualities may go over their limit.
- /history: pages of five compact buttons (site, title, format, when),
  a view per download with Send again, Thumbnail, Remove from history,
  and Clear history with a confirmation. Thumbnails are grouped under
  their video. Migration 00007 stores extractor, title, uploader and
  profile on deliveries and adds a hidden flag.
- docs/SCREENS.md shows every screen in en, es and nl, generated by the
  language test.

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

215 lines
8.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package app_test
import (
"fmt"
"strings"
"testing"
"github.com/noderunners/dlbot/internal/config"
"github.com/noderunners/dlbot/internal/tg/fakeserver"
"github.com/noderunners/dlbot/internal/ytdlp"
"github.com/noderunners/dlbot/internal/ytdlp/ytdlptest"
)
// --- /help -------------------------------------------------------------------
func TestHelpIsShortWithSections(t *testing.T) {
h := startApp(t, nil, nil)
seq := h.srv.Seq()
h.srv.PushMessage(alice, alice, "/help")
help := h.wait(seq, "sendMessage", textHas("How to use me"))
if got := labels(help); got != "Commands | Account | Groups | Open app | Upgrade" {
t.Fatalf("help buttons = %q", got)
}
// The default format and how to be asked every time are explained, and
// the limits are one line.
for _, want := range []string{"Your default format: <b>Best that fits my limit</b>", "Ask every time", "Your limits: 50 MB"} {
if !strings.Contains(help.Text(), want) {
t.Fatalf("help lacks %q: %q", want, help.Text())
}
}
section := func(prefix string) fakeserver.Call {
t.Helper()
seq := h.press(alice, alice, help, prefix)
return h.wait(seq, "editMessageText", func(c fakeserver.Call) bool { return c.MessageID == help.MessageID })
}
if cmds := section("hp:cmds"); !strings.Contains(cmds.Text(), "/audio") || !strings.Contains(cmds.Text(), "/history") {
t.Fatalf("commands = %q", cmds.Text())
}
if acct := section("hp:acct"); !strings.Contains(acct.Text(), "/buy") || !strings.Contains(acct.Text(), "/referral") {
t.Fatalf("account = %q", acct.Text())
}
// In a private chat the group commands are hidden; "Add me to a group"
// takes their place.
grp := section("hp:groups")
for _, cmd := range []string{"/groupwallet", "/grouppass", "/settings"} {
if strings.Contains(grp.Text(), cmd) {
t.Fatalf("private help names %s: %q", cmd, grp.Text())
}
}
add, ok := grp.Button("")
if !ok || !strings.Contains(add.URL, "?startgroup=") {
t.Fatalf("groups section buttons = %+v", grp.Buttons())
}
seq = h.press(alice, alice, grp, "hp:home")
if home := h.wait(seq, "editMessageText", func(c fakeserver.Call) bool { return c.MessageID == help.MessageID }); !strings.Contains(home.Text(), "How to use me") {
t.Fatalf("back = %q", home.Text())
}
// In a group the group commands are listed.
h.meetGroup(alice)
seq = h.srv.Seq()
h.srv.PushMessage(group, alice, "/help")
ghelp := h.wait(seq, "sendMessage", func(c fakeserver.Call) bool { return c.ChatID() == group })
if !strings.Contains(ghelp.Text(), "Format in this group") {
t.Fatalf("group help = %q", ghelp.Text())
}
seq = h.press(group, alice, ghelp, "hp:groups")
gg := h.wait(seq, "editMessageText", func(c fakeserver.Call) bool { return c.MessageID == ghelp.MessageID })
if !strings.Contains(gg.Text(), "/groupwallet") || !strings.Contains(gg.Text(), "/grouppass") {
t.Fatalf("group section in a group = %q", gg.Text())
}
}
// --- /quality ------------------------------------------------------------------
func TestQualityMarksTheActiveModeAndWarnsFreeUsers(t *testing.T) {
h := startMoney(t, nil, nil)
open := func() fakeserver.Call {
seq := h.srv.Seq()
h.srv.PushMessage(alice, alice, "/quality")
return h.wait(seq, "sendMessage", textHas("Your default format"))
}
menu := open()
// 50 MB and 10 minutes: every video quality can go over, MP3 cannot.
want := "Ask every time | ✓ Best that fits my limit | Best ⚠ | 1080p ⚠ | 720p ⚠ | 480p ⚠ | Audio only (MP3)"
if got := labels(menu); got != want {
t.Fatalf("free user's choices = %q, want %q", got, want)
}
if !strings.Contains(menu.Text(), "over your limit of 50 MB") {
t.Fatalf("no explanation of the warning: %q", menu.Text())
}
// "Ask every time" is checked when it is the choice.
seq := h.press(alice, alice, menu, "ql:ask")
h.wait(seq, "editMessageText", textHas("Default format: Ask every time"))
if got := labels(open()); !strings.HasPrefix(got, "✓ Ask every time | Best that fits my limit") {
t.Fatalf("after choosing ask = %q", got)
}
// Paying users get no warnings.
h.grant(alice, 10_000)
menu = open()
if strings.Contains(labels(menu), "⚠") || strings.Contains(menu.Text(), "over your limit") {
t.Fatalf("a paying user is warned: %q %q", labels(menu), menu.Text())
}
}
func TestBestThatFitsPicksTheQualityUnderTheLimit(t *testing.T) {
big := "https://example.com/big-but-720-fits"
huge := "https://example.com/nothing-fits"
mb := func(n int) ytdlp.Int { return ytdlp.Int(n << 20) }
info := func(url string, v1080, v720, v480 int) *ytdlp.Info {
i := ytdlptest.DefaultInfo(url)
i.Formats = []ytdlp.Format{
{FormatID: "a", Ext: "m4a", VCodec: "none", ACodec: "mp4a", Filesize: mb(3)},
{FormatID: "v480", Ext: "mp4", VCodec: "avc1", ACodec: "none", Height: 480, Filesize: mb(v480)},
{FormatID: "v720", Ext: "mp4", VCodec: "avc1", ACodec: "none", Height: 720, Filesize: mb(v720)},
{FormatID: "v1080", Ext: "mp4", VCodec: "avc1", ACodec: "none", Height: 1080, FilesizeAprx: mb(v1080)},
}
return i
}
h := startMoney(t, &ytdlptest.Fake{Infos: map[string]*ytdlp.Info{
big: info(big, 120, 40, 15), huge: info(huge, 900, 400, 200),
}}, nil)
h.askEveryTime(alice)
if err := h.app.Store.SetUserQuality(t.Context(), alice, "fit"); err != nil {
t.Fatal(err)
}
seq := h.srv.Seq()
h.srv.PushMessage(alice, alice, big)
h.finished(seq, alice)
if dl := h.fake.Downloads(); len(dl) != 1 || string(dl[0].Profile) != "720" {
t.Fatalf("downloads = %+v, want 720p", dl)
}
// Even 480p is over 50 MB: the over-limit offer, nothing downloaded.
seq = h.srv.Seq()
h.srv.PushMessage(alice, alice, huge)
h.wait(seq, "sendMessage", func(c fakeserver.Call) bool { _, ok := c.Button("ob:"); return ok })
if n := len(h.fake.Downloads()); n != 1 {
t.Fatalf("%d downloads, the over-limit file must wait for payment", n)
}
}
// --- /history ------------------------------------------------------------------
func TestHistoryPagesThroughDownloads(t *testing.T) {
h := start(t, nil, func(c *config.Config) { c.Limits.Free.DailyDownloads = 50 })
for i := 1; i <= 7; i++ {
h.fetch(alice, fmt.Sprintf("https://example.com/v%d", i))
}
// A thumbnail is not a download of its own in the list.
seq := h.srv.Seq()
h.srv.PushMessage(alice, alice, "/thumb https://example.com/v7")
h.finished(seq, alice)
seq = h.srv.Seq()
h.srv.PushMessage(alice, alice, "/history")
list := h.wait(seq, "sendMessage", textHas("Your past downloads"))
items := func(c fakeserver.Call) int {
n := 0
for _, b := range c.Buttons() {
if strings.HasPrefix(b.Data, "hv:") {
n++
if b.Text != "example.com · Test Video · Best · today" {
t.Errorf("label = %q", b.Text)
}
}
}
return n
}
if n := items(list); n != 5 || !strings.Contains(list.Text(), "· 7") || !strings.Contains(list.Text(), "Page 1 of 2") {
t.Fatalf("first page: %d items, %q", n, list.Text())
}
if !strings.Contains(labels(list), "Older ›") || strings.Contains(labels(list), "Newer") {
t.Fatalf("first page buttons = %q", labels(list))
}
edited := func(seq int) fakeserver.Call {
return h.wait(seq, "editMessageText", func(c fakeserver.Call) bool { return c.MessageID == list.MessageID })
}
page2 := edited(h.press(alice, alice, list, "hi:1"))
if n := items(page2); n != 2 || !strings.Contains(labels(page2), "‹ Newer") || strings.Contains(labels(page2), "Older") {
t.Fatalf("second page: %d items, buttons %q", n, labels(page2))
}
// One download, with the thumbnail as an action under it.
item := edited(h.press(alice, alice, page2, "hv:"))
if got := labels(item); got != "Send again | Thumbnail | Remove from history | Back" {
t.Fatalf("item buttons = %q", got)
}
back := edited(h.press(alice, alice, item, "hd:"))
if !strings.Contains(back.Text(), "· 6") {
t.Fatalf("after removing one = %q", back.Text())
}
// Clearing asks first.
ask := edited(h.press(alice, alice, back, "hc:"))
if !strings.Contains(ask.Text(), "Remove all 6 downloads") {
t.Fatalf("confirm = %q", ask.Text())
}
cancel := edited(h.press(alice, alice, ask, "hi:"))
if !strings.Contains(cancel.Text(), "· 6") {
t.Fatalf("cancel = %q", cancel.Text())
}
ask = edited(h.press(alice, alice, cancel, "hc:"))
cleared := edited(h.press(alice, alice, ask, "hy"))
if !strings.Contains(cleared.Text(), "empty") {
t.Fatalf("cleared = %q", cleared.Text())
}
seq = h.srv.Seq()
h.srv.PushMessage(alice, alice, "/history")
h.wait(seq, "sendMessage", textHas("no past downloads"))
// The rows stay for usage and billing; only the history forgets them.
if u := h.usage(alice); u.Downloads != 8 {
t.Fatalf("usage = %+v", u)
}
}