-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracks.test.ts
More file actions
209 lines (185 loc) · 5.19 KB
/
Copy pathtracks.test.ts
File metadata and controls
209 lines (185 loc) · 5.19 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
import { beforeEach, describe, expect, it, vi } from 'vitest'
const cacheMocks = vi.hoisted(() => ({
unstable_cache: vi.fn((fn: unknown) => fn)
}))
const dbMocks = vi.hoisted(() => ({
findMany: vi.fn()
}))
const authMocks = vi.hoisted(() => ({
getSpotifyAccessToken: vi.fn(),
hasSpotifyCredentials: vi.fn()
}))
vi.mock('next/cache', () => cacheMocks)
vi.mock('@/server/db/connection', () => ({
db: {
query: {
spotifyListens: {
findMany: dbMocks.findMany
}
}
},
schema: {
spotifyListens: {
playedAt: 'playedAt'
}
}
}))
vi.mock('@/server/spotify/auth', () => authMocks)
describe('spotify track retrieval', () => {
beforeEach(() => {
vi.resetModules()
dbMocks.findMany.mockReset()
authMocks.getSpotifyAccessToken.mockReset()
authMocks.hasSpotifyCredentials.mockReset()
vi.unstubAllGlobals()
})
it('falls back to stored tracks when credentials are missing', async () => {
authMocks.hasSpotifyCredentials.mockReturnValue(false)
dbMocks.findMany.mockResolvedValue([
{
trackId: 'stored-1',
trackName: 'Stored track',
artistName: 'Stored artist',
albumName: 'Stored album',
trackUrl: 'https://open.spotify.com/track/stored-1',
albumImage: 'https://i.scdn.co/image/stored-1',
playedAt: new Date('2026-03-30T06:00:00.000Z')
}
])
const { getSpotifyTracks } = await import('@/server/spotify/tracks')
const result = await getSpotifyTracks(5)
expect(result).toEqual([
{
id: 'stored-1',
name: 'Stored track',
artist: 'Stored artist',
album: 'Stored album',
url: 'https://open.spotify.com/track/stored-1',
image: 'https://i.scdn.co/image/stored-1',
played_at: '2026-03-30T06:00:00.000Z'
}
])
expect(authMocks.getSpotifyAccessToken).not.toHaveBeenCalled()
})
it('falls back to stored tracks when token retrieval fails', async () => {
authMocks.hasSpotifyCredentials.mockReturnValue(true)
authMocks.getSpotifyAccessToken.mockResolvedValue(null)
dbMocks.findMany.mockResolvedValue([])
const fetchSpy = vi.fn()
vi.stubGlobal('fetch', fetchSpy)
const { getSpotifyTracks } = await import('@/server/spotify/tracks')
const result = await getSpotifyTracks(3)
expect(result).toEqual([])
expect(fetchSpy).not.toHaveBeenCalled()
})
it('falls back to stored tracks when spotify responds non-ok', async () => {
authMocks.hasSpotifyCredentials.mockReturnValue(true)
authMocks.getSpotifyAccessToken.mockResolvedValue('token-123')
dbMocks.findMany.mockResolvedValue([
{
trackId: 'stored-2',
trackName: 'Backup listen',
artistName: 'Backup artist',
albumName: null,
trackUrl: 'https://open.spotify.com/track/stored-2',
albumImage: null,
playedAt: new Date('2026-03-29T06:00:00.000Z')
}
])
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: false
})
)
const { getSpotifyTracks } = await import('@/server/spotify/tracks')
const result = await getSpotifyTracks(10)
expect(result).toEqual([
{
id: 'stored-2',
name: 'Backup listen',
artist: 'Backup artist',
album: '',
url: 'https://open.spotify.com/track/stored-2',
image: '',
played_at: '2026-03-29T06:00:00.000Z'
}
])
})
it('normalizes successful spotify responses', async () => {
authMocks.hasSpotifyCredentials.mockReturnValue(true)
authMocks.getSpotifyAccessToken.mockResolvedValue('token-123')
dbMocks.findMany.mockResolvedValue([])
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({
items: [
{
played_at: '2026-03-30T06:10:00.000Z',
track: {
id: 'live-1',
name: 'Live listen',
artists: [
{ name: 'Artist one' },
{ name: 'Artist two' }
],
album: {
name: 'Album one',
images: [
{
url: 'https://i.scdn.co/image/live-1'
}
]
},
external_urls: {
spotify:
'https://open.spotify.com/track/live-1'
}
}
}
]
})
})
)
const { getSpotifyTracks } = await import('@/server/spotify/tracks')
const result = await getSpotifyTracks(1)
expect(result).toEqual([
{
id: 'live-1',
name: 'Live listen',
artist: 'Artist one, Artist two',
album: 'Album one',
url: 'https://open.spotify.com/track/live-1',
image: 'https://i.scdn.co/image/live-1',
played_at: '2026-03-30T06:10:00.000Z'
}
])
})
it('falls back to stored tracks when spotify returns no items', async () => {
authMocks.hasSpotifyCredentials.mockReturnValue(true)
authMocks.getSpotifyAccessToken.mockResolvedValue('token-123')
dbMocks.findMany.mockResolvedValue([
{
trackId: 'stored-3',
trackName: 'Cached listen',
artistName: 'Stored artist',
albumName: 'Stored album',
trackUrl: 'https://open.spotify.com/track/stored-3',
albumImage: 'https://i.scdn.co/image/stored-3',
playedAt: new Date('2026-03-28T06:00:00.000Z')
}
])
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: vi.fn().mockResolvedValue({ items: [] })
})
)
const { getSpotifyTracks } = await import('@/server/spotify/tracks')
const result = await getSpotifyTracks(5)
expect(result[0]?.id).toBe('stored-3')
})
})