videos_test.go
1812 bytes
1package handler
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 "github.com/stretchr/testify/require"
8)
9
10func TestParseVideoInput(t *testing.T) {
11 tests := []struct {
12 name string
13 input string
14 wantID string
15 wantProgress int
16 wantErr bool
17 }{
18 {
19 name: "plain video ID",
20 input: "dQw4w9WgXcQ",
21 wantID: "dQw4w9WgXcQ",
22 },
23 {
24 name: "www.youtube.com watch URL",
25 input: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
26 wantID: "dQw4w9WgXcQ",
27 },
28 {
29 name: "youtube.com watch URL with t param",
30 input: "https://youtube.com/watch?v=dQw4w9WgXcQ&t=9780",
31 wantID: "dQw4w9WgXcQ",
32 wantProgress: 9780,
33 },
34 {
35 name: "m.youtube.com with extra params",
36 input: "https://m.youtube.com/watch?v=dQw4w9WgXcQ&pp=abc",
37 wantID: "dQw4w9WgXcQ",
38 },
39 {
40 name: "youtube.com live URL",
41 input: "https://www.youtube.com/live/dQw4w9WgXcQ?si=abc",
42 wantID: "dQw4w9WgXcQ",
43 },
44 {
45 name: "youtu.be short URL",
46 input: "https://youtu.be/dQw4w9WgXcQ",
47 wantID: "dQw4w9WgXcQ",
48 },
49 {
50 name: "www.youtube.com watch URL with t param",
51 input: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=120",
52 wantID: "dQw4w9WgXcQ",
53 wantProgress: 120,
54 },
55 {
56 name: "URL missing video ID",
57 input: "https://www.youtube.com/watch",
58 wantErr: true,
59 },
60 {
61 name: "invalid t param",
62 input: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=abc",
63 wantErr: true,
64 },
65 }
66
67 for _, tt := range tests {
68 t.Run(tt.name, func(t *testing.T) {
69 got, err := parseVideoInput(tt.input)
70 if tt.wantErr {
71 require.Error(t, err)
72 return
73 }
74 require.NoError(t, err)
75 assert.Equal(t, tt.wantID, got.id)
76 assert.Equal(t, tt.wantProgress, got.progressSeconds)
77 })
78 }
79}