Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func TestSanitizePresetName(t *testing.T) {
input: "",
expected: "",
},
{
name: "all_uppercase_letters",
input: "ABCDEFG",
expected: "_______",
},
{
name: "mixed_case_with_valid_chars",
input: "aBcDeFg_123",
expected: "a_c_e_g____",
},
}

for _, tt := range tests {
Expand Down
18 changes: 18 additions & 0 deletions internal/audioutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@ func TestWriteAudioFileSlices(t *testing.T) {
assert.Greater(t, (*audioFiles)[i].Duration, 0.0, "Duration should be positive")
}
}

// TestWriteAudioFileSlicesWithNonExistentFile verifies that the function correctly handles
// errors when the input file doesn't exist.
func TestWriteAudioFileSlicesWithNonExistentFile(t *testing.T) {
outputDir, err := os.MkdirTemp("", "test-output")
require.NoError(t, err, "Failed to create output directory")
defer os.RemoveAll(outputDir)

// Use a non-existent file path
nonExistentFilePath := "/path/to/nonexistent/file.wav"

audioFiles, err := writeAudioFileSlices(nonExistentFilePath, outputDir, 4, "test_prefix")

// Verify that the function returns an error
assert.Error(t, err, "writeAudioFileSlices should fail with non-existent file")
assert.Nil(t, audioFiles, "audioFiles should be nil when an error occurs")
assert.Contains(t, err.Error(), "could not open source file", "Error message should indicate the file couldn't be opened")
}
79 changes: 79 additions & 0 deletions internal/fileutils_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package internal

import (
"archive/zip"
ablmodels2 "github.com/alexfedosov/move-tool/internal/ablmodels"
"io"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -78,6 +81,27 @@ func TestWritePresetFile(t *testing.T) {
assert.Greater(t, len(content), 0, "Preset file should not be empty")
}

// TestWritePresetFileWithInvalidDirectory verifies that the function correctly handles
// errors when the output directory doesn't exist.
func TestWritePresetFileWithInvalidDirectory(t *testing.T) {
// Use a non-existent directory
nonExistentDir := "/path/to/nonexistent/directory"

filePath := "TestPath"
audioFile := []ablmodels2.AudioFile{
{
FilePath: &filePath,
Duration: 1000.0,
},
}
preset := ablmodels2.NewDrumRackDevicePresetWithSamples(audioFile)

err := writePresetFile(preset, nonExistentDir)

// Verify that the function returns an error
assert.Error(t, err, "writePresetFile should fail with non-existent directory")
}

// TestArchivePresetBundle verifies that directories are correctly zipped into preset bundles
// with the expected file structure and naming convention.
func TestArchivePresetBundle(t *testing.T) {
Expand Down Expand Up @@ -112,4 +136,59 @@ func TestArchivePresetBundle(t *testing.T) {
zipPath := filepath.Join(outputDir, presetName+".ablpresetbundle")
_, err = os.Stat(zipPath)
assert.False(t, os.IsNotExist(err), "Archive file should exist at %s", zipPath)

// Verify the contents of the archive
extractDir, err := os.MkdirTemp("", "test-extract")
require.NoError(t, err, "Failed to create extraction directory")
defer os.RemoveAll(extractDir)

// Open the zip file
reader, err := zip.OpenReader(zipPath)
require.NoError(t, err, "Failed to open zip file")
defer reader.Close()

// Check that all expected files are in the archive
var foundFiles []string
for _, file := range reader.File {
foundFiles = append(foundFiles, file.Name)

// Extract and verify content of each file
rc, err := file.Open()
require.NoError(t, err, "Failed to open file in archive")

content, err := io.ReadAll(rc)
require.NoError(t, err, "Failed to read file content")
rc.Close()

// Verify content for non-directory entries
if !strings.HasSuffix(file.Name, "/") {
assert.Equal(t, testContent, content, "File content should match for %s", file.Name)
}
}

// Verify all expected files are in the archive
for _, expectedFile := range testFiles {
assert.Contains(t, foundFiles, expectedFile, "Archive should contain %s", expectedFile)
}
}

// TestArchivePresetBundleWithInvalidOutputDir verifies that the function correctly handles
// errors when the output directory doesn't exist.
func TestArchivePresetBundleWithInvalidOutputDir(t *testing.T) {
sourceDir, err := os.MkdirTemp("", "test-source")
require.NoError(t, err, "Failed to create source directory")
defer os.RemoveAll(sourceDir)

// Create a test file in the source directory
testFilePath := filepath.Join(sourceDir, "test.txt")
err = os.WriteFile(testFilePath, []byte("test content"), 0644)
require.NoError(t, err, "Failed to create test file")

// Use a non-existent output directory
nonExistentDir := "/path/to/nonexistent/directory"

err = archivePresetBundle("test_preset", sourceDir, nonExistentDir)

// Verify that the function returns an error
assert.Error(t, err, "archivePresetBundle should fail with non-existent output directory")
}