package service import ( "context" "database/sql" "testing" "vidarchive/internal/models" ) // newSubscriptionFor inserts a subscription owning dirName, the shape the // scheduler creates downloads from. func (e *execEnv) newSubscriptionFor(t *testing.T, dirName, refreshMode string) *models.Subscription { t.Helper() sub := &models.Subscription{ Name: "Channel", URL: "https://example.com/channel", Enabled: true, RefreshMode: refreshMode, ScheduleKind: "daily", CronExpr: "0 3 * * *", OutputDir: dirName, } if err := e.subRepo.Create(sub); err != nil { t.Fatalf("create subscription: %v", err) } // The scheduler records "queued" when it enqueues the run; the worker is // expected to move it on from there. if err := e.subRepo.SetLastStatus(sub.ID, "queued"); err != nil { t.Fatalf("seed last_status: %v", err) } return sub } func (e *execEnv) subStatus(t *testing.T, id int64) string { t.Helper() got, err := e.subRepo.GetByID(id) if err != nil { t.Fatalf("reload subscription %d: %v", id, err) } return got.LastStatus.String } // A finished subscription run must leave the subscription reporting the outcome. // Without the write-back the row keeps the scheduler's "queued" forever, so the // subscriptions page never shows that a run succeeded. func TestSubscriptionRunRecordsCompleted(t *testing.T) { e := newExecEnv(t) sub := e.newSubscriptionFor(t, "channel", "overwrite") e.fakeYTDLP(t, e.writeItem(t, "item-00001", "My Clip", "abc123")) d := e.queue(t, &models.Download{SubscriptionID: sql.NullInt64{Int64: sub.ID, Valid: true}}) if _, err := e.svc.ExecuteDownload(context.Background(), d); err != nil { t.Fatalf("ExecuteDownload: %v", err) } if got := e.subStatus(t, sub.ID); got != "completed" { t.Errorf("subscription last_status = %q, want completed", got) } } // A failed run must be visible on the subscription, not only on the download. func TestSubscriptionRunRecordsError(t *testing.T) { e := newExecEnv(t) sub := e.newSubscriptionFor(t, "channel", "overwrite") e.fakeYTDLP(t, `echo "boom" >&2; exit 1`) d := e.queue(t, &models.Download{SubscriptionID: sql.NullInt64{Int64: sub.ID, Valid: true}}) if _, err := e.svc.ExecuteDownload(context.Background(), d); err == nil { t.Fatal("expected the failing run to report an error") } if got := e.subStatus(t, sub.ID); got != "error" { t.Errorf("subscription last_status = %q, want error", got) } } // A user-initiated cancel is terminal, so the subscription reports it rather // than staying on "downloading". func TestSubscriptionRunRecordsCancelled(t *testing.T) { e := newExecEnv(t) sub := e.newSubscriptionFor(t, "channel", "overwrite") e.fakeYTDLP(t, `touch "$SCRATCH/started"; sleep 300`) d := e.queue(t, &models.Download{SubscriptionID: sql.NullInt64{Int64: sub.ID, Valid: true}}) done := make(chan struct{}) go func() { defer close(done) e.svc.ExecuteDownload(context.Background(), d) }() waitForFile(t, e.scratch+"/started") e.svc.cancelDownload(d.ID) <-done if got := e.subStatus(t, sub.ID); got != "cancelled" { t.Errorf("subscription last_status = %q, want cancelled", got) } } // A plain download has no subscription to report to; the write-back must not // try to touch one. func TestPlainDownloadLeavesSubscriptionsAlone(t *testing.T) { e := newExecEnv(t) sub := e.newSubscriptionFor(t, "channel", "overwrite") e.fakeYTDLP(t, e.writeItem(t, "item-00001", "My Clip", "abc123")) d := e.queue(t, &models.Download{}) if _, err := e.svc.ExecuteDownload(context.Background(), d); err != nil { t.Fatalf("ExecuteDownload: %v", err) } if got := e.subStatus(t, sub.ID); got != "queued" { t.Errorf("subscription last_status = %q, want it untouched at queued", got) } }