freshyo/apps/backend/tests/api/user-surface.test.ts
2026-10-14 17:27:52 +05:30

108 lines
3.3 KiB
TypeScript

/**
* User-facing product / coupon / profile surface + the complaint lifecycle
* (raise as the user, resolve as staff).
*/
import { apiClient, captureError } from '../helpers/api'
import { createStoreWithSku } from '../helpers/fixtures'
import { staffSession, userSession } from '../helpers/sessions'
describe('user surface', () => {
let token = ''
let userId = 0
let productId = 0
beforeAll(async () => {
const store = await createStoreWithSku()
const user = await userSession()
token = user.token
userId = user.userId
productId = store.productId
})
it('returns the product detail for a product id', async () => {
const detail = await apiClient().user.product.getProductDetails.query({
id: String(productId),
regionId: 1,
})
expect((detail as { name?: string })?.name).toBeTruthy()
})
it('creates a review and returns it from getProductReviews', async () => {
const body = `API test review ${Date.now()}`
await apiClient(token).user.product.createReview.mutate({
productId,
reviewBody: body,
ratings: 5,
})
const reviews = await apiClient().user.product.getProductReviews.query({
productId,
limit: 50,
offset: 0,
})
expect(Array.isArray(reviews.reviews)).toBe(true)
expect(reviews.reviews.some((r) => r.reviewBody === body)).toBe(true)
})
it('returns presigned upload URLs', async () => {
const res = await apiClient(token).common.generateUploadUrls.mutate({
contextString: 'complaint',
mimeTypes: ['image/jpeg'],
})
expect(Array.isArray(res.uploadUrls)).toBe(true)
expect(res.uploadUrls).toHaveLength(1)
})
it('exposes eligible and owned coupons', async () => {
const eligible = await apiClient(token).user.coupon.getEligible.query()
expect(Array.isArray(eligible.data)).toBe(true)
const mine = await apiClient(token).user.coupon.getMyCoupons.query()
expect(Array.isArray(mine.data.personal)).toBe(true)
expect(Array.isArray(mine.data.general)).toBe(true)
})
it('rejects redeeming an unknown coupon code', async () => {
const error = await captureError(() =>
apiClient(token).user.coupon.redeemReservedCoupon.mutate({
secretCode: `NO-SUCH-COUPON-${Date.now()}`,
})
)
expect(error.message).toMatch(/invalid|redeemed/i)
})
it('returns the signed-in user profile', async () => {
const self = await apiClient(token).user.user.getSelfData.query()
expect(self.data.user.id).toBe(userId)
})
it('raises a complaint that staff can resolve', async () => {
const staff = await staffSession()
const admin = apiClient(staff.token)
const body = `API test complaint ${Date.now()}`
await apiClient(token).user.complaint.raise.mutate({ complaintBody: body })
const listed = await apiClient(token).user.complaint.getAll.query()
const complaint = listed.complaints.find((c) => c.complaintBody === body)
expect(complaint).toBeDefined()
expect(complaint!.isResolved).toBe(false)
await admin.admin.complaint.resolve.mutate({
id: String(complaint!.id),
response: 'Resolved by API test',
})
const after = await apiClient(token).user.complaint.getAll.query()
const resolved = after.complaints.find((c) => c.id === complaint!.id)
expect(resolved!.isResolved).toBe(true)
})
})