admin/e2e/global-setup.ts
alisaza e1eaf5eff5 feat: initial ghabilee-admin backoffice app
Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js
app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
2026-09-05 13:12:59 +03:30

80 lines
2.0 KiB
TypeScript

import { createServer, type Server, type ServerResponse } from 'node:http'
import {
E2E_MOCK_API_HOST,
E2E_MOCK_API_PORT,
e2eBlogArticleDetail,
e2eBlogArticleSlug,
e2eBlogArticleSummary,
} from './fixtures/blogApiMock'
import { matchConsumerE2eMockGet } from './fixtures/consumerEventApi'
declare global {
var __ghabileeE2eMockApiServer: Server | undefined
}
function writeJson(res: ServerResponse, status: number, body: unknown) {
res.writeHead(status, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(body))
}
export default async function globalSetup() {
const server = createServer((req, res) => {
const url = new URL(req.url ?? '/', `http://${E2E_MOCK_API_HOST}:${E2E_MOCK_API_PORT}`)
const { pathname } = url
if (req.method !== 'GET') {
writeJson(res, 405, { success: false, message: 'Method not allowed' })
return
}
if (pathname === '/api/v1/blog-articles') {
writeJson(res, 200, { success: true, data: [e2eBlogArticleSummary] })
return
}
if (pathname === `/api/v1/blog-articles/${e2eBlogArticleSlug}`) {
writeJson(res, 200, { success: true, data: e2eBlogArticleDetail })
return
}
if (pathname === `/api/v1/blog-articles/${e2eBlogArticleSlug}/related`) {
writeJson(res, 200, { success: true, data: [] })
return
}
const consumerMock = matchConsumerE2eMockGet(pathname)
if (consumerMock) {
writeJson(res, consumerMock.status, consumerMock.body)
return
}
writeJson(res, 404, { success: false, message: 'Not found' })
})
await new Promise<void>((resolve, reject) => {
server.once('error', (error: NodeJS.ErrnoException) => {
if (error.code === 'EADDRINUSE') {
// Full `pnpm test:e2e` already runs the Nest API on :3000 — use its blog seed data.
resolve()
return
}
reject(error)
})
server.listen(E2E_MOCK_API_PORT, E2E_MOCK_API_HOST, () => {
resolve()
})
})
if (server.listening) {
globalThis.__ghabileeE2eMockApiServer = server
}
}