-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrack_test.go
More file actions
346 lines (327 loc) · 9.55 KB
/
track_test.go
File metadata and controls
346 lines (327 loc) · 9.55 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package scdl
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
)
func TestGetTrack(t *testing.T) {
// Sample hydration data (minified)
hydrationData := `[{"hydratable":"user","data":{}},{"hydratable":"sound","data":{"id":123456,"title":"Test Title","description":"Test Description","genre":"Rock","duration":60000,"artwork_url":"https://i1.sndcdn.com/artworks-000-large.jpg","track_authorization":"auth-token","user":{"username":"Test Artist"},"media":{"transcodings":[{"url":"https://api-v2.soundcloud.com/media/soundcloud:tracks:123456/stream/hls","format":{"protocol":"hls","mime_type":"audio/mpeg"}},{"url":"https://api-v2.soundcloud.com/media/soundcloud:tracks:123456/stream/progressive","format":{"protocol":"progressive","mime_type":"audio/mpeg"}}]}}}]`
htmlResponse := `
<html>
<body>
<script>
window.__sc_hydration = ` + hydrationData + `;
</script>
</body>
</html>
`
client := &Client{
httpClient: &http.Client{
Transport: &mockTransport{
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
if req.URL.String() == "https://soundcloud.com/artist/song" {
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(htmlResponse)),
Header: make(http.Header),
}, nil
}
return &http.Response{
StatusCode: 404,
Body: io.NopCloser(strings.NewReader("Not Found")),
}, nil
},
},
},
}
track, err := client.GetTrack(context.Background(), "https://soundcloud.com/artist/song")
if err != nil {
t.Fatalf("GetTrack() error = %v", err)
}
if track.ID != 123456 {
t.Errorf("got ID %d, want 123456", track.ID)
}
if track.Title != "Test Title" {
t.Errorf("got Title %q, want %q", track.Title, "Test Title")
}
if track.Artist != "Test Artist" {
t.Errorf("got Artist %q, want %q", track.Artist, "Test Artist")
}
if track.HLSURL != "https://api-v2.soundcloud.com/media/soundcloud:tracks:123456/stream/hls" {
t.Errorf("got HLSURL %q, want %q", track.HLSURL, "https://api-v2.soundcloud.com/media/soundcloud:tracks:123456/stream/hls")
}
}
func TestGetTrack_Errors(t *testing.T) {
client := &Client{
httpClient: &http.Client{
Transport: &mockTransport{
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
u := req.URL.String()
if strings.Contains(u, "fail") {
return nil, fmt.Errorf("fail")
}
if strings.Contains(u, "no-hydration") {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`no data`))}, nil
}
if strings.Contains(u, "bad-json") {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`window.__sc_hydration = [invalid];`))}, nil
}
if strings.Contains(u, "no-sound") {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`window.__sc_hydration = [{"hydratable":"user","data":{}}];`))}, nil
}
if strings.Contains(u, "no-hls") {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`window.__sc_hydration = [{"hydratable":"sound","data":{"id":1,"media":{"transcodings":[]}}}];`))}, nil
}
return &http.Response{StatusCode: 404, Body: io.NopCloser(strings.NewReader("404"))}, nil
},
},
},
}
t.Run("FetchFail", func(t *testing.T) {
_, err := client.GetTrack(context.Background(), "http://fail")
if err == nil {
t.Error("expected error")
}
})
t.Run("NoHydration", func(t *testing.T) {
_, err := client.GetTrack(context.Background(), "http://no-hydration")
if err == nil || !strings.Contains(err.Error(), "hydration data not found") {
t.Errorf("unexpected error: %v", err)
}
})
t.Run("BadJSON", func(t *testing.T) {
_, err := client.GetTrack(context.Background(), "http://bad-json")
if err == nil {
t.Error("expected error")
}
})
t.Run("BadDataJSON", func(t *testing.T) {
localClient := &Client{
httpClient: &http.Client{
Transport: &mockTransport{
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`window.__sc_hydration = [{"hydratable":"sound","data":{"id":"not-an-int"}}];`))}, nil
},
},
},
}
_, err := localClient.GetTrack(context.Background(), "http://bad-data-json")
if err == nil {
t.Error("expected error")
}
})
t.Run("NoSound", func(t *testing.T) {
_, err := client.GetTrack(context.Background(), "http://no-sound")
if err == nil || !strings.Contains(err.Error(), "no sound entry found") {
t.Errorf("unexpected error: %v", err)
}
})
t.Run("NoHLS", func(t *testing.T) {
_, err := client.GetTrack(context.Background(), "http://no-hls")
if err == nil || !strings.Contains(err.Error(), "no HLS audio/mpeg transcoding found") {
t.Errorf("unexpected error: %v", err)
}
})
}
func TestCleanupTrackTitle(t *testing.T) {
tests := []struct {
name string
title string
artist string
album string
expected string
}{
{
name: "No Changes Needed",
title: "My Awesome Song",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song",
},
{
name: "Empty Artist And Album",
title: "My Awesome Song - Joe",
artist: "",
album: "",
expected: "My Awesome Song - Joe",
},
{
name: "Exact Match Artist",
title: "Joe - My Awesome Song",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song",
},
{
name: "Exact Match Album",
title: "My Awesome Song - The Album",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song",
},
{
name: "Artist And Album Removed",
title: "Joe - My Awesome Song - The Album",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song",
},
{
name: "Case Insensitive Match",
title: "my awesome song - JOE",
artist: "joe",
album: "the album",
expected: "my awesome song",
},
{
name: "Ampersand List Match First",
title: "Joe & Steve - My Awesome Song",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song",
},
{
name: "Ampersand List Match Second",
title: "Steve & Joe - My Awesome Song",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song",
},
{
name: "Comma List Match",
title: "My Awesome Song - Steve, Joe",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song",
},
{
name: "And List Match",
title: "Steve and Joe - My Awesome Song",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song",
},
{
name: "Similar Album Substring Not Removed",
title: "My Awesome Song - The Album Deluxe",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song - The Album Deluxe",
},
{
name: "Multiple Empty Separators",
title: "Joe - - My Awesome Song",
artist: "Joe",
album: "The Album",
expected: "My Awesome Song",
},
{
name: "Hyphenated Title Preserved",
title: "Joe - Part-One - The Album",
artist: "Joe",
album: "The Album",
expected: "Part-One",
},
{
name: "Artist Contains Ampersand",
title: "Joe & Steve - My Awesome Song",
artist: "Joe & Steve",
album: "The Album",
expected: "My Awesome Song",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := cleanupTrackTitle(tt.title, tt.artist, tt.album)
if got != tt.expected {
t.Errorf("cleanupTrackTitle(%q, %q, %q) = %q; want %q",
tt.title, tt.artist, tt.album, got, tt.expected)
}
})
}
}
func TestNullableTime(t *testing.T) {
tests := []struct {
name string
input string
wantZero bool
wantYear int
wantError bool
}{
{
name: "null JSON value",
input: `null`,
wantZero: true,
},
{
name: "empty string",
input: `""`,
wantZero: true,
},
{
name: "valid RFC3339 timestamp",
input: `"2023-06-15T12:00:00Z"`,
wantZero: false,
wantYear: 2023,
},
{
name: "invalid value",
input: `"not-a-date"`,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var nt nullableTime
err := json.Unmarshal([]byte(tt.input), &nt)
if tt.wantError {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.wantZero && !nt.IsZero() {
t.Errorf("expected zero time, got %v", nt.Time)
}
if !tt.wantZero && nt.Year() != tt.wantYear {
t.Errorf("expected year %d, got %d", tt.wantYear, nt.Year())
}
})
}
t.Run("unmarshals correctly inside struct", func(t *testing.T) {
var data struct {
CreatedAt nullableTime `json:"created_at"`
ReleaseDate nullableTime `json:"release_date"`
}
err := json.Unmarshal([]byte(`{"created_at":"2021-03-01T00:00:00Z","release_date":null}`), &data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if data.CreatedAt.Year() != 2021 {
t.Errorf("expected 2021, got %d", data.CreatedAt.Year())
}
if !data.ReleaseDate.IsZero() {
t.Errorf("expected zero time for null release_date, got %v", data.ReleaseDate.Time)
}
})
t.Run("empty string in struct does not error", func(t *testing.T) {
var data struct {
ReleaseDate nullableTime `json:"release_date"`
}
err := json.Unmarshal([]byte(`{"release_date":""}`), &data)
if err != nil {
t.Fatalf("empty string should not cause unmarshal error, got: %v", err)
}
if !data.ReleaseDate.IsZero() {
t.Errorf("expected zero time for empty string, got %v", data.ReleaseDate.Time)
}
})
}