Parent directory

handler_test.go

2000 bytes
 1package handler
 2
 3import (
 4	"context"
 5	"io"
 6	"log/slog"
 7	"testing"
 8	"time"
 9
10	"github.com/stretchr/testify/assert"
11
12	"github.com/TheEdgeOfRage/ytrssil-api/config"
13	db_mock "github.com/TheEdgeOfRage/ytrssil-api/mocks/db"
14	parser_mock "github.com/TheEdgeOfRage/ytrssil-api/mocks/feedparser"
15	youtube_mock "github.com/TheEdgeOfRage/ytrssil-api/mocks/youtube"
16	"github.com/TheEdgeOfRage/ytrssil-api/models"
17)
18
19var testConfig config.Config
20
21func init() {
22	testConfig = config.TestConfig()
23}
24
25func TestChannelIDFromInput(t *testing.T) {
26	tests := []struct {
27		name    string
28		input   string
29		want    string
30		wantErr error
31	}{
32		{name: "handle", input: "@username", want: "@username"},
33		{name: "YouTube handle URL", input: "https://youtube.com/@username", want: "@username"},
34		{name: "mobile YouTube handle URL", input: "https://m.youtube.com/@username", want: "@username"},
35		{
36			name:  "channel ID URL",
37			input: "https://www.youtube.com/channel/UC1234567890123456789012",
38			want:  "UC1234567890123456789012",
39		},
40		{name: "non-YouTube URL", input: "https://example.com/@username", wantErr: ErrInvalidChannelURL},
41	}
42
43	for _, tt := range tests {
44		t.Run(tt.name, func(t *testing.T) {
45			got, err := channelIDFromInput(tt.input)
46			assert.ErrorIs(t, err, tt.wantErr)
47			assert.Equal(t, tt.want, got)
48		})
49	}
50}
51
52func TestGetNewVideos(t *testing.T) {
53	l := slog.New(slog.NewTextHandler(io.Discard, nil))
54	handler := New(
55		l,
56		&db_mock.DBMock{
57			GetNewVideosFunc: func(ctx context.Context, _ bool) ([]models.Video, error) {
58				return []models.Video{
59					{
60						ID:            "test",
61						ChannelName:   "test",
62						Title:         "test",
63						PublishedTime: time.Now(),
64					},
65				}, nil
66			},
67		},
68		&parser_mock.ParserMock{},
69		&youtube_mock.ClientMock{},
70		nil,
71		testConfig,
72	)
73	resp, err := handler.GetNewVideos(context.TODO(), false)
74
75	if assert.NoError(t, err) {
76		if assert.NotNil(t, resp) {
77			assert.Equal(t, resp[0].ID, "test")
78			assert.Equal(t, resp[0].Title, "test")
79		}
80	}
81}