46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
/**
|
|
* Jest global setup for the live API suite.
|
|
*
|
|
* Runs once per `bun run test:api` invocation, before any suite:
|
|
* - logs in as the seeded staff admin,
|
|
* - calls the guarded `POST /api/test/order-trackers/clear` route so the KV
|
|
* `first_order:<userId>` tracker does not leak across runs (local SQLite
|
|
* reuses user ids after `remove-test-data`).
|
|
*/
|
|
import path from 'node:path'
|
|
import dotenv from 'dotenv'
|
|
|
|
export default async function globalSetup(): Promise<void> {
|
|
dotenv.config({ path: path.resolve(__dirname, '.env') })
|
|
|
|
const base = (process.env.API_BASE_URL ?? 'http://localhost:8787').replace(/\/+$/, '')
|
|
const name = process.env.TEST_STAFF_NAME
|
|
const password = process.env.TEST_STAFF_PASSWORD
|
|
if (!name || !password) {
|
|
throw new Error('[api-tests] globalSetup: TEST_STAFF_NAME/TEST_STAFF_PASSWORD missing (run tests/seed-local.ts)')
|
|
}
|
|
|
|
const loginRes = await fetch(`${base}/api/trpc/admin.staffUser.login?batch=1`, {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify({ 0: { name, password } }),
|
|
})
|
|
const loginBody: any = await loginRes.json()
|
|
const item = Array.isArray(loginBody) ? loginBody[0] : loginBody
|
|
const token = item?.result?.data?.token
|
|
if (!token) {
|
|
throw new Error(
|
|
`[api-tests] globalSetup: staff login failed (${loginRes.status}). Is the backend up and seeded?`
|
|
)
|
|
}
|
|
|
|
const clearRes = await fetch(`${base}/api/test/order-trackers/clear`, {
|
|
method: 'POST',
|
|
headers: { authorization: `Bearer ${token}` },
|
|
})
|
|
if (!clearRes.ok) {
|
|
throw new Error(
|
|
`[api-tests] globalSetup: tracker clear failed (${clearRes.status}). Is ENABLE_TEST_ROUTES=true in .dev.vars?`
|
|
)
|
|
}
|
|
}
|