fix(cli): check and snapshot run where a sandbox refuses to listen on localhost (#5618)

This commit is contained in:
Miguel Ángel
2026-10-14 19:19:37 +00:00
committed by GitHub
parent b14a456471
commit 71595dd820
4 changed files with 266 additions and 7 deletions
@@ -2,7 +2,12 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { serveStaticProjectHtml, type StaticProjectServer } from "./staticProjectServer.js";
import { Server } from "node:http";
import {
inProcessAnswer,
serveStaticProjectHtml,
type StaticProjectServer,
} from "./staticProjectServer.js";
// `serveStaticProjectHtml` reaches these two studio-server helpers via a
// absolute module regardless of which file's relative specifier reaches it).
@@ -413,3 +418,53 @@ describe("serveStaticProjectHtml transparent media proxies", () => {
expect(mocks.resolveProxy).not.toHaveBeenCalled();
});
});
describe("serveStaticProjectHtml when a sandbox refuses listen()", () => {
const refuse = (code: string) =>
vi.spyOn(Server.prototype, "listen").mockImplementation(function (this: Server) {
process.nextTick(() =>
this.emit("error", Object.assign(new Error(`listen ${code}`), { code })),
);
return this;
});
afterEach(() => vi.restoreAllMocks());
it("answers the project in this process, Range included, with nothing listening", async () => {
const listen = refuse("EPERM");
dir = mkdtempSync(join(tmpdir(), "hf-static-"));
writeFileSync(join(dir, "tone.wav"), Buffer.from("0123456789", "utf-8"));
server = await serveStaticProjectHtml(dir, "<html><body>composition</body></html>");
expect(listen).toHaveBeenCalledOnce();
expect(server.port).toBe(0);
expect(inProcessAnswer(server.url)).toBe(server.answer);
const page = await server.answer!({ method: "GET", path: "/", headers: {} });
expect(page.status).toBe(200);
expect(page.body.toString()).toContain("composition");
const slice = await server.answer!({
method: "GET",
path: "/tone.wav",
headers: { range: "bytes=2-5" },
});
expect(slice.status).toBe(206);
expect(slice.headers["content-range"]).toBe("bytes 2-5/10");
expect(slice.body.toString()).toBe("2345");
expect(
(await server.answer!({ method: "GET", path: "/missing.png", headers: {} })).status,
).toBe(404);
const url = server.url;
await server.close();
server = undefined;
expect(inProcessAnswer(url)).toBeUndefined();
});
it("still fails on a bind error that is not a refusal", async () => {
refuse("EADDRINUSE");
dir = mkdtempSync(join(tmpdir(), "hf-static-"));
await expect(serveStaticProjectHtml(dir, "<html></html>")).rejects.toMatchObject({
code: "EADDRINUSE",
});
});
});