summaryrefslogtreecommitdiff
path: root/internal/service/progress_test.go
blob: 3b95c53ea56fb00d39f29daec73e42c863304e2e (plain)
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
package service

import (
	"context"
	"errors"
	"testing"

	"codeberg.org/snonux/player/internal/model"
	"codeberg.org/snonux/player/internal/repository"
)

func TestProgressService_UpdateProgress(t *testing.T) {
	ctx := context.Background()

	tests := []struct {
		name              string
		sessionID         string
		userID            int64
		mediaID           int64
		position          float64
		accLastPosition   float64
		accAccumulated    float64
		accCounted        bool
		accErr            error
		upsertProgressErr error
		upsertAccErr      error
		incrementErr      error
		wantErr           bool
		wantCounted       bool
	}{
		{
			name:            "fresh accumulator does not reach 60 due to clamp",
			sessionID:       "sess1",
			userID:          1,
			mediaID:         10,
			position:        65,
			accLastPosition: 0,
			accAccumulated:  0,
			accCounted:      false,
			wantCounted:     false,
		},
		{
			name:            "accumulator reaches 60",
			sessionID:       "sess1",
			userID:          1,
			mediaID:         10,
			position:        12,
			accLastPosition: 0,
			accAccumulated:  48,
			accCounted:      false,
			wantCounted:     true,
		},
		{
			name:            "delta clamped to 12",
			sessionID:       "sess1",
			userID:          1,
			mediaID:         10,
			position:        20,
			accLastPosition: 0,
			accAccumulated:  0,
			accCounted:      false,
			wantCounted:     false,
		},
		{
			name:            "negative delta clamped",
			sessionID:       "sess1",
			userID:          1,
			mediaID:         10,
			position:        5,
			accLastPosition: 10,
			accAccumulated:  50,
			accCounted:      false,
			wantCounted:     false,
		},
		{
			name:            "already counted",
			sessionID:       "sess1",
			userID:          1,
			mediaID:         10,
			position:        10,
			accLastPosition: 0,
			accAccumulated:  65,
			accCounted:      true,
			wantCounted:     true,
		},
		{
			name:              "upsert progress error",
			sessionID:         "sess1",
			userID:            1,
			mediaID:           10,
			position:          12,
			accAccumulated:    48,
			upsertProgressErr: errors.New("boom"),
			wantErr:           true,
		},
		{
			name:           "get accumulator error",
			sessionID:      "sess1",
			userID:         1,
			mediaID:        10,
			position:       12,
			accAccumulated: 48,
			accErr:         errors.New("boom"),
			wantErr:        true,
		},
		{
			name:           "upsert accumulator error",
			sessionID:      "sess1",
			userID:         1,
			mediaID:        10,
			position:       12,
			accAccumulated: 48,
			upsertAccErr:   errors.New("boom"),
			wantErr:        true,
		},
		{
			name:           "increment error",
			sessionID:      "sess1",
			userID:         1,
			mediaID:        10,
			position:       12,
			accAccumulated: 48,
			incrementErr:   errors.New("boom"),
			wantErr:        true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var savedAcc *model.PlaybackAccumulator
			var incremented int64

			store := &repository.MockStore{
				PlaybackProgressRepo: repository.MockPlaybackProgressRepo{
					UpsertProgressFunc: func(ctx context.Context, progress *model.PlaybackProgress) error {
						return tt.upsertProgressErr
					},
				},
				PlaybackAccumulatorRepo: repository.MockPlaybackAccumulatorRepo{
					GetAccumulatorFunc: func(ctx context.Context, sessionID string, mediaID int64) (*model.PlaybackAccumulator, error) {
						if tt.accErr != nil {
							return nil, tt.accErr
						}
						return &model.PlaybackAccumulator{
							SessionID:          sessionID,
							MediaID:            mediaID,
							LastPosition:       tt.accLastPosition,
							AccumulatedSeconds: tt.accAccumulated,
							Counted:            tt.accCounted,
						}, nil
					},
					UpsertAccumulatorFunc: func(ctx context.Context, acc *model.PlaybackAccumulator) error {
						savedAcc = acc
						return tt.upsertAccErr
					},
				},
				MediaRepo: repository.MockMediaRepo{
					IncrementPlayCountFunc: func(ctx context.Context, id int64) error {
						incremented = id
						return tt.incrementErr
					},
				},
			}

			svc := NewProgressService(store, newMockClock())
			err := svc.UpdateProgress(ctx, tt.sessionID, tt.userID, tt.mediaID, tt.position)
			if tt.wantErr {
				if err == nil {
					t.Fatal("expected error")
				}
				return
			}
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
			if savedAcc == nil {
				t.Fatal("expected accumulator saved")
			}
			if savedAcc.Counted != tt.wantCounted {
				t.Fatalf("expected Counted=%v, got %v", tt.wantCounted, savedAcc.Counted)
			}
			if tt.wantCounted && !tt.accCounted && incremented != tt.mediaID {
				t.Fatalf("expected IncrementPlayCount called with %d", tt.mediaID)
			}
		})
	}
}