import { describe, expect, test } from "bun:test";
import { OpenGeniApiError } from "@opengeni/sdk";
import { ApiError } from "./app-api";
import { bootstrapErrorPresentation } from "./bootstrap-error";
describe("bootstrap error presentation", () => {
test("turns the maintenance API payload into calm maintenance copy", () => {
const presentation = bootstrapErrorPresentation(
new ApiError(503, JSON.stringify({ error: "maintenance" })),
"client_configuration",
);
expect(presentation).toEqual({
title: "Under maintenance",
description: "We'll be back shortly. Try again in a moment.",
});
});
test("recognizes typed nested maintenance responses", () => {
const presentation = bootstrapErrorPresentation(
new OpenGeniApiError(503, JSON.stringify({ error: { code: "maintenance_mode" } })),
"workspace_access",
);
expect(presentation.title).toBe("Under maintenance");
});
test("does not expose proxy HTML or API framing", () => {
const presentation = bootstrapErrorPresentation(
new ApiError(502, "
upstream failed
"),
"client_configuration",
);
expect(presentation).toEqual({
title: "Temporarily unavailable",
description: "The service could not finish loading. Try again shortly.",
});
expect(JSON.stringify(presentation)).not.toContain("");
expect(JSON.stringify(presentation)).not.toContain("API 502");
});
test("explains a successful non-JSON config response without showing parser output", () => {
const presentation = bootstrapErrorPresentation(
new SyntaxError("Unexpected token '<', \" {
expect(
bootstrapErrorPresentation(new TypeError("Failed to fetch"), "client_configuration"),
).toEqual({
title: "Can't connect",
description: "The app could not reach its server. Check your connection and try again.",
});
expect(
bootstrapErrorPresentation(
new ApiError(500, "internal configuration failure"),
"client_configuration",
),
).toEqual({
title: "Couldn't start",
description:
"The client configuration could not be loaded. Check the deployment settings and server logs, then try again.",
});
});
test("keeps workspace-access failures concise", () => {
const presentation = bootstrapErrorPresentation(
new ApiError(403, "
forbidden by proxy
"),
"workspace_access",
);
expect(presentation).toEqual({
title: "Workspace access unavailable",
description: "Your workspace access couldn't be loaded. Try again.",
});
expect(JSON.stringify(presentation)).not.toContain("forbidden by proxy");
});
});