215 lines
5.8 KiB
Go
215 lines
5.8 KiB
Go
package vtt
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestParse(t *testing.T) {
|
|
// Create a temporary test file
|
|
content := `WEBVTT
|
|
|
|
1
|
|
00:00:01.000 --> 00:00:04.000
|
|
This is the first line.
|
|
|
|
2
|
|
00:00:05.000 --> 00:00:08.000
|
|
This is the second line.
|
|
|
|
3
|
|
00:00:09.500 --> 00:00:12.800
|
|
This is the third line
|
|
with a line break.
|
|
`
|
|
tempDir := t.TempDir()
|
|
testFile := filepath.Join(tempDir, "test.vtt")
|
|
if err := os.WriteFile(testFile, []byte(content), 0644); err != nil {
|
|
t.Fatalf("Failed to create test file: %v", err)
|
|
}
|
|
|
|
// Test parsing
|
|
subtitle, err := Parse(testFile)
|
|
if err != nil {
|
|
t.Fatalf("Parse failed: %v", err)
|
|
}
|
|
|
|
// Verify results
|
|
if subtitle.Format != "vtt" {
|
|
t.Errorf("Expected format 'vtt', got '%s'", subtitle.Format)
|
|
}
|
|
|
|
if len(subtitle.Entries) != 3 {
|
|
t.Errorf("Expected 3 entries, got %d", len(subtitle.Entries))
|
|
}
|
|
|
|
// Check first entry
|
|
if subtitle.Entries[0].Index != 1 {
|
|
t.Errorf("First entry index: expected 1, got %d", subtitle.Entries[0].Index)
|
|
}
|
|
if subtitle.Entries[0].StartTime.Hours != 0 || subtitle.Entries[0].StartTime.Minutes != 0 ||
|
|
subtitle.Entries[0].StartTime.Seconds != 1 || subtitle.Entries[0].StartTime.Milliseconds != 0 {
|
|
t.Errorf("First entry start time: expected 00:00:01.000, got %+v", subtitle.Entries[0].StartTime)
|
|
}
|
|
if subtitle.Entries[0].EndTime.Hours != 0 || subtitle.Entries[0].EndTime.Minutes != 0 ||
|
|
subtitle.Entries[0].EndTime.Seconds != 4 || subtitle.Entries[0].EndTime.Milliseconds != 0 {
|
|
t.Errorf("First entry end time: expected 00:00:04.000, got %+v", subtitle.Entries[0].EndTime)
|
|
}
|
|
if subtitle.Entries[0].Text != "This is the first line." {
|
|
t.Errorf("First entry text: expected 'This is the first line.', got '%s'", subtitle.Entries[0].Text)
|
|
}
|
|
|
|
// Check third entry with line break
|
|
if subtitle.Entries[2].Index != 3 {
|
|
t.Errorf("Third entry index: expected 3, got %d", subtitle.Entries[2].Index)
|
|
}
|
|
expectedText := "This is the third line\nwith a line break."
|
|
if subtitle.Entries[2].Text != expectedText {
|
|
t.Errorf("Third entry text: expected '%s', got '%s'", expectedText, subtitle.Entries[2].Text)
|
|
}
|
|
}
|
|
|
|
func TestParse_WithHeader(t *testing.T) {
|
|
// Create a temporary test file with title
|
|
content := `WEBVTT - Test Title
|
|
|
|
1
|
|
00:00:01.000 --> 00:00:04.000
|
|
This is the first line.
|
|
`
|
|
tempDir := t.TempDir()
|
|
testFile := filepath.Join(tempDir, "test.vtt")
|
|
if err := os.WriteFile(testFile, []byte(content), 0644); err != nil {
|
|
t.Fatalf("Failed to create test file: %v", err)
|
|
}
|
|
|
|
// Test parsing
|
|
subtitle, err := Parse(testFile)
|
|
if err != nil {
|
|
t.Fatalf("Parse failed: %v", err)
|
|
}
|
|
|
|
// Verify title was extracted
|
|
if subtitle.Title != "Test Title" {
|
|
t.Errorf("Expected title 'Test Title', got '%s'", subtitle.Title)
|
|
}
|
|
}
|
|
|
|
func TestParse_WithStyles(t *testing.T) {
|
|
// Create a temporary test file with CSS styling
|
|
content := `WEBVTT
|
|
|
|
STYLE
|
|
::cue {
|
|
color: white;
|
|
background-color: black;
|
|
}
|
|
|
|
1
|
|
00:00:01.000 --> 00:00:04.000 align:start position:10%
|
|
This is <b>styled</b> text.
|
|
`
|
|
tempDir := t.TempDir()
|
|
testFile := filepath.Join(tempDir, "test.vtt")
|
|
if err := os.WriteFile(testFile, []byte(content), 0644); err != nil {
|
|
t.Fatalf("Failed to create test file: %v", err)
|
|
}
|
|
|
|
// Test parsing
|
|
subtitle, err := Parse(testFile)
|
|
if err != nil {
|
|
t.Fatalf("Parse failed: %v", err)
|
|
}
|
|
|
|
// First check if we have entries at all
|
|
if len(subtitle.Entries) == 0 {
|
|
t.Fatalf("No entries found in parsed subtitle")
|
|
}
|
|
|
|
// Verify styling was captured
|
|
if subtitle.Entries[0].Styles == nil {
|
|
t.Fatalf("Entry styles map is nil")
|
|
}
|
|
|
|
// Verify HTML tags were detected
|
|
if _, ok := subtitle.Entries[0].FormatData["has_html_tags"]; !ok {
|
|
t.Errorf("Expected HTML tags to be detected in entry")
|
|
}
|
|
|
|
// Verify cue settings were captured
|
|
if subtitle.Entries[0].Styles["align"] != "start" {
|
|
t.Errorf("Expected align style 'start', got '%s'", subtitle.Entries[0].Styles["align"])
|
|
}
|
|
if subtitle.Entries[0].Styles["position"] != "10%" {
|
|
t.Errorf("Expected position style '10%%', got '%s'", subtitle.Entries[0].Styles["position"])
|
|
}
|
|
}
|
|
|
|
func TestParse_WithComments(t *testing.T) {
|
|
// Create a temporary test file with comments
|
|
content := `WEBVTT
|
|
|
|
NOTE This is a comment
|
|
NOTE This is another comment
|
|
|
|
1
|
|
00:00:01.000 --> 00:00:04.000
|
|
This is the first line.
|
|
`
|
|
tempDir := t.TempDir()
|
|
testFile := filepath.Join(tempDir, "test_comments.vtt")
|
|
if err := os.WriteFile(testFile, []byte(content), 0644); err != nil {
|
|
t.Fatalf("Failed to create test file: %v", err)
|
|
}
|
|
|
|
// Test parsing
|
|
subtitle, err := Parse(testFile)
|
|
if err != nil {
|
|
t.Fatalf("Parse failed: %v", err)
|
|
}
|
|
|
|
// Verify comments were captured
|
|
if len(subtitle.Comments) != 2 {
|
|
t.Errorf("Expected 2 comments, got %d", len(subtitle.Comments))
|
|
}
|
|
|
|
if subtitle.Comments[0] != "This is a comment" {
|
|
t.Errorf("Expected first comment 'This is a comment', got '%s'", subtitle.Comments[0])
|
|
}
|
|
|
|
if subtitle.Comments[1] != "This is another comment" {
|
|
t.Errorf("Expected second comment 'This is another comment', got '%s'", subtitle.Comments[1])
|
|
}
|
|
}
|
|
|
|
func TestParse_FileErrors(t *testing.T) {
|
|
// Test with empty file
|
|
tempDir := t.TempDir()
|
|
emptyFile := filepath.Join(tempDir, "empty.vtt")
|
|
if err := os.WriteFile(emptyFile, []byte(""), 0644); err != nil {
|
|
t.Fatalf("Failed to create empty file: %v", err)
|
|
}
|
|
|
|
_, err := Parse(emptyFile)
|
|
if err == nil {
|
|
t.Error("Expected error when parsing empty file, got nil")
|
|
}
|
|
|
|
// Test with invalid WEBVTT header
|
|
invalidFile := filepath.Join(tempDir, "invalid.vtt")
|
|
if err := os.WriteFile(invalidFile, []byte("INVALID HEADER\n\n"), 0644); err != nil {
|
|
t.Fatalf("Failed to create invalid file: %v", err)
|
|
}
|
|
|
|
_, err = Parse(invalidFile)
|
|
if err == nil {
|
|
t.Error("Expected error when parsing file with invalid header, got nil")
|
|
}
|
|
|
|
// Test with non-existent file
|
|
_, err = Parse("/nonexistent/file.vtt")
|
|
if err == nil {
|
|
t.Error("Expected error when parsing non-existent file, got nil")
|
|
}
|
|
}
|