package vtt import ( "os" "path/filepath" "strings" "testing" ) func TestFormat(t *testing.T) { // Create a temporary test file with valid VTT content // 注意格式必须严格符合 WebVTT 规范,否则 Parse 会失败 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 align:center 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) } // Format the file err := Format(testFile) if err != nil { t.Fatalf("Format failed: %v", err) } // Read the formatted file formatted, err := os.ReadFile(testFile) if err != nil { t.Fatalf("Failed to read formatted file: %v", err) } // 检查基本的内容是否存在 formattedStr := string(formatted) // 检查标题行 if !strings.Contains(formattedStr, "WEBVTT") { t.Errorf("Expected WEBVTT header in output, not found") } // 检查内容是否保留 if !strings.Contains(formattedStr, "This is the first line.") { t.Errorf("Expected 'This is the first line.' in output, not found") } if !strings.Contains(formattedStr, "This is the second line.") { t.Errorf("Expected 'This is the second line.' in output, not found") } if !strings.Contains(formattedStr, "This is the third line") { t.Errorf("Expected 'This is the third line' in output, not found") } if !strings.Contains(formattedStr, "with a line break.") { t.Errorf("Expected 'with a line break.' in output, not found") } } func TestFormat_FileErrors(t *testing.T) { // Test with non-existent file err := Format("/nonexistent/file.vtt") if err == nil { t.Error("Expected error when formatting non-existent file, got nil") } }