Files
buzz-sheet/lib/commission/discord.test.ts
T
gunshiz 0022625edb
CI / Verify (push) Successful in 3m14s
CI / Build immutable images and deploy (push) Successful in 4m0s
feat : big update
2026-10-01 18:37:15 +07:00

58 lines
2.4 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("server-only", () => ({}));
const { notifyPaidTicketDiscord } = await import("./discord");
const ticketId = "11111111-1111-4111-8111-111111111111";
describe("paid commission Discord notification", () => {
const fetchMock = vi.fn();
const originalFetch = globalThis.fetch;
const originalWebhookUrl = process.env.COMMISSION_DISCORD_WEBHOOK_URL;
const originalAuthUrl = process.env.BETTER_AUTH_URL;
beforeEach(() => {
process.env.COMMISSION_DISCORD_WEBHOOK_URL = "https://discord.example.test/webhook";
process.env.BETTER_AUTH_URL = "https://guide.sudloh.com";
globalThis.fetch = fetchMock as unknown as typeof fetch;
fetchMock.mockReset().mockResolvedValue(new Response(null, { status: 204 }));
});
afterEach(() => {
if (originalWebhookUrl === undefined) delete process.env.COMMISSION_DISCORD_WEBHOOK_URL;
else process.env.COMMISSION_DISCORD_WEBHOOK_URL = originalWebhookUrl;
if (originalAuthUrl === undefined) delete process.env.BETTER_AUTH_URL;
else process.env.BETTER_AUTH_URL = originalAuthUrl;
globalThis.fetch = originalFetch;
vi.restoreAllMocks();
});
it("sends an embed linking to the admin ticket chat", async () => {
await notifyPaidTicketDiscord(ticketId, 140);
expect(fetchMock).toHaveBeenCalledOnce();
const [url, options] = fetchMock.mock.calls[0] as [string, RequestInit];
expect(url).toBe("https://discord.example.test/webhook");
expect(options.method).toBe("POST");
const payload = JSON.parse(options.body as string);
expect(payload.allowed_mentions).toEqual({ parse: [] });
expect(payload.embeds[0]).toMatchObject({
url: `https://guide.sudloh.com/admin/commission/${ticketId}`,
description: expect.stringContaining("฿140"),
});
});
it("skips the request when no webhook is configured", async () => {
delete process.env.COMMISSION_DISCORD_WEBHOOK_URL;
await notifyPaidTicketDiscord(ticketId, 140);
expect(fetchMock).not.toHaveBeenCalled();
});
it("does not turn a paid ticket into an error when Discord fails", async () => {
vi.spyOn(console, "error").mockImplementation(() => undefined);
fetchMock.mockRejectedValueOnce(new Error("network unavailable"));
await expect(notifyPaidTicketDiscord(ticketId, 140)).resolves.toBeUndefined();
expect(console.error).toHaveBeenCalledWith("Commission Discord notification failed");
});
});