import { type AddressInfo } from "node:net"; import { afterEach } from "vitest"; import { createNextcloudTalkWebhookServer } from "./monitor.js"; import type { NextcloudTalkWebhookServerOptions } from "./types.js"; export type WebhookHarness = { webhookUrl: string; stop: () => Promise; }; const cleanupFns: Array<() => Promise> = []; afterEach(async () => { while (cleanupFns.length > 0) { const cleanup = cleanupFns.pop(); if (cleanup) { await cleanup(); } } }); export type StartWebhookServerParams = Omit< NextcloudTalkWebhookServerOptions, "port" | "host" | "path" | "secret" > & { path: string; secret?: string; host?: string; port?: number; }; export async function startWebhookServer( params: StartWebhookServerParams, ): Promise { const host = params.host ?? "127.0.0.1"; const port = params.port ?? 0; const secret = params.secret ?? "nextcloud-secret"; const { server, start } = createNextcloudTalkWebhookServer({ ...params, port, host, secret, }); await start(); const address = server.address() as AddressInfo | null; if (!address) { throw new Error("missing server address"); } const harness: WebhookHarness = { webhookUrl: `http://${host}:${address.port}${params.path}`, stop: () => new Promise((resolve) => { server.close(() => resolve()); }), }; cleanupFns.push(harness.stop); return harness; }