sub-cli/internal/converter/converter_test.go
2025-04-23 16:30:45 +08:00

249 lines
6.5 KiB
Go

package converter
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestConvert(t *testing.T) {
// Setup test cases
testCases := []struct {
name string
sourceContent string
sourceExt string
targetExt string
expectedError bool
validateOutput func(t *testing.T, filePath string)
}{
{
name: "SRT to VTT",
sourceContent: `1
00:00:01,000 --> 00:00:04,000
This is a test subtitle.
2
00:00:05,000 --> 00:00:08,000
This is another test subtitle.
`,
sourceExt: "srt",
targetExt: "vtt",
expectedError: false,
validateOutput: func(t *testing.T, filePath string) {
content, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Failed to read output file: %v", err)
}
// Check content
contentStr := string(content)
if !strings.Contains(contentStr, "WEBVTT") {
t.Errorf("Expected output to contain WEBVTT header, got: %s", contentStr)
}
if !strings.Contains(contentStr, "00:00:01.000 --> 00:00:04.000") {
t.Errorf("Expected output to contain correct timestamp, got: %s", contentStr)
}
if !strings.Contains(contentStr, "This is a test subtitle.") {
t.Errorf("Expected output to contain subtitle text, got: %s", contentStr)
}
},
},
{
name: "LRC to SRT",
sourceContent: `[ti:Test Title]
[ar:Test Artist]
[00:01.00]This is a test lyric.
[00:05.00]This is another test lyric.
`,
sourceExt: "lrc",
targetExt: "srt",
expectedError: false,
validateOutput: func(t *testing.T, filePath string) {
content, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Failed to read output file: %v", err)
}
// Check content
contentStr := string(content)
if !strings.Contains(contentStr, "00:00:01,000 --> ") {
t.Errorf("Expected output to contain correct SRT timestamp, got: %s", contentStr)
}
if !strings.Contains(contentStr, "This is a test lyric.") {
t.Errorf("Expected output to contain lyric text, got: %s", contentStr)
}
},
},
{
name: "VTT to LRC",
sourceContent: `WEBVTT
1
00:00:01.000 --> 00:00:04.000
This is a test subtitle.
2
00:00:05.000 --> 00:00:08.000
This is another test subtitle.
`,
sourceExt: "vtt",
targetExt: "lrc",
expectedError: false,
validateOutput: func(t *testing.T, filePath string) {
content, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Failed to read output file: %v", err)
}
// Check content
contentStr := string(content)
if !strings.Contains(contentStr, "[00:01.000]") {
t.Errorf("Expected output to contain correct LRC timestamp, got: %s", contentStr)
}
if !strings.Contains(contentStr, "This is a test subtitle.") {
t.Errorf("Expected output to contain subtitle text, got: %s", contentStr)
}
},
},
{
name: "SRT to TXT",
sourceContent: `1
00:00:01,000 --> 00:00:04,000
This is a test subtitle.
2
00:00:05,000 --> 00:00:08,000
This is another test subtitle.
`,
sourceExt: "srt",
targetExt: "txt",
expectedError: false,
validateOutput: func(t *testing.T, filePath string) {
content, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Failed to read output file: %v", err)
}
// Check content
contentStr := string(content)
if strings.Contains(contentStr, "00:00:01") {
t.Errorf("TXT should not contain timestamps, got: %s", contentStr)
}
if !strings.Contains(contentStr, "This is a test subtitle.") {
t.Errorf("Expected output to contain subtitle text, got: %s", contentStr)
}
},
},
{
name: "TXT to SRT",
sourceContent: "This is a test line.",
sourceExt: "txt",
targetExt: "srt",
expectedError: true,
validateOutput: nil, // No validation needed as we expect an error
},
{
name: "Invalid source format",
sourceContent: "Random content",
sourceExt: "xyz",
targetExt: "srt",
expectedError: true,
validateOutput: nil, // No validation needed as we expect an error
},
{
name: "Invalid target format",
sourceContent: `1
00:00:01,000 --> 00:00:04,000
This is a test subtitle.
`,
sourceExt: "srt",
targetExt: "xyz",
expectedError: true,
validateOutput: nil, // No validation needed as we expect an error
},
}
// Run test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create temporary directory
tempDir := t.TempDir()
// Create source file
sourceFile := filepath.Join(tempDir, "source."+tc.sourceExt)
if err := os.WriteFile(sourceFile, []byte(tc.sourceContent), 0644); err != nil {
t.Fatalf("Failed to create source file: %v", err)
}
// Create target file path
targetFile := filepath.Join(tempDir, "target."+tc.targetExt)
// Call Convert
err := Convert(sourceFile, targetFile)
// Check error
if tc.expectedError && err == nil {
t.Errorf("Expected error but got none")
}
if !tc.expectedError && err != nil {
t.Errorf("Expected no error but got: %v", err)
}
// If no error expected and validation function provided, validate output
if !tc.expectedError && tc.validateOutput != nil {
tc.validateOutput(t, targetFile)
}
})
}
}
func TestConvert_NonExistentFile(t *testing.T) {
tempDir := t.TempDir()
sourceFile := filepath.Join(tempDir, "nonexistent.srt")
targetFile := filepath.Join(tempDir, "target.vtt")
err := Convert(sourceFile, targetFile)
if err == nil {
t.Errorf("Expected error when source file doesn't exist, but got none")
}
}
func TestConvert_ReadOnlyTarget(t *testing.T) {
// This test might not be applicable on all platforms
// Skip it if running on a platform where permissions can't be enforced
if os.Getenv("SKIP_PERMISSION_TESTS") != "" {
t.Skip("Skipping permission test")
}
// Create temporary directory
tempDir := t.TempDir()
// Create source file
sourceContent := `1
00:00:01,000 --> 00:00:04,000
This is a test subtitle.
`
sourceFile := filepath.Join(tempDir, "source.srt")
if err := os.WriteFile(sourceFile, []byte(sourceContent), 0644); err != nil {
t.Fatalf("Failed to create source file: %v", err)
}
// Create read-only directory
readOnlyDir := filepath.Join(tempDir, "readonly")
if err := os.Mkdir(readOnlyDir, 0500); err != nil {
t.Fatalf("Failed to create read-only directory: %v", err)
}
// Target in read-only directory
targetFile := filepath.Join(readOnlyDir, "target.vtt")
// Call Convert
err := Convert(sourceFile, targetFile)
// We expect an error due to permissions
if err == nil {
t.Errorf("Expected error when target is in read-only directory, but got none")
}
}