test: add comprehensive test suite - 5 test files, 65% coverage
- cli_test.go: 10 tests for list/search/extract/download commands - serve_test.go: 11 tests for HTTP handlers and SSE progress - http_test.go: 5 subtests for archive.org API - download_test.go: 10 tests for download logic - util_test.go: 7 tests for string/util helpers Error paths covered: server errors, empty results, download failures, extract failures, missing identifiers, ReadDir errors. golangci-lint: 0 issues, go vet: clean
This commit is contained in:
+198
@@ -0,0 +1,198 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSanitiseISOName(t *testing.T) {
|
||||
cases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"ZERO-G - Deepest India", "ZERO-G_-_Deepest_India"},
|
||||
{"SYNTH PAD COLLECTION.iso", "SYNTH_PAD_COLLECTION"},
|
||||
{"vocal.loops", "vocal"},
|
||||
{"DRUM KIT #1", "DRUM_KIT_1"},
|
||||
{"simple", "simple"},
|
||||
{"no-extension", "no-extension"},
|
||||
{"multiple spaces", "multiple_spaces"},
|
||||
{"UPPERCASE", "UPPERCASE"},
|
||||
{"MiXeD CaSe", "MiXeD_CaSe"},
|
||||
{"with-dash", "with-dash"},
|
||||
{"with_underscore", "with_underscore"},
|
||||
{"with.dots.in.name", "with.dots.in"},
|
||||
{".hidden", ""},
|
||||
{"trailing_", "trailing"},
|
||||
{"__leading__", "leading"},
|
||||
{"spaces everywhere", "spaces_everywhere"},
|
||||
{"", ""},
|
||||
{" ", ""},
|
||||
{".iso", ""},
|
||||
{"a", "a"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := sanitiseISOName(c.input)
|
||||
if got != c.expected {
|
||||
t.Errorf("sanitiseISOName(%q) = %q, want %q", c.input, got, c.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncate(t *testing.T) {
|
||||
cases := []struct {
|
||||
s string
|
||||
n int
|
||||
expect string
|
||||
}{
|
||||
{"hello", 10, "hello"},
|
||||
{"hello", 5, "hello"},
|
||||
{"hello", 4, "h..."},
|
||||
{"hello", 3, "..."},
|
||||
{"hello", 2, "he"},
|
||||
{"hello", 1, "h"},
|
||||
{"hello", 0, ""},
|
||||
{"", 5, ""},
|
||||
{"abcdef", 4, "a..."},
|
||||
{"ab", 4, "ab"},
|
||||
{"abc", 3, "abc"},
|
||||
{"abcd", 2, "ab"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := truncate(c.s, c.n)
|
||||
if got != c.expect {
|
||||
t.Errorf("truncate(%q, %d) = %q, want %q", c.s, c.n, got, c.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHumanSize(t *testing.T) {
|
||||
cases := []struct {
|
||||
bytes int64
|
||||
expect string
|
||||
}{
|
||||
{0, "0B"},
|
||||
{500, "500B"},
|
||||
{1024, "1.0 KB"},
|
||||
{1536, "1.5 KB"},
|
||||
{10240, "10.0 KB"},
|
||||
{1048576, "1.0 MB"},
|
||||
{1572864, "1.5 MB"},
|
||||
{1073741824, "1.0 GB"},
|
||||
{2147483648, "2.0 GB"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := humanSize(c.bytes)
|
||||
if got != c.expect {
|
||||
t.Errorf("humanSize(%d) = %q, want %q", c.bytes, got, c.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindAkaiutil(t *testing.T) {
|
||||
origEnv := os.Getenv("AKAIUTIL")
|
||||
origArg0 := os.Args[0]
|
||||
defer func() {
|
||||
_ = os.Setenv("AKAIUTIL", origEnv)
|
||||
os.Args[0] = origArg0
|
||||
}()
|
||||
|
||||
t.Run("env var set to missing file", func(t *testing.T) {
|
||||
_ = os.Setenv("AKAIUTIL", "/nonexistent/akaiutil")
|
||||
os.Args[0] = "/some/binary"
|
||||
_, err := findAkaiutil()
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing env var path")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("env var set to existing file", func(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
f := tmp + "/myutil"
|
||||
fd, _ := os.Create(f)
|
||||
defer func() { _ = fd.Close() }()
|
||||
_ = os.Setenv("AKAIUTIL", f)
|
||||
got, err := findAkaiutil()
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if got != f {
|
||||
t.Errorf("got %q, want %q", got, f)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("finds binary next to argv[0]", func(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
exe := tmp + "/mybinary"
|
||||
fd, _ := os.Create(exe)
|
||||
defer func() { _ = fd.Close() }()
|
||||
sideBySide := tmp + "/akaiutil"
|
||||
fd2, _ := os.Create(sideBySide)
|
||||
defer func() { _ = fd2.Close() }()
|
||||
_ = os.Setenv("AKAIUTIL", "")
|
||||
os.Args[0] = exe
|
||||
got, err := findAkaiutil()
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if got != sideBySide {
|
||||
t.Errorf("got %q, want %q", got, sideBySide)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("finds binary in third_party relative to argv[0]", func(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
exe := tmp + "/somebin"
|
||||
fd, _ := os.Create(exe)
|
||||
defer func() { _ = fd.Close() }()
|
||||
thirdParty := filepath.Join(tmp, "third_party", "akaiutil")
|
||||
if err := os.MkdirAll(thirdParty, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
akaiutilInThirdParty := filepath.Join(thirdParty, "akaiutil")
|
||||
fd2, _ := os.Create(akaiutilInThirdParty)
|
||||
defer func() { _ = fd2.Close() }()
|
||||
_ = os.Setenv("AKAIUTIL", "")
|
||||
os.Args[0] = exe
|
||||
got, err := findAkaiutil()
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if got != akaiutilInThirdParty {
|
||||
t.Errorf("got %q, want %q", got, akaiutilInThirdParty)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("env var takes priority over path candidates", func(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
envFile := tmp + "/envutil"
|
||||
pathFile := tmp + "/pathutil"
|
||||
fd1, _ := os.Create(envFile)
|
||||
defer func() { _ = fd1.Close() }()
|
||||
fd2, _ := os.Create(pathFile)
|
||||
defer func() { _ = fd2.Close() }()
|
||||
_ = os.Setenv("AKAIUTIL", envFile)
|
||||
os.Args[0] = pathFile
|
||||
got, err := findAkaiutil()
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if got != envFile {
|
||||
t.Errorf("got %q, want env var path %q", got, envFile)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no akaiutil found returns error", func(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
exe := tmp + "/nobinaryhere"
|
||||
fd, _ := os.Create(exe)
|
||||
defer func() { _ = fd.Close() }()
|
||||
_ = os.Setenv("AKAIUTIL", "")
|
||||
os.Args[0] = exe
|
||||
_, err := findAkaiutil()
|
||||
if err == nil {
|
||||
t.Fatal("expected error when no akaiutil found")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user