#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
// @vitest-environment node
import { beforeEach, describe, expect, it, vi } from "vitest";
/**
* What this pins is the lane the request lands on. `self_hosted` is what selects the Keygen
* licence lane, and a client-supplied email is what lets it bill the wrong customer, so both
* absences are the contract — not incidental.
*/
const { invoke, getAccessToken } = vi.hoisted(() => ({
invoke: vi.fn(),
getAccessToken: vi.fn(),
}));
vi.mock("@editor/services/supabaseClient", () => ({
isSupabaseConfigured: true,
supabase: { functions: { invoke } },
}));
vi.mock("@editor/auth/session", () => ({ getAccessToken }));
import {
createServerPlanCheckoutSession,
verifyTeamCheckout,
} from "@editor/services/serverPlanCheckout";
const plan = {
lookupKey: "selfhosted:team:yearly",
serverQuantity: 2,
installationId: "inst-1",
uiMode: "embedded",
} as const;
beforeEach(() => {
invoke.mockReset();
getAccessToken.mockReset().mockResolvedValue("native-token");
});
describe("createServerPlanCheckoutSession", () => {
it("preserves the server's subscription failure reason", async () => {
invoke.mockResolvedValue({
data: null,
error: {
message: "Edge Function returned a non-2xx status code",
context: new Response(
JSON.stringify({
error: "Resolve the pending payment in Manage billing first.",
}),
{ status: 400 },
),
},
});
await expect(createServerPlanCheckoutSession(plan)).rejects.toThrow(
"Resolve the pending payment in Manage billing first.",
);
});
it("keeps Enterprise on the licence-issuing path", async () => {
getAccessToken.mockResolvedValue(null);
invoke.mockResolvedValue({
data: { clientSecret: "cs", sessionId: "cs_enterprise" },
error: null,
});
await createServerPlanCheckoutSession({
...plan,
requiresSeats: true,
currentLicenseKey: "key-1",
});
expect(invoke.mock.calls[0][1].body).toMatchObject({
self_hosted: true,
requires_seats: true,
current_license_key: "key-1",
});
});
it("buys as the signed-in account: no self_hosted lane, no client-supplied email", async () => {
invoke.mockResolvedValue({
data: { clientSecret: "cs_1", sessionId: "cs_sess" },
error: null,
});
const session = await createServerPlanCheckoutSession(plan);
expect(session).toEqual({
clientSecret: "cs_1",
url: null,
sessionId: "cs_sess",
});
const [fn, options] = invoke.mock.calls[0];
expect(fn).toBe("create-checkout");
expect(options.body).not.toHaveProperty("self_hosted");
expect(options.body).not.toHaveProperty("email");
expect(options.body).toMatchObject({
lookup_key: "selfhosted:team:yearly",
server_quantity: 2,
installation_id: "inst-1",
ui_mode: "embedded",
requires_seats: false,
seat_count: 1,
});
});
it("passes the hosted-mode return URLs through", async () => {
invoke.mockResolvedValue({
data: { url: "https://checkout.stripe.com/x", sessionId: "s" },
error: null,
});
const session = await createServerPlanCheckoutSession({
...plan,
uiMode: "hosted",
successUrl: "https://host/ok",
cancelUrl: "https://host/no",
});
expect(session.url).toBe("https://checkout.stripe.com/x");
expect(session.clientSecret).toBeNull();
expect(invoke.mock.calls[0][1].body).toMatchObject({
success_url: "https://host/ok",
cancel_url: "https://host/no",
});
});
it("refuses before invoking when no Stirling account is signed in", async () => {
getAccessToken.mockResolvedValue(null);
await expect(createServerPlanCheckoutSession(plan)).rejects.toThrow(
/Sign in to the Stirling account/,
);
expect(invoke).not.toHaveBeenCalled();
});
it("throws the edge function's own error", async () => {
invoke.mockResolvedValue({ data: { error: "Unauthorized" }, error: null });
await expect(createServerPlanCheckoutSession(plan)).rejects.toThrow(
/Unauthorized/,
);
});
it("throws when the session carries neither a secret nor a URL", async () => {
invoke.mockResolvedValue({ data: { sessionId: "s" }, error: null });
await expect(createServerPlanCheckoutSession(plan)).rejects.toThrow(
/neither/,
);
});
});
it("attaches the platform token to checkout and completion without a browser session", async () => {
invoke
.mockResolvedValueOnce({ data: { clientSecret: "cs" } })
.mockResolvedValueOnce({ data: { ready: true, licensedUsers: 300 } });
await createServerPlanCheckoutSession(plan);
expect(await verifyTeamCheckout("cs", 3)).toBe(300);
for (const [, options] of invoke.mock.calls) {
expect(options.headers).toEqual({ Authorization: "Bearer native-token" });
}
});