mirror of
https://github.com/tiennm99/tiennm99bot.git
synced 2026-10-11 03:13:46 +00:00
/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.
152 lines
4.3 KiB
Go
152 lines
4.3 KiB
Go
package util
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// packTitle is the only ownership check available offline, so its edges matter:
|
|
// a false accept sends the bot at a set it cannot manage, and a false reject
|
|
// disables /addsticker outright.
|
|
func TestPackTitle(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
packName string
|
|
username string
|
|
wantTitle string
|
|
wantErr error
|
|
}{
|
|
{
|
|
name: "slug half becomes the title",
|
|
packName: "miti99_by_miti99bot",
|
|
username: "miti99bot",
|
|
wantTitle: "miti99",
|
|
},
|
|
{
|
|
// Telegram documents <bot_username> as case insensitive, and
|
|
// returns SetName with whatever casing the set was created with.
|
|
name: "suffix matches case insensitively",
|
|
packName: "Shared_By_TestBot",
|
|
username: "testbot",
|
|
wantTitle: "Shared",
|
|
},
|
|
{
|
|
name: "underscores inside the slug survive",
|
|
packName: "my_shared_pack_by_testbot",
|
|
username: "testbot",
|
|
wantTitle: "my_shared_pack",
|
|
},
|
|
{
|
|
name: "another bot's set",
|
|
packName: "shared_by_otherbot",
|
|
username: "testbot",
|
|
wantErr: errPackNotBotOwned,
|
|
},
|
|
{
|
|
name: "no suffix at all",
|
|
packName: "shared",
|
|
username: "testbot",
|
|
wantErr: errPackNotBotOwned,
|
|
},
|
|
{
|
|
// The suffix alone leaves no slug, and Telegram requires a name to
|
|
// begin with a letter.
|
|
name: "empty slug",
|
|
packName: "_by_testbot",
|
|
username: "testbot",
|
|
wantErr: errPackNotBotOwned,
|
|
},
|
|
{
|
|
name: "past Telegram's name length cap",
|
|
packName: strings.Repeat("a", maxSetNameLen-len("_by_testbot")+1) + "_by_testbot",
|
|
username: "testbot",
|
|
wantErr: errPackNotBotOwned,
|
|
},
|
|
{
|
|
// A GetMe that returned no username must not be read as "matches
|
|
// nothing", which would report a configuration fault instead of a
|
|
// transient one.
|
|
name: "unknown bot username",
|
|
packName: "shared_by_testbot",
|
|
username: "",
|
|
wantErr: errNoUsername,
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, err := packTitle(tc.packName, tc.username)
|
|
if tc.wantErr != nil {
|
|
if !errors.Is(err, tc.wantErr) {
|
|
t.Fatalf("packTitle(%q, %q) err = %v, want %v", tc.packName, tc.username, err, tc.wantErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("packTitle(%q, %q): %v", tc.packName, tc.username, err)
|
|
}
|
|
if got != tc.wantTitle {
|
|
t.Errorf("title = %q, want %q", got, tc.wantTitle)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The name is at Telegram's exact cap, which must be accepted rather than
|
|
// rejected off-by-one.
|
|
func TestPackTitle_AtNameLengthCap(t *testing.T) {
|
|
slug := strings.Repeat("a", maxSetNameLen-len("_by_testbot"))
|
|
got, err := packTitle(slug+"_by_testbot", "testbot")
|
|
if err != nil {
|
|
t.Fatalf("packTitle at the %d-char cap: %v", maxSetNameLen, err)
|
|
}
|
|
if got != slug {
|
|
t.Errorf("title = %q, want %q", got, slug)
|
|
}
|
|
}
|
|
|
|
func TestLoadStickerPack(t *testing.T) {
|
|
t.Run("defaults the name and requires an owner", func(t *testing.T) {
|
|
t.Setenv("OWNER_ID", "42")
|
|
t.Setenv("STICKER_PACK_NAME", "")
|
|
pack, err := loadStickerPack()
|
|
if err != nil {
|
|
t.Fatalf("loadStickerPack: %v", err)
|
|
}
|
|
if pack.Name != defaultStickerPackName {
|
|
t.Errorf("name = %q, want %q", pack.Name, defaultStickerPackName)
|
|
}
|
|
if pack.OwnerID != 42 {
|
|
t.Errorf("ownerID = %d, want 42", pack.OwnerID)
|
|
}
|
|
})
|
|
|
|
// A zero owner is the unset case, not a valid user: AddStickerToSet needs a
|
|
// real account, so it must fail here rather than at the API.
|
|
for _, owner := range []string{"", "0", "not-a-number"} {
|
|
t.Run("rejects owner "+owner, func(t *testing.T) {
|
|
t.Setenv("OWNER_ID", owner)
|
|
if _, err := loadStickerPack(); !errors.Is(err, errNoPackOwner) {
|
|
t.Errorf("loadStickerPack() err = %v, want errNoPackOwner", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsStickerSetMissingAndOccupied(t *testing.T) {
|
|
// isStickerSetMissing is positive-only: it authorises creating a set, so a
|
|
// transport failure must never satisfy it.
|
|
if isStickerSetMissing(errors.New("connection reset")) {
|
|
t.Error("a plain error was read as a missing sticker set")
|
|
}
|
|
if isStickerSetMissing(errors.New("Bad Request: STICKERSET_INVALID")) {
|
|
t.Error("a codeless error was read as a missing sticker set")
|
|
}
|
|
if !isPackNameOccupied(errors.New("Bad Request: PACK_SHORT_NAME_OCCUPIED")) {
|
|
t.Error("PACK_SHORT_NAME_OCCUPIED not recognised")
|
|
}
|
|
if isPackNameOccupied(nil) {
|
|
t.Error("nil error read as occupied")
|
|
}
|
|
}
|