203 lines
5.8 KiB
TypeScript
203 lines
5.8 KiB
TypeScript
/**
|
|
* Fixtures created through the API (there is no data seed for these):
|
|
* staff login -> store -> product + SKU -> future slot
|
|
* user login -> address
|
|
*
|
|
* Everything is left behind afterwards (no cleanup), so names carry a unique
|
|
* suffix to keep reruns valid.
|
|
*/
|
|
|
|
import { apiClient, uniqueSuffix } from './api'
|
|
import { staffSession, userSession } from './sessions'
|
|
|
|
export interface StoreWithSku {
|
|
storeId: number
|
|
storeName: string
|
|
productId: number
|
|
productName: string
|
|
skuId: number
|
|
}
|
|
|
|
export interface CreatedSlot {
|
|
slotId: number
|
|
deliveryTime: string
|
|
freezeTime: string
|
|
skuIds: number[]
|
|
}
|
|
|
|
/** Create a store and a one-SKU product inside it; returns the ids. */
|
|
export async function createStoreWithSku(
|
|
options: { price?: number } = {}
|
|
): Promise<StoreWithSku> {
|
|
const staff = await staffSession()
|
|
const admin = apiClient(staff.token)
|
|
const suffix = uniqueSuffix()
|
|
|
|
const storeName = `API Test Store ${suffix}`
|
|
const storeRes = await admin.admin.store.createStore.mutate({
|
|
name: storeName,
|
|
description: 'Created by apps/backend/tests',
|
|
owner: staff.staffId,
|
|
})
|
|
|
|
const storeId = (storeRes as { store?: { id?: number } })?.store?.id
|
|
if (!storeId) {
|
|
throw new Error(
|
|
`[api-tests] admin.store.createStore returned no store id: ${JSON.stringify(storeRes)}`
|
|
)
|
|
}
|
|
|
|
const price = options.price ?? 199
|
|
const productName = `API Test Product ${suffix}`
|
|
await admin.admin.product.createProduct.mutate({
|
|
name: productName,
|
|
shortDescription: 'API test product',
|
|
productType: 'item',
|
|
storeId,
|
|
skus: [
|
|
{
|
|
name: '1pc',
|
|
price,
|
|
marketPrice: price + 50,
|
|
isOutOfStock: false,
|
|
features: [{ featureName: 'quantity', featureValue: '1pc' }],
|
|
},
|
|
],
|
|
})
|
|
|
|
// Read the product back by name — the create response shape is not relied on.
|
|
const listed = await admin.admin.product.getProducts.query()
|
|
const product = listed.products.find((p) => p.name === productName)
|
|
const skuId = product?.skus?.[0]?.id
|
|
const productId = product?.id
|
|
if (!productId || !skuId) {
|
|
throw new Error(
|
|
`[api-tests] created product "${productName}" not found via admin.product.getProducts`
|
|
)
|
|
}
|
|
|
|
return { storeId, storeName, productId, productName, skuId }
|
|
}
|
|
|
|
/** Register a throwaway user (token returned immediately) for isolation tests. */
|
|
export async function registerThrowawayUser(): Promise<{ token: string; userId: number }> {
|
|
const suffix = uniqueSuffix()
|
|
const mobile = `9${String(Math.floor(Math.random() * 1e9)).padStart(9, '0')}`
|
|
|
|
const res = await apiClient().user.auth.register.mutate({
|
|
name: `API Test User ${suffix}`,
|
|
email: `api-test-${suffix}@example.com`,
|
|
mobile,
|
|
password: 'TestPass123',
|
|
})
|
|
|
|
const token = res?.data?.token
|
|
const userId = res?.data?.user?.id
|
|
if (!token || !userId) {
|
|
throw new Error(
|
|
`[api-tests] user.auth.register returned no token/user id: ${JSON.stringify(res)}`
|
|
)
|
|
}
|
|
|
|
return { token, userId: Number(userId) }
|
|
}
|
|
|
|
/**
|
|
* Create a delivery slot in the future (the public slots cache only includes
|
|
* future, non-full slots) and return its id, resolved through the public list.
|
|
*/
|
|
export async function createFutureSlot(
|
|
skuIds: number[],
|
|
options: { deliveryInHours?: number; freezeInHours?: number } = {}
|
|
): Promise<CreatedSlot> {
|
|
const staff = await staffSession()
|
|
const admin = apiClient(staff.token)
|
|
|
|
const deliveryTime = new Date(
|
|
Date.now() + (options.deliveryInHours ?? 3) * 3600_000
|
|
).toISOString()
|
|
const freezeTime = new Date(
|
|
Date.now() + (options.freezeInHours ?? 2) * 3600_000
|
|
).toISOString()
|
|
|
|
await admin.admin.slots.createSlot.mutate({
|
|
deliveryTime,
|
|
freezeTime,
|
|
isActive: true,
|
|
skuIds,
|
|
})
|
|
|
|
const listed = await apiClient().user.slots.getSlots.query()
|
|
const slot = listed.slots.find(
|
|
(s) => new Date(s.deliveryTime).toISOString() === deliveryTime
|
|
)
|
|
if (!slot) {
|
|
throw new Error(
|
|
`[api-tests] created slot (delivery ${deliveryTime}) was not returned by user.slots.getSlots`
|
|
)
|
|
}
|
|
|
|
return { slotId: slot.id, deliveryTime, freezeTime, skuIds }
|
|
}
|
|
|
|
/** Return an address for the supplied user, creating one if they have none. */
|
|
export async function ensureUserAddress(): Promise<{ addressId: number; created: boolean }> {
|
|
const user = await userSession()
|
|
const client = apiClient(user.token)
|
|
|
|
const existing = await client.user.address.getUserAddresses.query()
|
|
const first = existing?.data?.[0]
|
|
if (first) return { addressId: first.id, created: false }
|
|
|
|
const created = await client.user.address.createAddress.mutate({
|
|
regionId: 1,
|
|
name: 'API Test Address',
|
|
phone: '9999999999',
|
|
addressLine1: '1 API Test Street',
|
|
city: 'Mahabubnagar',
|
|
state: 'Telangana',
|
|
isDefault: true,
|
|
})
|
|
|
|
const addressId = created?.data?.id
|
|
if (!addressId) {
|
|
throw new Error(
|
|
`[api-tests] user.address.createAddress returned no address id: ${JSON.stringify(created)}`
|
|
)
|
|
}
|
|
|
|
return { addressId, created: true }
|
|
}
|
|
|
|
/**
|
|
* Register a throwaway user and give it a default address, so suites that need
|
|
* an isolated account (coupon scoping, first-order-only, per-user limits) can
|
|
* place orders without touching the shared user.
|
|
*/
|
|
export async function registerUserWithAddress(): Promise<{
|
|
token: string
|
|
userId: number
|
|
addressId: number
|
|
}> {
|
|
const { token, userId } = await registerThrowawayUser()
|
|
const client = apiClient(token)
|
|
|
|
const created = await client.user.address.createAddress.mutate({
|
|
regionId: 1,
|
|
name: 'API Test Address',
|
|
phone: '9999999999',
|
|
addressLine1: '1 API Test Street',
|
|
city: 'Mahabubnagar',
|
|
state: 'Telangana',
|
|
isDefault: true,
|
|
})
|
|
|
|
const addressId = created?.data?.id
|
|
if (!addressId) {
|
|
throw new Error(
|
|
`[api-tests] registerUserWithAddress: no address id in ${JSON.stringify(created)}`
|
|
)
|
|
}
|
|
|
|
return { token, userId, addressId }
|
|
}
|