mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-06-17 15:05:39 +02:00
104 lines
3.3 KiB
Go
104 lines
3.3 KiB
Go
package convert
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"streamrip-go/internal/config"
|
|
)
|
|
|
|
func TestAllowedSampleRates(t *testing.T) {
|
|
got := allowedSampleRates(96000)
|
|
want := []string{"44100", "48000", "88200", "96000"}
|
|
if strings.Join(got, ",") != strings.Join(want, ",") {
|
|
t.Fatalf("rates=%v want=%v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestBuildFFmpegArgsLossless(t *testing.T) {
|
|
cfg := config.ConversionConfig{Enabled: true, Codec: "FLAC", SamplingRate: 48000, BitDepth: 16}
|
|
args := buildFFmpegArgs("in.flac", "out.flac", profiles["FLAC"], cfg)
|
|
joined := strings.Join(args, " ")
|
|
if !strings.Contains(joined, "-c:a flac") {
|
|
t.Fatalf("missing flac codec args=%s", joined)
|
|
}
|
|
if !strings.Contains(joined, "sample_rates=44100|48000") {
|
|
t.Fatalf("missing sample rate filter args=%s", joined)
|
|
}
|
|
if !strings.Contains(joined, "sample_fmts=s16p|s16") {
|
|
t.Fatalf("missing bit depth filter args=%s", joined)
|
|
}
|
|
if !strings.Contains(joined, "-map 0:v:0?") {
|
|
t.Fatalf("missing optional cover map args=%s", joined)
|
|
}
|
|
if !strings.Contains(joined, "-disposition:v:0 attached_pic") {
|
|
t.Fatalf("missing attached_pic disposition args=%s", joined)
|
|
}
|
|
}
|
|
|
|
func TestBuildFFmpegArgsLossy(t *testing.T) {
|
|
cfg := config.ConversionConfig{Enabled: true, Codec: "MP3", LossyBitrate: 320}
|
|
args := buildFFmpegArgs("in.flac", "out.mp3", profiles["MP3"], cfg)
|
|
joined := strings.Join(args, " ")
|
|
if !strings.Contains(joined, "-c:a libmp3lame") {
|
|
t.Fatalf("missing mp3 codec args=%s", joined)
|
|
}
|
|
if !strings.Contains(joined, "-b:a 320k") {
|
|
t.Fatalf("missing bitrate args=%s", joined)
|
|
}
|
|
if !strings.Contains(joined, "-id3v2_version 3") {
|
|
t.Fatalf("missing id3v2 args=%s", joined)
|
|
}
|
|
}
|
|
|
|
func TestBuildFFmpegArgsNoCoverForOpus(t *testing.T) {
|
|
cfg := config.ConversionConfig{Enabled: true, Codec: "OPUS", LossyBitrate: 192}
|
|
args := buildFFmpegArgs("in.flac", "out.opus", profiles["OPUS"], cfg)
|
|
joined := strings.Join(args, " ")
|
|
if strings.Contains(joined, "-map 0:v:0?") {
|
|
t.Fatalf("unexpected cover map args=%s", joined)
|
|
}
|
|
}
|
|
|
|
func TestConvertKeepsSourceWhenRenameFails(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
in := filepath.Join(tmp, "song.flac")
|
|
if err := os.WriteFile(in, []byte("src"), 0o644); err != nil {
|
|
t.Fatalf("write input: %v", err)
|
|
}
|
|
|
|
fakeBin := filepath.Join(tmp, "bin")
|
|
if err := os.MkdirAll(fakeBin, 0o755); err != nil {
|
|
t.Fatalf("mkdir bin: %v", err)
|
|
}
|
|
fakeFFmpeg := filepath.Join(fakeBin, "ffmpeg")
|
|
script := "#!/bin/sh\nout=\"\"\nfor arg in \"$@\"; do out=\"$arg\"; done\n: > \"$out\"\n"
|
|
if err := os.WriteFile(fakeFFmpeg, []byte(script), 0o755); err != nil {
|
|
t.Fatalf("write fake ffmpeg: %v", err)
|
|
}
|
|
t.Setenv("PATH", fakeBin)
|
|
|
|
finalPath := strings.TrimSuffix(in, filepath.Ext(in)) + ".m4a"
|
|
if err := os.Mkdir(finalPath, 0o755); err != nil {
|
|
t.Fatalf("mkdir final path: %v", err)
|
|
}
|
|
|
|
cfg := config.ConversionConfig{Enabled: true, Codec: "ALAC"}
|
|
out, err := Convert(in, cfg)
|
|
if err == nil {
|
|
t.Fatalf("expected rename failure")
|
|
}
|
|
if out != in {
|
|
t.Fatalf("returned path = %q, want %q", out, in)
|
|
}
|
|
if _, statErr := os.Stat(in); statErr != nil {
|
|
t.Fatalf("expected source to remain, stat err=%v", statErr)
|
|
}
|
|
tmpPath := finalPath + ".tmp.m4a"
|
|
if _, statErr := os.Stat(tmpPath); !os.IsNotExist(statErr) {
|
|
t.Fatalf("expected temp output cleanup, stat err=%v", statErr)
|
|
}
|
|
}
|