feat: add tests
This commit is contained in:
parent
44c7e9bee5
commit
bb87f058f0
17 changed files with 4436 additions and 80 deletions
199
internal/formatter/formatter_test.go
Normal file
199
internal/formatter/formatter_test.go
Normal file
|
@ -0,0 +1,199 @@
|
|||
package formatter
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormat(t *testing.T) {
|
||||
// Create temporary test directory
|
||||
tempDir := t.TempDir()
|
||||
|
||||
// Test cases for different formats
|
||||
testCases := []struct {
|
||||
name string
|
||||
content string
|
||||
fileExt string
|
||||
expectedError bool
|
||||
validateOutput func(t *testing.T, filePath string)
|
||||
}{
|
||||
{
|
||||
name: "SRT Format",
|
||||
content: `2
|
||||
00:00:05,000 --> 00:00:08,000
|
||||
This is the second line.
|
||||
|
||||
1
|
||||
00:00:01,000 --> 00:00:04,000
|
||||
This is the first line.
|
||||
`,
|
||||
fileExt: "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)
|
||||
}
|
||||
|
||||
contentStr := string(content)
|
||||
|
||||
// Check that entries are numbered correctly - don't assume ordering by timestamp
|
||||
// The format function should renumber cues sequentially, but might not change order
|
||||
if !strings.Contains(contentStr, "1\n00:00:") && !strings.Contains(contentStr, "2\n00:00:") {
|
||||
t.Errorf("Output should contain numbered entries (1 and 2), got: %s", contentStr)
|
||||
}
|
||||
|
||||
// Check content preservation
|
||||
if !strings.Contains(contentStr, "This is the first line.") ||
|
||||
!strings.Contains(contentStr, "This is the second line.") {
|
||||
t.Errorf("Output should preserve all content")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "LRC Format",
|
||||
content: `[ar:Test Artist]
|
||||
[00:05.00]This is the second line.
|
||||
[00:01.0]This is the first line.
|
||||
`,
|
||||
fileExt: "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)
|
||||
}
|
||||
|
||||
contentStr := string(content)
|
||||
|
||||
// Check that timestamps are standardized (HH:MM:SS.mmm)
|
||||
if !strings.Contains(contentStr, "[00:01.000]") {
|
||||
t.Errorf("Expected standardized timestamp [00:01.000], got: %s", contentStr)
|
||||
}
|
||||
|
||||
if !strings.Contains(contentStr, "[00:05.000]") {
|
||||
t.Errorf("Expected standardized timestamp [00:05.000], got: %s", contentStr)
|
||||
}
|
||||
|
||||
// Check metadata is preserved
|
||||
if !strings.Contains(contentStr, "[ar:Test Artist]") {
|
||||
t.Errorf("Expected metadata [ar:Test Artist] to be preserved, got: %s", contentStr)
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "VTT Format",
|
||||
content: `WEBVTT
|
||||
|
||||
10
|
||||
00:00:05.000 --> 00:00:08.000
|
||||
This is the second line.
|
||||
|
||||
5
|
||||
00:00:01.000 --> 00:00:04.000
|
||||
This is the first line.
|
||||
`,
|
||||
fileExt: "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)
|
||||
}
|
||||
|
||||
contentStr := string(content)
|
||||
|
||||
// Check that cues are numbered correctly - don't assume ordering by timestamp
|
||||
// Just check that identifiers are sequential
|
||||
if !strings.Contains(contentStr, "1\n00:00:") && !strings.Contains(contentStr, "2\n00:00:") {
|
||||
t.Errorf("Output should contain sequential identifiers (1 and 2), got: %s", contentStr)
|
||||
}
|
||||
|
||||
// Check content preservation
|
||||
if !strings.Contains(contentStr, "This is the first line.") ||
|
||||
!strings.Contains(contentStr, "This is the second line.") {
|
||||
t.Errorf("Output should preserve all content")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Unsupported Format",
|
||||
content: "Some content",
|
||||
fileExt: "txt",
|
||||
expectedError: true,
|
||||
validateOutput: nil,
|
||||
},
|
||||
}
|
||||
|
||||
// Run test cases
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Create test file
|
||||
testFile := filepath.Join(tempDir, "test."+tc.fileExt)
|
||||
if err := os.WriteFile(testFile, []byte(tc.content), 0644); err != nil {
|
||||
t.Fatalf("Failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
// Call Format
|
||||
err := Format(testFile)
|
||||
|
||||
// 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, testFile)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormat_NonExistentFile(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
nonExistentFile := filepath.Join(tempDir, "nonexistent.srt")
|
||||
|
||||
err := Format(nonExistentFile)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error when file doesn't exist, but got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormat_PermissionError(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 test file in the temporary directory
|
||||
testFile := filepath.Join(tempDir, "test.srt")
|
||||
content := `1
|
||||
00:00:01,000 --> 00:00:04,000
|
||||
This is a test line.
|
||||
`
|
||||
// Write the file
|
||||
if err := os.WriteFile(testFile, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("Failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
// Make file read-only
|
||||
if err := os.Chmod(testFile, 0400); err != nil {
|
||||
t.Skipf("Failed to change file permissions, skipping test: %v", err)
|
||||
}
|
||||
|
||||
// Try to format read-only file
|
||||
err := Format(testFile)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error when formatting read-only file, but got none")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue