Skip to content

Commit b8b9d97

Browse files
committed
fix(download): pause & resume not working
1 parent 5323052 commit b8b9d97

File tree

4 files changed

+86
-13
lines changed

4 files changed

+86
-13
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ https://github.com/user-attachments/assets/4e3f98c7-554f-4b9e-adac-52511ae69f32
3333

3434
**Requirements:**
3535

36-
- Installation: https://github.com/yt-dlp/yt-dlp#installation
36+
- **yt-dlp**: Core video downloader
37+
- Installation: https://github.com/yt-dlp/yt-dlp#installation
3738
- **ffmpeg** - Required for full features
3839
- Installation: https://ffmpeg.org/download.html
3940

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//go:build !windows
2+
3+
package utils
4+
5+
import (
6+
"os/exec"
7+
"testing"
8+
9+
"github.com/xdagiz/xytz/internal/types"
10+
)
11+
12+
func TestPauseResumeDownload_NoProcessNoMessage(t *testing.T) {
13+
dm := NewDownloadManager()
14+
15+
pauseCmd := PauseDownload(dm)
16+
if pauseCmd == nil {
17+
t.Fatalf("PauseDownload returned nil command")
18+
}
19+
if msg := pauseCmd(); msg != nil {
20+
t.Fatalf("pause msg = %T, want nil when no process is attached", msg)
21+
}
22+
if dm.IsPaused() {
23+
t.Fatalf("manager paused = true, want false")
24+
}
25+
26+
resumeCmd := ResumeDownload(dm)
27+
if resumeCmd == nil {
28+
t.Fatalf("ResumeDownload returned nil command")
29+
}
30+
if msg := resumeCmd(); msg != nil {
31+
t.Fatalf("resume msg = %T, want nil when no process is attached", msg)
32+
}
33+
}
34+
35+
func TestPauseResumeDownload_WithRunningProcessSendsMessages(t *testing.T) {
36+
dm := NewDownloadManager()
37+
38+
cmd := exec.Command("sh", "-c", "sleep 5")
39+
if err := cmd.Start(); err != nil {
40+
t.Fatalf("failed to start helper process: %v", err)
41+
}
42+
defer func() {
43+
_ = cmd.Process.Kill()
44+
_, _ = cmd.Process.Wait()
45+
}()
46+
47+
dm.SetCmd(cmd)
48+
49+
pauseCmd := PauseDownload(dm)
50+
pauseMsg := pauseCmd()
51+
if _, ok := pauseMsg.(types.PauseDownloadMsg); !ok {
52+
t.Fatalf("pause msg = %T, want types.PauseDownloadMsg", pauseMsg)
53+
}
54+
if !dm.IsPaused() {
55+
t.Fatalf("manager paused = false, want true after pause")
56+
}
57+
58+
resumeCmd := ResumeDownload(dm)
59+
resumeMsg := resumeCmd()
60+
if _, ok := resumeMsg.(types.ResumeDownloadMsg); !ok {
61+
t.Fatalf("resume msg = %T, want types.ResumeDownloadMsg", resumeMsg)
62+
}
63+
if dm.IsPaused() {
64+
t.Fatalf("manager paused = true, want false after resume")
65+
}
66+
}

internal/utils/download_unix.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,33 @@ import (
1414
func PauseDownload(dm *DownloadManager) tea.Cmd {
1515
return tea.Cmd(func() tea.Msg {
1616
cmd := dm.GetCmd()
17-
if cmd != nil && cmd.Process != nil && !dm.IsPaused() {
18-
dm.SetPaused(true)
19-
if err := cmd.Process.Signal(syscall.SIGSTOP); err != nil {
20-
log.Printf("Failed to pause download: %v", err)
21-
}
17+
if cmd == nil || cmd.Process == nil || dm.IsPaused() {
18+
return nil
2219
}
2320

21+
if err := cmd.Process.Signal(syscall.SIGSTOP); err != nil {
22+
log.Printf("Failed to pause download: %v", err)
23+
return nil
24+
}
25+
26+
dm.SetPaused(true)
2427
return types.PauseDownloadMsg{}
2528
})
2629
}
2730

2831
func ResumeDownload(dm *DownloadManager) tea.Cmd {
2932
return tea.Cmd(func() tea.Msg {
3033
cmd := dm.GetCmd()
31-
if cmd != nil && cmd.Process != nil && dm.IsPaused() {
32-
dm.SetPaused(false)
33-
if err := cmd.Process.Signal(syscall.SIGCONT); err != nil {
34-
log.Printf("Failed to resume download: %v", err)
35-
}
34+
if cmd == nil || cmd.Process == nil || !dm.IsPaused() {
35+
return nil
36+
}
37+
38+
if err := cmd.Process.Signal(syscall.SIGCONT); err != nil {
39+
log.Printf("Failed to resume download: %v", err)
40+
return nil
3641
}
3742

43+
dm.SetPaused(false)
3844
return types.ResumeDownloadMsg{}
3945
})
4046
}

internal/utils/download_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func PauseDownload(dm *DownloadManager) tea.Cmd {
1717
log.Print("pause not supported on windows")
1818
}
1919

20-
return types.PauseDownloadMsg{}
20+
return nil
2121
})
2222
}
2323

@@ -28,6 +28,6 @@ func ResumeDownload(dm *DownloadManager) tea.Cmd {
2828
log.Print("resume not supported on windows")
2929
}
3030

31-
return types.ResumeDownloadMsg{}
31+
return nil
3232
})
3333
}

0 commit comments

Comments
 (0)