101 lines
3.7 KiB
TypeScript
101 lines
3.7 KiB
TypeScript
/**
|
|
* Static JSON cache helpers.
|
|
*
|
|
* The cache is written through the R2 S3 API and served from the assets domain
|
|
* (there is no backend route for it), so tests read the versioned public URL.
|
|
* Every file lives under the region that owns it:
|
|
* slots ${assetsDomain}regions/${regionId}/slots/v-<slotsVersionNum>/slots.json
|
|
* availability ${assetsDomain}regions/${regionId}/av-<availabilityVersionNum>/availability.json
|
|
* products ${assetsDomain}${apiCacheKey}/regions/${regionId}/v-<cacheVersion>/products.json
|
|
* store detail ${assetsDomain}${apiCacheKey}/regions/${regionId}/v-<cacheVersion>/stores/<id>.json
|
|
*
|
|
* Versions come from common.essentialConsts.
|
|
*/
|
|
|
|
import { apiClient } from './api'
|
|
|
|
export interface EssentialConsts {
|
|
assetsDomain: string
|
|
apiCacheKey: string
|
|
cacheVersion: number
|
|
slotsVersionNum: number
|
|
availabilityVersionNum: number
|
|
regionId?: number
|
|
[key: string]: unknown
|
|
}
|
|
|
|
export async function essentialConsts(): Promise<EssentialConsts> {
|
|
const consts = (await apiClient().common.essentialConsts.query()) as EssentialConsts
|
|
if (!consts?.assetsDomain || !consts?.apiCacheKey) {
|
|
throw new Error(
|
|
`[api-tests] common.essentialConsts is missing assetsDomain/apiCacheKey: ${JSON.stringify(consts)}`
|
|
)
|
|
}
|
|
return consts
|
|
}
|
|
|
|
// Cache files are region-owned: `regions/<id>/...` sits between the assets
|
|
// domain and the versioned filename.
|
|
const regionPath = (c: EssentialConsts) =>
|
|
c.regionId != null ? `regions/${c.regionId}/` : ''
|
|
|
|
export const slotsCacheUrl = (c: EssentialConsts, version: number) =>
|
|
`${c.assetsDomain}${regionPath(c)}slots/v-${version}/slots.json`
|
|
|
|
export const availabilityCacheUrl = (c: EssentialConsts, version: number) =>
|
|
`${c.assetsDomain}${regionPath(c)}av-${version}/availability.json`
|
|
|
|
export const productsCacheUrl = (c: EssentialConsts, version: number) =>
|
|
`${c.assetsDomain}${c.apiCacheKey}/${regionPath(c)}v-${version}/products.json`
|
|
|
|
export const storesCacheUrl = (c: EssentialConsts, version: number) =>
|
|
`${c.assetsDomain}${c.apiCacheKey}/${regionPath(c)}v-${version}/stores.json`
|
|
|
|
export const essentialConstsCacheUrl = (c: EssentialConsts, version: number) =>
|
|
`${c.assetsDomain}${c.apiCacheKey}/${regionPath(c)}v-${version}/essential-consts.json`
|
|
|
|
export const storeDetailCacheUrl = (c: EssentialConsts, version: number, storeId: number) =>
|
|
`${c.assetsDomain}${c.apiCacheKey}/${regionPath(c)}v-${version}/stores/${storeId}.json`
|
|
|
|
/** GET a cache file and parse it; throws with the status on a non-2xx. */
|
|
export async function fetchCacheJson<T = unknown>(url: string): Promise<T> {
|
|
const res = await fetch(url)
|
|
if (!res.ok) {
|
|
throw new Error(`[api-tests] ${res.status} ${res.statusText} fetching cache file ${url}`)
|
|
}
|
|
return (await res.json()) as T
|
|
}
|
|
|
|
/**
|
|
* Retry `fn` until it returns a value or the timeout elapses. Used for the
|
|
* eventual products/stores cache (regenerated ~30s later by the CACHE_CREATOR
|
|
* Durable Object).
|
|
*/
|
|
export async function pollUntil<T>(
|
|
label: string,
|
|
fn: () => Promise<T | null | undefined>,
|
|
options: { timeoutMs?: number; intervalMs?: number } = {}
|
|
): Promise<T> {
|
|
const timeoutMs = options.timeoutMs ?? 90_000
|
|
const intervalMs = options.intervalMs ?? 5_000
|
|
const deadline = Date.now() + timeoutMs
|
|
|
|
let lastError: unknown = null
|
|
for (;;) {
|
|
try {
|
|
const value = await fn()
|
|
if (value) return value
|
|
} catch (error) {
|
|
lastError = error
|
|
}
|
|
|
|
if (Date.now() >= deadline) {
|
|
throw new Error(
|
|
`[api-tests] timed out after ${timeoutMs}ms waiting for: ${label}` +
|
|
(lastError ? ` (last error: ${(lastError as Error).message})` : '')
|
|
)
|
|
}
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, intervalMs))
|
|
}
|
|
}
|