freshyo/apps/backend/tests/api/admin-catalog.test.ts
2026-09-26 17:50:33 +05:30

114 lines
3.5 KiB
TypeScript

/**
* Admin read/update surface: store, product, groups, coupons, vendor snippets
* and complaints — all with the staff token.
*/
import { apiClient } from '../helpers/api'
import { createStoreWithSku, type StoreWithSku } from '../helpers/fixtures'
import { staffSession } from '../helpers/sessions'
describe('admin catalog surface', () => {
let admin: ReturnType<typeof apiClient>
let store: StoreWithSku
let staffId = 0
beforeAll(async () => {
const staff = await staffSession()
staffId = staff.staffId
admin = apiClient(staff.token)
store = await createStoreWithSku()
})
it('getStoreById returns the created store', async () => {
const res = await admin.admin.store.getStoreById.query({ id: store.storeId })
expect(res.store?.id).toBe(store.storeId)
})
it('updateStore renames the store', async () => {
const name = `${store.storeName} (renamed)`
await admin.admin.store.updateStore.mutate({
id: store.storeId,
name,
owner: staffId,
description: 'Renamed by API tests',
})
const after = await admin.admin.store.getStoreById.query({ id: store.storeId })
expect(after.store.name).toBe(name)
})
it('getProductById returns the product with its SKU', async () => {
const res = (await admin.admin.product.getProductById.query({
id: store.productId,
})) as {
product?: { id: number; skus?: { id: number }[] }
id?: number
skus?: { id: number }[]
}
const product = res.product ?? res
expect(product.id).toBe(store.productId)
expect(product.skus!.some((s) => s.id === store.skuId)).toBe(true)
})
it('updateProduct renames the product', async () => {
const name = `${store.productName} (renamed)`
await admin.admin.product.updateProduct.mutate({
id: store.productId,
name,
storeId: store.storeId,
productType: 'item',
skus: [
{
id: store.skuId,
price: 199,
// The backend requires a feature literally named "quantity".
features: [{ featureName: 'quantity', featureValue: '1pc' }],
},
],
})
// Read back through the list — a known shape.
const list = await admin.admin.product.getProducts.query()
const product = list.products.find((p) => p.id === store.productId)
expect(product?.name).toBe(name)
})
it('getGroups lists product groups', async () => {
const res = await admin.admin.product.getGroups.query()
expect(Array.isArray(res.groups)).toBe(true)
})
it('createGroup adds a group that getGroups returns', async () => {
const groupName = `API Test Group ${Date.now()}`
await admin.admin.product.createGroup.mutate({
group_name: groupName,
description: 'Created by API tests',
product_ids: [store.skuId],
})
const res = await admin.admin.product.getGroups.query()
const names = res.groups.map(
(g: { groupName?: string; group_name?: string }) => g.groupName ?? g.group_name
)
expect(names).toContain(groupName)
})
it('coupon.getAll returns a coupons array', async () => {
const res = await admin.admin.coupon.getAll.query({ limit: 5 })
expect(Array.isArray(res.coupons)).toBe(true)
})
it('vendorSnippets.getAll returns an array', async () => {
const res = await admin.admin.vendorSnippets.getAll.query()
expect(Array.isArray(res)).toBe(true)
})
it('complaint.getAll returns a complaints array', async () => {
const res = await admin.admin.complaint.getAll.query({ limit: 10 })
expect(Array.isArray(res.complaints)).toBe(true)
})
})