test(extensions): cover bounded marketplace details

This commit is contained in:
wiiiii123
2026-07-11 11:19:59 +07:00
parent cf6d299f11
commit e032f3705f
2 changed files with 28 additions and 4 deletions
+22
View File
@@ -28,6 +28,28 @@ describe("formatMarketplaceHttpError", () => {
).toBe("Marketplace request failed (HTTP 400): Invalid search query");
});
it("uses a JSON message when an error field is absent", () => {
expect(
formatMarketplaceHttpError({
status: 409,
contentType: "application/json",
body: JSON.stringify({ message: "Extension version already exists" }),
}),
).toBe("Marketplace request failed (HTTP 409): Extension version already exists");
});
it("bounds long JSON details and marks truncation without splitting Unicode", () => {
const detail = `🚀${"x".repeat(200)}`;
const message = formatMarketplaceHttpError({
status: 400,
contentType: "application/problem+json",
body: JSON.stringify({ error: detail }),
});
expect(message).toBe(`Marketplace request failed (HTTP 400): 🚀${"x".repeat(198)}…`);
expect(Array.from(message.split(": ")[1])).toHaveLength(200);
});
it("does not expose non-JSON response bodies", () => {
expect(
formatMarketplaceHttpError({
+6 -4
View File
@@ -25,10 +25,12 @@ export function formatMarketplaceHttpError({
const { error, message } = payload as { error?: unknown; message?: unknown };
const value = typeof error === "string" ? error : message;
if (typeof value === "string" && value.trim()) {
detail = value
.trim()
.replace(/\s+/g, " ")
.slice(0, MAX_MARKETPLACE_ERROR_DETAIL_LENGTH);
const normalized = value.trim().replace(/\s+/g, " ");
const codePoints = Array.from(normalized);
detail =
codePoints.length > MAX_MARKETPLACE_ERROR_DETAIL_LENGTH
? `${codePoints.slice(0, MAX_MARKETPLACE_ERROR_DETAIL_LENGTH - 1).join("")}…`
: normalized;
}
}
} catch {