Files
buzz-sheet/lib/commission/slip-provider.test.ts
T
gunshiz 7ba513c2bb
CI / Verify (push) Successful in 1m56s
CI / Build immutable images and deploy (push) Successful in 3m26s
feat : add slipok and switcher to switch between slip2go and slipok
2026-10-10 00:31:12 +07:00

32 lines
1.6 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
const { limit } = vi.hoisted(() => ({ limit: vi.fn() }));
vi.mock("server-only", () => ({}));
vi.mock("@/db", () => ({ getDb: () => ({ select: () => ({ from: () => ({ where: () => ({ limit }) }) }) }) }));
import { getCommissionSlipProvider, slipProviderConfigured } from "./slip-provider";
afterEach(() => { vi.unstubAllEnvs(); vi.clearAllMocks(); });
describe("commission provider configuration", () => {
it("reads fresh settings after switching", async () => {
limit.mockResolvedValueOnce([{ slipProvider: "slip2go" }]).mockResolvedValueOnce([{ slipProvider: "slipok" }]);
expect(await getCommissionSlipProvider()).toBe("slip2go");
expect(await getCommissionSlipProvider()).toBe("slipok");
expect(limit).toHaveBeenCalledTimes(2);
});
it.each([{ rows: [] }, { rows: [{ slipProvider: "invalid" }] }])("rejects missing or invalid settings", async ({ rows }) => {
limit.mockResolvedValueOnce(rows);
await expect(getCommissionSlipProvider()).rejects.toMatchObject({ status: 503 });
});
it("requires an HTTPS Slip2Go endpoint and key", () => {
vi.stubEnv("SLIP2GO_API_SECRET", "secret");
vi.stubEnv("SLIP2GO_VERIFY_URL", "http://slip.test");
expect(slipProviderConfigured("slip2go")).toBe(false);
vi.stubEnv("SLIP2GO_VERIFY_URL", "invalid");
expect(slipProviderConfigured("slip2go")).toBe(false);
vi.stubEnv("SLIP2GO_VERIFY_URL", "https://slip.test");
expect(slipProviderConfigured("slip2go")).toBe(true);
vi.stubEnv("SLIP2GO_API_SECRET", "");
expect(slipProviderConfigured("slip2go")).toBe(false);
});
});