freshyo/apps/backend/tests/api/user-cart-slots.test.ts
2026-10-20 00:31:05 +05:30

242 lines
9.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* UCART-01..08 (`user.cart.getCartSlots`, §6.8) and USLOT-01..10
* (`user.slots.getSlots`, §6.13).
*
* `getCartSlots.input.productIds` is matched against the slot's SKU ids (the
* value the slot-store keys by), so these tests pass SKU ids.
*/
import { apiClient, expectTrpcError } from '../helpers/api'
import { staffClient, superadminClient } from '../helpers/sessions'
import { createFutureSlot, createProduct, createRegion, createStore } from '../helpers/fixtures'
import { fetchCacheJson, getEssentialConsts, slotsCacheUrl } from '../helpers/cache'
jest.setTimeout(180_000)
const REGION_ID = 1
const OTHER_REGION_ID = 2
interface Catalog {
skuId: number
slotId: number
}
async function catalog(): Promise<Catalog> {
const store = await createStore()
const product = await createProduct({ storeId: store.storeId, price: 199 })
const slot = await createFutureSlot([product.skuId])
return { skuId: product.skuId, slotId: slot.slotId }
}
async function setCapacityFull(slotId: number, isCapacityFull: boolean): Promise<void> {
const admin = await staffClient()
await admin.admin.slots.updateSlotCapacity.mutate({ slotId, isCapacityFull })
}
/* -------------------------------------------------------------------------- */
/* 6.8 Cart slots */
/* -------------------------------------------------------------------------- */
describe('user.cart.getCartSlots (UCART-01..08)', () => {
it('UCART-01: a product with slots maps its SKU id to the slot list', async () => {
const { skuId, slotId } = await catalog()
const res: any = await apiClient().user.cart.getCartSlots.query({
regionId: REGION_ID,
productIds: [skuId],
})
expect(Array.isArray(res[skuId])).toBe(true)
const entry = res[skuId].find((s: any) => s.id === slotId)
expect(entry).toBeDefined()
expect(entry).toEqual(
expect.objectContaining({
id: slotId,
deliveryTime: expect.any(String),
freezeTime: expect.any(String),
isCapacityFull: false,
})
)
})
it('UCART-02: an empty productIds list returns {}', async () => {
const res: any = await apiClient().user.cart.getCartSlots.query({
regionId: REGION_ID,
productIds: [],
})
expect(res).toEqual({})
})
it('UCART-03: an unknown SKU resolves to an empty array', async () => {
const res: any = await apiClient().user.cart.getCartSlots.query({
regionId: REGION_ID,
productIds: [987_654_321],
})
expect(res[987_654_321]).toEqual([])
})
it('UCART-04: a SKU only offered in another region resolves to []', async () => {
const { skuId } = await catalog()
const res: any = await apiClient().user.cart.getCartSlots.query({
regionId: OTHER_REGION_ID,
productIds: [skuId],
})
expect(res[skuId]).toEqual([])
})
it('UCART-05: regionId is required (BAD_REQUEST)', async () => {
await expectTrpcError(
() =>
apiClient().user.cart.getCartSlots.query({ productIds: [1] } as any),
'BAD_REQUEST'
)
})
it('UCART-06 (current): capacity-full slots are dropped but freeze-passed ones are kept', async () => {
const { skuId } = await catalog()
// A capacity-full slot: excluded by the slot store.
const full = await createFutureSlot([skuId])
await setCapacityFull(full.slotId, true)
// A delivery-in-the-past slot: excluded by the DB (deliveryTime > now).
const past = await createFutureSlot([skuId], { deliveryInHours: -1, freezeInHours: -2 })
// A freeze-in-the-past but delivery-in-the-future slot: the slot store does
// NOT re-check freezeTime, so it survives (placeOrder would still reject it).
const pastFreeze = await createFutureSlot([skuId], { deliveryInHours: 3, freezeInHours: -1 })
const res: any = await apiClient().user.cart.getCartSlots.query({
regionId: REGION_ID,
productIds: [skuId],
})
const ids = res[skuId].map((s: any) => s.id)
expect(ids).not.toContain(full.slotId)
expect(ids).not.toContain(past.slotId)
expect(ids).toContain(pastFreeze.slotId)
})
it('UCART-07: a global SKU is served in the requested region', async () => {
// Global products have regionId null and are servable everywhere. Creating
// one through `superadmin.globalProducts.create` is currently broken (500),
// so an existing seeded global SKU is used instead and given a region slot.
const superadmin = await superadminClient()
const list: any = await superadmin.superadmin.globalProducts.list.query()
const global = (list.products as any[]).find((p) => p.skus?.length > 0)
expect(global).toBeDefined()
const globalSkuId = Number(global.skus[0].id)
const slot = await createFutureSlot([globalSkuId])
const res: any = await apiClient().user.cart.getCartSlots.query({
regionId: REGION_ID,
productIds: [globalSkuId],
})
expect(res[globalSkuId].map((s: any) => s.id)).toContain(slot.slotId)
})
it('UCART-08: getCartSlots is public (no token required)', async () => {
const { skuId, slotId } = await catalog()
const res: any = await apiClient().user.cart.getCartSlots.query({
regionId: REGION_ID,
productIds: [skuId],
})
expect(res[skuId].map((s: any) => s.id)).toContain(slotId)
})
})
/* -------------------------------------------------------------------------- */
/* 6.13 User slots */
/* -------------------------------------------------------------------------- */
describe('user.slots.getSlots (USLOT-01..10)', () => {
it('USLOT-01: getSlots returns only this region’s future, still-open slots', async () => {
const { slotId } = await catalog()
const res: any = await apiClient().user.slots.getSlots.query({ regionId: REGION_ID })
expect(res.count).toBe(res.slots.length)
expect(res.slots.map((s: any) => s.id)).toContain(slotId)
const now = Date.now()
for (const slot of res.slots) {
expect(new Date(slot.deliveryTime).getTime()).toBeGreaterThan(now)
expect(new Date(slot.freezeTime).getTime()).toBeGreaterThan(now)
}
})
it('USLOT-02: a slot whose delivery has passed is excluded', async () => {
const { skuId } = await catalog()
const past = await createFutureSlot([skuId], { deliveryInHours: -1, freezeInHours: -2 })
const res: any = await apiClient().user.slots.getSlots.query({ regionId: REGION_ID })
expect(res.slots.map((s: any) => s.id)).not.toContain(past.slotId)
})
it('USLOT-03: a slot whose freeze time has passed is excluded', async () => {
const { skuId } = await catalog()
const pastFreeze = await createFutureSlot([skuId], { deliveryInHours: 3, freezeInHours: -1 })
const res: any = await apiClient().user.slots.getSlots.query({ regionId: REGION_ID })
expect(res.slots.map((s: any) => s.id)).not.toContain(pastFreeze.slotId)
})
it('USLOT-04 (current): a capacity-full slot is still returned', async () => {
// `getSlots` filters delivery/freeze only; unlike the slots cache
// (`scaffoldSlotsWithProducts`) it does NOT filter isCapacityFull.
const { skuId } = await catalog()
const full = await createFutureSlot([skuId])
await setCapacityFull(full.slotId, true)
const res: any = await apiClient().user.slots.getSlots.query({ regionId: REGION_ID })
expect(res.slots.map((s: any) => s.id)).toContain(full.slotId)
})
it('USLOT-05: an unknown region returns an empty list', async () => {
const res: any = await apiClient().user.slots.getSlots.query({ regionId: 99_999 })
expect(res).toEqual({ slots: [], count: 0 })
})
it('USLOT-06: regionId is required (BAD_REQUEST)', async () => {
await expectTrpcError(
() => apiClient().user.slots.getSlots.query({} as any),
'BAD_REQUEST'
)
})
it('USLOT-07: another region does not leak its slots', async () => {
const { slotId } = await catalog()
const res: any = await apiClient().user.slots.getSlots.query({ regionId: OTHER_REGION_ID })
expect(res.slots.map((s: any) => s.id)).not.toContain(slotId)
})
it('USLOT-08: a region with no slots returns count 0', async () => {
const region = await createRegion()
const res: any = await apiClient().user.slots.getSlots.query({ regionId: region.regionId })
expect(res).toEqual({ slots: [], count: 0 })
})
it('USLOT-09: getSlots is public (no token required)', async () => {
const res: any = await apiClient().user.slots.getSlots.query({ regionId: REGION_ID })
expect(Array.isArray(res.slots)).toBe(true)
expect(res.count).toBeGreaterThanOrEqual(0)
})
it('USLOT-10 (current): getSlots itself has no products; the cache projects id+images', async () => {
// The slot list rows carry no `products`; the `{ id, images }` product
// projection lives only in the synchronous slots.json cache file.
const { slotId } = await catalog()
const res: any = await apiClient().user.slots.getSlots.query({ regionId: REGION_ID })
const slot = res.slots.find((s: any) => s.id === slotId)
expect(slot).toBeDefined()
expect('products' in slot).toBe(false)
const consts = await getEssentialConsts()
const doc: any = await fetchCacheJson(slotsCacheUrl(consts, REGION_ID))
const cached = (doc.slots || []).find((s: any) => Number(s.id) === slotId)
expect(cached).toBeDefined()
expect(Array.isArray(cached.products)).toBe(true)
expect(cached.products.length).toBeGreaterThan(0)
for (const product of cached.products) {
expect(Object.keys(product).sort()).toEqual(['id', 'images'])
expect(Number(product.id)).toBeGreaterThan(0)
expect(Array.isArray(product.images)).toBe(true)
}
})
})