sub-cli/internal/format/lrc/formatter_test.go
2025-04-23 19:22:41 +08:00

72 lines
1.9 KiB
Go

package lrc
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestFormat(t *testing.T) {
// Create a temporary test file with messy formatting
content := `[ti:Test LRC File]
[ar:Test Artist]
[00:01.00]This should be first.
[00:05.00]This is the second line.
[00:09.50]This is the third line.
`
tempDir := t.TempDir()
testFile := filepath.Join(tempDir, "test.lrc")
if err := os.WriteFile(testFile, []byte(content), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Format the file
err := Format(testFile)
if err != nil {
t.Fatalf("Format failed: %v", err)
}
// Read the formatted file
formatted, err := os.ReadFile(testFile)
if err != nil {
t.Fatalf("Failed to read formatted file: %v", err)
}
// Check that the file was at least generated successfully
lines := strings.Split(string(formatted), "\n")
if len(lines) < 4 {
t.Fatalf("Expected at least 4 lines, got %d", len(lines))
}
// Check that the metadata was preserved
if !strings.Contains(string(formatted), "[ti:Test LRC File]") {
t.Errorf("Expected title metadata in output, not found")
}
if !strings.Contains(string(formatted), "[ar:Test Artist]") {
t.Errorf("Expected artist metadata in output, not found")
}
// Check that all the content lines are present
if !strings.Contains(string(formatted), "This should be first") {
t.Errorf("Expected 'This should be first' in output, not found")
}
if !strings.Contains(string(formatted), "This is the second line") {
t.Errorf("Expected 'This is the second line' in output, not found")
}
if !strings.Contains(string(formatted), "This is the third line") {
t.Errorf("Expected 'This is the third line' in output, not found")
}
}
func TestFormat_FileError(t *testing.T) {
// Test with non-existent file
err := Format("/nonexistent/file.lrc")
if err == nil {
t.Error("Expected error when formatting non-existent file, got nil")
}
}