package service import ( "os" "path/filepath" "testing" "time" "vidarchive/internal/config" "vidarchive/internal/models" "vidarchive/internal/repository" ) func TestComputeNextRun(t *testing.T) { svc := &SubscriptionService{} // A fixed reference time: 2026-06-21 12:30 local. from := time.Date(2026, 6, 21, 12, 30, 0, 0, time.Local) cases := []struct { kind string custom string wantErr bool check func(t *testing.T, next time.Time) }{ {kind: "hourly", check: func(t *testing.T, next time.Time) { if !next.After(from) || next.Minute() != 0 { t.Errorf("hourly: got %v", next) } }}, {kind: "daily", check: func(t *testing.T, next time.Time) { if next.Hour() != 3 || next.Minute() != 0 || !next.After(from) { t.Errorf("daily: got %v", next) } }}, {kind: "weekly", check: func(t *testing.T, next time.Time) { if next.Weekday() != time.Sunday || !next.After(from) { t.Errorf("weekly: got %v", next) } }}, {kind: "monthly", check: func(t *testing.T, next time.Time) { if next.Day() != 1 || !next.After(from) { t.Errorf("monthly: got %v", next) } }}, {kind: "cron", custom: "*/15 * * * *", check: func(t *testing.T, next time.Time) { if next.Minute() != 45 || !next.After(from) { t.Errorf("custom cron */15: got %v", next) } }}, {kind: "cron", custom: "not a cron", wantErr: true}, {kind: "cron", custom: "", wantErr: true}, {kind: "bogus", wantErr: true}, } for _, c := range cases { sub := &models.Subscription{ScheduleKind: c.kind, CronExpr: c.custom} next, err := svc.ComputeNextRun(sub, from) if c.wantErr { if err == nil { t.Errorf("kind=%q custom=%q: expected error, got %v", c.kind, c.custom, next) } continue } if err != nil { t.Errorf("kind=%q custom=%q: unexpected error: %v", c.kind, c.custom, err) continue } c.check(t, next) } } // Deleting a subscription removes its download-archive file. A later // subscription that reuses the id would otherwise silently skip entries it // never downloaded. func TestSubscriptionDeleteRemovesArchive(t *testing.T) { db := setupTestDB(t) defer db.Close() cfg := &config.Config{DataDir: t.TempDir()} svc := NewSubscriptionService(repository.NewSubscriptionRepository(db), cfg) sub := &models.Subscription{ Name: "Channel", URL: "https://example.com/channel", Enabled: true, RefreshMode: "skip", ScheduleKind: "daily", } if err := svc.Create(sub); err != nil { t.Fatalf("create: %v", err) } archive := svc.ArchivePath(sub.ID) if err := os.MkdirAll(filepath.Dir(archive), 0755); err != nil { t.Fatal(err) } if err := os.WriteFile(archive, []byte("youtube abc123\n"), 0644); err != nil { t.Fatal(err) } if err := svc.Delete(sub.ID); err != nil { t.Fatalf("delete: %v", err) } if _, err := os.Stat(archive); !os.IsNotExist(err) { t.Errorf("archive file still present after delete: %v", err) } if _, err := svc.GetByID(sub.ID); err == nil { t.Error("subscription row still exists after delete") } } // A subscription with no archive file (any mode other than "skip") deletes // cleanly rather than reporting the missing file as a failure. func TestSubscriptionDeleteWithoutArchive(t *testing.T) { db := setupTestDB(t) defer db.Close() svc := NewSubscriptionService(repository.NewSubscriptionRepository(db), &config.Config{DataDir: t.TempDir()}) sub := &models.Subscription{ Name: "Channel", URL: "https://example.com/channel", RefreshMode: "overwrite", ScheduleKind: "daily", } if err := svc.Create(sub); err != nil { t.Fatalf("create: %v", err) } if err := svc.Delete(sub.ID); err != nil { t.Errorf("delete without an archive file: %v", err) } }