1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
package service
import (
"context"
"errors"
"testing"
"codeberg.org/snonux/player/internal/model"
"codeberg.org/snonux/player/internal/repository"
)
func TestService_NoRows_ReturnsNil(t *testing.T) {
ctx := context.Background()
t.Run("GetMediaDetail nil media", func(t *testing.T) {
store := &repository.MockStore{
MediaRepo: repository.MockMediaRepo{
GetMediaByIDFunc: func(ctx context.Context, id int64) (*model.Media, error) {
return nil, nil
},
},
}
svc := NewMediaService(store, newMockClock(), "/tmp/media", nil, nil)
detail, err := svc.GetMediaDetail(ctx, 99, 1)
if !errors.Is(err, ErrNotFound) {
t.Fatalf("expected ErrNotFound, got %v", err)
}
if detail != nil {
t.Fatalf("expected nil detail, got %+v", detail)
}
})
t.Run("GetNote nil", func(t *testing.T) {
store := &repository.MockStore{
MediaRepo: repository.MockMediaRepo{
GetMediaByIDFunc: func(ctx context.Context, id int64) (*model.Media, error) {
return &model.Media{ID: 1, SetID: 1}, nil
},
},
UserRepo: repository.MockUserRepo{
GetUserByIDFunc: func(ctx context.Context, id int64) (*model.User, error) {
return &model.User{ID: id, IsAdmin: true}, nil
},
},
SetRepo: repository.MockSetRepo{
GetSetByIDFunc: func(ctx context.Context, id int64) (*model.Set, error) {
return &model.Set{ID: id}, nil
},
},
NoteRepo: repository.MockNoteRepo{
GetNoteFunc: func(ctx context.Context, mediaID, userID int64) (*model.Note, error) {
return nil, nil
},
},
}
svc := NewMediaService(store, newMockClock(), "/tmp/media", nil, nil)
note, err := svc.GetNote(ctx, 1, 1)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if note != nil {
t.Fatalf("expected nil note, got %+v", note)
}
})
t.Run("AssignTag creates missing tag", func(t *testing.T) {
store := &repository.MockStore{
MediaRepo: repository.MockMediaRepo{
GetMediaByIDFunc: func(ctx context.Context, id int64) (*model.Media, error) {
return &model.Media{ID: 1, SetID: 1}, nil
},
},
UserRepo: repository.MockUserRepo{
GetUserByIDFunc: func(ctx context.Context, id int64) (*model.User, error) {
return &model.User{ID: id, IsAdmin: true}, nil
},
},
SetRepo: repository.MockSetRepo{
GetSetByIDFunc: func(ctx context.Context, id int64) (*model.Set, error) {
return &model.Set{ID: id}, nil
},
},
TagRepo: repository.MockTagRepo{
GetTagByNameFunc: func(ctx context.Context, name string) (*model.Tag, error) {
return nil, nil
},
CreateTagFunc: func(ctx context.Context, name string) (int64, error) {
return 42, nil
},
AssignTagFunc: func(ctx context.Context, mediaID, tagID int64) error {
return nil
},
},
}
svc := NewMediaService(store, newMockClock(), "/tmp/media", nil, nil)
if err := svc.AssignTag(ctx, 1, 1, "newtag"); err != nil {
t.Fatalf("expected no error, got %v", err)
}
})
t.Run("UpdateProgress with nil accumulator", func(t *testing.T) {
store := &repository.MockStore{
PlaybackProgressRepo: repository.MockPlaybackProgressRepo{
UpsertProgressFunc: func(ctx context.Context, progress *model.PlaybackProgress) error {
return nil
},
},
PlaybackAccumulatorRepo: repository.MockPlaybackAccumulatorRepo{
GetAccumulatorFunc: func(ctx context.Context, sessionID string, mediaID int64) (*model.PlaybackAccumulator, error) {
return nil, nil
},
UpsertAccumulatorFunc: func(ctx context.Context, acc *model.PlaybackAccumulator) error {
return nil
},
},
MediaRepo: repository.MockMediaRepo{
// verifyAccess now runs before applyProgress; supply a
// non-nil media so the access check passes.
GetMediaByIDFunc: func(ctx context.Context, id int64) (*model.Media, error) {
return &model.Media{ID: id, SetID: 7}, nil
},
IncrementPlayCountFunc: func(ctx context.Context, id int64) error {
return nil
},
},
UserRepo: repository.MockUserRepo{
GetUserByIDFunc: func(ctx context.Context, id int64) (*model.User, error) {
return &model.User{ID: id, IsAdmin: true}, nil
},
},
}
svc := NewProgressService(store, newMockClock())
if err := svc.UpdateProgress(ctx, "sess", 1, 10, 5); err != nil {
t.Fatalf("expected no error, got %v", err)
}
})
t.Run("ValidateShareToken nil", func(t *testing.T) {
store := &repository.MockStore{
ShareRepo: repository.MockShareRepo{
GetShareByTokenFunc: func(ctx context.Context, token string) (*model.Share, error) {
return nil, nil
},
},
}
svc := NewMediaService(store, newMockClock(), "/tmp/media", nil, nil)
sh, err := svc.ValidateShareToken(ctx, "nope")
if !errors.Is(err, ErrShareNotFound) {
t.Fatalf("expected ErrShareNotFound, got %v", err)
}
if sh != nil {
t.Fatalf("expected nil share, got %+v", sh)
}
})
t.Run("StreamSharedMedia missing share", func(t *testing.T) {
store := &repository.MockStore{
ShareRepo: repository.MockShareRepo{
GetShareByTokenFunc: func(ctx context.Context, token string) (*model.Share, error) {
return nil, nil
},
},
}
svc := NewMediaService(store, newMockClock(), "/tmp/media", nil, nil)
_, err := svc.StreamSharedMedia(ctx, "nope")
if !errors.Is(err, ErrShareNotFound) {
t.Fatalf("expected ErrShareNotFound, got %v", err)
}
})
t.Run("verifyAccess missing permission", func(t *testing.T) {
store := &repository.MockStore{
MediaRepo: repository.MockMediaRepo{
GetMediaByIDFunc: func(ctx context.Context, id int64) (*model.Media, error) {
return &model.Media{ID: 1, SetID: 1}, nil
},
},
UserRepo: repository.MockUserRepo{
GetUserByIDFunc: func(ctx context.Context, id int64) (*model.User, error) {
return &model.User{ID: 1, IsAdmin: false}, nil
},
},
SetRepo: repository.MockSetRepo{
GetSetByIDFunc: func(ctx context.Context, id int64) (*model.Set, error) {
return &model.Set{ID: 1}, nil
},
},
SetPermissionRepo: repository.MockSetPermissionRepo{
GetPermissionFunc: func(ctx context.Context, setID, userID int64) (*model.SetPermission, error) {
return nil, nil
},
},
}
svc := NewMediaService(store, newMockClock(), "/tmp/media", nil, nil)
_, err := svc.StreamMedia(ctx, 1, 1)
if !errors.Is(err, ErrForbidden) {
t.Fatalf("expected ErrForbidden, got %v", err)
}
})
}
|