|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const { cacheDefinitions, hashMock } = vi.hoisted(() => ({ |
| 4 | + cacheDefinitions: [] as Array<{ getKey?: (...args: any[]) => string }>, |
| 5 | + hashMock: vi.fn((value: unknown) => `hashed:${JSON.stringify(value)}`), |
| 6 | +})) |
| 7 | + |
| 8 | +vi.mock('nitropack/runtime', () => ({ |
| 9 | + defineCachedFunction: vi.fn((handler, options) => { |
| 10 | + cacheDefinitions.push(options) |
| 11 | + return handler |
| 12 | + }), |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('ohash', () => ({ |
| 16 | + hash: hashMock, |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('ofetch', () => ({ |
| 20 | + $fetch: Object.assign(vi.fn(), { raw: vi.fn() }), |
| 21 | +})) |
| 22 | + |
| 23 | +const { createCachedBinaryFetch, createCachedJsonFetch } = await import( |
| 24 | + '../../packages/script/src/runtime/server/utils/cached-upstream', |
| 25 | +) |
| 26 | + |
| 27 | +beforeEach(() => { |
| 28 | + cacheDefinitions.length = 0 |
| 29 | + hashMock.mockClear() |
| 30 | +}) |
| 31 | + |
| 32 | +describe('upstream cache keys', () => { |
| 33 | + it('hashes normalized JSON cache keys before passing them to Nitro storage', () => { |
| 34 | + const normalizeKey = vi.fn((url: string) => new URL(url).searchParams.get('actor') || url) |
| 35 | + createCachedJsonFetch('profile', 60, normalizeKey) |
| 36 | + |
| 37 | + const getKey = cacheDefinitions[0]?.getKey |
| 38 | + expect(getKey).toBeTypeOf('function') |
| 39 | + expect(getKey?.('https://public.api.bsky.app/profile?actor=nuxt.com')).toBe('hashed:"nuxt.com"') |
| 40 | + expect(normalizeKey).toHaveBeenCalledOnce() |
| 41 | + expect(hashMock).toHaveBeenCalledWith('nuxt.com') |
| 42 | + }) |
| 43 | + |
| 44 | + it('hashes binary URLs instead of exposing reserved characters to storage', () => { |
| 45 | + createCachedBinaryFetch('image', 60) |
| 46 | + |
| 47 | + const getKey = cacheDefinitions[0]?.getKey |
| 48 | + const url = 'https://cdn.example.com/image.jpg?width=640&format=webp' |
| 49 | + expect(getKey?.(url)).toBe(`hashed:${JSON.stringify(url)}`) |
| 50 | + expect(hashMock).toHaveBeenCalledWith(url) |
| 51 | + }) |
| 52 | + |
| 53 | + it('uses stable binary option ordering and varies on response-error behavior', () => { |
| 54 | + createCachedBinaryFetch('image', 60) |
| 55 | + |
| 56 | + const getKey = cacheDefinitions[0]?.getKey |
| 57 | + const key = getKey?.('https://cdn.example.com/image.jpg', { |
| 58 | + headers: { Zebra: 'last', Accept: 'image/webp' }, |
| 59 | + redirect: 'manual', |
| 60 | + ignoreResponseError: true, |
| 61 | + }) |
| 62 | + |
| 63 | + expect(key).toBe('hashed:["https://cdn.example.com/image.jpg","Accept=image/webp","Zebra=last","redirect=manual","ignoreResponseError=true"]') |
| 64 | + }) |
| 65 | + |
| 66 | + it('hashes Instagram embed URLs together with their response-varying headers', async () => { |
| 67 | + await import('../../packages/script/src/runtime/server/instagram-embed') |
| 68 | + |
| 69 | + const getKey = cacheDefinitions[0]?.getKey |
| 70 | + const key = getKey?.('https://www.instagram.com/p/example/embed/captioned/', { |
| 71 | + 'User-Agent': 'Nuxt Scripts', |
| 72 | + 'Accept': 'text/html', |
| 73 | + }) |
| 74 | + |
| 75 | + expect(key).toBe('hashed:["https://www.instagram.com/p/example/embed/captioned/","Accept=text/html","User-Agent=Nuxt Scripts"]') |
| 76 | + }) |
| 77 | +}) |
0 commit comments