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 { 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((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() }) })