freshyo/apps/backend/tests/api/superadmin-complaint.test.ts
2026-10-20 00:31:05 +05:30

128 lines
5.1 KiB
TypeScript

/**
* SCOMP-01..06 — `superadmin.complaint` (catalogue §8.9).
*
* Live HTTP/tRPC suite. One `it()` per catalogue row; the row that pins current
* (not intended) behaviour is prefixed `(current)`.
*/
import { apiClient, expectTrpcError, uniqueSuffix } from '../helpers/api'
import { anonClient, staffSession, superadminClient, userSession } from '../helpers/sessions'
import { createAddress, registerThrowawayUser } from '../helpers/fixtures'
import { getEssentialConsts } from '../helpers/cache'
jest.setTimeout(180_000)
/** Raise a complaint for a throwaway user scoped to `regionId` via a default address. */
async function raiseComplaint(
options: { regionId: number; body: string; imageUrls?: string[] }
): Promise<{ token: string; userId: number; body: string }> {
const user = await registerThrowawayUser()
await createAddress(user.token, { regionId: options.regionId })
await apiClient(user.token).user.complaint.raise.mutate({
complaintBody: options.body,
...(options.imageUrls ? { imageUrls: options.imageUrls } : {}),
})
return { token: user.token, userId: user.userId, body: options.body }
}
/* -------------------------------------------------------------------------- */
/* getAll */
/* -------------------------------------------------------------------------- */
describe('superadmin.complaint.getAll (SCOMP-01..04)', () => {
it('SCOMP-01: getAll returns complaints from every region', async () => {
const marker = uniqueSuffix()
const region1 = await raiseComplaint({ regionId: 1, body: `SCOMP-01 r1 ${marker}` })
const region2 = await raiseComplaint({ regionId: 2, body: `SCOMP-01 r2 ${marker}` })
const superadmin = await superadminClient()
const res: any = await superadmin.superadmin.complaint.getAll.query({ limit: 100 })
const first = res.complaints.find((complaint: any) => complaint.text === region1.body)
const second = res.complaints.find((complaint: any) => complaint.text === region2.body)
expect(first).toEqual(
expect.objectContaining({ userId: region1.userId, regionId: 1, status: expect.any(String) })
)
expect(second).toEqual(
expect.objectContaining({ userId: region2.userId, regionId: 2, status: expect.any(String) })
)
})
it('SCOMP-02: pagination returns a nextCursor and the older page', async () => {
const superadmin = await superadminClient()
const first: any = await superadmin.superadmin.complaint.getAll.query({ limit: 1 })
expect(first.complaints).toHaveLength(1)
expect(first.nextCursor).toEqual(expect.any(Number))
const second: any = await superadmin.superadmin.complaint.getAll.query({
limit: 1,
cursor: first.nextCursor,
})
expect(second.complaints).toHaveLength(1)
expect(Number(second.complaints[0].id)).toBeLessThan(Number(first.complaints[0].id))
})
it('(current) SCOMP-03: a cursor below every id yields an empty page', async () => {
// The seeded DB always has complaints, so a truly empty result is
// unreachable; `lt(id, cursor)` with cursor=1 is the reachable empty page.
const superadmin = await superadminClient()
const res: any = await superadmin.superadmin.complaint.getAll.query({ cursor: 1, limit: 5 })
expect(res.complaints).toEqual([])
expect(res.nextCursor).toBeUndefined()
})
it('SCOMP-04: status, region and images are mapped and signed', async () => {
const marker = uniqueSuffix()
const file = `complaint-${marker.replace(/[^a-z0-9]/gi, '')}.png`
const complaint = await raiseComplaint({
regionId: 1,
body: `SCOMP-04 ${marker}`,
imageUrls: [`product-images/${file}`],
})
const superadmin = await superadminClient()
const res: any = await superadmin.superadmin.complaint.getAll.query({ limit: 100 })
const row = res.complaints.find((entry: any) => entry.text === complaint.body)
expect(row).toEqual(
expect.objectContaining({
status: 'pending',
regionId: 1,
regionName: expect.any(String),
userName: expect.any(String),
userMobile: expect.any(String),
})
)
const consts = await getEssentialConsts()
const assetsDomain = String(consts.assetsDomain).replace(/\/+$/, '')
expect(row.images).toEqual([`${assetsDomain}/product-images/${file}`])
})
})
/* -------------------------------------------------------------------------- */
/* auth */
/* -------------------------------------------------------------------------- */
describe('superadmin.complaint auth (SCOMP-05..06)', () => {
it('SCOMP-05: no token is 401', async () => {
await expectTrpcError(
() => anonClient().superadmin.complaint.getAll.query({ limit: 5 }),
'UNAUTHORIZED'
)
})
it('SCOMP-06: staff and user tokens are 401', async () => {
const staff = await staffSession()
const user = await userSession()
for (const client of [apiClient(staff.token), apiClient(user.token)]) {
await expectTrpcError(
() => client.superadmin.complaint.getAll.query({ limit: 5 }),
'UNAUTHORIZED'
)
}
})
})