|
| 1 | +import { HttpService } from "@nestjs/axios"; |
| 2 | +import { |
| 3 | + BadRequestException, |
| 4 | + ServiceUnavailableException, |
| 5 | +} from "@nestjs/common"; |
| 6 | +import { ConfigService } from "@nestjs/config"; |
| 7 | +import { of, throwError } from "rxjs"; |
| 8 | +import { PaymentStatus } from "../../interfaces/payment-processor.interface"; |
| 9 | +import { GrantfoxAdapter } from "./grantfox.adapter"; |
| 10 | + |
| 11 | +const API_URL = "https://api.grantfox.example"; |
| 12 | +const API_KEY = "secret-key"; |
| 13 | + |
| 14 | +function makeConfig(overrides: Record<string, string> = {}): ConfigService { |
| 15 | + const env: Record<string, string> = { |
| 16 | + GRANTFOX_API_URL: API_URL, |
| 17 | + GRANTFOX_API_KEY: API_KEY, |
| 18 | + ...overrides, |
| 19 | + }; |
| 20 | + return { |
| 21 | + get: jest.fn((key: string, def?: unknown) => env[key] ?? def), |
| 22 | + } as unknown as ConfigService; |
| 23 | +} |
| 24 | + |
| 25 | +describe("GrantfoxAdapter", () => { |
| 26 | + let http: { post: jest.Mock; get: jest.Mock }; |
| 27 | + let adapter: GrantfoxAdapter; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + http = { post: jest.fn(), get: jest.fn() }; |
| 31 | + adapter = new GrantfoxAdapter(http as unknown as HttpService, makeConfig()); |
| 32 | + }); |
| 33 | + |
| 34 | + it("advertises its identity", () => { |
| 35 | + expect(adapter.name).toBe("grantfox"); |
| 36 | + expect(adapter.displayName).toBe("Grantfox"); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("createPayment", () => { |
| 40 | + it("POSTs to /payments with auth + idempotency headers and maps the response", async () => { |
| 41 | + http.post.mockReturnValue( |
| 42 | + of({ data: { id: "gf_123", status: "processing" } }), |
| 43 | + ); |
| 44 | + |
| 45 | + const created = await adapter.createPayment({ |
| 46 | + amount: "25.50", |
| 47 | + currency: "USD", |
| 48 | + destination: "acct_1", |
| 49 | + idempotencyKey: "idem-1", |
| 50 | + }); |
| 51 | + |
| 52 | + expect(http.post).toHaveBeenCalledTimes(1); |
| 53 | + const [url, body, config] = http.post.mock.calls[0]; |
| 54 | + expect(url).toBe(`${API_URL}/payments`); |
| 55 | + expect(body).toMatchObject({ amount: "25.50", currency: "USD" }); |
| 56 | + expect(config.headers.Authorization).toBe(`Bearer ${API_KEY}`); |
| 57 | + expect(config.headers["Idempotency-Key"]).toBe("idem-1"); |
| 58 | + |
| 59 | + expect(created.paymentId).toBe("gf_123"); |
| 60 | + expect(created.status).toBe(PaymentStatus.PROCESSING); |
| 61 | + }); |
| 62 | + |
| 63 | + it("throws ServiceUnavailable when the API URL is not configured", async () => { |
| 64 | + const unconfigured = new GrantfoxAdapter( |
| 65 | + http as unknown as HttpService, |
| 66 | + makeConfig({ GRANTFOX_API_URL: "" }), |
| 67 | + ); |
| 68 | + |
| 69 | + await expect( |
| 70 | + unconfigured.createPayment({ |
| 71 | + amount: "1", |
| 72 | + currency: "USD", |
| 73 | + destination: "acct_1", |
| 74 | + idempotencyKey: "idem-1", |
| 75 | + }), |
| 76 | + ).rejects.toBeInstanceOf(ServiceUnavailableException); |
| 77 | + expect(http.post).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("getStatus", () => { |
| 82 | + it("GETs /payments/:id and maps status + hash", async () => { |
| 83 | + http.get.mockReturnValue( |
| 84 | + of({ |
| 85 | + data: { |
| 86 | + id: "gf_123", |
| 87 | + status: "confirmed", |
| 88 | + transactionHash: "0xabc", |
| 89 | + amount: "25.50", |
| 90 | + }, |
| 91 | + }), |
| 92 | + ); |
| 93 | + |
| 94 | + const status = await adapter.getStatus("gf_123"); |
| 95 | + |
| 96 | + expect(http.get).toHaveBeenCalledWith( |
| 97 | + `${API_URL}/payments/gf_123`, |
| 98 | + expect.objectContaining({ |
| 99 | + headers: expect.objectContaining({ |
| 100 | + Authorization: `Bearer ${API_KEY}`, |
| 101 | + }), |
| 102 | + }), |
| 103 | + ); |
| 104 | + expect(status.status).toBe(PaymentStatus.CONFIRMED); |
| 105 | + expect(status.transactionHash).toBe("0xabc"); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("refund", () => { |
| 110 | + it("POSTs a full refund and maps REFUNDED", async () => { |
| 111 | + http.post.mockReturnValue( |
| 112 | + of({ data: { refundId: "rf_1", status: "refunded", amount: "25.50" } }), |
| 113 | + ); |
| 114 | + |
| 115 | + const result = await adapter.refund({ |
| 116 | + paymentId: "gf_123", |
| 117 | + idempotencyKey: "r-1", |
| 118 | + }); |
| 119 | + |
| 120 | + const [url, body] = http.post.mock.calls[0]; |
| 121 | + expect(url).toBe(`${API_URL}/payments/gf_123/refund`); |
| 122 | + expect(body).not.toHaveProperty("amount"); |
| 123 | + expect(result.status).toBe(PaymentStatus.REFUNDED); |
| 124 | + expect(result.refundId).toBe("rf_1"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("forwards a partial amount in the refund body", async () => { |
| 128 | + http.post.mockReturnValue( |
| 129 | + of({ |
| 130 | + data: { refundId: "rf_2", status: "partially_refunded", amount: "5" }, |
| 131 | + }), |
| 132 | + ); |
| 133 | + |
| 134 | + const result = await adapter.refund({ |
| 135 | + paymentId: "gf_123", |
| 136 | + amount: "5", |
| 137 | + idempotencyKey: "r-2", |
| 138 | + }); |
| 139 | + |
| 140 | + const [, body] = http.post.mock.calls[0]; |
| 141 | + expect(body).toMatchObject({ amount: "5" }); |
| 142 | + expect(result.status).toBe(PaymentStatus.PARTIALLY_REFUNDED); |
| 143 | + expect(result.refundedAmount).toBe("5"); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe("error mapping", () => { |
| 148 | + it("maps a 4xx upstream error to BadRequest", async () => { |
| 149 | + http.post.mockReturnValue( |
| 150 | + throwError(() => ({ |
| 151 | + response: { status: 422, data: { message: "invalid destination" } }, |
| 152 | + message: "Request failed with status code 422", |
| 153 | + })), |
| 154 | + ); |
| 155 | + |
| 156 | + await expect( |
| 157 | + adapter.createPayment({ |
| 158 | + amount: "1", |
| 159 | + currency: "USD", |
| 160 | + destination: "bad", |
| 161 | + idempotencyKey: "idem-1", |
| 162 | + }), |
| 163 | + ).rejects.toBeInstanceOf(BadRequestException); |
| 164 | + }); |
| 165 | + |
| 166 | + it("maps a 5xx upstream error to ServiceUnavailable", async () => { |
| 167 | + http.get.mockReturnValue( |
| 168 | + throwError(() => ({ |
| 169 | + response: { status: 503, data: {} }, |
| 170 | + message: "Service Unavailable", |
| 171 | + })), |
| 172 | + ); |
| 173 | + |
| 174 | + await expect(adapter.getStatus("gf_123")).rejects.toBeInstanceOf( |
| 175 | + ServiceUnavailableException, |
| 176 | + ); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments