158 lines
5.8 KiB
TypeScript
158 lines
5.8 KiB
TypeScript
/**
|
||
* UCOMP-01..11 — `user.complaint.*` (catalogue §6.9).
|
||
*/
|
||
|
||
import { apiClient, expectTrpcError } from '../helpers/api'
|
||
import {
|
||
createCatalogOrderFixture,
|
||
placeOrder,
|
||
registerThrowawayUser,
|
||
} from '../helpers/fixtures'
|
||
|
||
jest.setTimeout(180_000)
|
||
|
||
async function raise(token: string, input: any): Promise<any> {
|
||
return apiClient(token).user.complaint.raise.mutate(input)
|
||
}
|
||
|
||
async function getAll(token: string): Promise<any[]> {
|
||
const res: any = await apiClient(token).user.complaint.getAll.query()
|
||
return res.complaints
|
||
}
|
||
|
||
async function orderFixture() {
|
||
const fixture = await createCatalogOrderFixture({ price: 349 })
|
||
const placed = await placeOrder(fixture.user.token, {
|
||
addressId: fixture.address.id,
|
||
skuId: fixture.product.skuId,
|
||
slotId: fixture.slot.slotId,
|
||
})
|
||
const orderId = Number(placed[0].id ?? (placed[0] as any).orderId)
|
||
return { ...fixture, orderId }
|
||
}
|
||
|
||
describe('user.complaint (UCOMP-01..11)', () => {
|
||
it('UCOMP-01: a complaint without an order is raised and listed', async () => {
|
||
const user = await registerThrowawayUser()
|
||
const res: any = await raise(user.token, { complaintBody: 'Something is wrong' })
|
||
|
||
expect(res.success).toBe(true)
|
||
expect(res.message).toEqual(expect.any(String))
|
||
|
||
const complaints = await getAll(user.token)
|
||
expect(complaints).toHaveLength(1)
|
||
expect(complaints[0]).toEqual(
|
||
expect.objectContaining({ complaintBody: 'Something is wrong', orderId: null })
|
||
)
|
||
})
|
||
|
||
it('UCOMP-02: a numeric order id is linked', async () => {
|
||
const { user, orderId } = await orderFixture()
|
||
await raise(user.token, { complaintBody: 'Numeric order', orderId: String(orderId) })
|
||
|
||
const complaints = await getAll(user.token)
|
||
const complaint = complaints.find((c) => c.complaintBody === 'Numeric order')
|
||
expect(complaint.orderId).toBe(orderId)
|
||
})
|
||
|
||
it('UCOMP-03: ORD123 and ORD-123 both link to the order', async () => {
|
||
const { user, orderId } = await orderFixture()
|
||
await raise(user.token, { complaintBody: 'Ord form', orderId: `ORD${orderId}` })
|
||
await raise(user.token, { complaintBody: 'Ord dash form', orderId: `ORD-${orderId}` })
|
||
|
||
const complaints = await getAll(user.token)
|
||
expect(complaints.find((c) => c.complaintBody === 'Ord form').orderId).toBe(orderId)
|
||
expect(complaints.find((c) => c.complaintBody === 'Ord dash form').orderId).toBe(orderId)
|
||
})
|
||
|
||
it('UCOMP-04: an unparseable order string is stored with a null orderId', async () => {
|
||
const user = await registerThrowawayUser()
|
||
await raise(user.token, { complaintBody: 'No parse', orderId: 'not-an-order' })
|
||
|
||
const complaints = await getAll(user.token)
|
||
const complaint = complaints.find((c) => c.complaintBody === 'No parse')
|
||
expect(complaint.orderId).toBeNull()
|
||
})
|
||
|
||
it('UCOMP-05: an empty complaint body is BAD_REQUEST', async () => {
|
||
const user = await registerThrowawayUser()
|
||
await expectTrpcError(
|
||
() => raise(user.token, { complaintBody: '' }),
|
||
'BAD_REQUEST'
|
||
)
|
||
})
|
||
|
||
it('UCOMP-06: the complaint body is stored trimmed', async () => {
|
||
const user = await registerThrowawayUser()
|
||
await raise(user.token, { complaintBody: ' padded body ' })
|
||
|
||
const complaints = await getAll(user.token)
|
||
expect(complaints[0].complaintBody).toBe('padded body')
|
||
})
|
||
|
||
it('UCOMP-07 (current): imageUrls are accepted but not returned by getAll', async () => {
|
||
// The handler persists `images`, but `getUserComplaints` selects only
|
||
// id/body/response/isResolved/createdAt/orderId — the URLs never come back
|
||
// (and are not signed on this read path).
|
||
const user = await registerThrowawayUser()
|
||
const res: any = await raise(user.token, {
|
||
complaintBody: 'With image',
|
||
imageUrls: ['https://example.com/complaint-a.png'],
|
||
})
|
||
expect(res.success).toBe(true)
|
||
|
||
const complaints = await getAll(user.token)
|
||
expect(complaints[0]).toEqual(
|
||
expect.objectContaining({ complaintBody: 'With image' })
|
||
)
|
||
expect('images' in complaints[0]).toBe(false)
|
||
})
|
||
|
||
it('UCOMP-08: getAll returns only the caller’s complaints', async () => {
|
||
const user = await registerThrowawayUser()
|
||
await raise(user.token, { complaintBody: 'Mine one' })
|
||
await raise(user.token, { complaintBody: 'Mine two' })
|
||
|
||
const complaints = await getAll(user.token)
|
||
expect(complaints.map((c) => c.complaintBody).sort()).toEqual(['Mine one', 'Mine two'])
|
||
})
|
||
|
||
it('UCOMP-09: another user cannot see the complaint', async () => {
|
||
const owner = await registerThrowawayUser()
|
||
const other = await registerThrowawayUser()
|
||
await raise(owner.token, { complaintBody: 'Private complaint' })
|
||
|
||
const ownerComplaints = await getAll(owner.token)
|
||
expect(ownerComplaints.some((c) => c.complaintBody === 'Private complaint')).toBe(true)
|
||
|
||
const otherComplaints = await getAll(other.token)
|
||
expect(otherComplaints.some((c) => c.complaintBody === 'Private complaint')).toBe(false)
|
||
})
|
||
|
||
it("UCOMP-10 (current): a complaint may reference another user's order id", async () => {
|
||
// Known gap (catalogue §13.9): `raise` never checks that orderId belongs to
|
||
// the caller, so a foreign order id is accepted and linked.
|
||
const { orderId } = await orderFixture()
|
||
const stranger = await registerThrowawayUser()
|
||
|
||
const res: any = await raise(stranger.token, {
|
||
complaintBody: 'Foreign order',
|
||
orderId: String(orderId),
|
||
})
|
||
expect(res.success).toBe(true)
|
||
|
||
const complaints = await getAll(stranger.token)
|
||
expect(complaints.find((c) => c.complaintBody === 'Foreign order').orderId).toBe(orderId)
|
||
})
|
||
|
||
it('UCOMP-11: no token is 401', async () => {
|
||
await expectTrpcError(
|
||
() => apiClient().user.complaint.getAll.query(),
|
||
'UNAUTHORIZED'
|
||
)
|
||
await expectTrpcError(
|
||
() => apiClient().user.complaint.raise.mutate({ complaintBody: 'nope' }),
|
||
'UNAUTHORIZED'
|
||
)
|
||
})
|
||
})
|