import http from "node:http"; import type { AddressInfo } from "node:net"; import express, { type Router } from "express"; import { createMockCtx, type MockPluginContext, } from "@termix/plugin-sdk/testing"; import type { PluginManifest } from "@termix/plugin-sdk/manifest"; import type { PluginHostSummary } from "@termix/plugin-sdk/backend"; import manifestJson from "../../manifest.json"; export const manifest = manifestJson as unknown as PluginManifest; export interface TestServer { mock: MockPluginContext; request: ( method: string, path: string, options?: { user?: string; body?: unknown }, // eslint-disable-next-line @typescript-eslint/no-explicit-any ) => Promise<{ status: number; body: any }>; close: () => Promise; } export async function startServer( options: { permissions?: string[]; hosts?: PluginHostSummary[]; capabilities?: string[]; } = {}, ): Promise { let router: Router | null = null; const mock = createMockCtx({ pluginId: manifest.id, manifest, capabilities: options.capabilities ?? manifest.capabilities, router: () => (router = express.Router()), permissions: options.permissions ?? ["wake-on-lan.send"], hosts: options.hosts ?? [], actor: "user-1", }); const { activate } = await import("../../src/backend/index.js"); await activate(mock.ctx); const app = express(); app.use(express.json()); app.use((req, _res, next) => { mock.setActor(req.header("x-test-user") ?? undefined); next(); }); app.use((req, res, next) => router!(req, res, next)); const server = http.createServer(app); await new Promise((resolve) => server.listen(0, resolve)); const { port } = server.address() as AddressInfo; return { mock, async request(method, path, { user = "user-1", body: given } = {}) { const body = method === "GET" ? undefined : given; const response = await fetch(`http://127.0.0.1:${port}${path}`, { method, headers: { "x-test-user": user, ...(body !== undefined ? { "content-type": "application/json" } : {}), }, body: body !== undefined ? JSON.stringify(body) : undefined, }); const text = await response.text(); return { status: response.status, body: text ? JSON.parse(text) : null }; }, async close() { await new Promise((resolve) => server.close(() => resolve())); }, }; }