package app_test import ( "encoding/json" "strings" "testing" "time" "github.com/noderunners/dlbot/internal/config" "github.com/noderunners/dlbot/internal/tg/fakeserver" ) type inlineAnswer struct { Results []struct { Type string `json:"type"` ID string `json:"id"` VideoFileID string `json:"video_file_id"` Caption string `json:"caption"` ReplyMarkup struct { Rows [][]struct { URL string `json:"url"` } `json:"inline_keyboard"` } `json:"reply_markup"` } Button struct { Text string `json:"text"` StartParameter string `json:"start_parameter"` } Personal bool } func (h *harness) inline(user int64, query string) inlineAnswer { h.t.Helper() seq := h.srv.Seq() id := h.srv.PushInlineQuery(user, query) c := h.wait(seq, "answerInlineQuery", func(c fakeserver.Call) bool { return c.Params["inline_query_id"] == id }) var a inlineAnswer if err := json.Unmarshal([]byte(c.Params["results"]), &a.Results); err != nil { h.t.Fatalf("results: %v (%s)", err, c.Params["results"]) } _ = json.Unmarshal([]byte(c.Params["button"]), &a.Button) a.Personal = c.Params["is_personal"] == "true" return a } func TestInlineModeSendsCachedFilesWithinLimits(t *testing.T) { h := start(t, nil, func(c *config.Config) { c.InlineModeEnabled = true c.Limits.Free.DailyDownloads = 1 }) h.fetch(alice, link) // the file is now in the cache entry := h.entry(link, "best") // Bob gets the cached file itself, by file_id, with the usual caption. a := h.inline(bob, link) if len(a.Results) != 1 || a.Results[0].Type != "video" || a.Results[0].VideoFileID != entry.TgFileID || !a.Personal { t.Fatalf("answer = %+v, want the cached video %q", a, entry.TgFileID) } if !strings.Contains(a.Results[0].Caption, "Test Video") { t.Fatalf("caption = %q", a.Results[0].Caption) } if n := len(h.fake.Downloads()); n != 1 { t.Fatalf("an inline answer downloaded (%d downloads)", n) } // Sending it counts as his download of the day. h.srv.PushChosenInlineResult(bob, a.Results[0].ID, link) waitFor(t, func() bool { return h.usage(bob).Downloads == 1 }) // The same result reported twice counts once. h.srv.PushChosenInlineResult(bob, a.Results[0].ID, link) // His one download is used up: no file, a way into the bot instead. a = h.inline(bob, link) if len(a.Results) != 1 || a.Results[0].Type != "article" { t.Fatalf("over the limit: %+v", a) } if u := h.usage(bob); u.Downloads != 1 { t.Fatalf("usage = %+v", u) } } func TestInlineModeOpensTheBotForWhatIsNotCached(t *testing.T) { h := start(t, nil, func(c *config.Config) { c.InlineModeEnabled = true }) a := h.inline(carol, "look https://example.com/new?utm_source=x") if len(a.Results) != 1 || a.Results[0].Type != "article" || !strings.HasPrefix(a.Button.StartParameter, "il_") { t.Fatalf("answer = %+v", a) } open := a.Results[0].ReplyMarkup.Rows[0][0].URL if open != "https://t.me/"+fakeserver.BotUsername+"?start="+a.Button.StartParameter || len(a.Button.StartParameter) > 64 { t.Fatalf("deep link %q, start parameter %q", open, a.Button.StartParameter) } // Opening the bot with it runs the usual flow for the link. seq := h.srv.Seq() h.srv.PushMessage(carol, carol, "/start "+a.Button.StartParameter) h.wait(seq, "sendPhoto", func(c fakeserver.Call) bool { _, ok := c.Button("dl:"); return ok && c.ChatID() == carol }) // Not a link: a button that explains inline mode in the bot. if a := h.inline(carol, "cats"); len(a.Results) != 0 || a.Button.StartParameter != "inline" { t.Fatalf("no link: %+v", a) } seq = h.srv.Seq() h.srv.PushMessage(carol, carol, "/start inline") h.wait(seq, "sendMessage", textHas("@"+fakeserver.BotUsername)) } func TestInlineModeIsOffByDefault(t *testing.T) { h := start(t, nil, nil) h.fetch(alice, link) if a := h.inline(bob, link); len(a.Results) != 0 { t.Fatalf("inline mode answered while off: %+v", a) } } // waitFor polls a condition for a few seconds. func waitFor(t *testing.T, cond func() bool) { t.Helper() deadline := time.Now().Add(5 * time.Second) for !cond() { if time.Now().After(deadline) { t.Fatal("condition not met in time") } time.Sleep(20 * time.Millisecond) } }