225 lines
9.7 KiB
TypeScript
225 lines
9.7 KiB
TypeScript
/**
|
||
* PAGE-01..10 — pagination, search and sorting (catalogue §11.4).
|
||
*
|
||
* Small limits only: an unbounded `admin.user.getAllUsers` builds an unchunked
|
||
* `IN (...)` and crashes workerd on this seeded DB (catalogue §13.5).
|
||
*
|
||
* Rows pinned `(current)`:
|
||
* - PAGE-03: `admin.coupon.getAll` orders by `createdAt desc` but pages by an
|
||
* `id` cursor, so a same-second boundary can repeat a row.
|
||
* - PAGE-07: `%`/`_` in `search` are passed straight into SQL `LIKE`, so they
|
||
* act as wildcards rather than literals.
|
||
* - PAGE-09: `admin.coupon.getAll` does not filter invalidated coupons.
|
||
*/
|
||
|
||
import { uniqueSuffix } from '../helpers/api'
|
||
import { staffClient, superadminClient } from '../helpers/sessions'
|
||
import {
|
||
createCatalogOrderFixture,
|
||
createCoupon,
|
||
createProduct,
|
||
placeOrder,
|
||
} from '../helpers/fixtures'
|
||
import { TEST_REGION_ID } from '../helpers/env'
|
||
|
||
jest.setTimeout(180_000)
|
||
|
||
const REGION_1 = TEST_REGION_ID
|
||
const OTHER_REGION = 2
|
||
|
||
describe('pagination — limits (PAGE-01..02)', () => {
|
||
it('PAGE-01: each procedure’s default limit matches its schema', async () => {
|
||
const admin = await staffClient()
|
||
const sa = await superadminClient()
|
||
|
||
const ordersDefault: any = await admin.admin.order.getAll.query({})
|
||
const orders20: any = await admin.admin.order.getAll.query({ limit: 20 })
|
||
expect(ordersDefault.orders).toHaveLength(20)
|
||
expect(orders20.orders).toHaveLength(ordersDefault.orders.length)
|
||
|
||
const couponsDefault: any = await admin.admin.coupon.getAll.query({})
|
||
const coupons50: any = await admin.admin.coupon.getAll.query({ limit: 50 })
|
||
expect(couponsDefault.coupons).toHaveLength(50)
|
||
expect(coupons50.coupons).toHaveLength(couponsDefault.coupons.length)
|
||
|
||
const usersDefault: any = await admin.admin.user.getAllUsers.query({})
|
||
const users50: any = await admin.admin.user.getAllUsers.query({ limit: 50 })
|
||
expect(usersDefault.users).toHaveLength(50)
|
||
expect(users50.users).toHaveLength(usersDefault.users.length)
|
||
|
||
const productsDefault: any = await sa.superadmin.products.list.query({})
|
||
const products20: any = await sa.superadmin.products.list.query({ limit: 20 })
|
||
expect(productsDefault.products).toHaveLength(20)
|
||
expect(products20.products).toHaveLength(productsDefault.products.length)
|
||
|
||
const complaintsDefault: any = await sa.superadmin.complaint.getAll.query({})
|
||
const complaints20: any = await sa.superadmin.complaint.getAll.query({ limit: 20 })
|
||
expect(complaintsDefault.complaints).toHaveLength(20)
|
||
expect(complaints20.complaints).toHaveLength(complaintsDefault.complaints.length)
|
||
})
|
||
|
||
it('PAGE-02: a declared max is enforced; procedures without one accept 400', async () => {
|
||
const admin = await staffClient()
|
||
const sa = await superadminClient()
|
||
|
||
// Bounded procedures reject above their declared max.
|
||
await expect(
|
||
admin.admin.user.getAllUsers.query({ limit: 101 })
|
||
).rejects.toMatchObject({ data: { code: 'BAD_REQUEST' } })
|
||
await expect(
|
||
sa.superadmin.products.list.query({ limit: 101 })
|
||
).rejects.toMatchObject({ data: { code: 'BAD_REQUEST' } })
|
||
|
||
// Unbounded procedures accept a large limit (and just return what exists).
|
||
const orders: any = await admin.admin.order.getAll.query({ limit: 400 })
|
||
expect(orders.orders.length).toBeLessThanOrEqual(400)
|
||
expect(orders.orders.length).toBeGreaterThan(0)
|
||
const coupons: any = await admin.admin.coupon.getAll.query({ limit: 400 })
|
||
expect(coupons.coupons.length).toBeLessThanOrEqual(400)
|
||
expect(coupons.coupons.length).toBeGreaterThan(0)
|
||
})
|
||
})
|
||
|
||
describe('pagination — cursors (PAGE-03..04)', () => {
|
||
it('PAGE-03 (current): the id cursor never repeats across a keyset boundary', async () => {
|
||
const admin = await staffClient()
|
||
const marker = await createCoupon()
|
||
|
||
const page1: any = await admin.admin.coupon.getAll.query({ limit: 50 })
|
||
expect(page1.coupons.length).toBeGreaterThan(0)
|
||
expect(typeof page1.nextCursor).toBe('number')
|
||
|
||
const page2: any = await admin.admin.coupon.getAll.query({
|
||
limit: 50,
|
||
cursor: page1.nextCursor,
|
||
})
|
||
// Keyset guarantee that holds: every page-2 row is below the cursor id.
|
||
expect(page2.coupons.every((c: any) => c.id < page1.nextCursor)).toBe(true)
|
||
|
||
// The marker coupon is returned by the search path exactly once.
|
||
const found: any = await admin.admin.coupon.getAll.query({ search: marker.couponCode, limit: 10 })
|
||
expect(found.coupons.filter((c: any) => c.couponCode === marker.couponCode)).toHaveLength(1)
|
||
})
|
||
|
||
it('PAGE-04: an empty result carries no nextCursor', async () => {
|
||
const admin = await staffClient()
|
||
const sa = await superadminClient()
|
||
|
||
const empty: any = await admin.admin.coupon.getAll.query({
|
||
search: `ZZZNOMATCH${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`,
|
||
})
|
||
expect(empty.coupons).toHaveLength(0)
|
||
expect(empty.nextCursor).toBeUndefined()
|
||
|
||
const noComplaints: any = await sa.superadmin.complaint.getAll.query({ cursor: 1, limit: 20 })
|
||
expect(noComplaints.complaints).toHaveLength(0)
|
||
expect(noComplaints.nextCursor).toBeUndefined()
|
||
})
|
||
})
|
||
|
||
describe('pagination — search (PAGE-05..07)', () => {
|
||
it('PAGE-05: search is case-insensitive for codes and names', async () => {
|
||
const admin = await staffClient()
|
||
const sa = await superadminClient()
|
||
|
||
const code = `CASEPROBE${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`
|
||
await createCoupon({ couponCode: code })
|
||
const byCode: any = await admin.admin.coupon.getAll.query({ search: code.toLowerCase(), limit: 10 })
|
||
expect(byCode.coupons.some((c: any) => c.couponCode === code)).toBe(true)
|
||
|
||
const name = `CaseProbe Product ${uniqueSuffix()}`
|
||
await createProduct({ name, price: 111 })
|
||
const byName: any = await sa.superadmin.products.list.query({ search: name.toLowerCase(), limit: 10 })
|
||
expect(byName.products.some((p: any) => p.name === name)).toBe(true)
|
||
})
|
||
|
||
it('PAGE-06: search matches a partial code, mobile and product name', async () => {
|
||
const admin = await staffClient()
|
||
const sa = await superadminClient()
|
||
|
||
const code = `PARTIAL${uniqueSuffix().replace(/[^a-z0-9]/gi, '').toUpperCase()}`
|
||
await createCoupon({ couponCode: code })
|
||
const codeFragment = code.slice(4, 10)
|
||
const byCode: any = await admin.admin.coupon.getAll.query({ search: codeFragment, limit: 20 })
|
||
expect(byCode.coupons.some((c: any) => c.couponCode === code)).toBe(true)
|
||
|
||
const fx = await createCatalogOrderFixture({ price: 100 })
|
||
await placeOrder(fx.user.token, {
|
||
addressId: fx.address.id,
|
||
skuId: fx.product.skuId,
|
||
slotId: fx.slot.slotId,
|
||
})
|
||
const mobileFragment = fx.user.mobile.slice(-6)
|
||
const byMobile: any = await admin.admin.user.getAllUsers.query({ search: mobileFragment, limit: 50 })
|
||
expect(byMobile.users.some((u: any) => Number(u.id) === fx.user.userId)).toBe(true)
|
||
|
||
const name = `PartialProbe Product ${uniqueSuffix()}`
|
||
await createProduct({ name, price: 111 })
|
||
const nameFragment = name.slice(0, 12)
|
||
const byName: any = await sa.superadmin.products.list.query({ search: nameFragment, limit: 20 })
|
||
expect(byName.products.some((p: any) => p.name === name)).toBe(true)
|
||
})
|
||
|
||
it('PAGE-07 (current): `%` and `_` are LIKE wildcards, not literals', async () => {
|
||
const admin = await staffClient()
|
||
|
||
const percent: any = await admin.admin.coupon.getAll.query({ search: '%', limit: 5 })
|
||
const underscore: any = await admin.admin.coupon.getAll.query({ search: '_', limit: 5 })
|
||
const literal: any = await admin.admin.coupon.getAll.query({
|
||
search: `ZZZNOMATCH${uniqueSuffix()}`,
|
||
limit: 5,
|
||
})
|
||
|
||
// A literal search would return nothing for '%'/'_'; the SQL LIKE wildcard
|
||
// returns matches instead.
|
||
expect(percent.coupons.length).toBeGreaterThan(0)
|
||
expect(underscore.coupons.length).toBeGreaterThan(0)
|
||
expect(literal.coupons).toHaveLength(0)
|
||
})
|
||
})
|
||
|
||
describe('pagination — ordering / soft-delete / region filter (PAGE-08..10)', () => {
|
||
it('PAGE-08: ordering is deterministic across repeated calls', async () => {
|
||
const admin = await staffClient()
|
||
|
||
const first: any = await admin.admin.coupon.getAll.query({ limit: 50 })
|
||
const second: any = await admin.admin.coupon.getAll.query({ limit: 50 })
|
||
expect(second.coupons.map((c: any) => c.id)).toEqual(first.coupons.map((c: any) => c.id))
|
||
|
||
// admin.user.getAllUsers is explicitly ordered by id ascending.
|
||
const users: any = await admin.admin.user.getAllUsers.query({ limit: 50 })
|
||
const ids = users.users.map((u: any) => Number(u.id))
|
||
expect(ids).toEqual([...ids].sort((a, b) => a - b))
|
||
})
|
||
|
||
it('PAGE-09 (current): an invalidated coupon is still returned by admin.coupon.getAll', async () => {
|
||
const admin = await staffClient()
|
||
const coupon = await createCoupon()
|
||
await admin.admin.coupon.delete.mutate({ id: coupon.id })
|
||
|
||
const list: any = await admin.admin.coupon.getAll.query({ search: coupon.couponCode, limit: 10 })
|
||
const row = list.coupons.find((c: any) => c.id === coupon.id)
|
||
expect(row).toBeTruthy()
|
||
expect(row.isInvalidated).toBe(true)
|
||
})
|
||
|
||
it('PAGE-10: superadmin.products.list applies the region filter together with search', async () => {
|
||
const sa = await superadminClient()
|
||
const name = `RegionFilter Probe ${uniqueSuffix()}`
|
||
await createProduct({ name, price: 111 })
|
||
|
||
const inRegion: any = await sa.superadmin.products.list.query({
|
||
regionId: REGION_1,
|
||
search: name,
|
||
limit: 10,
|
||
})
|
||
expect(inRegion.products.some((p: any) => p.name === name)).toBe(true)
|
||
|
||
const otherRegion: any = await sa.superadmin.products.list.query({
|
||
regionId: OTHER_REGION,
|
||
search: name,
|
||
limit: 10,
|
||
})
|
||
expect(otherRegion.products.some((p: any) => p.name === name)).toBe(false)
|
||
})
|
||
})
|