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

164 lines
6.4 KiB
TypeScript

/**
* Harness smoke suite — proves the live plumbing before the per-router suites.
*
* Covers HARNESS-01..04, HARNESS-08, HTTP-01, COMMON-46..52 (read paths),
* CACHE-01/25/27 (shape only) and one end-to-end create → order → read flow.
*/
import { expectTrpcError, rest } from '../helpers/api'
import { anonClient, staffSession, userSession, superadminSession } from '../helpers/sessions'
import { getEssentialConsts, scaffoldCache, versionOf } from '../helpers/cache'
import {
createCatalogOrderFixture,
createFutureSlot,
createProduct,
createStore,
createAddress,
registerThrowawayUser,
} from '../helpers/fixtures'
jest.setTimeout(180_000)
const anon = anonClient()
describe('harness', () => {
it('HARNESS-01: staff login returns a token and a staff identity', async () => {
const staff = await staffSession()
expect(staff.token).toEqual(expect.any(String))
expect(staff.token.length).toBeGreaterThan(20)
expect(staff.staffId).toBeGreaterThan(0)
expect(staff.regionId).toBeGreaterThan(0)
})
it('HARNESS-02: user login returns a token and a user id', async () => {
const user = await userSession()
expect(user.token).toEqual(expect.any(String))
expect(user.userId).toBeGreaterThan(0)
})
it('HARNESS-03: superadmin login returns a token and identity', async () => {
const superadmin = await superadminSession()
expect(superadmin.token).toEqual(expect.any(String))
expect(superadmin.superadminId).toBeGreaterThan(0)
expect(superadmin.username).toEqual(expect.any(String))
})
it('HARNESS-04: bearer token authenticates the matching principal', async () => {
const { staffSession: staff } = await import('../helpers/sessions')
const session = await staff()
const { apiClient } = await import('../helpers/api')
const me = await apiClient(session.token).admin.staffUser.me.query()
expect(Number(me.id)).toBe(session.staffId)
})
it('HARNESS-08: garbage token is treated as anonymous (401 on a protected call)', async () => {
const { apiClient } = await import('../helpers/api')
await expectTrpcError(
() => apiClient('not-a-real-token').user.order.getOrders.query({ page: 1, pageSize: 1 }),
'UNAUTHORIZED'
)
})
it('HTTP-01: GET /api/health returns OK with timestamp/uptime/message', async () => {
const res = await rest('/api/health')
expect(res.status).toBe(200)
const body: any = await res.json()
expect(body.status).toBe('OK')
expect(body.timestamp).toEqual(expect.any(String))
expect(body.uptime).toEqual(expect.any(Number))
expect(body.message).toEqual(expect.any(String))
})
it('COMMON-46/47: common.healthCheck is public and truthy', async () => {
const result = await anon.common.healthCheck.query()
expect(result).toBeTruthy()
})
it('COMMON-49/52: common.listRegions is public and returns region summaries', async () => {
const regions = (await anon.common.listRegions.query()) as any[]
expect(Array.isArray(regions)).toBe(true)
expect(regions.length).toBeGreaterThan(0)
const region = regions[0]
expect(region).toEqual(
expect.objectContaining({
id: expect.any(Number),
name: expect.any(String),
})
)
})
it('CACHE-01: essentialConsts exposes identity, domain, cache key and versions', async () => {
const consts = await getEssentialConsts()
expect(consts.regionId).toBeGreaterThan(0)
expect(consts.boundId).toBeGreaterThan(0)
expect(consts.assetsDomain).toEqual(expect.any(String))
expect(consts.apiCacheKey).toEqual(expect.any(String))
expect(versionOf(consts, 'products')).toBeGreaterThanOrEqual(0)
expect(versionOf(consts, 'slots')).toBeGreaterThanOrEqual(0)
expect(versionOf(consts, 'availability')).toBeGreaterThanOrEqual(0)
})
it('CACHE-25/26/27: products.json parses and exposes the product contract', async () => {
const doc: any = await scaffoldCache('products', { regionId: 1 })
expect(Array.isArray(doc.products)).toBe(true)
expect(doc.count).toBe(doc.products.length)
if (doc.products.length > 0) {
expect(doc.products[0]).toEqual(
expect.objectContaining({
id: expect.any(Number),
name: expect.any(String),
storeId: expect.anything(),
})
)
}
})
})
describe('catalogue → order golden smoke', () => {
it('creates a store, product, slot, user and address through the API', async () => {
const store = await createStore()
const product = await createProduct({ storeId: store.storeId, price: 349 })
const slot = await createFutureSlot([product.skuId])
const user = await registerThrowawayUser()
const address = await createAddress(user.token)
expect(store.storeId).toBeGreaterThan(0)
expect(product.productId).toBeGreaterThan(0)
expect(product.skuId).toBeGreaterThan(0)
expect(slot.slotId).toBeGreaterThan(0)
expect(user.userId).toBeGreaterThan(0)
expect(address.id).toBeGreaterThan(0)
expect(address.regionId).toBe(1)
})
it('places a COD order and reads it back through getOrders/getOrderById', async () => {
const fixture = await createCatalogOrderFixture({ price: 349 })
const { apiClient } = await import('../helpers/api')
const client = apiClient(fixture.user.token)
const placed = (await client.user.order.placeOrder.mutate({
selectedItems: [{ skuId: fixture.product.skuId, quantity: 1, slotId: fixture.slot.slotId }],
addressId: fixture.address.id,
regionId: 1,
})) as any
expect(placed.success).toBe(true)
expect(Array.isArray(placed.data)).toBe(true)
expect(placed.data.length).toBeGreaterThan(0)
const orderId = Number(placed.data[0].id)
expect(orderId).toBeGreaterThan(0)
const list: any = await client.user.order.getOrders.query({ page: 1, pageSize: 10 })
expect(list.data.some((order: any) => Number(order.id) === orderId)).toBe(true)
const detail: any = await client.user.order.getOrderById.query({ orderId: String(orderId) })
const detailId = Number(detail.id ?? detail.order?.id)
expect(detailId).toBe(orderId)
})
it('rejects anonymous protected reads across all three routers', async () => {
await expectTrpcError(() => anon.user.order.getOrders.query({ page: 1, pageSize: 1 }), 'UNAUTHORIZED')
await expectTrpcError(() => anon.admin.product.getProducts.query(), 'UNAUTHORIZED')
await expectTrpcError(() => anon.superadmin.regions.list.query(), 'UNAUTHORIZED')
})
})