169 lines
5.9 KiB
TypeScript
169 lines
5.9 KiB
TypeScript
/**
|
||
* E2E-34..40 — golden coupon flow (catalogue §2.3).
|
||
*
|
||
* Live HTTP/tRPC suite. Each `it()` maps to one catalogue row. Coupon business
|
||
* errors are thrown as raw `Error`s, so they surface as INTERNAL_SERVER_ERROR
|
||
* (pinned where the catalogue expects a plain rejection).
|
||
*/
|
||
|
||
import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api'
|
||
import { staffClient } from '../helpers/sessions'
|
||
import {
|
||
createCatalogOrderFixture,
|
||
createCoupon,
|
||
placeOrder,
|
||
registerThrowawayUser,
|
||
} from '../helpers/fixtures'
|
||
import { TEST_REGION_ID } from '../helpers/env'
|
||
|
||
jest.setTimeout(180_000)
|
||
|
||
const REGION_1 = TEST_REGION_ID
|
||
|
||
function code(prefix: string): string {
|
||
return `${prefix}${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`
|
||
}
|
||
|
||
async function createAdminCoupon(payload: Record<string, unknown>): Promise<any> {
|
||
const admin = await staffClient()
|
||
const res: any = await admin.admin.coupon.create.mutate(payload as any)
|
||
return Array.isArray(res) ? res[0] : res?.coupons?.[0]
|
||
}
|
||
|
||
const eligible = (token: string): Promise<any> =>
|
||
apiClient(token).user.coupon.getEligible.query({ regionId: REGION_1 }) as Promise<any>
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* Coupon create + read (E2E-34..35) */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('coupon golden create/read (E2E-34..35)', () => {
|
||
it('E2E-34: an admin flat coupon round-trips through getById', async () => {
|
||
const admin = await staffClient()
|
||
const expected = code('FLAT')
|
||
const coupon = await createAdminCoupon({
|
||
couponCode: expected,
|
||
skuIds: [],
|
||
isApplyForAll: true,
|
||
flatDiscount: 75,
|
||
})
|
||
|
||
const read: any = await admin.admin.coupon.getById.query({ id: coupon.id })
|
||
expect(read.couponCode).toBe(expected)
|
||
expect(Number(read.flatDiscount)).toBe(75)
|
||
expect(read.discountPercent).toBeNull()
|
||
expect(read.isInvalidated).toBe(false)
|
||
expect(read.regionId).toBe(REGION_1)
|
||
})
|
||
|
||
it('E2E-35: a general coupon is visible in the user’s getEligible', async () => {
|
||
const user = await registerThrowawayUser()
|
||
const coupon = await createCoupon({ flatDiscount: 50, minOrder: 1, isApplyForAll: true })
|
||
|
||
const res = await eligible(user.token)
|
||
expect(res.success).toBe(true)
|
||
expect(res.data.some((c: any) => c.id === coupon.id)).toBe(true)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* Coupon on an order (E2E-36..37) */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('coupon golden order flow (E2E-36..37)', () => {
|
||
it('E2E-36: placing an order with a flat coupon sets the discount and coupon code', async () => {
|
||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||
const coupon = await createCoupon({ flatDiscount: 60, minOrder: 1, isApplyForAll: true })
|
||
|
||
const placed = await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
couponIds: [coupon.id],
|
||
})
|
||
const d: any = await apiClient(fx.user.token).user.order.getOrderById.query({
|
||
orderId: String(placed[0].id),
|
||
})
|
||
expect(d.couponCode).toBe(coupon.couponCode)
|
||
expect(d.discountAmount).toBe(60)
|
||
expect(d.totalAmount).toBe(199 - 60 + 20)
|
||
})
|
||
|
||
it('E2E-37: an invalidated coupon leaves getEligible and rejects at order time', async () => {
|
||
const admin = await staffClient()
|
||
const fx = await createCatalogOrderFixture({ price: 199 })
|
||
const coupon = await createCoupon({ flatDiscount: 40, minOrder: 1, isApplyForAll: true })
|
||
|
||
await admin.admin.coupon.delete.mutate({ id: coupon.id })
|
||
|
||
const res = await eligible(fx.user.token)
|
||
expect(res.data.some((c: any) => c.id === coupon.id)).toBe(false)
|
||
|
||
await expectTrpcError(
|
||
() =>
|
||
placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
couponIds: [coupon.id],
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'no longer valid'
|
||
)
|
||
})
|
||
})
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* Coupon validation edges (E2E-38..40) */
|
||
/* -------------------------------------------------------------------------- */
|
||
|
||
describe('coupon golden edges (E2E-38..40)', () => {
|
||
it('E2E-38: a coupon with both discount types is rejected', async () => {
|
||
await expectTrpcError(
|
||
() =>
|
||
createAdminCoupon({
|
||
couponCode: code('BOTH'),
|
||
skuIds: [],
|
||
isApplyForAll: true,
|
||
discountPercent: 20,
|
||
flatDiscount: 30,
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Either discountPercent or flatDiscount'
|
||
)
|
||
})
|
||
|
||
it('E2E-39: a duplicate coupon code is rejected', async () => {
|
||
const admin = await staffClient()
|
||
const duplicate = code('DUP')
|
||
await createAdminCoupon({ couponCode: duplicate, skuIds: [], isApplyForAll: true, flatDiscount: 10 })
|
||
|
||
await expectTrpcError(
|
||
() => admin.admin.coupon.create.mutate({
|
||
couponCode: duplicate,
|
||
skuIds: [],
|
||
isApplyForAll: true,
|
||
flatDiscount: 10,
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'already exists'
|
||
)
|
||
})
|
||
|
||
it("E2E-40: a user-based coupon is never visible to another user", async () => {
|
||
const owner = await registerThrowawayUser()
|
||
const other = await registerThrowawayUser()
|
||
const coupon = await createCoupon({
|
||
flatDiscount: 50,
|
||
isUserBased: true,
|
||
applicableUsers: [owner.userId],
|
||
isApplyForAll: false,
|
||
})
|
||
|
||
const ownerView = await eligible(owner.token)
|
||
expect(ownerView.data.some((c: any) => c.id === coupon.id)).toBe(true)
|
||
|
||
const otherView = await eligible(other.token)
|
||
expect(otherView.data.some((c: any) => c.id === coupon.id)).toBe(false)
|
||
})
|
||
})
|