/** * CACHE-63..76 — `slots.json` (catalogue §4.4). * * Unlike products/stores, the slots cache is written SYNCHRONOUSLY: the mutation * calls `createSlotsCacheFile` (bump `slots_version_num` → upload → KV refresh) * before it returns. Every mutation row can therefore assert the version moved * immediately; the file is fetched with a short retry for upload propagation. * * Rows pinned `(current)`: * - CACHE-76: an unknown vendor-snippet SKU id is a 500 `Invalid SKU IDs`; an * unknown group id is silently dropped (`groupIds: []`). */ import { expectTrpcError, uniqueSuffix, type ApiClient } from '../helpers/api' import { staffClient } from '../helpers/sessions' import { createProduct, createStore } from '../helpers/fixtures' import { scaffoldCache } from '../helpers/cache' import { TEST_REGION_ID, TEST_OTHER_BOUND_ID, TEST_OTHER_REGION_ID } from '../helpers/env' jest.setTimeout(300_000) const FAR_FUTURE = '2030-06-01T10:00:00.000Z' const FAR_FUTURE_FREEZE = '2030-06-01T08:00:00.000Z' const hoursFromNow = (hours: number) => new Date(Date.now() + hours * 3_600_000).toISOString() async function slotsVersion(admin: ApiClient): Promise { const rows: any = await admin.admin.region.getRegionConstants.query() const row = rows.find((entry: any) => entry.key === 'slots_version_num') return Number(row?.value ?? 0) } interface SlotInput { deliveryTime?: string freezeTime?: string isActive?: boolean skuIds?: number[] vendorSnippets?: Array<{ name: string; skuIds: number[] }> groupIds?: number[] } async function createSlotAt(admin: ApiClient, input: SlotInput = {}): Promise { return admin.admin.slots.createSlot.mutate({ deliveryTime: input.deliveryTime ?? hoursFromNow(3), freezeTime: input.freezeTime ?? hoursFromNow(2), ...(input.isActive !== undefined ? { isActive: input.isActive } : {}), ...(input.skuIds !== undefined ? { skuIds: input.skuIds } : {}), ...(input.vendorSnippets !== undefined ? { vendorSnippets: input.vendorSnippets } : {}), ...(input.groupIds !== undefined ? { groupIds: input.groupIds } : {}), } as any) } /** * Region 1's slots document from the scaffold function (test route): the same * object the cache writer serialises, straight from D1 (no DO/S3 wait). */ async function currentSlots(_admin: ApiClient): Promise { return scaffoldCache('slots', { regionId: TEST_REGION_ID }) } const findSlot = (doc: any, slotId: number) => (doc.slots ?? []).find((slot: any) => Number(slot.id) === slotId) /* -------------------------------------------------------------------------- */ /* Shared fixture */ /* -------------------------------------------------------------------------- */ let shared: any beforeAll(async () => { const admin = await staffClient() shared = await currentSlots(admin) }) /* -------------------------------------------------------------------------- */ /* shape */ /* -------------------------------------------------------------------------- */ describe('slots.json shape (CACHE-63..64, 68..69)', () => { it('CACHE-63: the file is valid JSON with slots and a matching count', () => { expect(Array.isArray(shared.slots)).toBe(true) expect(shared.count).toBe(shared.slots.length) }) it('CACHE-64: a slot carries id/deliveryTime/freezeTime/products', () => { expect(shared.slots.length).toBeGreaterThan(0) for (const slot of shared.slots.slice(0, 20)) { expect(typeof slot.id).toBe('number') expect(typeof slot.deliveryTime).toBe('string') expect(typeof slot.freezeTime).toBe('string') expect(Array.isArray(slot.products)).toBe(true) } }) it('CACHE-68: slot product images are public asset URLs', async () => { const admin = await staffClient() const store = await createStore() const gen: any = await admin.common.generateUploadUrls.mutate({ contextString: 'product_info', mimeTypes: ['image/png'], }) const product = await createProduct({ storeId: store.storeId, price: 149 }) // Attach the claimed image to a fresh SKU via an update. await admin.admin.product.updateProduct.mutate({ id: product.productId, name: product.productName, storeId: store.storeId, skus: [ { id: product.skuId, name: product.skuName, price: product.price, marketPrice: product.price + 50, images: [gen.uploadUrls[0]], isFlashAvailable: false, flashPrice: null, isOutOfStock: false, isOffer: false, isComboOnly: false, isSuspended: false, features: [{ featureName: 'quantity', featureValue: product.skuName }], }, ], }) const created = await createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, skuIds: [product.skuId], }) const doc = await currentSlots(admin) const slot = findSlot(doc, Number(created.slot.id)) expect(slot).toBeTruthy() const slotProduct = slot.products.find((entry: any) => Number(entry.id) === product.skuId) expect(slotProduct).toBeTruthy() expect(slotProduct.images.length).toBeGreaterThan(0) expect(slotProduct.images[0]).toMatch(/^https?:\/\//) expect(slotProduct.images[0]).toContain('/product-images/') }) it('CACHE-69: productAvailability is present in the slots payload', () => { expect(Array.isArray(shared.productAvailability)).toBe(true) expect(shared.productAvailability.length).toBeGreaterThan(0) for (const entry of shared.productAvailability.slice(0, 20)) { expect(entry).toEqual( expect.objectContaining({ id: expect.any(Number), isOutOfStock: expect.any(Boolean) }) ) } }) }) /* -------------------------------------------------------------------------- */ /* exclusions */ /* -------------------------------------------------------------------------- */ describe('slots.json exclusions (CACHE-65..67, 75)', () => { it('CACHE-65: a slot whose delivery time has passed is excluded', async () => { const admin = await staffClient() const created = await createSlotAt(admin, { deliveryTime: '2020-01-01T00:00:00.000Z', freezeTime: '2019-12-31T00:00:00.000Z', }) const doc = await currentSlots(admin) expect(findSlot(doc, Number(created.slot.id))).toBeUndefined() }) it('CACHE-66: a slot whose freeze time has passed is excluded', async () => { const admin = await staffClient() const created = await createSlotAt(admin, { deliveryTime: hoursFromNow(5), freezeTime: '2020-01-01T00:00:00.000Z', }) const doc = await currentSlots(admin) expect(findSlot(doc, Number(created.slot.id))).toBeUndefined() }) it('CACHE-67: a capacity-full slot is excluded', async () => { const admin = await staffClient() const created = await createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, }) const slotId = Number(created.slot.id) expect(findSlot(await currentSlots(admin), slotId)).toBeTruthy() await admin.admin.slots.updateSlotCapacity.mutate({ slotId, isCapacityFull: true }) expect(findSlot(await currentSlots(admin), slotId)).toBeUndefined() }) it('CACHE-75: a slot with no products is present with an empty products array', async () => { const admin = await staffClient() const created = await createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, }) const doc = await currentSlots(admin) const slot = findSlot(doc, Number(created.slot.id)) expect(slot).toBeTruthy() expect(slot.products).toEqual([]) }) }) /* -------------------------------------------------------------------------- */ /* synchronous mutations */ /* -------------------------------------------------------------------------- */ describe('slots.json synchronous mutations (CACHE-70..73)', () => { it('CACHE-70: createSlot bumps slots_version_num synchronously and the slot appears', async () => { const admin = await staffClient() const before = await slotsVersion(admin) const created = await createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, }) expect(await slotsVersion(admin)).toBeGreaterThan(before) const doc = await currentSlots(admin) expect(findSlot(doc, Number(created.slot.id))).toBeTruthy() }) it('CACHE-71: updateSlot bumps the version and the times update', async () => { const admin = await staffClient() const created = await createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, }) const slotId = Number(created.slot.id) const before = await slotsVersion(admin) const newDelivery = hoursFromNow(9) const newFreeze = hoursFromNow(8) await admin.admin.slots.updateSlot.mutate({ id: slotId, deliveryTime: newDelivery, freezeTime: newFreeze, isActive: true, }) expect(await slotsVersion(admin)).toBeGreaterThan(before) const slot = findSlot(await currentSlots(admin), slotId) expect(slot).toBeTruthy() expect(slot.deliveryTime).toBe(newDelivery) expect(slot.freezeTime).toBe(newFreeze) }) it('CACHE-72: capacity full bumps the version and removes the slot', async () => { const admin = await staffClient() const created = await createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, }) const slotId = Number(created.slot.id) expect(findSlot(await currentSlots(admin), slotId)).toBeTruthy() const before = await slotsVersion(admin) await admin.admin.slots.updateSlotCapacity.mutate({ slotId, isCapacityFull: true }) expect(await slotsVersion(admin)).toBeGreaterThan(before) expect(findSlot(await currentSlots(admin), slotId)).toBeUndefined() }) it('CACHE-73: restoring capacity bumps the version and the slot returns', async () => { const admin = await staffClient() const created = await createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, }) const slotId = Number(created.slot.id) await admin.admin.slots.updateSlotCapacity.mutate({ slotId, isCapacityFull: true }) expect(findSlot(await currentSlots(admin), slotId)).toBeUndefined() const before = await slotsVersion(admin) await admin.admin.slots.updateSlotCapacity.mutate({ slotId, isCapacityFull: false }) expect(await slotsVersion(admin)).toBeGreaterThan(before) expect(findSlot(await currentSlots(admin), slotId)).toBeTruthy() }) }) /* -------------------------------------------------------------------------- */ /* cross-region + validation */ /* -------------------------------------------------------------------------- */ describe('slots.json cross-region + validation (CACHE-74, 76)', () => { it('CACHE-74: a region-1 slot is absent from region 2’s slots.json', async () => { const admin = await staffClient() const created = await createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, }) const slotId = Number(created.slot.id) expect(findSlot(await currentSlots(admin), slotId)).toBeTruthy() const doc2: any = await scaffoldCache('slots', { regionId: TEST_OTHER_REGION_ID }) expect(findSlot(doc2, slotId)).toBeUndefined() }) it('(current) CACHE-76: unknown snippet SKU ids are 500; unknown group ids are dropped', async () => { const admin = await staffClient() const snippetName = `E2E UNKNOWN ${uniqueSuffix()}` await expectTrpcError( () => createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, vendorSnippets: [{ name: snippetName, skuIds: [999_999_999] }], }), 'INTERNAL_SERVER_ERROR', 'Invalid SKU' ) // Unknown group ids pass validation and are silently normalised to []. const created = await createSlotAt(admin, { deliveryTime: FAR_FUTURE, freezeTime: FAR_FUTURE_FREEZE, groupIds: [999_999_999], }) expect(created.slot.groupIds).toEqual([]) }) })