feat(FN-2322): centralize loopback integration test gating

- Add a shared createLoopbackIntegrationTest helper that probes 127.0.0.1 binding once and caches the result
- Use the helper in webhook, websocket, and static asset integration tests to replace duplicated loopback detection logic
- Improve skip diagnostics by including a consistent skip reason and integration scope in skipped test names
This commit is contained in:
Fusion
2026-04-23 10:09:26 -07:00
committed by gsxdsm
parent 29056b412d
commit c7f7ac722a
4 changed files with 43 additions and 39 deletions

View File

@@ -0,0 +1,36 @@
import http from "node:http";
import { it } from "vitest";
type IntegrationTestCase = (name: string, fn: () => unknown | Promise<unknown>, timeout?: number) => ReturnType<typeof it>;
const LOOPBACK_SKIP_REASON = "loopback binding to 127.0.0.1 is unavailable in this environment";
let loopbackBindingAvailablePromise: Promise<boolean> | null = null;
async function detectLoopbackBinding(): Promise<boolean> {
return await new Promise((resolve) => {
const server = http.createServer();
server.once("error", () => resolve(false));
server.listen(0, "127.0.0.1", () => {
server.close(() => resolve(true));
});
});
}
async function isLoopbackBindingAvailable(): Promise<boolean> {
if (!loopbackBindingAvailablePromise) {
loopbackBindingAvailablePromise = detectLoopbackBinding();
}
return await loopbackBindingAvailablePromise;
}
export async function createLoopbackIntegrationTest(scope: string): Promise<IntegrationTestCase> {
const loopbackBindingAvailable = await isLoopbackBindingAvailable();
if (loopbackBindingAvailable) {
return (name, fn, timeout) => it(name, fn, timeout);
}
return (name, fn, timeout) => it.skip(`${name} (skipped: ${LOOPBACK_SKIP_REASON}; scope: ${scope})`, fn, timeout);
}