Files
go-iar-notificator/utils/encoder_test.go
2025-09-08 13:27:09 -04:00

67 lines
1.8 KiB
Go

package utils_test
import (
"os"
"path/filepath"
"strings"
"testing"
"git.savin.nyc/alex/go-iar-notificator/utils"
)
func TestMp3ToBase64_Success(t *testing.T) {
// Create a temporary MP3 file
tempDir := t.TempDir()
mp3File := filepath.Join(tempDir, "test.mp3")
testData := "fake mp3 data"
err := os.WriteFile(mp3File, []byte(testData), 0644)
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
// Test the function
result, err := utils.Mp3ToBase64(mp3File)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
// Verify the result is base64 encoded
expected := "ZmFrZSBtcDMgZGF0YQ==" // base64 of "fake mp3 data"
if result != expected {
t.Errorf("Expected %s, got %s", expected, result)
}
}
func TestMp3ToBase64_FileNotFound(t *testing.T) {
_, err := utils.Mp3ToBase64("/nonexistent/file.mp3")
if err == nil {
t.Error("Expected error for non-existent file, got nil")
}
if !strings.Contains(err.Error(), "failed to open MP3 file") {
t.Errorf("Expected error message to contain 'failed to open MP3 file', got %v", err)
}
}
func TestMp3ToBase64_NotMP3(t *testing.T) {
// Create a temporary non-MP3 file
tempDir := t.TempDir()
txtFile := filepath.Join(tempDir, "test.txt")
err := os.WriteFile(txtFile, []byte("text data"), 0644)
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
_, err = utils.Mp3ToBase64(txtFile)
if err == nil {
t.Error("Expected error for non-MP3 file, got nil")
}
if !strings.Contains(err.Error(), "file is not an MP3") {
t.Errorf("Expected error message to contain 'file is not an MP3', got %v", err)
}
}
func TestMp3ToBase64_ReadError(t *testing.T) {
// This is harder to test without mocking, but we can assume the file creation/read works as above
// For now, the success test covers the happy path
}