package srt import ( "os" "path/filepath" "strings" "testing" ) func TestFormat(t *testing.T) { // Create a temporary test file with out-of-order numbers 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. 3 00:00:09,500 --> 00:00:12,800 This is the third line. ` tempDir := t.TempDir() testFile := filepath.Join(tempDir, "test.srt") 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) } // The Format function should standardize the numbering lines := strings.Split(string(formatted), "\n") // The numbers should be sequential starting from 1 if !strings.HasPrefix(lines[0], "1") { t.Errorf("First entry should be renumbered to 1, got '%s'", lines[0]) } // Find the second entry (after the first entry's content and a blank line) var secondEntryIndex int for i := 1; i < len(lines); i++ { if lines[i] == "" && i+1 < len(lines) && lines[i+1] != "" { secondEntryIndex = i + 1 break } } if secondEntryIndex > 0 && !strings.HasPrefix(lines[secondEntryIndex], "2") { t.Errorf("Second entry should be renumbered to 2, got '%s'", lines[secondEntryIndex]) } } func TestFormat_FileError(t *testing.T) { // Test with non-existent file err := Format("/nonexistent/file.srt") if err == nil { t.Error("Expected error when formatting non-existent file, got nil") } }