a
File size: 9,706 Bytes
6c38a8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
094c13f
6c38a8d
 
 
 
 
 
 
 
 
 
 
 
 
094c13f
 
 
02c8c3c
 
f54670e
58dc9a1
f0e0245
 
58dc9a1
 
 
 
 
 
 
6db0b76
0b7bef2
 
 
6c38a8d
 
 
 
 
 
 
094c13f
 
 
02c8c3c
 
f54670e
58dc9a1
f0e0245
 
58dc9a1
 
 
 
 
 
 
6db0b76
0b7bef2
 
 
6c38a8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0e0245
0b7bef2
f0e0245
 
 
58dc9a1
f0e0245
 
0b7bef2
f0e0245
7f36b83
 
 
 
 
58dc9a1
7f36b83
 
58dc9a1
7f36b83
 
8762688
 
 
 
58dc9a1
8762688
 
58dc9a1
8762688
 
7f36b83
 
 
 
58dc9a1
7f36b83
 
58dc9a1
7f36b83
 
 
 
 
 
58dc9a1
7f36b83
 
58dc9a1
7f36b83
76b8a0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f54670e
76b8a0b
 
 
 
 
 
 
 
 
 
6db0b76
76b8a0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c38a8d
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
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createPinia, setActivePinia } from 'pinia'

const mockAuthStore = {
  token: null as string | null,
  apiKeyToken: 'test-jwt-token',
  refresh: vi.fn(),
  logout: vi.fn(),
}

vi.mock('@/stores/auth', () => ({
  useAuthStore: () => mockAuthStore,
}))

import { selfApi } from '@/lib/api'

function makeFetchResponse(status: number, body: unknown = {}) {
  return {
    ok: status >= 200 && status < 300,
    status,
    json: () => Promise.resolve(body),
  }
}

