APP-Backend / tests /server-binding.test.mjs
Luca448's picture
Savestate: Fix Hugging Face health-check binding
d1f95aa
Raw History Blame Contribute Delete
1.47 kB
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { networkInterfaces } from 'node:os';
import test from 'node:test';
function firstExternalIpv4() {
for (const addresses of Object.values(networkInterfaces())) {
for (const address of addresses ?? []) {
if (address.family === 'IPv4' && !address.internal) return address.address;
}
}
throw new Error('No external IPv4 address is available for the binding test');
}
async function waitForResponse(url, child) {
const deadline = Date.now() + 15_000;
let lastError;
while (Date.now() < deadline) {
if (child.exitCode !== null) {
throw new Error(`Server exited before becoming reachable (code ${child.exitCode})`);
}
try {
return await fetch(url);
} catch (error) {
lastError = error;
await new Promise((resolve) => setTimeout(resolve, 100));
}
}
throw lastError ?? new Error('Server did not become reachable');
}
test('serves the Space app through a non-loopback container interface', async (t) => {
const port = 20_000 + Math.floor(Math.random() * 20_000);
const child = spawn(process.execPath, ['server.js'], {
cwd: new URL('..', import.meta.url),
env: { ...process.env, PORT: String(port) },
stdio: 'ignore',
});
t.after(() => child.kill());
const response = await waitForResponse(`http://${firstExternalIpv4()}:${port}/`, child);
assert.equal(response.status, 200);
});