|
| 1 | +import type { Storage, StorageValue } from 'unstorage' |
| 2 | + |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +import useCache from '../../src/runtime/utils/clients/cache' |
| 6 | + |
| 7 | +const options = { |
| 8 | + short: { maxAge: 1, staleMaxAge: 9, swr: true }, |
| 9 | + long: { maxAge: 3600, staleMaxAge: 82800, swr: true }, |
| 10 | +} |
| 11 | + |
| 12 | +let store: Map<string, unknown> |
| 13 | +let ttls: Map<string, unknown> |
| 14 | + |
| 15 | +function createStorage() { |
| 16 | + return { |
| 17 | + hasItem: async (key: string) => store.has(key), |
| 18 | + getItem: async (key: string) => store.get(key) ?? null, |
| 19 | + setItem: async (key: string, value: unknown, opts?: unknown) => { |
| 20 | + store.set(key, value) |
| 21 | + ttls.set(key, opts) |
| 22 | + }, |
| 23 | + } as unknown as Storage<StorageValue> |
| 24 | +} |
| 25 | + |
| 26 | +function createRequest(data: unknown = { product: { id: '1' } }) { |
| 27 | + return vi.fn(async (..._args: unknown[]) => ({ data, headers: new Headers() })) |
| 28 | +} |
| 29 | + |
| 30 | +function requestOptions(request: ReturnType<typeof createRequest>) { |
| 31 | + return request.mock.calls[0]?.[1] |
| 32 | +} |
| 33 | + |
| 34 | +beforeEach(() => { |
| 35 | + store = new Map() |
| 36 | + ttls = new Map() |
| 37 | +}) |
| 38 | + |
| 39 | +describe('client cache', () => { |
| 40 | + it('serves a repeated request from the cache', async () => { |
| 41 | + const storage = createStorage() |
| 42 | + const request = createRequest() |
| 43 | + |
| 44 | + const first = await useCache(storage, request as never, 'query X { a }' as never, { cache: 'short' } as never, options) |
| 45 | + const second = await useCache(storage, request as never, 'query X { a }' as never, { cache: 'short' } as never, options) |
| 46 | + |
| 47 | + expect(request).toHaveBeenCalledTimes(1) |
| 48 | + expect(second).toStrictEqual(first) |
| 49 | + }) |
| 50 | + |
| 51 | + it('derives the ttl from the named cache tier', async () => { |
| 52 | + const storage = createStorage() |
| 53 | + |
| 54 | + await useCache(storage, createRequest() as never, 'query X { a }' as never, { cache: 'short' } as never, options) |
| 55 | + await useCache(storage, createRequest() as never, 'query Y { b }' as never, { cache: 'long' } as never, options) |
| 56 | + |
| 57 | + expect([...ttls.values()]).toStrictEqual([{ ttl: 10_000 }, { ttl: 86_400_000 }]) |
| 58 | + }) |
| 59 | + |
| 60 | + it('keys separately per operation and per variables', async () => { |
| 61 | + const storage = createStorage() |
| 62 | + const request = createRequest() |
| 63 | + |
| 64 | + await useCache(storage, request as never, 'query X { a }' as never, { cache: 'short' } as never, options) |
| 65 | + await useCache(storage, request as never, 'query Y { b }' as never, { cache: 'short' } as never, options) |
| 66 | + await useCache(storage, request as never, 'query X { a }' as never, { cache: 'short', variables: { handle: 'a' } } as never, options) |
| 67 | + await useCache(storage, request as never, 'query X { a }' as never, { cache: 'short', variables: { handle: 'b' } } as never, options) |
| 68 | + |
| 69 | + expect(request).toHaveBeenCalledTimes(4) |
| 70 | + expect(store.size).toBe(4) |
| 71 | + }) |
| 72 | + |
| 73 | + it('does not cache when no storage is available', async () => { |
| 74 | + const request = createRequest() |
| 75 | + |
| 76 | + await useCache(undefined, request as never, 'query X { a }' as never, { cache: 'short' } as never, options) |
| 77 | + await useCache(undefined, request as never, 'query X { a }' as never, { cache: 'short' } as never, options) |
| 78 | + |
| 79 | + expect(request).toHaveBeenCalledTimes(2) |
| 80 | + }) |
| 81 | + |
| 82 | + it('does not cache an unknown cache tier', async () => { |
| 83 | + const storage = createStorage() |
| 84 | + const request = createRequest() |
| 85 | + |
| 86 | + await useCache(storage, request as never, 'query X { a }' as never, { cache: 'nope' } as never, options) |
| 87 | + await useCache(storage, request as never, 'query X { a }' as never, { cache: 'nope' } as never, options) |
| 88 | + |
| 89 | + expect(request).toHaveBeenCalledTimes(2) |
| 90 | + expect(store.size).toBe(0) |
| 91 | + }) |
| 92 | + |
| 93 | + it('does not cache a response that carries errors', async () => { |
| 94 | + const storage = createStorage() |
| 95 | + const request = vi.fn(async () => ({ data: undefined, errors: { message: 'boom' }, headers: new Headers() })) |
| 96 | + |
| 97 | + await useCache(storage, request as never, 'query X { a }' as never, { cache: 'short' } as never, options) |
| 98 | + await useCache(storage, request as never, 'query X { a }' as never, { cache: 'short' } as never, options) |
| 99 | + |
| 100 | + expect(request).toHaveBeenCalledTimes(2) |
| 101 | + expect(store.size).toBe(0) |
| 102 | + }) |
| 103 | + |
| 104 | + it('asks the proxy for the same tier it caches under', async () => { |
| 105 | + const request = createRequest() |
| 106 | + |
| 107 | + await useCache(createStorage(), request as never, 'query X { a }' as never, { cache: 'long' } as never, options) |
| 108 | + |
| 109 | + expect(requestOptions(request)).toMatchObject({ headers: { 'X-Shopify-Proxy-Cache': 'long' } }) |
| 110 | + }) |
| 111 | + |
| 112 | + it('supports split client and proxy cache tiers', async () => { |
| 113 | + const request = createRequest() |
| 114 | + |
| 115 | + await useCache( |
| 116 | + createStorage(), |
| 117 | + request as never, |
| 118 | + 'query X { a }' as never, |
| 119 | + { cache: { client: 'short', proxy: 'long' } } as never, |
| 120 | + options, |
| 121 | + ) |
| 122 | + |
| 123 | + expect(requestOptions(request)).toMatchObject({ headers: { 'X-Shopify-Proxy-Cache': 'long' } }) |
| 124 | + expect([...ttls.values()]).toStrictEqual([{ ttl: 10_000 }]) |
| 125 | + }) |
| 126 | +}) |
0 commit comments