subscription_test.go
⎇
Raw
1package service
2
3import (
4 "testing"
5 "time"
6
7 "vidarchive/internal/models"
8)
9
10func TestComputeNextRun(t *testing.T) {
11 svc := &SubscriptionService{}
12 // A fixed reference time: 2026-06-21 12:30 local.
13 from := time.Date(2026, 6, 21, 12, 30, 0, 0, time.Local)
14
15 cases := []struct {
16 kind string
17 custom string
18 wantErr bool
19 check func(t *testing.T, next time.Time)
20 }{
21 {kind: "hourly", check: func(t *testing.T, next time.Time) {
22 if !next.After(from) || next.Minute() != 0 {
23 t.Errorf("hourly: got %v", next)
24 }
25 }},
26 {kind: "daily", check: func(t *testing.T, next time.Time) {
27 if next.Hour() != 3 || next.Minute() != 0 || !next.After(from) {
28 t.Errorf("daily: got %v", next)
29 }
30 }},
31 {kind: "weekly", check: func(t *testing.T, next time.Time) {
32 if next.Weekday() != time.Sunday || !next.After(from) {
33 t.Errorf("weekly: got %v", next)
34 }
35 }},
36 {kind: "monthly", check: func(t *testing.T, next time.Time) {
37 if next.Day() != 1 || !next.After(from) {
38 t.Errorf("monthly: got %v", next)
39 }
40 }},
41 {kind: "cron", custom: "*/15 * * * *", check: func(t *testing.T, next time.Time) {
42 if next.Minute() != 45 || !next.After(from) {
43 t.Errorf("custom cron */15: got %v", next)
44 }
45 }},
46 {kind: "cron", custom: "not a cron", wantErr: true},
47 {kind: "cron", custom: "", wantErr: true},
48 {kind: "bogus", wantErr: true},
49 }
50
51 for _, c := range cases {
52 sub := &models.Subscription{ScheduleKind: c.kind, CronExpr: c.custom}
53 next, err := svc.ComputeNextRun(sub, from)
54 if c.wantErr {
55 if err == nil {
56 t.Errorf("kind=%q custom=%q: expected error, got %v", c.kind, c.custom, next)
57 }
58 continue
59 }
60 if err != nil {
61 t.Errorf("kind=%q custom=%q: unexpected error: %v", c.kind, c.custom, err)
62 continue
63 }
64 c.check(t, next)
65 }
66}
67