fix(cli): harden publish retry behavior

This commit is contained in:
Miguel Ángel
2026-08-07 15:13:04 +00:00
parent f2d6ce3245
commit dd629697d3
2 changed files with 112 additions and 21 deletions
+75 -6
View File
@@ -146,9 +146,13 @@ function directFetch(completeData?: Record<string, unknown>) {
.mockResolvedValueOnce(publishedResponse(completeData));
}
function networkFailure(code: string, message: string): TypeError {
function networkFailure(
code: string,
message: string,
metadata: Record<string, unknown> = {},
): TypeError {
return new TypeError("fetch failed", {
cause: Object.assign(new Error(message), { code }),
cause: Object.assign(new Error(message), { code, ...metadata }),
});
}
@@ -760,6 +764,37 @@ describe("publishProjectArchive", () => {
}
});
it("waits briefly before retrying a transport failure", async () => {
vi.useFakeTimers();
const dir = makeProjectDir();
const fetchMock = vi
.fn()
.mockResolvedValueOnce(uploadResponse())
.mockRejectedValueOnce(networkFailure("EAI_AGAIN", "temporary DNS failure"))
.mockResolvedValueOnce(new Response(null, { status: 200 }))
.mockResolvedValueOnce(publishedResponse());
vi.stubGlobal("fetch", fetchMock);
try {
writeFileSync(join(dir, "index.html"), "<html></html>", "utf-8");
const publish = publishProjectArchive(dir);
await vi.advanceTimersByTimeAsync(0);
expect(fetchMock).toHaveBeenCalledTimes(2);
await vi.advanceTimersByTimeAsync(199);
expect(fetchMock).toHaveBeenCalledTimes(2);
await vi.advanceTimersByTimeAsync(1);
await expect(publish).resolves.toMatchObject({ projectId: "hfp_123" });
expect(fetchMock).toHaveBeenCalledTimes(4);
} finally {
await vi.runAllTimersAsync();
vi.useRealTimers();
rmSync(dir, { recursive: true, force: true });
}
});
it("reports the upload stage and transport cause after the retry is exhausted", async () => {
const dir = makeProjectDir();
const signedUrl =
@@ -767,8 +802,18 @@ describe("publishProjectArchive", () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(uploadResponse({ upload_url: signedUrl }))
.mockRejectedValueOnce(networkFailure("EAI_AGAIN", "getaddrinfo EAI_AGAIN s3.example.com"))
.mockRejectedValueOnce(networkFailure("EAI_AGAIN", "getaddrinfo EAI_AGAIN s3.example.com"));
.mockRejectedValueOnce(
networkFailure("EAI_AGAIN", "getaddrinfo EAI_AGAIN s3.example.com", {
errno: -3001,
syscall: "getaddrinfo",
}),
)
.mockRejectedValueOnce(
networkFailure("EAI_AGAIN", "getaddrinfo EAI_AGAIN s3.example.com", {
errno: -3001,
syscall: "getaddrinfo",
}),
);
vi.stubGlobal("fetch", fetchMock);
try {
@@ -776,7 +821,7 @@ describe("publishProjectArchive", () => {
const promise = publishProjectArchive(dir);
await expect(promise).rejects.toThrow(
"Failed to upload project archive after 2 attempts: fetch failed (EAI_AGAIN: getaddrinfo EAI_AGAIN s3.example.com)",
"Failed to upload project archive after 2 attempts: fetch failed (EAI_AGAIN, syscall=getaddrinfo, errno=-3001: getaddrinfo EAI_AGAIN s3.example.com)",
);
await expect(promise).rejects.not.toThrow("do-not-print");
expect(fetchMock).toHaveBeenCalledTimes(3);
@@ -799,7 +844,7 @@ describe("publishProjectArchive", () => {
writeFileSync(join(dir, "index.html"), "<html></html>", "utf-8");
await expect(publishProjectArchive(dir)).rejects.toThrow(
"Failed to prepare project upload after 2 attempts: fetch failed (ENETUNREACH: network is unreachable). Proxy variables are set, but Node fetch proxy support is disabled; retry with NODE_USE_ENV_PROXY=1",
"Failed to prepare project upload after 2 attempts: fetch failed (ENETUNREACH: network is unreachable). Proxy variables are set but ignored by Node fetch; if this network requires them, retry with NODE_USE_ENV_PROXY=1",
);
expect(fetchMock).toHaveBeenCalledTimes(2);
} finally {
@@ -835,6 +880,30 @@ describe("publishProjectArchive", () => {
}
});
it("does not retry a request that reached its timeout", async () => {
const dir = makeProjectDir();
const fetchMock = vi
.fn()
.mockRejectedValueOnce(
new DOMException("The operation was aborted due to timeout", "TimeoutError"),
)
.mockResolvedValueOnce(uploadResponse())
.mockResolvedValueOnce(new Response(null, { status: 200 }))
.mockResolvedValueOnce(publishedResponse());
vi.stubGlobal("fetch", fetchMock);
try {
writeFileSync(join(dir, "index.html"), "<html></html>", "utf-8");
await expect(publishProjectArchive(dir)).rejects.toThrow(
"Failed to prepare project upload: The operation was aborted due to timeout",
);
expect(fetchMock).toHaveBeenCalledTimes(1);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("reports the finalize stage and transport cause", async () => {
const dir = makeProjectDir();
const fetchMock = vi