Phase 1: core bot
Link in, card, download, send, in private chats and groups. - cmd/core with an embedded download worker - Telegram front end on telego v1.13.0 (Bot API 10.4): link card with thumbnail and format buttons, one live status message, /dl /audio /thumb /info /queue /cancel, group support with requester-only buttons - Database backed queue: paid and free lanes, leases with heartbeats, one automatic retry, cancel, queue position and wait estimate - yt-dlp and ffprobe behind a Runner interface with classified, redacted errors mapped to friendly messages - SQLite via modernc with goose migrations, portable schema - Local file storage behind a Storage interface - go-i18n with an English catalog, no literal text in handlers - Free tier limits enforced before queueing and again on the real file - Bot token and webhook secret are stripped from all log output - Tests: URL parsing, job lifecycle, worker, yt-dlp wrapper, and end to end flows against a fake Bot API server; opt-in test with the real yt-dlp Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func lookup(env map[string]string) func(string) (string, bool) {
|
||||
return func(k string) (string, bool) {
|
||||
v, ok := env[k]
|
||||
return v, ok
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultsAreValidOnceTokenIsSet(t *testing.T) {
|
||||
cfg := Default()
|
||||
if err := cfg.Validate(); err == nil || !strings.Contains(err.Error(), "BOT_TOKEN") {
|
||||
t.Fatalf("missing token must be reported, got %v", err)
|
||||
}
|
||||
cfg.Telegram.Token = "x"
|
||||
if err := cfg.Validate(); err != nil {
|
||||
t.Fatalf("defaults should validate: %v", err)
|
||||
}
|
||||
if cfg.Limits.Free.MaxSizeMB != 50 || cfg.Limits.Free.MaxDuration.D() != 10*time.Minute ||
|
||||
cfg.Limits.Free.ActiveJobs != 1 || cfg.Queue.FreeMaxLen != 20 {
|
||||
t.Fatalf("free tier defaults changed: %+v %+v", cfg.Limits.Free, cfg.Queue)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyEnv(t *testing.T) {
|
||||
cfg := Default()
|
||||
env := map[string]string{
|
||||
"BOT_TOKEN": "123:abc",
|
||||
"TELEGRAM_MODE": "webhook",
|
||||
"WEBHOOK_URL": "https://bot.example.com",
|
||||
"WORKER_EMBEDDED": "false",
|
||||
"WORKER_CONCURRENCY": "5",
|
||||
"DOWNLOAD_TIMEOUT": "45m",
|
||||
"OWNER_IDS": "11, 22;33",
|
||||
"DB_DRIVER": "postgres",
|
||||
"LOG_LEVEL": "",
|
||||
}
|
||||
if err := applyEnv(reflect.ValueOf(&cfg).Elem(), lookup(env)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Telegram.Token != "123:abc" || cfg.Telegram.Mode != "webhook" || cfg.Worker.Embedded ||
|
||||
cfg.Worker.Concurrency != 5 || cfg.Worker.DownloadTimeout.D() != 45*time.Minute ||
|
||||
cfg.Database.Driver != "postgres" {
|
||||
t.Fatalf("env not applied: %+v", cfg)
|
||||
}
|
||||
if !reflect.DeepEqual(cfg.Owners, []int64{11, 22, 33}) {
|
||||
t.Fatalf("owners = %v", cfg.Owners)
|
||||
}
|
||||
if cfg.Log.Level != "info" {
|
||||
t.Fatalf("an empty variable must not override the default, got %q", cfg.Log.Level)
|
||||
}
|
||||
if !cfg.IsOwner(22) || cfg.IsOwner(44) {
|
||||
t.Fatal("IsOwner is wrong")
|
||||
}
|
||||
if err := applyTierEnv("FREE", &cfg.Limits.Free, lookup(map[string]string{
|
||||
"FREE_MAX_SIZE_MB": "25", "FREE_MAX_DURATION": "5m", "FREE_ACTIVE_JOBS": "2",
|
||||
})); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Limits.Free.MaxSizeMB != 25 || cfg.Limits.Free.MaxDuration.D() != 5*time.Minute || cfg.Limits.Free.ActiveJobs != 2 {
|
||||
t.Fatalf("tier env not applied: %+v", cfg.Limits.Free)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyEnvRejectsBadValues(t *testing.T) {
|
||||
for k, v := range map[string]string{
|
||||
"WORKER_CONCURRENCY": "many", "WORKER_EMBEDDED": "maybe", "DOWNLOAD_TIMEOUT": "soon", "OWNER_IDS": "1,x",
|
||||
} {
|
||||
cfg := Default()
|
||||
err := applyEnv(reflect.ValueOf(&cfg).Elem(), lookup(map[string]string{k: v}))
|
||||
if err == nil || !strings.Contains(err.Error(), k) {
|
||||
t.Errorf("%s=%s: error %v should name the variable", k, v, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
bad := []func(*Config){
|
||||
func(c *Config) { c.Telegram.Mode = "carrier-pigeon" },
|
||||
func(c *Config) { c.Telegram.Mode = "webhook" }, // no URL
|
||||
func(c *Config) { c.Database.Driver = "oracle" },
|
||||
func(c *Config) { c.Storage.Backend = "tape" },
|
||||
func(c *Config) { c.Worker.Concurrency = 0 },
|
||||
func(c *Config) { c.Queue.MaxAttempts = 0 },
|
||||
func(c *Config) { c.Queue.Heartbeat = c.Queue.Lease },
|
||||
}
|
||||
for i, mut := range bad {
|
||||
cfg := Default()
|
||||
cfg.Telegram.Token = "x"
|
||||
mut(&cfg)
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Errorf("case %d should not validate", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFileThenEnv(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.toml")
|
||||
content := `
|
||||
owners = [5, 6]
|
||||
|
||||
[telegram]
|
||||
token = "from-file"
|
||||
max_upload_mb = 2000
|
||||
|
||||
[worker]
|
||||
concurrency = 3
|
||||
download_timeout = "1h"
|
||||
|
||||
[limits.free]
|
||||
max_size_mb = 30
|
||||
max_duration = "7m"
|
||||
active_jobs = 1
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Chdir(dir) // keeps a developer's real .env out of the test
|
||||
t.Setenv("BOT_TOKEN", "from-env")
|
||||
t.Setenv("DLBOT_CONFIG", "")
|
||||
cfg, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Telegram.Token != "from-env" {
|
||||
t.Errorf("env must override the file, token = %q", cfg.Telegram.Token)
|
||||
}
|
||||
if cfg.Worker.Concurrency != 3 || cfg.Worker.DownloadTimeout.D() != time.Hour ||
|
||||
cfg.Limits.Free.MaxSizeMB != 30 || cfg.Limits.Free.MaxDuration.D() != 7*time.Minute {
|
||||
t.Errorf("file values not loaded: %+v %+v", cfg.Worker, cfg.Limits.Free)
|
||||
}
|
||||
if cfg.MaxUploadBytes() != 2000<<20 || !reflect.DeepEqual(cfg.Owners, []int64{5, 6}) {
|
||||
t.Errorf("upload cap or owners wrong: %d %v", cfg.MaxUploadBytes(), cfg.Owners)
|
||||
}
|
||||
if cfg.HTTP.Listen != ":8080" {
|
||||
t.Errorf("unset values keep their defaults, listen = %q", cfg.HTTP.Listen)
|
||||
}
|
||||
if _, err := Load(filepath.Join(dir, "missing.toml")); err == nil {
|
||||
t.Error("an explicit config path that does not exist must be an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDotEnvDoesNotOverrideRealEnv(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
env := "# comment\nexport DLBOT_TEST_A=from-dotenv\nDLBOT_TEST_B=\"quoted value\"\nnot a pair\n"
|
||||
path := filepath.Join(dir, ".env")
|
||||
if err := os.WriteFile(path, []byte(env), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("DLBOT_TEST_A", "from-real-env")
|
||||
os.Unsetenv("DLBOT_TEST_B")
|
||||
t.Cleanup(func() { os.Unsetenv("DLBOT_TEST_B") })
|
||||
if err := loadDotEnv(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := os.Getenv("DLBOT_TEST_A"); got != "from-real-env" {
|
||||
t.Errorf("A = %q", got)
|
||||
}
|
||||
if got := os.Getenv("DLBOT_TEST_B"); got != "quoted value" {
|
||||
t.Errorf("B = %q", got)
|
||||
}
|
||||
if err := loadDotEnv(filepath.Join(dir, "absent")); err != nil {
|
||||
t.Errorf("a missing .env is fine, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user