148 lines
4.2 KiB
Go
148 lines
4.2 KiB
Go
package vtt
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"sub-cli/internal/model"
|
|
)
|
|
|
|
func TestGenerate(t *testing.T) {
|
|
// Create a test subtitle
|
|
subtitle := model.NewSubtitle()
|
|
subtitle.Format = "vtt"
|
|
subtitle.Title = "Test VTT"
|
|
|
|
// Add style section
|
|
subtitle.Styles = map[string]string{"css": "::cue { color: white; }"}
|
|
|
|
// Add comments
|
|
subtitle.Comments = append(subtitle.Comments, "This is a test comment")
|
|
|
|
// Create entries
|
|
entry1 := model.NewSubtitleEntry()
|
|
entry1.Index = 1
|
|
entry1.StartTime = model.Timestamp{Hours: 0, Minutes: 0, Seconds: 1, Milliseconds: 0}
|
|
entry1.EndTime = model.Timestamp{Hours: 0, Minutes: 0, Seconds: 4, Milliseconds: 0}
|
|
entry1.Text = "This is the first line."
|
|
|
|
entry2 := model.NewSubtitleEntry()
|
|
entry2.Index = 2
|
|
entry2.StartTime = model.Timestamp{Hours: 0, Minutes: 0, Seconds: 5, Milliseconds: 0}
|
|
entry2.EndTime = model.Timestamp{Hours: 0, Minutes: 0, Seconds: 8, Milliseconds: 0}
|
|
entry2.Text = "This is the second line."
|
|
entry2.Styles = map[string]string{"align": "center"}
|
|
|
|
subtitle.Entries = append(subtitle.Entries, entry1, entry2)
|
|
|
|
// Generate VTT file
|
|
tempDir := t.TempDir()
|
|
outputFile := filepath.Join(tempDir, "output.vtt")
|
|
err := Generate(subtitle, 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
|
|
contentStr := string(content)
|
|
|
|
// Verify header
|
|
if !strings.HasPrefix(contentStr, "WEBVTT - Test VTT") {
|
|
t.Errorf("Expected header with title, got: %s", strings.Split(contentStr, "\n")[0])
|
|
}
|
|
|
|
// Verify style section
|
|
if !strings.Contains(contentStr, "STYLE") {
|
|
t.Errorf("Expected STYLE section in output")
|
|
}
|
|
|
|
if !strings.Contains(contentStr, "::cue { color: white; }") {
|
|
t.Errorf("Expected CSS content in style section")
|
|
}
|
|
|
|
// Verify comment
|
|
if !strings.Contains(contentStr, "NOTE This is a test comment") {
|
|
t.Errorf("Expected comment in output")
|
|
}
|
|
|
|
// Verify first entry
|
|
if !strings.Contains(contentStr, "00:00:01.000 --> 00:00:04.000") {
|
|
t.Errorf("Expected first entry timestamp in output")
|
|
}
|
|
if !strings.Contains(contentStr, "This is the first line.") {
|
|
t.Errorf("Expected first entry text in output")
|
|
}
|
|
|
|
// Verify second entry with style
|
|
if !strings.Contains(contentStr, "00:00:05.000 --> 00:00:08.000 align:center") {
|
|
t.Errorf("Expected second entry timestamp with align style in output")
|
|
}
|
|
}
|
|
|
|
func TestGenerate_WithRegions(t *testing.T) {
|
|
// Create a subtitle with regions
|
|
subtitle := model.NewSubtitle()
|
|
subtitle.Format = "vtt"
|
|
|
|
// Add a region
|
|
region := model.NewSubtitleRegion("region1")
|
|
region.Settings["width"] = "40%"
|
|
region.Settings["lines"] = "3"
|
|
region.Settings["regionanchor"] = "0%,100%"
|
|
subtitle.Regions = append(subtitle.Regions, region)
|
|
|
|
// Add an entry using the region
|
|
entry := model.NewSubtitleEntry()
|
|
entry.Index = 1
|
|
entry.StartTime = model.Timestamp{Hours: 0, Minutes: 0, Seconds: 1, Milliseconds: 0}
|
|
entry.EndTime = model.Timestamp{Hours: 0, Minutes: 0, Seconds: 4, Milliseconds: 0}
|
|
entry.Text = "This is a regional cue."
|
|
entry.Styles = map[string]string{"region": "region1"}
|
|
subtitle.Entries = append(subtitle.Entries, entry)
|
|
|
|
// Generate VTT file
|
|
tempDir := t.TempDir()
|
|
outputFile := filepath.Join(tempDir, "regions.vtt")
|
|
err := Generate(subtitle, outputFile)
|
|
if err != nil {
|
|
t.Fatalf("Generate failed: %v", err)
|
|
}
|
|
|
|
// Verify by reading file content
|
|
content, err := os.ReadFile(outputFile)
|
|
if err != nil {
|
|
t.Fatalf("Failed to read output file: %v", err)
|
|
}
|
|
|
|
// Check if region is included
|
|
if !strings.Contains(string(content), "REGION region1:") {
|
|
t.Errorf("Expected REGION definition in output")
|
|
}
|
|
|
|
for k, v := range region.Settings {
|
|
if !strings.Contains(string(content), fmt.Sprintf("%s=%s", k, v)) {
|
|
t.Errorf("Expected region setting '%s=%s' in output", k, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGenerate_FileError(t *testing.T) {
|
|
// Create test subtitle
|
|
subtitle := model.NewSubtitle()
|
|
subtitle.Format = "vtt"
|
|
|
|
// Test with invalid path
|
|
err := Generate(subtitle, "/nonexistent/directory/file.vtt")
|
|
if err == nil {
|
|
t.Error("Expected error when generating to invalid path, got nil")
|
|
}
|
|
}
|