71 lines
2.9 KiB
TypeScript
71 lines
2.9 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|||
|
|
|
||
|
|
const publicGetSignedUrl = vi.fn()
|
||
|
|
const adminGetSignedUrl = vi.fn()
|
||
|
|
|
||
|
|
vi.mock('@/api/file', () => ({
|
||
|
|
fileApi: {
|
||
|
|
getSignedUrl: publicGetSignedUrl
|
||
|
|
}
|
||
|
|
}))
|
||
|
|
|
||
|
|
vi.mock('@/api/admin', () => ({
|
||
|
|
adminFileApi: {
|
||
|
|
getSignedUrl: adminGetSignedUrl
|
||
|
|
}
|
||
|
|
}))
|
||
|
|
|
||
|
|
describe('signed-image-cache', () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
vi.resetModules()
|
||
|
|
vi.clearAllMocks()
|
||
|
|
publicGetSignedUrl.mockResolvedValue({ data: 'https://public.test/image.jpg' })
|
||
|
|
adminGetSignedUrl.mockResolvedValue('https://admin.test/image.jpg')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('keeps public and admin signed url cache entries isolated', async () => {
|
||
|
|
const { getCachedSignedImageUrl, getCachedSignedImageUrlSync } = await import('./signed-image-cache')
|
||
|
|
|
||
|
|
await expect(getCachedSignedImageUrl('42', '', 'public')).resolves.toBe('https://public.test/image.jpg')
|
||
|
|
await expect(getCachedSignedImageUrl('42', '', 'admin')).resolves.toBe('https://admin.test/image.jpg')
|
||
|
|
|
||
|
|
expect(publicGetSignedUrl).toHaveBeenCalledTimes(1)
|
||
|
|
expect(adminGetSignedUrl).toHaveBeenCalledTimes(1)
|
||
|
|
expect(getCachedSignedImageUrlSync('42', 'public')).toBe('https://public.test/image.jpg')
|
||
|
|
expect(getCachedSignedImageUrlSync('42', 'admin')).toBe('https://admin.test/image.jpg')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('uses fallback when file id is empty or signing fails', async () => {
|
||
|
|
const { getCachedSignedImageUrl } = await import('./signed-image-cache')
|
||
|
|
|
||
|
|
await expect(getCachedSignedImageUrl('', 'fallback.png', 'public')).resolves.toBe('fallback.png')
|
||
|
|
publicGetSignedUrl.mockRejectedValueOnce(new Error('sign failed'))
|
||
|
|
await expect(getCachedSignedImageUrl('99', 'fallback.png', 'public')).resolves.toBe('fallback.png')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('normalizes signed image urls without scheme', async () => {
|
||
|
|
const { getCachedSignedImageUrl, getCachedSignedImageUrlSync } = await import('./signed-image-cache')
|
||
|
|
|
||
|
|
adminGetSignedUrl.mockResolvedValueOnce('img.yunail.art/325517220106911744.jpg?e=1&token=test')
|
||
|
|
|
||
|
|
await expect(getCachedSignedImageUrl('325517232303947776', '', 'admin')).resolves.toBe(
|
||
|
|
'http://img.yunail.art/325517220106911744.jpg?e=1&token=test'
|
||
|
|
)
|
||
|
|
expect(getCachedSignedImageUrlSync('325517232303947776', 'admin')).toBe(
|
||
|
|
'http://img.yunail.art/325517220106911744.jpg?e=1&token=test'
|
||
|
|
)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('refreshes a cached signed image url after clearing the old entry', async () => {
|
||
|
|
const { getCachedSignedImageUrl, refreshCachedSignedImageUrl } = await import('./signed-image-cache')
|
||
|
|
|
||
|
|
adminGetSignedUrl
|
||
|
|
.mockResolvedValueOnce('https://admin.test/old.jpg')
|
||
|
|
.mockResolvedValueOnce('https://admin.test/new.jpg')
|
||
|
|
|
||
|
|
await expect(getCachedSignedImageUrl('42', '', 'admin')).resolves.toBe('https://admin.test/old.jpg')
|
||
|
|
await expect(refreshCachedSignedImageUrl('42', '', 'admin')).resolves.toBe('https://admin.test/new.jpg')
|
||
|
|
expect(adminGetSignedUrl).toHaveBeenCalledTimes(2)
|
||
|
|
})
|
||
|
|
})
|