Files
Media-download-telegram-bot/internal/ffmpeg/ffmpeg_test.go
T
MichilisandClaude Fable 5.2 9c9d641eda Phase 3: cache
Repeat downloads are instant, and the cache never bypasses a limit.

- internal/cache: canonical key (extractor + video id + profile, URL hash
  fallback), per-bot file_id layer, stored copy layer, metadata layer,
  TTL by source type, LRU-plus-hits eviction that keeps file_ids, orphan
  sweeper, takedown that deletes, invalidates and blocks
- One admission gate: a cache hit passes the same size, duration, quota,
  volume, active job and queue checks as a fresh download, is judged by
  the real cached file, and costs the same. It only skips the worker
- Singleflight: a second requester joins the job that is queued or running
  for the same result. Each keeps their own caps, checked at delivery
- Derive: lower profiles and audio are transcoded from a stored copy with
  ffmpeg; a higher profile that already fits is served as an alias; a
  failed derive falls back to a download
- Self-healing: a rejected file_id falls back to the stored copy, then to
  a fresh download, and the entry is updated either way
- Private or login-gated content and live streams are never cached
- Playlists as per-item jobs started in turn, /subs, /history with resend,
  /fresh, owner /purge and /cache
- Worker removes its temp files right after the upload

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 08:38:38 +00:00

94 lines
3.3 KiB
Go

package ffmpeg
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/noderunners/dlbot/internal/media"
"github.com/noderunners/dlbot/internal/ytdlp"
)
func TestArgs(t *testing.T) {
tests := map[media.Profile]struct{ want, out string }{
media.P480: {"-vf scale=-2:480 -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k -movflags +faststart", "media.mp4"},
media.P720: {"-vf scale=-2:720", "media.mp4"},
media.MP3: {"-vn -c:a libmp3lame -b:a 192k", "media.mp3"},
media.M4A: {"-vn -c:a aac -b:a 160k -movflags +faststart", "media.m4a"},
}
for p, tc := range tests {
args, out, err := Args("/src/in", "/dst", p)
if err != nil {
t.Errorf("%s: %v", p, err)
continue
}
joined := strings.Join(args, " ")
if !strings.Contains(joined, tc.want) || out != filepath.Join("/dst", tc.out) || args[len(args)-1] != out {
t.Errorf("%s: args = %q, out = %q", p, joined, out)
}
if !strings.Contains(joined, "-i /src/in") || !strings.Contains(joined, "-nostdin") {
t.Errorf("%s: input or -nostdin missing: %q", p, joined)
}
}
// Profiles that are not made by transcoding.
for _, p := range []media.Profile{media.Best, media.Thumb, media.Subs("en")} {
if _, _, err := Args("in", "out", p); err == nil {
t.Errorf("%s must not be derivable", p)
}
}
}
// TestDeriveWithRealFfmpeg makes a two second 720p test clip and derives
// 480p and MP3 from it. It is skipped when ffmpeg is not installed.
func TestDeriveWithRealFfmpeg(t *testing.T) {
if _, err := exec.LookPath("ffmpeg"); err != nil {
t.Skip("ffmpeg is not installed")
}
dir := t.TempDir()
src := filepath.Join(dir, "source.mp4")
gen := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "error", "-y",
"-f", "lavfi", "-i", "testsrc=duration=2:size=1280x720:rate=15",
"-f", "lavfi", "-i", "sine=frequency=440:duration=2",
"-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest", src)
if out, err := gen.CombinedOutput(); err != nil {
t.Skipf("cannot generate a test clip with this ffmpeg build: %v %s", err, out)
}
e := &Exec{Timeout: time.Minute}
ctx := context.Background()
video, err := e.Derive(ctx, src, filepath.Join(dir, "v"), media.P480)
if err != nil {
t.Fatalf("derive 480: %v", err)
}
if video.Ext != "mp4" || video.Height != 480 || video.Width != 854 || video.Size == 0 ||
video.Duration < 1.5 || video.Duration > 2.6 {
t.Fatalf("480p result = %+v", video)
}
audio, err := e.Derive(ctx, src, filepath.Join(dir, "a"), media.MP3)
if err != nil {
t.Fatalf("derive mp3: %v", err)
}
if audio.Ext != "mp3" || audio.Size == 0 || audio.Width != 0 || audio.Duration < 1.5 {
t.Fatalf("mp3 result = %+v", audio)
}
// A source that is not media gives a classified error, not a crash.
bad := filepath.Join(dir, "bad.mp4")
if err := os.WriteFile(bad, []byte("this is not a video"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := e.Derive(ctx, bad, filepath.Join(dir, "b"), media.P480); ytdlp.CodeOf(err) != ytdlp.CodeProcessing {
t.Fatalf("bad source: %v, want processing_failed", err)
}
// Cancelling stops ffmpeg.
cctx, cancel := context.WithCancel(ctx)
cancel()
if _, err := e.Derive(cctx, src, filepath.Join(dir, "c"), media.P480); ytdlp.CodeOf(err) != ytdlp.CodeCancelled {
t.Fatalf("cancelled derive: %v, want cancelled", err)
}
}