Files
hermes-ui/tests/server/profiles-bulk-routes.test.ts
toor 421ffa0714
Build / build (push) Canceled after 0s
Playwright / e2e (push) Canceled after 0s
增加一键重启所有网关按钮
2026-10-21 17:21:11 +08:00

66 lines
2.3 KiB
TypeScript

import Koa from 'koa'
import bodyParser from '@koa/bodyparser'
import { createServer, type Server as HttpServer } from 'http'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const controllerMocks = vi.hoisted(() => ({
restartAllGateways: vi.fn(),
restartAllProfiles: vi.fn(),
}))
vi.mock('../../packages/server/src/controllers/hermes/profiles', async (importOriginal) => ({
...(await importOriginal() as any),
restartAllGateways: controllerMocks.restartAllGateways,
restartAllProfiles: controllerMocks.restartAllProfiles,
}))
import { profileRoutes } from '../../packages/server/src/routes/hermes/profiles'
function listen(server: HttpServer): Promise<string> {
return new Promise((resolve) => server.listen(0, '127.0.0.1', () => {
const addr = server.address()
if (!addr || typeof addr === 'string') throw new Error('missing address')
resolve(`http://127.0.0.1:${addr.port}`)
}))
}
describe('profile bulk restart routes', () => {
let httpServer: HttpServer
let baseUrl: string
beforeEach(async () => {
vi.clearAllMocks()
controllerMocks.restartAllGateways.mockImplementation((ctx: any) => {
ctx.body = { success: true }
})
controllerMocks.restartAllProfiles.mockImplementation((ctx: any) => {
ctx.body = { success: true }
})
const app = new Koa()
app.use(bodyParser())
app.use(profileRoutes.routes())
app.use(profileRoutes.allowedMethods())
httpServer = createServer(app.callback())
baseUrl = await listen(httpServer)
})
afterEach(async () => {
await new Promise<void>((resolve) => httpServer.close(() => resolve()))
})
it('routes the gateway bulk restart to its own handler', async () => {
const res = await fetch(`${baseUrl}/api/hermes/profiles/gateways/restart-all`, { method: 'POST' })
expect(res.status).toBe(200)
expect(controllerMocks.restartAllGateways).toHaveBeenCalledTimes(1)
expect(controllerMocks.restartAllProfiles).not.toHaveBeenCalled()
})
it('routes the profile bulk restart to its own handler', async () => {
const res = await fetch(`${baseUrl}/api/hermes/profiles/restart-all`, { method: 'POST' })
expect(res.status).toBe(200)
expect(controllerMocks.restartAllProfiles).toHaveBeenCalledTimes(1)
expect(controllerMocks.restartAllGateways).not.toHaveBeenCalled()
})
})