download_queue_test.go
⎇
Raw
1package repository
2
3import (
4 "context"
5 "database/sql"
6 "testing"
7
8 "vidarchive/internal/models"
9)
10
11func queueDownload(t *testing.T, repo *DownloadRepository, url, status string, subID *int64) *models.Download {
12 t.Helper()
13 d := &models.Download{URL: url, Status: status}
14 if subID != nil {
15 d.SubscriptionID = sql.NullInt64{Int64: *subID, Valid: true}
16 }
17 if err := repo.Create(d); err != nil {
18 t.Fatalf("create %s: %v", url, err)
19 }
20 return d
21}
22
23// The queue checker asks for the oldest queued rows up to the worker buffer
24// size. Anything else, in any other status, must stay out of the result.
25func TestGetQueuedOldestFirstWithinLimit(t *testing.T) {
26 db := setupTestDB(t)
27 defer db.Close()
28 repo := NewDownloadRepository(db)
29
30 first := queueDownload(t, repo, "first", "queued", nil)
31 second := queueDownload(t, repo, "second", "queued", nil)
32 queueDownload(t, repo, "third", "queued", nil)
33 queueDownload(t, repo, "running", "downloading", nil)
34 queueDownload(t, repo, "done", "completed", nil)
35
36 got, err := repo.GetQueued(2)
37 if err != nil {
38 t.Fatalf("get queued: %v", err)
39 }
40 if len(got) != 2 {
41 t.Fatalf("got %d rows, want 2 (the limit)", len(got))
42 }
43 if got[0].ID != first.ID || got[1].ID != second.ID {
44 t.Errorf("got ids %d,%d, want the two oldest %d,%d", got[0].ID, got[1].ID, first.ID, second.ID)
45 }
46}
47
48// The queue page filters by status; "" and "all" mean no filter.
49func TestGetAllStatusFilter(t *testing.T) {
50 db := setupTestDB(t)
51 defer db.Close()
52 repo := NewDownloadRepository(db)
53
54 queueDownload(t, repo, "a", "queued", nil)
55 queueDownload(t, repo, "b", "error", nil)
56 queueDownload(t, repo, "c", "completed", nil)
57
58 for _, tc := range []struct {
59 status string
60 want int
61 }{
62 {"", 3},
63 {"all", 3},
64 {"error", 1},
65 {"cancelled", 0},
66 } {
67 got, err := repo.GetAll(tc.status, "date")
68 if err != nil {
69 t.Fatalf("get all %q: %v", tc.status, err)
70 }
71 if len(got) != tc.want {
72 t.Errorf("status %q returned %d rows, want %d", tc.status, len(got), tc.want)
73 }
74 }
75
76 // Sorting by status must not drop rows.
77 got, err := repo.GetAll("", "status")
78 if err != nil {
79 t.Fatalf("get all sorted by status: %v", err)
80 }
81 if len(got) != 3 {
82 t.Errorf("status sort returned %d rows, want 3", len(got))
83 }
84}
85
86// The health endpoint reads these counts, aggregated in SQL so it never loads
87// the log column.
88func TestCountByStatus(t *testing.T) {
89 db := setupTestDB(t)
90 defer db.Close()
91 repo := NewDownloadRepository(db)
92
93 queueDownload(t, repo, "a", "queued", nil)
94 queueDownload(t, repo, "b", "queued", nil)
95 queueDownload(t, repo, "c", "error", nil)
96
97 counts, err := repo.CountByStatus(context.Background())
98 if err != nil {
99 t.Fatalf("count by status: %v", err)
100 }
101 if counts["queued"] != 2 || counts["error"] != 1 {
102 t.Errorf("counts = %v, want queued=2 error=1", counts)
103 }
104 if _, ok := counts["completed"]; ok {
105 t.Errorf("counts = %v, want no entry for an unused status", counts)
106 }
107}
108
109// Restart recovery: the stalled downloads are found by status and moved back to
110// queued in one statement.
111func TestIDsByStatusAndUpdateStatusWhere(t *testing.T) {
112 db := setupTestDB(t)
113 defer db.Close()
114 repo := NewDownloadRepository(db)
115
116 stalled := queueDownload(t, repo, "stalled", "downloading", nil)
117 queueDownload(t, repo, "done", "completed", nil)
118
119 ids, err := repo.IDsByStatus("downloading")
120 if err != nil {
121 t.Fatalf("ids by status: %v", err)
122 }
123 if len(ids) != 1 || ids[0] != stalled.ID {
124 t.Fatalf("ids = %v, want [%d]", ids, stalled.ID)
125 }
126
127 if err := repo.UpdateStatusWhere("downloading", "queued"); err != nil {
128 t.Fatalf("update status: %v", err)
129 }
130 if got, _ := repo.GetByID(stalled.ID); got.Status != "queued" {
131 t.Errorf("status = %q, want queued", got.Status)
132 }
133 if left, _ := repo.IDsByStatus("downloading"); len(left) != 0 {
134 t.Errorf("%d rows still downloading", len(left))
135 }
136}
137
138// Logs are flushed in slices as yt-dlp produces them, so appends must
139// accumulate rather than replace — including the first one, onto a NULL column.
140func TestAppendLogsAccumulates(t *testing.T) {
141 db := setupTestDB(t)
142 defer db.Close()
143 repo := NewDownloadRepository(db)
144
145 d := queueDownload(t, repo, "a", "downloading", nil)
146 for _, chunk := range []string{"first\n", "second\n"} {
147 if err := repo.AppendLogs(d.ID, chunk); err != nil {
148 t.Fatalf("append %q: %v", chunk, err)
149 }
150 }
151
152 got, err := repo.GetByID(d.ID)
153 if err != nil {
154 t.Fatal(err)
155 }
156 if got.Logs.String != "first\nsecond\n" {
157 t.Errorf("logs = %q, want both chunks in order", got.Logs.String)
158 }
159}
160
161func TestMarkErrorAndCompleted(t *testing.T) {
162 db := setupTestDB(t)
163 defer db.Close()
164 repo := NewDownloadRepository(db)
165
166 failed := queueDownload(t, repo, "bad", "downloading", nil)
167 if err := repo.MarkError(failed.ID, "yt-dlp exploded"); err != nil {
168 t.Fatalf("mark error: %v", err)
169 }
170 got, _ := repo.GetByID(failed.ID)
171 if got.Status != "error" || got.ErrorMessage.String != "yt-dlp exploded" {
172 t.Errorf("got status=%q message=%q", got.Status, got.ErrorMessage.String)
173 }
174
175 ok := queueDownload(t, repo, "good", "downloading", nil)
176 if err := repo.MarkCompleted(ok.ID, "cancelled"); err != nil {
177 t.Fatalf("mark completed: %v", err)
178 }
179 got, _ = repo.GetByID(ok.ID)
180 if got.Status != "cancelled" || !got.CompletedAt.Valid {
181 t.Errorf("got status=%q completed_at valid=%v", got.Status, got.CompletedAt.Valid)
182 }
183}
184
185// The scheduler and the manual run button both use this to avoid stacking a
186// second run on a subscription that is still working.
187func TestHasActiveForSubscription(t *testing.T) {
188 db := setupTestDB(t)
189 defer db.Close()
190 subRepo := NewSubscriptionRepository(db)
191 repo := NewDownloadRepository(db)
192
193 sub := &models.Subscription{
194 Name: "s", URL: "u", Enabled: true, RefreshMode: "overwrite",
195 ScheduleKind: "daily", CronExpr: "0 3 * * *", OutputDir: "dir",
196 }
197 if err := subRepo.Create(sub); err != nil {
198 t.Fatal(err)
199 }
200
201 if active, err := repo.HasActiveForSubscription(sub.ID); err != nil || active {
202 t.Fatalf("active = %v (err %v), want false with no downloads", active, err)
203 }
204
205 for _, status := range []string{"queued", "downloading"} {
206 d := queueDownload(t, repo, "u", status, &sub.ID)
207 active, err := repo.HasActiveForSubscription(sub.ID)
208 if err != nil {
209 t.Fatal(err)
210 }
211 if !active {
212 t.Errorf("status %q should count as active", status)
213 }
214 if err := repo.Delete(d.ID); err != nil {
215 t.Fatal(err)
216 }
217 }
218
219 // A finished run no longer blocks the next one.
220 queueDownload(t, repo, "u", "completed", &sub.ID)
221 if active, _ := repo.HasActiveForSubscription(sub.ID); active {
222 t.Error("a completed run must not count as active")
223 }
224}
225
226func TestDeleteAllClearsQueue(t *testing.T) {
227 db := setupTestDB(t)
228 defer db.Close()
229 repo := NewDownloadRepository(db)
230
231 queueDownload(t, repo, "a", "queued", nil)
232 queueDownload(t, repo, "b", "completed", nil)
233
234 if err := repo.DeleteAll(); err != nil {
235 t.Fatalf("delete all: %v", err)
236 }
237 got, err := repo.GetAll("", "date")
238 if err != nil {
239 t.Fatal(err)
240 }
241 if len(got) != 0 {
242 t.Errorf("%d rows left after DeleteAll", len(got))
243 }
244}
245