mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-17 16:11:33 +00:00
* fix(mattermost): add timeout to REST client requests * fixup: preserve Mattermost DM retry timeout * test(mattermost): reuse hanging server helper * test(mattermost): satisfy timeout lint --------- Co-authored-by: Peter Steinberger <steipete@gmail.com>
26 lines
945 B
TypeScript
26 lines
945 B
TypeScript
// Plugin SDK test helper for temporary local HTTP servers.
|
|
import { createServer, type RequestListener } from "node:http";
|
|
import type { AddressInfo } from "node:net";
|
|
|
|
/** Run an ephemeral loopback HTTP server for the duration of an async test callback. */
|
|
export async function withServer(handler: RequestListener, fn: (baseUrl: string) => Promise<void>) {
|
|
const server = createServer(handler);
|
|
await new Promise<void>((resolve) => {
|
|
server.listen(0, "127.0.0.1", () => resolve());
|
|
});
|
|
const address = server.address() as AddressInfo | null;
|
|
if (!address) {
|
|
throw new Error("missing server address");
|
|
}
|
|
try {
|
|
await fn(`http://127.0.0.1:${address.port}`);
|
|
} finally {
|
|
const closed = new Promise<void>((resolve) => {
|
|
server.close(() => resolve());
|
|
});
|
|
// Hanging-response tests must still release active sockets when their assertion fails.
|
|
server.closeAllConnections();
|
|
await closed;
|
|
}
|
|
}
|