/** * placeOrder guard/behaviour coverage that the existing suites do not reach: * stock, quantity, slot validity, flash pricing, suspension and double-submit. * * Guards now enforced by placeOrderUtil (region, stock, slot state/membership) * are asserted as rejections. Remaining gaps (empty carts, no quantity cap, no * idempotency) are still pinned as known bugs. See tests/BUGS.md. */ import { apiClient, captureError } from '../helpers/api' import { asNumber, getConstants, setConst } from '../helpers/consts' import { createFutureSlot, createStoreWithSku, registerUserWithAddress, } from '../helpers/fixtures' import { staffSession } from '../helpers/sessions' describe('placeOrder edge cases', () => { let admin: ReturnType let userToken = '' let addressId = 0 let userId = 0 let skuId = 0 let slotId = 0 let otherSkuId = 0 let flashSkuId = 0 let flashFallbackSkuId = 0 let plainSkuId = 0 let flashDeliveryCharge = 0 beforeAll(async () => { const staff = await staffSession() admin = apiClient(staff.token) const store = await createStoreWithSku({ price: 100 }) const other = await createStoreWithSku({ price: 100 }) const flash = await createStoreWithSku({ price: 100 }) const flashFallback = await createStoreWithSku({ price: 100 }) const plain = await createStoreWithSku({ price: 100 }) const slot = await createFutureSlot([store.skuId]) const buyer = await registerUserWithAddress() const consts = await getConstants() userToken = buyer.token addressId = buyer.addressId userId = buyer.userId skuId = store.skuId otherSkuId = other.skuId flashSkuId = flash.skuId flashFallbackSkuId = flashFallback.skuId plainSkuId = plain.skuId slotId = slot.slotId flashDeliveryCharge = asNumber(consts.flashDeliveryCharge) }) const place = (over: Record = {}) => apiClient(userToken).user.order.placeOrder.mutate({ selectedItems: [{ skuId, quantity: 1, slotId }], addressId, regionId: 1, paymentMethod: 'cod', ...over, }) const withFlashEnabled = async (fn: () => Promise): Promise => { const before = await getConstants() const wasEnabled = before.isFlashDeliveryEnabled === true if (!wasEnabled) await setConst('isFlashDeliveryEnabled', true) try { return await fn() } finally { if (!wasEnabled) await setConst('isFlashDeliveryEnabled', false) } } describe('stock', () => { it('rejects an out-of-stock product', async () => { const store = await createStoreWithSku({ price: 100 }) const slot = await createFutureSlot([store.skuId]) await admin.admin.product.toggleOutOfStock.mutate({ id: store.productId }) try { const error = await captureError(() => place({ selectedItems: [{ skuId: store.skuId, quantity: 1, slotId: slot.slotId }] }) ) expect(error.message).toMatch(/unavailable/i) } finally { await admin.admin.product.toggleOutOfStock.mutate({ id: store.productId }) } }) }) describe('input validation gaps', () => { it('pins: an empty selectedItems list fails with a raw DB error, not validation', async () => { // There is no `.min(1)` on selectedItems, so the empty list reaches // Drizzle, which throws "values() must be called with at least one value" // instead of surfacing a 400 "no items" message. const error = await captureError(() => place({ selectedItems: [] })) expect(error.message).toMatch(/at least one value/i) }) it('pins: an arbitrarily large quantity is accepted', async () => { const res = await place({ selectedItems: [{ skuId, quantity: 1_000_000, slotId }] }) expect(res.success).toBe(true) expect(res.data[0].id).toBeGreaterThan(0) }) }) describe('slot validity', () => { it('rejects an inactive slot', async () => { const created = await admin.admin.slots.createSlot.mutate({ deliveryTime: new Date(Date.now() + 3 * 3600_000).toISOString(), freezeTime: new Date(Date.now() + 2 * 3600_000).toISOString(), isActive: false, skuIds: [skuId], }) const error = await captureError(() => place({ selectedItems: [{ skuId, quantity: 1, slotId: created.slot.id }] }) ) expect(error.message).toMatch(/not available/i) }) it('rejects a slot whose freeze/delivery time has passed', async () => { const created = await admin.admin.slots.createSlot.mutate({ deliveryTime: new Date(Date.now() - 3600_000).toISOString(), freezeTime: new Date(Date.now() - 2 * 3600_000).toISOString(), isActive: true, skuIds: [skuId], }) const error = await captureError(() => place({ selectedItems: [{ skuId, quantity: 1, slotId: created.slot.id }] }) ) expect(error.message).toMatch(/closed for orders/i) }) it('rejects a slot that does not carry the ordered SKU', async () => { const slotWithOtherSku = await createFutureSlot([otherSkuId]) const error = await captureError(() => place({ selectedItems: [{ skuId, quantity: 1, slotId: slotWithOtherSku.slotId }], }) ) expect(error.message).toMatch(/not available in the selected/i) }) }) describe('flash delivery pricing', () => { it('rejects a non-flash SKU on a flash order', async () => { await withFlashEnabled(async () => { const error = await captureError(() => place({ selectedItems: [{ skuId: plainSkuId, quantity: 1, slotId: null }], isFlashDelivery: true, }) ) expect(error.message).toMatch(/not available for flash delivery/i) }) }) it('charges flashPrice plus the flash delivery charge', async () => { await admin.admin.product.updateProductPrices.mutate({ updates: [{ skuId: flashSkuId, flashPrice: 60, isFlashAvailable: true }], }) await withFlashEnabled(async () => { const res = await place({ selectedItems: [{ skuId: flashSkuId, quantity: 1, slotId: null }], isFlashDelivery: true, }) // 60 (flashPrice, not the 100 list price) < flashFreeDeliveryThreshold, // so the flash delivery charge is added on top. expect(parseFloat(res.data[0].totalAmount)).toBe(60 + flashDeliveryCharge) }) }) it('falls back to the regular price when flashPrice is null', async () => { await admin.admin.product.updateProductPrices.mutate({ updates: [{ skuId: flashFallbackSkuId, flashPrice: null, isFlashAvailable: true }], }) await withFlashEnabled(async () => { const res = await place({ selectedItems: [{ skuId: flashFallbackSkuId, quantity: 1, slotId: null }], isFlashDelivery: true, }) expect(parseFloat(res.data[0].totalAmount)).toBe(100 + flashDeliveryCharge) }) }) }) describe('suspension', () => { it('rejects a suspended user with a 403-style error', async () => { await admin.admin.user.updateUserSuspension.mutate({ userId, isSuspended: true }) try { const error = await captureError(() => place()) expect(error.message).toMatch(/unable to place order/i) } finally { await admin.admin.user.updateUserSuspension.mutate({ userId, isSuspended: false }) } }) }) describe('duplicate submission', () => { it('pins: two identical requests create two orders (no idempotency key)', async () => { const first = await place() const second = await place() expect(first.data[0].id).toBeGreaterThan(0) expect(second.data[0].id).toBeGreaterThan(0) expect(first.data[0].id).not.toBe(second.data[0].id) }) }) })