describe('selfApi', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
    vi.restoreAllMocks()
    mockAuthStore.apiKeyToken = 'test-jwt-token'
    mockAuthStore.token = null
  })

  it('getInfo sends GET with JWT token as Bearer token', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200, { name: 'k', key_prefix: 'ab' }))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.getInfo()

    expect(fetchMock).toHaveBeenCalledTimes(1)
    const [url, options] = fetchMock.mock.calls[0]
    expect(url).toContain('/api/key/me')
    expect(options.headers.get('Authorization')).toBe('Bearer test-jwt-token')
  })

  it('getSettings sends GET to /api/key/me/settings', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200, { image_source: 't' }))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.getSettings()

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('/api/key/me/settings')
  })

  it('updateSettings sends PUT with JSON body', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200, { ok: true }))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.updateSettings({
      image_source: 'f',
      lang: 'de',
      textless: true,
      ratings_limit: 3,
      ratings_order: 'mal,imdb,trakt',
      ratings_exclude: 'rt',
      poster_position: 'bc',
      logo_ratings_limit: 3,
      backdrop_ratings_limit: 3,
      poster_badge_style: 'h',
      logo_badge_style: 'h',
      backdrop_badge_style: 'v',
      poster_label_style: 't',
      logo_label_style: 't',
      backdrop_label_style: 't',
      poster_badge_direction: 'd',
      poster_badge_split: false,
      poster_badge_size: 'l',
      logo_badge_size: 's',
      backdrop_badge_size: 'xl',
    })

    const [url, options] = fetchMock.mock.calls[0]
    expect(url).toContain('/api/key/me/settings')
    expect(options.method).toBe('PUT')
    expect(options.headers.get('Content-Type')).toBe('application/json')
    expect(JSON.parse(options.body)).toEqual({
      image_source: 'f',
      lang: 'de',
      textless: true,
      ratings_limit: 3,
      ratings_order: 'mal,imdb,trakt',
      ratings_exclude: 'rt',
      poster_position: 'bc',
      logo_ratings_limit: 3,
      backdrop_ratings_limit: 3,
      poster_badge_style: 'h',
      logo_badge_style: 'h',
      backdrop_badge_style: 'v',
      poster_label_style: 't',
      logo_label_style: 't',
      backdrop_label_style: 't',
      poster_badge_direction: 'd',
      poster_badge_split: false,
      poster_badge_size: 'l',
      logo_badge_size: 's',
      backdrop_badge_size: 'xl',
    })
  })

  it('resetSettings sends DELETE', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200, { ok: true }))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.resetSettings()

    const [url, options] = fetchMock.mock.calls[0]
    expect(url).toContain('/api/key/me/settings')
    expect(options.method).toBe('DELETE')
  })

  it('does not set Authorization when apiKeyToken is null', async () => {
    mockAuthStore.apiKeyToken = null as unknown as string
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.getInfo()

    const [, options] = fetchMock.mock.calls[0]
    expect(options.headers.has('Authorization')).toBe(false)
  })

  it('does not include credentials (no cookie needed)', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.getInfo()

    const [, options] = fetchMock.mock.calls[0]
    // keyRequest does not set credentials: 'include'
    expect(options.credentials).toBeUndefined()
  })

  it('previewPoster includes position when provided', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewPoster(3, 'imdb,rt', 'r')

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('position=r')
  })

  it('previewPoster includes label_style when provided', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewPoster(3, 'imdb,rt', 'bc', 'h', 'i')

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('label_style=i')
  })

  it('previewPoster includes badge_direction when provided', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewPoster(3, 'imdb,rt', 'bc', 'h', 'i', 'v')

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('badge_direction=v')
  })

  it('previewLogo includes label_style when provided', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewLogo(3, 'imdb,rt', 'h', 'i')

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('label_style=i')
  })

  it('previewBackdrop includes label_style when provided', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewBackdrop(3, 'imdb,rt', 'v', 'i')

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('label_style=i')
  })

  it('previewEpisode calls GET with correct URL and params', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewEpisode(3, 'imdb,rt,tmdb')

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('/api/key/me/preview/episode')
    expect(url).toContain('ratings_limit=3')
    expect(url).toContain('ratings_order=imdb%2Crt%2Ctmdb')
  })

  it('previewEpisode includes position when provided', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewEpisode(3, 'imdb,rt', undefined, undefined, undefined, 'tr')

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('position=tr')
  })

  it('previewEpisode includes badge_direction when provided', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewEpisode(3, 'imdb,rt', undefined, undefined, undefined, 'tr', 'h')

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('badge_direction=h')
  })

  it('previewEpisode includes blur=true when enabled', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewEpisode(3, 'imdb,rt', undefined, undefined, undefined, undefined, undefined, true)

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('blur=true')
  })

  it('previewEpisode omits blur when false', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewEpisode(3, 'imdb,rt', undefined, undefined, undefined, undefined, undefined, false)

    const [url] = fetchMock.mock.calls[0]
    expect(url).not.toContain('blur')
  })

  it('previewEpisode includes label_style when provided', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.previewEpisode(3, 'imdb,rt', 'v', 'i')

    const [url] = fetchMock.mock.calls[0]
    expect(url).toContain('label_style=i')
  })

  it('updateSettings includes episode fields', async () => {
    const fetchMock = vi.fn().mockResolvedValue(makeFetchResponse(200, { ok: true }))
    vi.stubGlobal('fetch', fetchMock)

    await selfApi.updateSettings({
      image_source: 't',
      lang: 'en',
      textless: false,
      ratings_limit: 3,
      ratings_order: 'imdb,rt,tmdb',
      ratings_exclude: '',
      poster_position: 'bc',
      logo_ratings_limit: 3,
      backdrop_ratings_limit: 3,
      poster_badge_style: 'h',
      logo_badge_style: 'h',
      backdrop_badge_style: 'v',
      poster_label_style: 't',
      logo_label_style: 't',
      backdrop_label_style: 't',
      poster_badge_direction: 'd',
      poster_badge_split: false,
      poster_badge_size: 'l',
      logo_badge_size: 's',
      backdrop_badge_size: 'xl',
      episode_ratings_limit: 1,
      episode_badge_style: 'v',
      episode_label_style: 'o',
      episode_badge_size: 'l',
      episode_position: 'tr',
      episode_badge_direction: 'v',
      episode_blur: false,
    })

    const [, options] = fetchMock.mock.calls[0]
    const body = JSON.parse(options.body)
    expect(body.episode_ratings_limit).toBe(1)
    expect(body.episode_badge_style).toBe('v')
    expect(body.episode_label_style).toBe('o')
    expect(body.episode_badge_size).toBe('l')
    expect(body.episode_position).toBe('tr')
    expect(body.episode_badge_direction).toBe('v')
    expect(body.episode_blur).toBe(false)
  })
})