343 lines
14 KiB
TypeScript
343 lines
14 KiB
TypeScript
/**
|
|
* SPROD-01..16 — `superadmin.products` / `superadmin.stores` (catalogue §8.8).
|
|
*
|
|
* Live HTTP/tRPC suite. One `it()` per catalogue row; rows that pin current
|
|
* (not intended) behaviour are prefixed `(current)`.
|
|
*
|
|
* Observed contracts (do NOT "fix" here):
|
|
* - `products.elevate` throws raw `Error`s from the db layer, which the router
|
|
* wraps as BAD_REQUEST. The router's `if (!product) -> 404` branch is
|
|
* unreachable, so an unknown product is 400 (not 404).
|
|
* - `stores.elevate` likewise: an unknown / already-global store is 400.
|
|
* - The all-region cache rebuild is fire-and-forget via the CacheCreator DO,
|
|
* which processes regions sequentially and stalls on this local DB, so the
|
|
* async version movement is not deterministically observable; the reachable
|
|
* contract is asserted through a freshly created region's cache.
|
|
*/
|
|
|
|
import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api'
|
|
import { anonClient, staffSession, superadminClient, userSession } from '../helpers/sessions'
|
|
import { createProduct, createRegion, createStore } from '../helpers/fixtures'
|
|
import { scaffoldCache } from '../helpers/cache'
|
|
import { TEST_OTHER_REGION_ID } from '../helpers/env'
|
|
|
|
jest.setTimeout(300_000)
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Fixtures */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
async function createGlobalStore(): Promise<number> {
|
|
const superadmin = await superadminClient()
|
|
const res: any = await superadmin.superadmin.globalStores.create.mutate({
|
|
name: `E2E SPROD store ${uniqueSuffix()}`,
|
|
description: 'SPROD fixture',
|
|
})
|
|
return Number(res.store.id)
|
|
}
|
|
|
|
/**
|
|
* A brand-new region's products + stores documents from the scaffold functions
|
|
* (test routes). No versioned-file read / Durable Object wait.
|
|
*/
|
|
async function freshRegionCache(): Promise<{ products: any[]; stores: any[] }> {
|
|
const region = await createRegion()
|
|
const productsDoc: any = await scaffoldCache('products', { regionId: region.regionId })
|
|
const storesDoc: any = await scaffoldCache('stores', { regionId: region.regionId })
|
|
return { products: productsDoc.products, stores: storesDoc.stores }
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* products.list */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.products.list (SPROD-01..05)', () => {
|
|
it('SPROD-01: list defaults to 20 rows', async () => {
|
|
const superadmin = await superadminClient()
|
|
const res: any = await superadmin.superadmin.products.list.query({})
|
|
|
|
expect(res.products).toHaveLength(20)
|
|
expect(res.total).toBeGreaterThan(20)
|
|
})
|
|
|
|
it('SPROD-02: a regionId filter returns only that region', async () => {
|
|
const superadmin = await superadminClient()
|
|
const res: any = await superadmin.superadmin.products.list.query({ regionId: 1, limit: 20 })
|
|
|
|
expect(res.products.length).toBeGreaterThan(0)
|
|
expect(res.products.every((product: any) => product.regionId === 1)).toBe(true)
|
|
expect(res.total).toBeGreaterThanOrEqual(res.products.length)
|
|
})
|
|
|
|
it('SPROD-03: search matches the product name', async () => {
|
|
const product = await createProduct()
|
|
const superadmin = await superadminClient()
|
|
const res: any = await superadmin.superadmin.products.list.query({
|
|
search: product.productName,
|
|
limit: 20,
|
|
})
|
|
|
|
expect(res.products.some((row: any) => Number(row.id) === product.productId)).toBe(true)
|
|
})
|
|
|
|
it('SPROD-04: limit accepts 1..100 and rejects 0/101', async () => {
|
|
const superadmin = await superadminClient()
|
|
|
|
const one: any = await superadmin.superadmin.products.list.query({ limit: 1 })
|
|
expect(one.products).toHaveLength(1)
|
|
|
|
const hundred: any = await superadmin.superadmin.products.list.query({ limit: 100 })
|
|
expect(hundred.products).toHaveLength(100)
|
|
|
|
await expectTrpcError(
|
|
() => superadmin.superadmin.products.list.query({ limit: 0 }),
|
|
'BAD_REQUEST'
|
|
)
|
|
await expectTrpcError(
|
|
() => superadmin.superadmin.products.list.query({ limit: 101 }),
|
|
'BAD_REQUEST'
|
|
)
|
|
})
|
|
|
|
it('SPROD-05: offset paginates without overlap', async () => {
|
|
const superadmin = await superadminClient()
|
|
const first: any = await superadmin.superadmin.products.list.query({ limit: 5, offset: 0 })
|
|
const second: any = await superadmin.superadmin.products.list.query({ limit: 5, offset: 5 })
|
|
|
|
expect(first.products).toHaveLength(5)
|
|
expect(second.products).toHaveLength(5)
|
|
|
|
const firstIds = first.products.map((product: any) => Number(product.id))
|
|
const secondIds = second.products.map((product: any) => Number(product.id))
|
|
expect(secondIds.some((id: number) => firstIds.includes(id))).toBe(false)
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* products.elevate */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.products.elevate (SPROD-06..09)', () => {
|
|
it('SPROD-06: elevate attaches a regional product to a global store', async () => {
|
|
const superadmin = await superadminClient()
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price: 250 })
|
|
const globalStoreId = await createGlobalStore()
|
|
|
|
const res: any = await superadmin.superadmin.products.elevate.mutate({
|
|
productId: product.productId,
|
|
storeId: globalStoreId,
|
|
})
|
|
expect(res.message).toBe('Product elevated to global successfully')
|
|
expect(res.product.regionId).toBeNull()
|
|
expect(Number(res.product.storeId)).toBe(globalStoreId)
|
|
expect(res.product.skus.map((sku: any) => Number(sku.id))).toContain(product.skuId)
|
|
|
|
const fetched: any = await superadmin.superadmin.globalProducts.get.query({
|
|
id: product.productId,
|
|
})
|
|
expect(Number(fetched.product.storeId)).toBe(globalStoreId)
|
|
})
|
|
|
|
it('(current) SPROD-07: elevating an unknown product is BAD_REQUEST (not 404)', async () => {
|
|
// `elevateProductToGlobal` throws Error('Product not found'); the router's
|
|
// catch maps every db error to 400, so the 404 branch is unreachable.
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() => superadmin.superadmin.products.elevate.mutate({ productId: 987_654_321, storeId: 1 }),
|
|
'BAD_REQUEST',
|
|
'Product not found'
|
|
)
|
|
})
|
|
|
|
it('SPROD-08: elevating into a non-global store is BAD_REQUEST', async () => {
|
|
const superadmin = await superadminClient()
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price: 199 })
|
|
|
|
await expectTrpcError(
|
|
() =>
|
|
superadmin.superadmin.products.elevate.mutate({
|
|
productId: product.productId,
|
|
storeId: store.storeId,
|
|
}),
|
|
'BAD_REQUEST',
|
|
'not a global store'
|
|
)
|
|
})
|
|
|
|
it('(current) SPROD-09: an elevated product reaches a region cache (all-region rebuild not awaited)', async () => {
|
|
const superadmin = await superadminClient()
|
|
const store = await createStore()
|
|
const product = await createProduct({ storeId: store.storeId, price: 275 })
|
|
const globalStoreId = await createGlobalStore()
|
|
await superadmin.superadmin.products.elevate.mutate({
|
|
productId: product.productId,
|
|
storeId: globalStoreId,
|
|
})
|
|
|
|
const { products } = await freshRegionCache()
|
|
const found = products.find((row: any) => Number(row.id) === product.skuId)
|
|
expect(found).toEqual(expect.objectContaining({ id: product.skuId, storeId: globalStoreId }))
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* products.elevate + combo dependents */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.products.elevate dependents (SPROD-15..16)', () => {
|
|
it("SPROD-15: elevationDependents lists a combo's regional component products", async () => {
|
|
const superadmin = await superadminClient()
|
|
const store = await createStore()
|
|
const component = await createProduct({
|
|
storeId: store.storeId,
|
|
name: `E2E Combo Part ${uniqueSuffix()}`,
|
|
price: 120,
|
|
})
|
|
const combo = await createProduct({
|
|
storeId: store.storeId,
|
|
name: `E2E Combo ${uniqueSuffix()}`,
|
|
price: 300,
|
|
productType: 'combo',
|
|
comboItems: [{ skuId: component.skuId }],
|
|
})
|
|
|
|
const res: any = await superadmin.superadmin.products.elevationDependents.query({
|
|
productId: combo.productId,
|
|
})
|
|
|
|
expect(res.dependents).toHaveLength(1)
|
|
expect(res.dependents[0]).toEqual(
|
|
expect.objectContaining({
|
|
id: component.productId,
|
|
name: component.productName,
|
|
regionId: 1,
|
|
})
|
|
)
|
|
expect(typeof res.dependents[0].regionName).toBe('string')
|
|
})
|
|
|
|
it('SPROD-16: elevating a combo elevates its regional components', async () => {
|
|
const superadmin = await superadminClient()
|
|
const store = await createStore()
|
|
const component = await createProduct({
|
|
storeId: store.storeId,
|
|
name: `E2E Combo Part ${uniqueSuffix()}`,
|
|
price: 120,
|
|
})
|
|
const combo = await createProduct({
|
|
storeId: store.storeId,
|
|
name: `E2E Combo ${uniqueSuffix()}`,
|
|
price: 300,
|
|
productType: 'combo',
|
|
comboItems: [{ skuId: component.skuId }],
|
|
})
|
|
const globalStoreId = await createGlobalStore()
|
|
|
|
const res: any = await superadmin.superadmin.products.elevate.mutate({
|
|
productId: combo.productId,
|
|
storeId: globalStoreId,
|
|
})
|
|
expect(res.product.regionId).toBeNull()
|
|
expect(Number(res.product.storeId)).toBe(globalStoreId)
|
|
|
|
const globals: any = await superadmin.superadmin.globalProducts.list.query()
|
|
const globalIds = new Set(globals.products.map((product: any) => Number(product.id)))
|
|
expect(globalIds.has(combo.productId)).toBe(true)
|
|
expect(globalIds.has(component.productId)).toBe(true)
|
|
|
|
// The component's market row was seeded for the other region too.
|
|
const other: any = await scaffoldCache('products', { regionId: TEST_OTHER_REGION_ID })
|
|
const rows = new Map(
|
|
(other.products ?? []).map((product: any) => [Number(product.id), product])
|
|
)
|
|
expect(rows.has(combo.skuId)).toBe(true)
|
|
expect(rows.has(component.skuId)).toBe(true)
|
|
expect(Number((rows.get(combo.skuId) as any).price)).toBe(300)
|
|
expect(Number((rows.get(component.skuId) as any).price)).toBe(120)
|
|
|
|
// The whole point: no regional component is left behind, so region B's
|
|
// cache filter (which drops non-global components) keeps the full combo.
|
|
const after: any = await superadmin.superadmin.products.elevationDependents.query({
|
|
productId: combo.productId,
|
|
})
|
|
expect(after.dependents).toEqual([])
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* stores */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.stores.list / elevate (SPROD-10..13)', () => {
|
|
it('SPROD-10: stores.list returns the regional stores of one region', async () => {
|
|
const store = await createStore()
|
|
const superadmin = await superadminClient()
|
|
const stores: any = await superadmin.superadmin.stores.list.query({ regionId: 1 })
|
|
|
|
expect(Array.isArray(stores)).toBe(true)
|
|
expect(stores.length).toBeGreaterThan(0)
|
|
expect(stores.every((entry: any) => entry.isGlobal === false)).toBe(true)
|
|
expect(stores.some((entry: any) => Number(entry.id) === store.storeId)).toBe(true)
|
|
})
|
|
|
|
it('SPROD-11: elevate turns a regional store global', async () => {
|
|
const store = await createStore()
|
|
const superadmin = await superadminClient()
|
|
|
|
const res: any = await superadmin.superadmin.stores.elevate.mutate({ storeId: store.storeId })
|
|
expect(res.message).toBe('Store elevated to global successfully')
|
|
expect(res.store.isGlobal).toBe(true)
|
|
|
|
const fetched: any = await superadmin.superadmin.globalStores.get.query({ id: store.storeId })
|
|
expect(fetched.store.isGlobal).toBe(true)
|
|
expect(fetched.store.name).toBe(store.storeName)
|
|
})
|
|
|
|
it('SPROD-12: elevating an unknown store is BAD_REQUEST', async () => {
|
|
const superadmin = await superadminClient()
|
|
await expectTrpcError(
|
|
() => superadmin.superadmin.stores.elevate.mutate({ storeId: 987_654_321 }),
|
|
'BAD_REQUEST',
|
|
'Store not found'
|
|
)
|
|
})
|
|
|
|
it('(current) SPROD-13: an elevated store reaches a region cache (all-region rebuild not awaited)', async () => {
|
|
const store = await createStore()
|
|
const superadmin = await superadminClient()
|
|
await superadmin.superadmin.stores.elevate.mutate({ storeId: store.storeId })
|
|
|
|
const { stores } = await freshRegionCache()
|
|
const found = stores.find((entry: any) => Number(entry.id) === store.storeId)
|
|
expect(found).toEqual(expect.objectContaining({ id: store.storeId, name: store.storeName }))
|
|
})
|
|
})
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* auth */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
describe('superadmin.products/stores auth (SPROD-14)', () => {
|
|
it('SPROD-14: anonymous, staff and user tokens are rejected', async () => {
|
|
const staff = await staffSession()
|
|
const user = await userSession()
|
|
|
|
for (const client of [anonClient(), apiClient(staff.token), apiClient(user.token)]) {
|
|
await expectTrpcError(() => client.superadmin.products.list.query({}), 'UNAUTHORIZED')
|
|
await expectTrpcError(
|
|
() => client.superadmin.products.elevate.mutate({ productId: 1, storeId: 1 }),
|
|
'UNAUTHORIZED'
|
|
)
|
|
await expectTrpcError(
|
|
() => client.superadmin.stores.list.query({ regionId: 1 }),
|
|
'UNAUTHORIZED'
|
|
)
|
|
await expectTrpcError(
|
|
() => client.superadmin.stores.elevate.mutate({ storeId: 1 }),
|
|
'UNAUTHORIZED'
|
|
)
|
|
}
|
|
})
|
|
})
|