Files
tiennm99bot/internal/modules/util/sticker_source_test.go
T
tiennm99 2503353e68 feat(util): replace per-user sticker packs with one shared, self-creating pack
/addsticker becomes a single stateless command in util, writing to one
env-configured set (STICKER_PACK_NAME, default miti99_by_miti99bot).
AddStickerToSet takes the set owner's user ID rather than the caller's, so
nothing is per-user any more: the sticker module's pack records, slug
reservations, pending deletes, per-user locks and its eight other commands are
removed with it.

The pack creates itself on first use. A positive STICKERSET_INVALID from the
add triggers createNewStickerSet owned by OWNER_ID, seeded with the sticker
that triggered it and titled with the slug half of the name; a name that is
occupied but unwritable is reported instead of taken over. The mandatory
"_by_<bot_username>" suffix is Telegram's own proof of authorship, so a
misconfigured pack name is refused offline before any API call. StickerSet
exposes no owner ID, so ownership is only provable when Telegram refuses.

Video, GIF, animation and video-note sources are transcoded to WEBM/VP9 with
ffmpeg: long edge scaled to exactly 512 in either direction, cut to 3s, capped
at 30fps, audio dropped, retried down a CRF ladder until under 256KB. Animated
and video stickers are copied by file_id with no conversion. Sticker format is
per-sticker since Bot API 7.2, so one pack holds all three.

ffmpeg cannot ship in distroless/static and Go has no VP9 encoder, so the
runtime base becomes alpine with apk add ffmpeg. The image grows from roughly
20MB to 213MB, and the transcode holds the single dispatcher worker — bounded
at 20s per encode and a 45s handler deadline for moving sources, against 10s
for stills.
2026-09-04 10:34:29 +07:00

128 lines
4.4 KiB
Go

package util
import (
"testing"
"github.com/go-telegram/bot/models"
)
// hasMovingSource picks which of the two conversion paths a reply takes, from
// the message alone. Getting it wrong sends a video at the image resampler, or
// an image at ffmpeg.
func TestHasMovingSource(t *testing.T) {
cases := []struct {
name string
replied *models.Message
want bool
}{
{name: "nil reply", replied: nil, want: false},
{name: "animation", replied: &models.Message{Animation: &models.Animation{FileID: "a"}}, want: true},
{name: "video", replied: &models.Message{Video: &models.Video{FileID: "v"}}, want: true},
{name: "video note", replied: &models.Message{VideoNote: &models.VideoNote{FileID: "n"}}, want: true},
{name: "gif document", replied: &models.Message{Document: &models.Document{MimeType: "image/gif"}}, want: true},
{name: "mp4 document", replied: &models.Message{Document: &models.Document{MimeType: "video/mp4"}}, want: true},
{name: "webm document", replied: &models.Message{Document: &models.Document{MimeType: "video/webm"}}, want: true},
{name: "mov document", replied: &models.Message{Document: &models.Document{MimeType: "video/quicktime"}}, want: true},
{name: "png document", replied: &models.Message{Document: &models.Document{MimeType: "image/png"}}, want: false},
{name: "photo", replied: &models.Message{Photo: []models.PhotoSize{{FileID: "p"}}}, want: false},
{name: "sticker", replied: &models.Message{Sticker: &models.Sticker{FileID: "s"}}, want: false},
{name: "pdf document", replied: &models.Message{Document: &models.Document{MimeType: "application/pdf"}}, want: false},
{name: "plain text", replied: &models.Message{Text: "hello"}, want: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := hasMovingSource(tc.replied); got != tc.want {
t.Errorf("hasMovingSource = %v, want %v", got, tc.want)
}
})
}
}
// Source selection happens before any download, so an oversized clip costs
// nothing.
func TestVideoFileID(t *testing.T) {
cases := []struct {
name string
replied *models.Message
want string
ok bool
}{
{
name: "animation",
replied: &models.Message{Animation: &models.Animation{FileID: "anim", FileSize: 1 << 20}},
want: "anim", ok: true,
},
{
name: "video",
replied: &models.Message{Video: &models.Video{FileID: "vid", FileSize: 1 << 20}},
want: "vid", ok: true,
},
{
name: "video note",
replied: &models.Message{VideoNote: &models.VideoNote{FileID: "note", FileSize: 1 << 20}},
want: "note", ok: true,
},
{
name: "document",
replied: &models.Message{Document: &models.Document{FileID: "doc", MimeType: "video/mp4"}},
want: "doc", ok: true,
},
{
// Animation is checked before Video: Telegram attaches both to a
// GIF-style message, and the animation is the one to convert.
name: "animation wins over video",
replied: &models.Message{
Animation: &models.Animation{FileID: "anim"},
Video: &models.Video{FileID: "vid"},
},
want: "anim", ok: true,
},
{
name: "oversized animation refused",
replied: &models.Message{Animation: &models.Animation{FileID: "anim", FileSize: maxVideoSourceBytes + 1}},
ok: false,
},
{
name: "oversized video refused",
replied: &models.Message{Video: &models.Video{FileID: "vid", FileSize: maxVideoSourceBytes + 1}},
ok: false,
},
{
name: "oversized document refused",
replied: &models.Message{Document: &models.Document{FileID: "doc", MimeType: "video/mp4", FileSize: maxVideoSourceBytes + 1}},
ok: false,
},
{
name: "nothing usable",
replied: &models.Message{Text: "hello"},
ok: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := videoFileID(tc.replied)
if tc.ok {
if err != nil {
t.Fatalf("videoFileID: %v", err)
}
if got != tc.want {
t.Errorf("file_id = %q, want %q", got, tc.want)
}
return
}
if err == nil {
t.Fatalf("videoFileID = %q, want a refusal", got)
}
})
}
}
// The video cap is the one on the source, and it is an order of magnitude above
// the image cap on purpose — the 256 KB sticker limit applies to the transcode
// output, not the input.
func TestVideoSourceCapExceedsImageCap(t *testing.T) {
if maxVideoSourceBytes <= maxSourceBytes {
t.Errorf("maxVideoSourceBytes (%d) must exceed maxSourceBytes (%d)", maxVideoSourceBytes, maxSourceBytes)
}
}