Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

4.3 KB
Raw Blame
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.

import { afterEach, describe, expect, it, vi } from "vitest";
import {
  PANEL_FORBIDDEN_MESSAGE,
  PanelTimeoutError,
  PanelUnauthorizedError,
  panelFetch
} from "./session-storage";

/** A fetch that never settles until its signal aborts. */
function hang() {
  vi.stubGlobal(
    "fetch",
    vi.fn(
      (_input: string, init: RequestInit) =>
        new Promise<Response>((_resolve, reject) => {
          init.signal?.addEventListener("abort", () =>
            reject(new DOMException("Aborted", "AbortError"))
          );
        })
    )
  );
}

function answer(body: string, status: number, contentType?: string) {
  vi.stubGlobal(
    "fetch",
    vi.fn(
      async () =>
        new Response(body, {
          status,
          headers: contentType ? { "Content-Type": contentType } : {}
        })
    )
  );
}

afterEach(() => {
  vi.unstubAllGlobals();
});

describe("panelFetch", () => {
  it("sends the token as a bearer header", async () => {
    const fetchMock = vi.fn(async () => new Response("{}", { status: 200 }));
    vi.stubGlobal("fetch", fetchMock);
    await panelFetch("cps_token", "/api/x");
    const init = (fetchMock.mock.calls[0] as unknown[])[1] as RequestInit;
    expect(new Headers(init.headers).get("Authorization")).toBe(
      "Bearer cps_token"
    );
  });

  it("throws on a 401 so the panel offers sign-in again", async () => {
    answer("Unauthorized", 401);
    await expect(panelFetch("t", "/api/x")).rejects.toBeInstanceOf(
      PanelUnauthorizedError
    );
  });

  it("turns a plain-text 403 into readable JSON", async () => {
    answer("Forbidden", 403, "text/plain");
    const response = await panelFetch("t", "/api/x");
    expect(response.status).toBe(403);
    expect(await response.json()).toEqual({
      error: PANEL_FORBIDDEN_MESSAGE,
      status: 403
    });
  });

  it("uses the permission wording even when a 403 is JSON", async () => {
    answer(JSON.stringify({ error: "nope" }), 403, "application/json");
    expect((await (await panelFetch("t", "/api/x")).json()).error).toBe(
      PANEL_FORBIDDEN_MESSAGE
    );
  });

  it("turns an HTML error page into a sentence, not a parser message", async () => {
    answer("<!doctype html><h1>502 Bad Gateway</h1>", 502, "text/html");
    const response = await panelFetch("t", "/api/x");
    expect(response.status).toBe(502);
    const body = await response.json();
    expect(body.error).toMatch(/unexpected response \(HTTP 502\)/);
  });

  it("passes a JSON error through untouched, so route messages survive", async () => {
    answer(
      JSON.stringify({ error: "Onshape is still building this BOM." }),
      504,
      "application/json; charset=utf-8"
    );
    expect(await (await panelFetch("t", "/api/x")).json()).toEqual({
      error: "Onshape is still building this BOM."
    });
  });

  it("never touches a successful response", async () => {
    answer("not json at all", 200, "text/plain");
    expect(await (await panelFetch("t", "/api/x")).text()).toBe(
      "not json at all"
    );
  });

  it("gives up on a request that never settles", async () => {
    hang();
    await expect(panelFetch("t", "/api/x", {}, 10)).rejects.toBeInstanceOf(
      PanelTimeoutError
    );
  });

  it("reports a caller's own cancellation as itself, not as a timeout", async () => {
    hang();
    const caller = new AbortController();
    const pending = panelFetch("t", "/api/x", { signal: caller.signal });
    caller.abort();
    const error = await pending.catch((e: unknown) => e);
    expect(error).toBeInstanceOf(DOMException);
    expect(error).not.toBeInstanceOf(PanelTimeoutError);
  });

  it("waits indefinitely when the timeout is null", async () => {
    vi.useFakeTimers();
    try {
      hang();
      let settled = false;
      const pending = panelFetch("t", "/api/x", {}, null).finally(() => {
        settled = true;
      });
      pending.catch(() => {
        // never settles; nothing to handle
      });
      await vi.advanceTimersByTimeAsync(10 * 60_000);
      expect(settled).toBe(false);
    } finally {
      vi.useRealTimers();
    }
  });
});