Files
Media-download-telegram-bot/internal/storage/local_test.go
T
MichilisandClaude Fable 5.2 3b361c17a1 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>
2026-11-18 07:33:32 +00:00

69 lines
1.7 KiB
Go

package storage
import (
"context"
"errors"
"io"
"os"
"strings"
"testing"
)
func TestLocalRoundTrip(t *testing.T) {
ctx := context.Background()
st, err := NewLocal(t.TempDir())
if err != nil {
t.Fatal(err)
}
if err := st.Put(ctx, "jobs/a/media.mp4", strings.NewReader("hello"), 5); err != nil {
t.Fatal(err)
}
rc, size, err := st.Open(ctx, "jobs/a/media.mp4")
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(rc)
rc.Close()
if string(b) != "hello" || size != 5 {
t.Fatalf("got %q size %d", b, size)
}
obj, err := st.Stat(ctx, "jobs/a/media.mp4")
if err != nil || obj.Size != 5 {
t.Fatalf("Stat = %+v, %v", obj, err)
}
var keys []string
if err := st.List(ctx, "jobs/", func(o Object) error { keys = append(keys, o.Key); return nil }); err != nil {
t.Fatal(err)
}
if len(keys) != 1 || keys[0] != "jobs/a/media.mp4" {
t.Fatalf("List = %v", keys)
}
if err := st.Delete(ctx, "jobs/a/media.mp4"); err != nil {
t.Fatal(err)
}
if _, _, err := st.Open(ctx, "jobs/a/media.mp4"); !errors.Is(err, ErrNotFound) {
t.Fatalf("Open after delete: %v", err)
}
if err := st.Delete(ctx, "jobs/a/media.mp4"); err != nil {
t.Fatalf("second delete should be a no-op: %v", err)
}
if _, err := os.Stat(st.root + "/jobs"); !os.IsNotExist(err) {
t.Fatalf("empty parent directories should be removed")
}
}
func TestLocalRejectsEscapingKeys(t *testing.T) {
ctx := context.Background()
st, err := NewLocal(t.TempDir())
if err != nil {
t.Fatal(err)
}
for _, key := range []string{"", "/abs", "../x", "a/../../x", "a//b", "a/./b", `a\b`} {
if err := st.Put(ctx, key, strings.NewReader("x"), 1); err == nil {
t.Errorf("Put(%q) should fail", key)
}
}
}