Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

2.8 KB
Raw Blame
// @vitest-environment node
import { beforeEach, describe, expect, test, vi } from "vitest";

const mocks = vi.hoisted(() => ({
  mode: null as "saas" | "selfhosted" | "local" | null,
  serverConfig: null as { url: string } | null,
  authenticated: true,
}));

vi.mock("@editor/services/connectionModeService", () => ({
  connectionModeService: {
    getCurrentConfig: async () => ({ local_processing_only: false }),
    getCurrentMode: async () => mocks.mode,
    getServerConfig: async () => mocks.serverConfig,
  },
}));
vi.mock("@editor/services/authService", () => ({
  authService: {
    isAuthenticated: async () => mocks.authenticated,
    awaitRefreshIfInProgress: async () => {},
  },
}));
vi.mock("@editor/services/tauriBackendService", () => ({
  tauriBackendService: {},
}));
vi.mock("@editor/services/endpointAvailabilityService", () => ({
  endpointAvailabilityService: {},
}));
vi.mock("@editor/services/selfHostedServerMonitor", () => ({
  selfHostedServerMonitor: {},
}));
vi.mock("@editor/i18n", () => ({
  default: { t: (_key: string, fallback: string) => fallback },
}));
vi.mock("@editor/constants/connection", () => ({
  STIRLING_SAAS_BACKEND_API_URL: "https://api.saas.test/",
}));

import { getAiBaseUrl } from "@editor/services/aiBaseUrl";

describe("getAiBaseUrl", () => {
  beforeEach(() => {
    mocks.mode = null;
    mocks.serverConfig = null;
    mocks.authenticated = true;
  });

  test("SaaS mode resolves the cloud base, trailing slash stripped", async () => {
    mocks.mode = "saas";
    await expect(getAiBaseUrl()).resolves.toBe("https://api.saas.test");
  });

  test("self-hosted resolves the connected server, not the cloud", async () => {
    mocks.mode = "selfhosted";
    mocks.serverConfig = { url: "https://pdf.example.internal/" };
    await expect(getAiBaseUrl()).resolves.toBe("https://pdf.example.internal");
  });

  test("self-hosted stays absolute so the raw orchestrate fetch cannot resolve against the webview origin", async () => {
    mocks.mode = "selfhosted";
    mocks.serverConfig = { url: "https://pdf.example.internal" };
    await expect(getAiBaseUrl()).resolves.toMatch(/^https:\/\//);
  });

  test("self-hosted with no server recorded rejects the request", async () => {
    mocks.mode = "selfhosted";
    await expect(getAiBaseUrl()).rejects.toThrow();
  });

  test("local mode has no engine to reach", async () => {
    mocks.mode = "local";
    await expect(getAiBaseUrl()).rejects.toThrow();
  });

  test("unresolved mode fails closed rather than guessing the cloud", async () => {
    await expect(getAiBaseUrl()).rejects.toThrow();
  });

  test("rejects a configured server without authentication", async () => {
    mocks.mode = "saas";
    mocks.authenticated = false;
    await expect(getAiBaseUrl()).rejects.toThrow("Sign in");
  });
});