151 lines
3.5 KiB
Go
151 lines
3.5 KiB
Go
package lrc
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"sub-cli/internal/model"
|
|
)
|
|
|
|
func TestGenerate(t *testing.T) {
|
|
// Create test lyrics
|
|
lyrics := model.Lyrics{
|
|
Metadata: map[string]string{
|
|
"ti": "Test LRC File",
|
|
"ar": "Test Artist",
|
|
},
|
|
Timeline: []model.Timestamp{
|
|
{Hours: 0, Minutes: 0, Seconds: 1, Milliseconds: 0},
|
|
{Hours: 0, Minutes: 0, Seconds: 5, Milliseconds: 0},
|
|
},
|
|
Content: []string{
|
|
"This is the first line.",
|
|
"This is the second line.",
|
|
},
|
|
}
|
|
|
|
// Generate LRC file
|
|
tempDir := t.TempDir()
|
|
outputFile := filepath.Join(tempDir, "output.lrc")
|
|
err := Generate(lyrics, outputFile)
|
|
if err != nil {
|
|
t.Fatalf("Generate failed: %v", err)
|
|
}
|
|
|
|
// Verify generated content
|
|
content, err := os.ReadFile(outputFile)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read output file: %v", err)
|
|
}
|
|
|
|
// Check content
|
|
lines := strings.Split(string(content), "\n")
|
|
if len(lines) < 4 {
|
|
t.Fatalf("Expected at least 4 lines, got %d", len(lines))
|
|
}
|
|
|
|
hasTitleLine := false
|
|
hasFirstTimeline := false
|
|
|
|
for _, line := range lines {
|
|
if line == "[ti:Test LRC File]" {
|
|
hasTitleLine = true
|
|
}
|
|
if line == "[00:01.000]This is the first line." {
|
|
hasFirstTimeline = true
|
|
}
|
|
}
|
|
|
|
if !hasTitleLine {
|
|
t.Errorf("Expected title line '[ti:Test LRC File]' not found")
|
|
}
|
|
|
|
if !hasFirstTimeline {
|
|
t.Errorf("Expected timeline line '[00:01.000]This is the first line.' not found")
|
|
}
|
|
}
|
|
|
|
func TestGenerate_FileError(t *testing.T) {
|
|
// Create test lyrics
|
|
lyrics := model.Lyrics{
|
|
Metadata: map[string]string{
|
|
"ti": "Test LRC File",
|
|
},
|
|
Timeline: []model.Timestamp{
|
|
{Hours: 0, Minutes: 0, Seconds: 1, Milliseconds: 0},
|
|
},
|
|
Content: []string{
|
|
"This is a test line.",
|
|
},
|
|
}
|
|
|
|
// Test with invalid path
|
|
err := Generate(lyrics, "/nonexistent/directory/file.lrc")
|
|
if err == nil {
|
|
t.Error("Expected error when generating to invalid path, got nil")
|
|
}
|
|
}
|
|
|
|
func TestGenerate_EdgeCases(t *testing.T) {
|
|
// Test with empty lyrics
|
|
emptyLyrics := model.Lyrics{
|
|
Metadata: map[string]string{
|
|
"ti": "Empty Test",
|
|
},
|
|
}
|
|
|
|
tempDir := t.TempDir()
|
|
emptyFile := filepath.Join(tempDir, "empty_output.lrc")
|
|
err := Generate(emptyLyrics, emptyFile)
|
|
if err != nil {
|
|
t.Fatalf("Generate failed with empty lyrics: %v", err)
|
|
}
|
|
|
|
// Verify content has metadata but no timeline entries
|
|
content, err := os.ReadFile(emptyFile)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read empty output file: %v", err)
|
|
}
|
|
|
|
if !strings.Contains(string(content), "[ti:Empty Test]") {
|
|
t.Errorf("Expected metadata in empty lyrics output, not found")
|
|
}
|
|
|
|
// Test with unequal timeline and content lengths
|
|
unequalLyrics := model.Lyrics{
|
|
Timeline: []model.Timestamp{
|
|
{Hours: 0, Minutes: 0, Seconds: 1, Milliseconds: 0},
|
|
{Hours: 0, Minutes: 0, Seconds: 5, Milliseconds: 0},
|
|
},
|
|
Content: []string{
|
|
"This is the only content line.",
|
|
},
|
|
}
|
|
|
|
unequalFile := filepath.Join(tempDir, "unequal_output.lrc")
|
|
err = Generate(unequalLyrics, unequalFile)
|
|
if err != nil {
|
|
t.Fatalf("Generate failed with unequal lyrics: %v", err)
|
|
}
|
|
|
|
// Should only generate for the entries that have both timeline and content
|
|
content, err = os.ReadFile(unequalFile)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read unequal output file: %v", err)
|
|
}
|
|
|
|
lines := strings.Split(string(content), "\n")
|
|
timelineLines := 0
|
|
for _, line := range lines {
|
|
if strings.HasPrefix(line, "[") && strings.Contains(line, "]") &&
|
|
strings.Contains(line, ":") && strings.Contains(line, ".") {
|
|
timelineLines++
|
|
}
|
|
}
|
|
|
|
if timelineLines > 1 {
|
|
t.Errorf("Expected only 1 timeline entry for unequal lyrics, got %d", timelineLines)
|
|
}
|
|
}
|