60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const requireCommissionUser = vi.fn();
|
|
const limitRequest = vi.fn();
|
|
const validateCommissionCatalog = vi.fn();
|
|
const promptPayConfig = vi.fn();
|
|
const transaction = vi.fn();
|
|
const forUpdate = vi.fn();
|
|
const returning = vi.fn();
|
|
const insert = vi.fn();
|
|
|
|
vi.mock("@/lib/commission/server", () => ({ requireCommissionUser }));
|
|
vi.mock("@/lib/security/rate-limit", () => ({ limitRequest }));
|
|
vi.mock("@/lib/commission/catalog", () => ({ validateCommissionCatalog }));
|
|
vi.mock("@/lib/commission/payment", () => ({ promptPayConfig }));
|
|
vi.mock("@/db", () => ({ getDb: () => ({ transaction }) }));
|
|
|
|
const { POST } = await import("./route");
|
|
|
|
function request() {
|
|
return new Request("https://guide.sudloh.com/api/commission/checkouts", {
|
|
method: "POST", headers: { Origin: "https://guide.sudloh.com", "Content-Type": "application/json" },
|
|
body: JSON.stringify({ teams: [], weapons: [{ characterKey: "c", weaponKeys: ["w1", "w2"] }],
|
|
constellations: [] }),
|
|
});
|
|
}
|
|
|
|
describe("commission checkout availability", () => {
|
|
beforeEach(() => {
|
|
process.env.BETTER_AUTH_URL = "https://guide.sudloh.com";
|
|
vi.clearAllMocks();
|
|
requireCommissionUser.mockResolvedValue({ id: "customer-1" });
|
|
limitRequest.mockResolvedValue(undefined);
|
|
validateCommissionCatalog.mockResolvedValue(true);
|
|
promptPayConfig.mockReturnValue({ identifier: "test" });
|
|
transaction.mockImplementation((callback) => callback({
|
|
select: () => ({ from: () => ({ where: () => ({ for: forUpdate }) }) }),
|
|
insert,
|
|
}));
|
|
insert.mockReturnValue({ values: () => ({ returning }) });
|
|
returning.mockResolvedValue([{ id: "checkout-1" }]);
|
|
});
|
|
|
|
it("rejects a stale form submission after closure", async () => {
|
|
forUpdate.mockResolvedValue([{ isOpen: false }]);
|
|
const response = await POST(request());
|
|
expect(response.status).toBe(409);
|
|
expect(await response.json()).toEqual({ error: "commission-closed" });
|
|
expect(insert).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("creates a checkout while open", async () => {
|
|
forUpdate.mockResolvedValue([{ isOpen: true }]);
|
|
const response = await POST(request());
|
|
expect(response.status).toBe(201);
|
|
expect(await response.json()).toEqual({ checkoutId: "checkout-1" });
|
|
expect(insert).toHaveBeenCalledOnce();
|
|
});
|
|
});
|