subscription_status_test.go
| 1 | package service |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "testing" |
| 7 | |
| 8 | "vidarchive/internal/models" |
| 9 | ) |
| 10 | |
| 11 | // newSubscriptionFor inserts a subscription owning dirName, the shape the |
| 12 | // scheduler creates downloads from. |
| 13 | func (e *execEnv) newSubscriptionFor(t *testing.T, dirName, refreshMode string) *models.Subscription { |
| 14 | t.Helper() |
| 15 | sub := &models.Subscription{ |
| 16 | Name: "Channel", |
| 17 | URL: "https://example.com/channel", |
| 18 | Enabled: true, |
| 19 | RefreshMode: refreshMode, |
| 20 | ScheduleKind: "daily", |
| 21 | CronExpr: "0 3 * * *", |
| 22 | OutputDir: dirName, |
| 23 | } |
| 24 | if err := e.subRepo.Create(sub); err != nil { |
| 25 | t.Fatalf("create subscription: %v", err) |
| 26 | } |
| 27 | // The scheduler records "queued" when it enqueues the run; the worker is |
| 28 | // expected to move it on from there. |
| 29 | if err := e.subRepo.SetLastStatus(sub.ID, "queued"); err != nil { |
| 30 | t.Fatalf("seed last_status: %v", err) |
| 31 | } |
| 32 | return sub |
| 33 | } |
| 34 | |
| 35 | func (e *execEnv) subStatus(t *testing.T, id int64) string { |
| 36 | t.Helper() |
| 37 | got, err := e.subRepo.GetByID(id) |
| 38 | if err != nil { |
| 39 | t.Fatalf("reload subscription %d: %v", id, err) |
| 40 | } |
| 41 | return got.LastStatus.String |
| 42 | } |
| 43 | |
| 44 | // A finished subscription run must leave the subscription reporting the outcome. |
| 45 | // Without the write-back the row keeps the scheduler's "queued" forever, so the |
| 46 | // subscriptions page never shows that a run succeeded. |
| 47 | func TestSubscriptionRunRecordsCompleted(t *testing.T) { |
| 48 | e := newExecEnv(t) |
| 49 | sub := e.newSubscriptionFor(t, "channel", "overwrite") |
| 50 | e.fakeYTDLP(t, e.writeItem(t, "item-00001", "My Clip", "abc123")) |
| 51 | |
| 52 | d := e.queue(t, &models.Download{SubscriptionID: sql.NullInt64{Int64: sub.ID, Valid: true}}) |
| 53 | if _, err := e.svc.ExecuteDownload(context.Background(), d); err != nil { |
| 54 | t.Fatalf("ExecuteDownload: %v", err) |
| 55 | } |
| 56 | |
| 57 | if got := e.subStatus(t, sub.ID); got != "completed" { |
| 58 | t.Errorf("subscription last_status = %q, want completed", got) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // A failed run must be visible on the subscription, not only on the download. |
| 63 | func TestSubscriptionRunRecordsError(t *testing.T) { |
| 64 | e := newExecEnv(t) |
| 65 | sub := e.newSubscriptionFor(t, "channel", "overwrite") |
| 66 | e.fakeYTDLP(t, `echo "boom" >&2; exit 1`) |
| 67 | |
| 68 | d := e.queue(t, &models.Download{SubscriptionID: sql.NullInt64{Int64: sub.ID, Valid: true}}) |
| 69 | if _, err := e.svc.ExecuteDownload(context.Background(), d); err == nil { |
| 70 | t.Fatal("expected the failing run to report an error") |
| 71 | } |
| 72 | |
| 73 | if got := e.subStatus(t, sub.ID); got != "error" { |
| 74 | t.Errorf("subscription last_status = %q, want error", got) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // A user-initiated cancel is terminal, so the subscription reports it rather |
| 79 | // than staying on "downloading". |
| 80 | func TestSubscriptionRunRecordsCancelled(t *testing.T) { |
| 81 | e := newExecEnv(t) |
| 82 | sub := e.newSubscriptionFor(t, "channel", "overwrite") |
| 83 | e.fakeYTDLP(t, `touch "$SCRATCH/started"; sleep 300`) |
| 84 | |
| 85 | d := e.queue(t, &models.Download{SubscriptionID: sql.NullInt64{Int64: sub.ID, Valid: true}}) |
| 86 | |
| 87 | done := make(chan struct{}) |
| 88 | go func() { |
| 89 | defer close(done) |
| 90 | e.svc.ExecuteDownload(context.Background(), d) |
| 91 | }() |
| 92 | waitForFile(t, e.scratch+"/started") |
| 93 | e.svc.cancelDownload(d.ID) |
| 94 | <-done |
| 95 | |
| 96 | if got := e.subStatus(t, sub.ID); got != "cancelled" { |
| 97 | t.Errorf("subscription last_status = %q, want cancelled", got) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // A plain download has no subscription to report to; the write-back must not |
| 102 | // try to touch one. |
| 103 | func TestPlainDownloadLeavesSubscriptionsAlone(t *testing.T) { |
| 104 | e := newExecEnv(t) |
| 105 | sub := e.newSubscriptionFor(t, "channel", "overwrite") |
| 106 | e.fakeYTDLP(t, e.writeItem(t, "item-00001", "My Clip", "abc123")) |
| 107 | |
| 108 | d := e.queue(t, &models.Download{}) |
| 109 | if _, err := e.svc.ExecuteDownload(context.Background(), d); err != nil { |
| 110 | t.Fatalf("ExecuteDownload: %v", err) |
| 111 | } |
| 112 | |
| 113 | if got := e.subStatus(t, sub.ID); got != "queued" { |
| 114 | t.Errorf("subscription last_status = %q, want it untouched at queued", got) |
| 115 | } |
| 116 | } |
| 117 |