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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
6843053582
commit
123cede7f3
@@ -219,7 +219,7 @@ const fileMethods = "sendVideo|sendAudio|sendDocument|sendPhoto"
|
||||
func isResult(chat int64) func(fakeserver.Call) bool {
|
||||
return func(c fakeserver.Call) bool {
|
||||
_, ok := c.Button("fx:")
|
||||
return ok && (chat == 0 || c.ChatID() == chat)
|
||||
return ok && !c.Failed && (chat == 0 || c.ChatID() == chat)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -843,7 +843,10 @@ func TestStartHelpAndHints(t *testing.T) {
|
||||
|
||||
seq = h.srv.Seq()
|
||||
h.srv.PushMessage(alice, alice, "/help")
|
||||
h.wait(seq, "sendMessage", textHas("Up to 50 MB and 10 minutes per download"))
|
||||
help := h.wait(seq, "sendMessage", textHas("Your limits: 50 MB and 10 minutes per download, 5 downloads a day."))
|
||||
if !strings.Contains(help.Text(), "Your default format: <b>Best that fits my limit</b>") {
|
||||
t.Fatalf("help = %q", help.Text())
|
||||
}
|
||||
|
||||
seq = h.srv.Seq()
|
||||
h.srv.PushMessage(alice, alice, "hello bot")
|
||||
|
||||
@@ -780,15 +780,17 @@ func TestHistoryResendsFromTheCache(t *testing.T) {
|
||||
seq = h.srv.Seq()
|
||||
h.srv.PushMessage(alice, alice, "/history")
|
||||
list := h.wait(seq, "sendMessage", textHas("Your past downloads"))
|
||||
if !strings.Contains(list.Text(), "1. Test Video · Best") || labels(list) != "1. Test Video" {
|
||||
if labels(list) != "example.com · Test Video · Best · today | Clear history" {
|
||||
t.Fatalf("history = %q buttons=%q", list.Text(), labels(list))
|
||||
}
|
||||
seq = h.press(alice, alice, list, "hv:")
|
||||
item := h.wait(seq, "editMessageText", func(c fakeserver.Call) bool { return c.MessageID == list.MessageID })
|
||||
// Bob cannot resend from Alice's history.
|
||||
seq = h.press(alice, bob, list, "rs:")
|
||||
seq = h.press(alice, bob, item, "rs:")
|
||||
h.wait(seq, "answerCallbackQuery", nil)
|
||||
h.never(seq, "sendVideo", nil)
|
||||
|
||||
seq = h.press(alice, alice, list, "rs:")
|
||||
seq = h.press(alice, alice, item, "rs:")
|
||||
again := h.wait(seq, "sendVideo", nil)
|
||||
if uploaded(again, "video") || len(h.fake.Downloads()) != 1 {
|
||||
t.Fatal("a resend should come from the cache")
|
||||
|
||||
@@ -2,8 +2,10 @@ package app_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -198,6 +200,24 @@ func tour(t *testing.T, h *moneyHarness, user int64, lang string) {
|
||||
seq = send(cmd)
|
||||
h.wait(seq, "sendMessage", inChat)
|
||||
}
|
||||
// Help sections and the history screens.
|
||||
seq = send("/help")
|
||||
help := h.wait(seq, "sendMessage", inChat)
|
||||
for _, key := range []string{"hp:cmds", "hp:acct", "hp:groups", "hp:home"} {
|
||||
seq = h.srv.Seq()
|
||||
h.srv.PushCallback(user, user, help.MessageID, key)
|
||||
h.wait(seq, "editMessageText", func(c fakeserver.Call) bool { return c.MessageID == help.MessageID })
|
||||
}
|
||||
seq = send("/history")
|
||||
hist := h.wait(seq, "sendMessage", inChat)
|
||||
editedHist := func(seq int) fakeserver.Call {
|
||||
return h.wait(seq, "editMessageText", func(c fakeserver.Call) bool { return c.MessageID == hist.MessageID })
|
||||
}
|
||||
item := editedHist(pressOn(hist, "hv:"))
|
||||
back := editedHist(pressOn(item, "hd:"))
|
||||
ask := editedHist(pressOn(back, "hc:"))
|
||||
editedHist(pressOn(ask, "hy"))
|
||||
|
||||
// An invoice.
|
||||
seq = send("/buy")
|
||||
buy := h.wait(seq, "sendMessage", inChat)
|
||||
@@ -224,15 +244,34 @@ func tourFake() *ytdlptest.Fake {
|
||||
}
|
||||
}
|
||||
|
||||
// TestEveryScreenInSpanishAndDutch walks every screen in Spanish and in
|
||||
// Dutch. With DLBOT_SCREENS=<file> it also walks them in English and
|
||||
// writes what each screen shows, in all three languages, to that file for
|
||||
// review: DLBOT_SCREENS=$PWD/docs/SCREENS.md go test ./internal/app -run TestEveryScreen
|
||||
func TestEveryScreenInSpanishAndDutch(t *testing.T) {
|
||||
foreign := englishOnly(t)
|
||||
for _, lang := range []string{"es", "nl"} {
|
||||
langs := []string{"es", "nl"}
|
||||
out := os.Getenv("DLBOT_SCREENS")
|
||||
if out != "" {
|
||||
langs = []string{"en", "es", "nl"}
|
||||
}
|
||||
var dump strings.Builder
|
||||
dump.WriteString("# Screens\n\nWhat the bot shows on every screen of a scripted walk through it, in each\n" +
|
||||
"language, in order. Generated by TestEveryScreenInSpanishAndDutch with\n" +
|
||||
"DLBOT_SCREENS set; do not edit by hand. Buttons are in brackets.\n")
|
||||
for _, lang := range langs {
|
||||
t.Run(lang, func(t *testing.T) {
|
||||
// A missing message or a fallback to English is logged as a bug,
|
||||
// which fails the test (see guardHandler).
|
||||
h := startApp(t, tourFake(), nil)
|
||||
const user = int64(501)
|
||||
tour(t, h, user, lang)
|
||||
if out != "" {
|
||||
writeScreens(&dump, h.harness, user, lang)
|
||||
}
|
||||
if lang == "en" {
|
||||
return
|
||||
}
|
||||
|
||||
var leaks []string
|
||||
for text, where := range shownTo(h.harness, user) {
|
||||
@@ -249,6 +288,57 @@ func TestEveryScreenInSpanishAndDutch(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
if out != "" {
|
||||
if err := os.WriteFile(out, []byte(dump.String()), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// writeScreens appends what a chat showed, call by call, to a review file.
|
||||
func writeScreens(sb *strings.Builder, h *harness, chat int64, lang string) {
|
||||
fmt.Fprintf(sb, "\n## %s\n", lang)
|
||||
last := ""
|
||||
for _, c := range h.srv.Calls("") {
|
||||
if c.Failed {
|
||||
continue
|
||||
}
|
||||
text := c.Text()
|
||||
switch {
|
||||
case c.Method == "answerCallbackQuery":
|
||||
text = c.Params["text"]
|
||||
if text == "" {
|
||||
continue
|
||||
}
|
||||
case c.ChatID() != chat || text == "" || strings.HasPrefix(c.Method, "get") || strings.HasPrefix(c.Method, "set"):
|
||||
continue
|
||||
}
|
||||
// One line per keyboard row, as Telegram shows them.
|
||||
var markup struct {
|
||||
Rows [][]struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"inline_keyboard"`
|
||||
}
|
||||
_ = json.Unmarshal([]byte(c.Params["reply_markup"]), &markup)
|
||||
var btns []string
|
||||
for _, row := range markup.Rows {
|
||||
var line []string
|
||||
for _, b := range row {
|
||||
line = append(line, "["+b.Text+"]")
|
||||
}
|
||||
btns = append(btns, strings.Join(line, " "))
|
||||
}
|
||||
entry := text + "\n" + strings.Join(btns, "\n")
|
||||
if entry == last {
|
||||
continue
|
||||
}
|
||||
last = entry
|
||||
fmt.Fprintf(sb, "\n%s\n\n```\n%s\n", c.Method, text)
|
||||
if len(btns) > 0 {
|
||||
fmt.Fprintf(sb, "%s\n", strings.Join(btns, "\n"))
|
||||
}
|
||||
sb.WriteString("```\n")
|
||||
}
|
||||
}
|
||||
|
||||
// --- language resolution -----------------------------------------------------
|
||||
|
||||
@@ -457,9 +457,9 @@ func TestUserDefaultQualityAndLanguageAndStaticPages(t *testing.T) {
|
||||
h := startMoney(t, nil, nil)
|
||||
seq := h.srv.Seq()
|
||||
h.srv.PushMessage(alice, alice, "/quality")
|
||||
menu := h.wait(seq, "sendMessage", textHas("Pick your default format"))
|
||||
menu := h.wait(seq, "sendMessage", textHas("Your default format"))
|
||||
// Somebody who never chose gets "best that fits my limit".
|
||||
if labels(menu) != "Ask every time | ✓ Best that fits my limit | Best | 1080p | 720p | 480p | Audio only (MP3)" {
|
||||
if labels(menu) != "Ask every time | ✓ Best that fits my limit | Best ⚠ | 1080p ⚠ | 720p ⚠ | 480p ⚠ | Audio only (MP3)" {
|
||||
t.Fatalf("quality buttons = %q", labels(menu))
|
||||
}
|
||||
seq = h.press(alice, alice, menu, "ql:720")
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user