/** * E2E-26..33 — golden order lifecycle (catalogue §2.2). * * Live HTTP/tRPC suite. Each `it()` maps to one catalogue row. The two-order * fixture places one call with two items in different slots, which the backend * splits into one order per slot (delivery charged once per sub-order). */ import { apiClient, expectTrpcError, type ApiClient } from '../helpers/api' import { staffClient } from '../helpers/sessions' import { createAddress, createCatalogOrderFixture, createCoupon, createFutureSlot, createProduct, createStore, placeOrder, registerThrowawayUser, } from '../helpers/fixtures' import { TEST_REGION_ID } from '../helpers/env' jest.setTimeout(300_000) const REGION_1 = TEST_REGION_ID /* -------------------------------------------------------------------------- */ /* Local helpers */ /* -------------------------------------------------------------------------- */ function placeCall(token: string, o: Record): Promise { const items = o.items ?? [{ skuId: o.skuId, quantity: o.quantity ?? 1, slotId: o.slotId ?? null }] return apiClient(token).user.order.placeOrder.mutate({ selectedItems: items, addressId: o.addressId, regionId: o.regionId ?? REGION_1, ...(o.couponIds ? { couponIds: o.couponIds } : {}), ...(o.userNotes !== undefined ? { userNotes: o.userNotes } : {}), } as any) as Promise } const detail = (token: string, orderId: number): Promise => apiClient(token).user.order.getOrderById.query({ orderId: String(orderId) }) as Promise const list = (token: string): Promise => apiClient(token).user.order.getOrders.query({ page: 1, pageSize: 50 }) as Promise /* -------------------------------------------------------------------------- */ /* Shared two-slot fixture (E2E-26..29) */ /* -------------------------------------------------------------------------- */ interface TwoSlot { admin: ApiClient user: Awaited> address: any slot1: number slot2: number order1: number order2: number initialOrder1: any initialOrder2: any } let twoSlot: TwoSlot beforeAll(async () => { const admin = await staffClient() const store = await createStore() const product = await createProduct({ storeId: store.storeId, price: 80 }) const slot1 = await createFutureSlot([product.skuId]) const slot2 = await createFutureSlot([product.skuId]) const user = await registerThrowawayUser() const address = await createAddress(user.token) // Cart goods subtotal 240 (80 + 160) stays under the 300 free-delivery // threshold, so the single cart-level delivery charge (20) is applied to the // primary sub-order (the one with the highest goods subtotal = slot2). const placed = await placeOrder(user.token, { addressId: address.id, skuId: product.skuId, slotId: slot1.slotId, items: [ { skuId: product.skuId, quantity: 1, slotId: slot1.slotId }, { skuId: product.skuId, quantity: 2, slotId: slot2.slotId }, ], }) const orderFor = (slotId: number) => placed.find((order: any) => Number(order.slotId) === slotId) const o1 = orderFor(slot1.slotId) const o2 = orderFor(slot2.slotId) if (!o1 || !o2) { throw new Error(`[e2e] split orders not found: ${JSON.stringify(placed)}`) } twoSlot = { admin, user, address, slot1: slot1.slotId, slot2: slot2.slotId, order1: Number(o1.id), order2: Number(o2.id), initialOrder1: await detail(user.token, Number(o1.id)), initialOrder2: await detail(user.token, Number(o2.id)), } }) /* -------------------------------------------------------------------------- */ /* Two-slot lifecycle (E2E-26..29) */ /* -------------------------------------------------------------------------- */ describe('order lifecycle — two slots (E2E-26..29)', () => { it('(current) E2E-26: two items in different slots make one order per slot, delivery charged once for the cart', () => { expect(twoSlot.order1).not.toBe(twoSlot.order2) expect(twoSlot.initialOrder1.deliveryDate).not.toBe(twoSlot.initialOrder2.deliveryDate) // Delivery is a single cart-level charge attached to the primary sub-order // (highest goods subtotal = slot2); it is NOT charged once per order. const charges = [twoSlot.initialOrder1.deliveryCharge, twoSlot.initialOrder2.deliveryCharge] expect(charges.reduce((a: number, b: number) => a + b, 0)).toBe(20) expect(charges.filter((c: number) => c === 20)).toHaveLength(1) expect(twoSlot.initialOrder2.deliveryCharge).toBe(20) expect(twoSlot.initialOrder1.deliveryCharge).toBe(0) expect(twoSlot.initialOrder1.totalAmount).toBe(80) expect(twoSlot.initialOrder2.totalAmount).toBe(180) }) it('E2E-27: admin getSlotOrders/getAll see both orders with exactly one first-order flag', async () => { const slot1Orders: any = await twoSlot.admin.admin.order.getSlotOrders.query({ slotId: String(twoSlot.slot1), }) const row1 = slot1Orders.data.find((o: any) => o.id === twoSlot.order1) expect(row1).toBeTruthy() const slot2Orders: any = await twoSlot.admin.admin.order.getSlotOrders.query({ slotId: String(twoSlot.slot2), }) const row2 = slot2Orders.data.find((o: any) => o.id === twoSlot.order2) expect(row2).toBeTruthy() // `isFirstOrder` marks the user's first order by id. A single placeOrder call // that splits across slots may assign the lower id to either sub-order, so // assert the invariant (exactly one) rather than which slot won. expect([row1.isFirstOrder, row2.isFirstOrder].filter(Boolean)).toHaveLength(1) const all: any = await twoSlot.admin.admin.order.getAll.query({ limit: 50 }) const ids = all.orders.map((o: any) => o.id) expect(ids).toContain(twoSlot.order1) expect(ids).toContain(twoSlot.order2) }) it('E2E-28: admin packaged then delivered flips the user deliveryStatus', async () => { await twoSlot.admin.admin.order.updatePackaged.mutate({ orderId: String(twoSlot.order1), isPackaged: true, }) const packaged: any = await detail(twoSlot.user.token, twoSlot.order1) expect(packaged.deliveryStatus).toBe('packaged') expect(packaged.orderStatus).toBe('success') await twoSlot.admin.admin.order.updateDelivered.mutate({ orderId: String(twoSlot.order1), isDelivered: true, }) const delivered: any = await detail(twoSlot.user.token, twoSlot.order1) expect(delivered.deliveryStatus).toBe('success') expect(delivered.orderStatus).toBe('success') }) it('E2E-29: admin removing the delivery charge zeroes the user charge', async () => { const res: any = await twoSlot.admin.admin.order.removeDeliveryCharge.mutate({ orderId: twoSlot.order2, }) expect(res.success).toBe(true) const after: any = await detail(twoSlot.user.token, twoSlot.order2) expect(after.deliveryCharge).toBe(0) expect(after.totalAmount).toBe(160) }) }) /* -------------------------------------------------------------------------- */ /* Cancellation (E2E-30..31) */ /* -------------------------------------------------------------------------- */ describe('order lifecycle — cancellation (E2E-30..31)', () => { it('E2E-30: an admin cancel surfaces cancelled + reason to the user', async () => { const admin = await staffClient() const fx = await createCatalogOrderFixture({ price: 199 }) const placed = await placeOrder(fx.user.token, { addressId: fx.address.id, skuId: fx.product.skuId, slotId: fx.slot.slotId, }) const id = Number(placed[0].id) await admin.admin.order.cancelOrder.mutate({ orderId: id, reason: 'admin out of stock' }) const d: any = await detail(fx.user.token, id) expect(d.deliveryStatus).toBe('cancelled') expect(d.orderStatus).toBe('cancelled') expect(d.cancelReason).toBe('admin out of stock') }) it('E2E-31: a user cancel is reflected in the list and the detail', async () => { const fx = await createCatalogOrderFixture({ price: 199 }) const placed = await placeOrder(fx.user.token, { addressId: fx.address.id, skuId: fx.product.skuId, slotId: fx.slot.slotId, }) const id = Number(placed[0].id) const res: any = await apiClient(fx.user.token).user.order.cancelOrder.mutate({ id, reason: 'changed my mind', }) expect(res.success).toBe(true) const listed = await list(fx.user.token) const row = listed.data.find((o: any) => Number(o.id) === id) expect(row.deliveryStatus).toBe('cancelled') expect(row.cancelReason).toBe('changed my mind') const d = await detail(fx.user.token, id) expect(d.deliveryStatus).toBe('cancelled') expect(d.cancelReason).toBe('changed my mind') }) }) /* -------------------------------------------------------------------------- */ /* Coupons (E2E-32..33) */ /* -------------------------------------------------------------------------- */ describe('order lifecycle — coupons (E2E-32..33)', () => { it('E2E-32: a first-order-only coupon applies once, then rejects on the next order', async () => { const store = await createStore() const product = await createProduct({ storeId: store.storeId, price: 199 }) const slot = await createFutureSlot([product.skuId]) const user = await registerThrowawayUser() const address = await createAddress(user.token) const coupon = await createCoupon({ flatDiscount: 30, minOrder: 1, isFirstOrderOnly: true, isApplyForAll: true, }) const first = await placeCall(user.token, { addressId: address.id, skuId: product.skuId, slotId: slot.slotId, couponIds: [coupon.id], }) expect(Number(first.data[0].discountAmount)).toBe(30) await expectTrpcError( () => placeCall(user.token, { addressId: address.id, skuId: product.skuId, slotId: slot.slotId, couponIds: [coupon.id], }), 'INTERNAL_SERVER_ERROR', 'first order only' ) }) it('E2E-33: cancelling releases the coupon usage and restores first-order eligibility', async () => { const store = await createStore() const product = await createProduct({ storeId: store.storeId, price: 199 }) const slot = await createFutureSlot([product.skuId]) const user = await registerThrowawayUser() const address = await createAddress(user.token) const coupon = await createCoupon({ flatDiscount: 30, minOrder: 1, maxLimitForUser: 1, isApplyForAll: true, }) const first = await placeCall(user.token, { addressId: address.id, skuId: product.skuId, slotId: slot.slotId, couponIds: [coupon.id], }) expect(Number(first.data[0].discountAmount)).toBe(30) await apiClient(user.token).user.order.cancelOrder.mutate({ id: Number(first.data[0].id), reason: 'release usage', }) const eligible: any = await apiClient(user.token).user.coupon.getEligible.query({ regionId: REGION_1 }) expect(eligible.isFirstOrder).toBe(true) // The released usage lets the same coupon apply again. const second = await placeCall(user.token, { addressId: address.id, skuId: product.skuId, slotId: slot.slotId, couponIds: [coupon.id], }) expect(second.success).toBe(true) expect(Number(second.data[0].discountAmount)).toBe(30) }) })