import { afterEach, expect, it, vi } from "vitest"; import { HttpError, json } from "../api"; afterEach(() => vi.unstubAllGlobals()); it("keeps the server's error message and status", async () => { vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ statusCode: 502, error: "Bad Gateway", message: "SSH authentication failed: check the user and the saved password or key" }), { status: 502, headers: { "Content-Type": "application/json" } }))); const error = (await json("/x").catch((e: HttpError) => e)) as HttpError; expect(error).toBeInstanceOf(HttpError); expect(error.status).toBe(502); expect(error.message).toBe("SSH authentication failed: check the user and the saved password or key"); }); it("falls back to the status when the body is not JSON", async () => { vi.stubGlobal("fetch", vi.fn(async () => new Response("", { status: 500 }))); expect(((await json("/x").catch((e: HttpError) => e)) as HttpError).message).toBe("Request failed (500)"); }); it("does not surface messages of unexpected server errors", async () => { vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ statusCode: 500, message: "EACCES: permission denied, open '/var/lib/shellbook/x'" }), { status: 500 }))); expect(((await json("/x").catch((e: HttpError) => e)) as HttpError).message).toBe("Request failed (500)"); });