|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | + |
| 3 | +const mockGetAuthContext = vi.fn(); |
| 4 | +vi.mock("@/lib/auth/get-user", () => ({ |
| 5 | + getAuthContext: (...args: any[]) => mockGetAuthContext(...args), |
| 6 | +})); |
| 7 | + |
| 8 | +const mockCreateServiceClient = vi.fn(); |
| 9 | +vi.mock("@/lib/supabase/service", () => ({ |
| 10 | + createServiceClient: () => mockCreateServiceClient(), |
| 11 | +})); |
| 12 | + |
| 13 | +const mockGetUserLnWallet = vi.fn(); |
| 14 | +const mockCreateInvoice = vi.fn(); |
| 15 | +const mockGetLnBalance = vi.fn(); |
| 16 | +vi.mock("@/lib/lightning/wallet-utils", () => ({ |
| 17 | + getUserLnWallet: (...args: any[]) => mockGetUserLnWallet(...args), |
| 18 | + createInvoice: (...args: any[]) => mockCreateInvoice(...args), |
| 19 | + getLnBalance: (...args: any[]) => mockGetLnBalance(...args), |
| 20 | +})); |
| 21 | + |
| 22 | +import { POST } from "./route"; |
| 23 | + |
| 24 | +function makeRequest(body: any) { |
| 25 | + return new Request("http://localhost/api/wallet/deposit", { |
| 26 | + method: "POST", |
| 27 | + headers: { "Content-Type": "application/json" }, |
| 28 | + body: JSON.stringify(body), |
| 29 | + }) as any; |
| 30 | +} |
| 31 | + |
| 32 | +function makeRawRequest(body: string) { |
| 33 | + return new Request("http://localhost/api/wallet/deposit", { |
| 34 | + method: "POST", |
| 35 | + headers: { "Content-Type": "application/json" }, |
| 36 | + body, |
| 37 | + }) as any; |
| 38 | +} |
| 39 | + |
| 40 | +describe("POST /api/wallet/deposit", () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks(); |
| 43 | + mockGetAuthContext.mockResolvedValue({ user: { id: "user-12345678" } }); |
| 44 | + mockGetUserLnWallet.mockResolvedValue({ |
| 45 | + invoice_key: "invoice-key-123", |
| 46 | + }); |
| 47 | + mockCreateInvoice.mockResolvedValue({ |
| 48 | + payment_request: "lnbc123", |
| 49 | + payment_hash: "hash-123", |
| 50 | + }); |
| 51 | + mockGetLnBalance.mockResolvedValue(250); |
| 52 | + }); |
| 53 | + |
| 54 | + it("rejects unauthenticated requests", async () => { |
| 55 | + mockGetAuthContext.mockResolvedValue(null); |
| 56 | + |
| 57 | + const res = await POST(makeRequest({ amount_sats: 100 })); |
| 58 | + |
| 59 | + expect(res.status).toBe(401); |
| 60 | + expect(mockCreateServiceClient).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("rejects malformed JSON without creating an invoice", async () => { |
| 64 | + const res = await POST(makeRawRequest("{not valid json")); |
| 65 | + |
| 66 | + expect(res.status).toBe(400); |
| 67 | + const data = await res.json(); |
| 68 | + expect(data.error).toBe("Invalid request body"); |
| 69 | + expect(mockCreateServiceClient).not.toHaveBeenCalled(); |
| 70 | + expect(mockCreateInvoice).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("rejects non-object JSON bodies without creating an invoice", async () => { |
| 74 | + const res = await POST(makeRequest(null)); |
| 75 | + |
| 76 | + expect(res.status).toBe(400); |
| 77 | + const data = await res.json(); |
| 78 | + expect(data.error).toBe("Invalid request body"); |
| 79 | + expect(mockCreateServiceClient).not.toHaveBeenCalled(); |
| 80 | + expect(mockCreateInvoice).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("rejects non-integer amounts without wallet lookup", async () => { |
| 84 | + const res = await POST(makeRequest({ amount_sats: 100.5 })); |
| 85 | + |
| 86 | + expect(res.status).toBe(400); |
| 87 | + const data = await res.json(); |
| 88 | + expect(data.error).toBe("Invalid amount (1-1,000,000 sats)"); |
| 89 | + expect(mockCreateServiceClient).not.toHaveBeenCalled(); |
| 90 | + expect(mockGetUserLnWallet).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | + |
| 93 | + it("creates an invoice for a valid deposit amount", async () => { |
| 94 | + const walletSelectSingle = vi.fn().mockResolvedValue({ |
| 95 | + data: { id: "wallet-1", balance_sats: 250 }, |
| 96 | + }); |
| 97 | + const walletEq = vi.fn().mockReturnValue({ single: walletSelectSingle }); |
| 98 | + const walletSelect = vi.fn().mockReturnValue({ eq: walletEq }); |
| 99 | + const transactionInsert = vi.fn().mockResolvedValue({ data: null }); |
| 100 | + const admin = { |
| 101 | + from: vi.fn((table: string) => { |
| 102 | + if (table === "wallets") { |
| 103 | + return { |
| 104 | + select: walletSelect, |
| 105 | + insert: vi.fn().mockResolvedValue({ data: null }), |
| 106 | + }; |
| 107 | + } |
| 108 | + if (table === "wallet_transactions") { |
| 109 | + return { insert: transactionInsert }; |
| 110 | + } |
| 111 | + return {}; |
| 112 | + }), |
| 113 | + }; |
| 114 | + mockCreateServiceClient.mockReturnValue(admin); |
| 115 | + |
| 116 | + const res = await POST(makeRequest({ amount_sats: 100 })); |
| 117 | + |
| 118 | + expect(res.status).toBe(200); |
| 119 | + const data = await res.json(); |
| 120 | + expect(data).toEqual({ |
| 121 | + ok: true, |
| 122 | + payment_request: "lnbc123", |
| 123 | + payment_hash: "hash-123", |
| 124 | + amount_sats: 100, |
| 125 | + }); |
| 126 | + expect(mockCreateInvoice).toHaveBeenCalledWith( |
| 127 | + "invoice-key-123", |
| 128 | + 100, |
| 129 | + "ugig.net deposit (user-123)", |
| 130 | + ); |
| 131 | + expect(transactionInsert).toHaveBeenCalledWith({ |
| 132 | + user_id: "user-12345678", |
| 133 | + type: "deposit", |
| 134 | + amount_sats: 100, |
| 135 | + balance_after: 250, |
| 136 | + bolt11: "lnbc123", |
| 137 | + payment_hash: "hash-123", |
| 138 | + status: "pending", |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments