package txt import ( "os" "path/filepath" "strings" "testing" "sub-cli/internal/model" ) func TestGenerateFromSubtitle(t *testing.T) { // Create test subtitle subtitle := model.NewSubtitle() entry1 := model.NewSubtitleEntry() entry1.Text = "This is the first line." entry2 := model.NewSubtitleEntry() entry2.Text = "This is the second line." entry3 := model.NewSubtitleEntry() entry3.Text = "This is the third line\nwith a line break." subtitle.Entries = append(subtitle.Entries, entry1, entry2, entry3) // Generate TXT file tempDir := t.TempDir() outputFile := filepath.Join(tempDir, "output.txt") err := GenerateFromSubtitle(subtitle, outputFile) if err != nil { t.Fatalf("GenerateFromSubtitle 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 lines := strings.Split(string(content), "\n") if len(lines) < 4 { // 3 entries with one having a line break t.Fatalf("Expected at least 4 lines, got %d", len(lines)) } if lines[0] != "This is the first line." { t.Errorf("Expected first line to be 'This is the first line.', got '%s'", lines[0]) } if lines[1] != "This is the second line." { t.Errorf("Expected second line to be 'This is the second line.', got '%s'", lines[1]) } if lines[2] != "This is the third line" { t.Errorf("Expected third line to be 'This is the third line', got '%s'", lines[2]) } if lines[3] != "with a line break." { t.Errorf("Expected fourth line to be 'with a line break.', got '%s'", lines[3]) } } func TestGenerateFromSubtitle_EmptySubtitle(t *testing.T) { // Create empty subtitle subtitle := model.NewSubtitle() // Generate TXT file tempDir := t.TempDir() outputFile := filepath.Join(tempDir, "empty.txt") err := GenerateFromSubtitle(subtitle, outputFile) if err != nil { t.Fatalf("GenerateFromSubtitle failed with empty subtitle: %v", err) } // Verify generated content content, err := os.ReadFile(outputFile) if err != nil { t.Fatalf("Failed to read output file: %v", err) } // Check content is empty if len(content) != 0 { t.Errorf("Expected empty file, got content: %s", string(content)) } } func TestGenerateFromSubtitle_WithTitle(t *testing.T) { // Create subtitle with title subtitle := model.NewSubtitle() subtitle.Title = "My Test Title" entry1 := model.NewSubtitleEntry() entry1.Text = "This is a test line." subtitle.Entries = append(subtitle.Entries, entry1) // Generate TXT file tempDir := t.TempDir() outputFile := filepath.Join(tempDir, "titled.txt") err := GenerateFromSubtitle(subtitle, outputFile) if err != nil { t.Fatalf("GenerateFromSubtitle failed with titled subtitle: %v", err) } // Verify generated content content, err := os.ReadFile(outputFile) if err != nil { t.Fatalf("Failed to read output file: %v", err) } // Check content has title and proper formatting lines := strings.Split(string(content), "\n") if len(lines) < 3 { // Title + blank line + content t.Fatalf("Expected at least 3 lines, got %d", len(lines)) } if lines[0] != "My Test Title" { t.Errorf("Expected first line to be title, got '%s'", lines[0]) } if lines[1] != "" { t.Errorf("Expected second line to be blank, got '%s'", lines[1]) } if lines[2] != "This is a test line." { t.Errorf("Expected third line to be content, got '%s'", lines[2]) } } func TestGenerateFromSubtitle_FileError(t *testing.T) { // Create test subtitle subtitle := model.NewSubtitle() entry1 := model.NewSubtitleEntry() entry1.Text = "Test line" subtitle.Entries = append(subtitle.Entries, entry1) // Test with invalid file path invalidPath := "/nonexistent/directory/file.txt" err := GenerateFromSubtitle(subtitle, invalidPath) // Verify error is returned if err == nil { t.Errorf("Expected error for invalid file path, got nil") } }