configs/server-dockers/jellyfin-news/main_test.go
server-dockers/jellyfin-news/main_test.goRaw
1package main
2
3import (
4 "context"
5 "net/http"
6 "net/http/httptest"
7 "strings"
8 "testing"
9 "time"
10)
11
12type fakeSource struct {
13 start time.Time
14 end time.Time
15 items []Item
16 err error
17}
18
19func (f *fakeSource) AddedBetween(_ context.Context, start, end time.Time) ([]Item, error) {
20 f.start, f.end = start, end
21 return f.items, f.err
22}
23
24func testApp(source *fakeSource) *app {
25 return &app{
26 jellyfin: source,
27 location: time.UTC,
28 labels: DefaultLabels(),
29 now: func() time.Time {
30 return time.Date(2026, 9, 2, 12, 0, 0, 0, time.UTC)
31 },
32 }
33}
34
35func TestCurrentWeek(t *testing.T) {
36 source := &fakeSource{}
37 request := httptest.NewRequest(http.MethodGet, "/api/current-week", nil)
38 response := httptest.NewRecorder()
39 testApp(source).routes().ServeHTTP(response, request)
40
41 if response.Code != http.StatusOK {
42 t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
43 }
44 if got := response.Body.String(); !strings.HasPrefix(got, "2026-08-31 - 2026-09-02 jellyfin weekly update:") {
45 t.Fatalf("body = %q", got)
46 }
47 wantStart := time.Date(2026, 8, 31, 0, 0, 0, 0, time.UTC)
48 if !source.start.Equal(wantStart) || !source.end.Equal(testApp(source).now()) {
49 t.Fatalf("range = %v to %v", source.start, source.end)
50 }
51}
52
53func TestStartOfWeekUsesMondayMidnight(t *testing.T) {
54 location := time.FixedZone("test", 2*60*60)
55 now := time.Date(2026, 9, 6, 23, 45, 0, 0, location) // Sunday
56 want := time.Date(2026, 8, 31, 0, 0, 0, 0, location)
57 if got := startOfWeek(now); !got.Equal(want) {
58 t.Fatalf("startOfWeek() = %v, want %v", got, want)
59 }
60}
61
62func TestSelectedWeek(t *testing.T) {
63 source := &fakeSource{}
64 request := httptest.NewRequest(http.MethodGet, "/api/week?offset=-2", nil)
65 response := httptest.NewRecorder()
66 testApp(source).routes().ServeHTTP(response, request)
67
68 if response.Code != http.StatusOK {
69 t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
70 }
71 if got := response.Body.String(); !strings.HasPrefix(got, "2026-08-17 - 2026-08-24 jellyfin weekly update:") {
72 t.Fatalf("body = %q", got)
73 }
74}
75
76func TestSelectedWeekRejectsInvalidOffset(t *testing.T) {
77 request := httptest.NewRequest(http.MethodGet, "/api/week?offset=0", nil)
78 response := httptest.NewRecorder()
79 testApp(&fakeSource{}).routes().ServeHTTP(response, request)
80 if response.Code != http.StatusBadRequest {
81 t.Fatalf("status = %d", response.Code)
82 }
83}
84
85func TestSelectedMonth(t *testing.T) {
86 source := &fakeSource{}
87 request := httptest.NewRequest(http.MethodGet, "/api/month?offset=-2", nil)
88 response := httptest.NewRecorder()
89 testApp(source).routes().ServeHTTP(response, request)
90
91 if response.Code != http.StatusOK {
92 t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
93 }
94 if got := response.Body.String(); !strings.HasPrefix(got, "2026-07 jellyfin monthly update:") {
95 t.Fatalf("body = %q", got)
96 }
97 if source.start.Month() != time.July || source.end.Month() != time.August {
98 t.Fatalf("range = %v to %v", source.start, source.end)
99 }
100}
101
102func TestSelectedMonthRejectsInvalidOffset(t *testing.T) {
103 request := httptest.NewRequest(http.MethodGet, "/api/month?offset=0", nil)
104 response := httptest.NewRecorder()
105 testApp(&fakeSource{}).routes().ServeHTTP(response, request)
106 if response.Code != http.StatusBadRequest {
107 t.Fatalf("status = %d", response.Code)
108 }
109}
110
111func TestIndex(t *testing.T) {
112 request := httptest.NewRequest(http.MethodGet, "/", nil)
113 response := httptest.NewRecorder()
114 testApp(&fakeSource{}).routes().ServeHTTP(response, request)
115 if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), "Jellyfin updates") {
116 t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
117 }
118}