132 lines
5.3 KiB
TypeScript
132 lines
5.3 KiB
TypeScript
/**
|
|
* Unit tests for the shared regional CDN URL builders
|
|
* (`packages/shared/region-cache-urls.ts`).
|
|
*
|
|
* Both the backend cache writer/publisher and the UIs use these, so the path
|
|
* shapes here are a compatibility contract with the existing R2 buckets.
|
|
*/
|
|
import {
|
|
CACHE_FILENAMES,
|
|
availabilityCacheUrl,
|
|
buildEssentialUrls,
|
|
buildStoreDetailUrlTemplate,
|
|
cachedFileUrl,
|
|
regionCacheUrl,
|
|
slotsCacheUrl,
|
|
storeDetailCacheUrl,
|
|
} from '@packages/shared'
|
|
|
|
const DOMAIN = 'https://assets.test/'
|
|
const KEY = 'api-cache-dev'
|
|
const REGION = 1
|
|
const VERSIONS = { products: 12, availability: 34, slots: 56 }
|
|
|
|
describe('region cache URLs', () => {
|
|
it('builds the versioned files under the api cache key', () => {
|
|
expect(regionCacheUrl(DOMAIN, KEY, REGION, VERSIONS.products, CACHE_FILENAMES.products)).toBe(
|
|
'https://assets.test/api-cache-dev/regions/1/v-12/products.json'
|
|
)
|
|
expect(regionCacheUrl(DOMAIN, KEY, REGION, VERSIONS.products, CACHE_FILENAMES.stores)).toBe(
|
|
'https://assets.test/api-cache-dev/regions/1/v-12/stores.json'
|
|
)
|
|
expect(regionCacheUrl(DOMAIN, KEY, REGION, VERSIONS.products, CACHE_FILENAMES.banners)).toBe(
|
|
'https://assets.test/api-cache-dev/regions/1/v-12/banners.json'
|
|
)
|
|
expect(
|
|
regionCacheUrl(DOMAIN, KEY, REGION, VERSIONS.products, CACHE_FILENAMES.essentialConsts)
|
|
).toBe('https://assets.test/api-cache-dev/regions/1/v-12/essential-consts.json')
|
|
})
|
|
|
|
it('keeps availability and slots outside the api cache key', () => {
|
|
expect(availabilityCacheUrl(DOMAIN, REGION, VERSIONS.availability)).toBe(
|
|
'https://assets.test/regions/1/av-34/availability.json'
|
|
)
|
|
expect(slotsCacheUrl(DOMAIN, REGION, VERSIONS.slots)).toBe(
|
|
'https://assets.test/regions/1/slots/v-56/slots.json'
|
|
)
|
|
})
|
|
|
|
it('builds the store-detail URL and its {storeId} template', () => {
|
|
expect(storeDetailCacheUrl(DOMAIN, KEY, REGION, VERSIONS.products, 42)).toBe(
|
|
'https://assets.test/api-cache-dev/regions/1/v-12/stores/42.json'
|
|
)
|
|
const template = buildStoreDetailUrlTemplate(DOMAIN, KEY, REGION, VERSIONS.products)
|
|
expect(template).toBe(
|
|
'https://assets.test/api-cache-dev/regions/1/v-12/stores/{storeId}.json'
|
|
)
|
|
expect(template.replace('{storeId}', '42')).toBe(
|
|
storeDetailCacheUrl(DOMAIN, KEY, REGION, VERSIONS.products, 42)
|
|
)
|
|
})
|
|
|
|
it('normalises a domain without a trailing slash', () => {
|
|
expect(regionCacheUrl('https://assets.test', KEY, REGION, 3, CACHE_FILENAMES.products)).toBe(
|
|
'https://assets.test/api-cache-dev/regions/1/v-3/products.json'
|
|
)
|
|
})
|
|
|
|
it('publishes every URL from buildEssentialUrls', () => {
|
|
expect(buildEssentialUrls({ assetsDomain: DOMAIN, apiCacheKey: KEY, regionId: REGION, versions: VERSIONS }))
|
|
.toEqual({
|
|
productsUrl: 'https://assets.test/api-cache-dev/regions/1/v-12/products.json',
|
|
storesUrl: 'https://assets.test/api-cache-dev/regions/1/v-12/stores.json',
|
|
bannersUrl: 'https://assets.test/api-cache-dev/regions/1/v-12/banners.json',
|
|
essentialConstsUrl:
|
|
'https://assets.test/api-cache-dev/regions/1/v-12/essential-consts.json',
|
|
availabilityUrl: 'https://assets.test/regions/1/av-34/availability.json',
|
|
slotsUrl: 'https://assets.test/regions/1/slots/v-56/slots.json',
|
|
storeDetailUrlTemplate:
|
|
'https://assets.test/api-cache-dev/regions/1/v-12/stores/{storeId}.json',
|
|
})
|
|
})
|
|
|
|
it('sends empty strings when the domain is unavailable', () => {
|
|
const urls = buildEssentialUrls({
|
|
assetsDomain: null,
|
|
apiCacheKey: KEY,
|
|
regionId: REGION,
|
|
versions: VERSIONS,
|
|
})
|
|
expect(Object.values(urls).every((url) => url === '')).toBe(true)
|
|
})
|
|
|
|
it('still publishes availability/slots when only the api cache key is unavailable', () => {
|
|
const urls = buildEssentialUrls({
|
|
assetsDomain: DOMAIN,
|
|
apiCacheKey: '',
|
|
regionId: REGION,
|
|
versions: VERSIONS,
|
|
})
|
|
expect(urls.productsUrl).toBe('')
|
|
expect(urls.storesUrl).toBe('')
|
|
expect(urls.bannersUrl).toBe('')
|
|
expect(urls.essentialConstsUrl).toBe('')
|
|
expect(urls.storeDetailUrlTemplate).toBe('')
|
|
expect(urls.availabilityUrl).toBe('https://assets.test/regions/1/av-34/availability.json')
|
|
expect(urls.slotsUrl).toBe('https://assets.test/regions/1/slots/v-56/slots.json')
|
|
})
|
|
})
|
|
|
|
describe('cachedFileUrl', () => {
|
|
const urls = buildEssentialUrls({
|
|
assetsDomain: DOMAIN,
|
|
apiCacheKey: KEY,
|
|
regionId: REGION,
|
|
versions: VERSIONS,
|
|
})
|
|
|
|
it('maps a filename to its published URL', () => {
|
|
expect(cachedFileUrl(urls, CACHE_FILENAMES.products)).toBe(urls.productsUrl)
|
|
expect(cachedFileUrl(urls, CACHE_FILENAMES.stores)).toBe(urls.storesUrl)
|
|
expect(cachedFileUrl(urls, CACHE_FILENAMES.banners)).toBe(urls.bannersUrl)
|
|
expect(cachedFileUrl(urls, CACHE_FILENAMES.essentialConsts)).toBe(urls.essentialConstsUrl)
|
|
expect(cachedFileUrl(urls, CACHE_FILENAMES.availability)).toBe(urls.availabilityUrl)
|
|
expect(cachedFileUrl(urls, CACHE_FILENAMES.slots)).toBe(urls.slotsUrl)
|
|
})
|
|
|
|
it('returns null for a missing payload, unknown file, or empty URL', () => {
|
|
expect(cachedFileUrl(undefined, CACHE_FILENAMES.products)).toBeNull()
|
|
expect(cachedFileUrl(urls, 'unknown.json')).toBeNull()
|
|
expect(cachedFileUrl({ ...urls, productsUrl: '' }, CACHE_FILENAMES.products)).toBeNull()
|
|
})
|
|
})
|