test: add simple unit tests for backend and logging packages
Build / build (push) Successful in 31s

This commit is contained in:
2026-05-22 22:49:25 +02:00
parent b71072f6c8
commit 92b82da3af
5 changed files with 634 additions and 0 deletions
+90
View File
@@ -0,0 +1,90 @@
package backend
import (
"io/fs"
"os"
"testing"
)
func TestIsImage(t *testing.T) {
tests := []struct {
name string
entry fs.DirEntry
expected bool
}{
{
name: "jpg file",
entry: &mockDirEntry{name: "test.jpg", isDir: false},
expected: true,
},
{
name: "jpeg file",
entry: &mockDirEntry{name: "test.jpeg", isDir: false},
expected: true,
},
{
name: "png file",
entry: &mockDirEntry{name: "test.png", isDir: false},
expected: true,
},
{
name: "directory",
entry: &mockDirEntry{name: "test", isDir: true},
expected: false,
},
{
name: "txt file",
entry: &mockDirEntry{name: "test.txt", isDir: false},
expected: false,
},
{
name: "mp3 file",
entry: &mockDirEntry{name: "test.mp3", isDir: false},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isImage(tt.entry)
if result != tt.expected {
t.Errorf("isImage() = %v, want %v", result, tt.expected)
}
})
}
}
type mockDirEntry struct {
name string
isDir bool
}
func (m *mockDirEntry) Name() string { return m.name }
func (m *mockDirEntry) IsDir() bool { return m.isDir }
func (m *mockDirEntry) Type() fs.FileMode { return 0 }
func (m *mockDirEntry) Info() (fs.FileInfo, error) { return nil, nil }
func (m *mockDirEntry) Sys() interface{} { return nil }
func TestGetCharacter(t *testing.T) {
os.Setenv("CHARACTERS_PATH", "/test/path")
defer os.Unsetenv("CHARACTERS_PATH")
result := GetCharacter("test.jpg")
expected := "/test/path/test.jpg"
if result != expected {
t.Errorf("GetCharacter() = %v, want %v", result, expected)
}
}
func TestGetCharacterWithTrailingSlash(t *testing.T) {
os.Setenv("CHARACTERS_PATH", "/test/path/")
defer os.Unsetenv("CHARACTERS_PATH")
result := GetCharacter("test.jpg")
expected := "/test/path/test.jpg"
if result != expected {
t.Errorf("GetCharacter() = %v, want %v", result, expected)
}
}