diff --git a/tests/rate_limit.test.ts b/tests/rate_limit.test.ts index 4d42aa2..90665b1 100644 --- a/tests/rate_limit.test.ts +++ b/tests/rate_limit.test.ts @@ -9,17 +9,14 @@ import { type RateLimitInfo, rateLimitUsagePct, } from "../mod.ts"; +import { createMockFetch as createLagoMockFetch } from "./utils.ts"; -// Simple fetch mock helper (replaces broken mock_fetch library) +// Route is irrelevant here since these tests never assert method/path, +// so any value satisfying LagoRoute works. function createMockFetch( - handler: ( - input: RequestInfo | URL, - init?: RequestInit, - ) => Response | Promise, + handler: (request: Request) => Response | Promise, ): typeof fetch { - return (input: RequestInfo | URL, init?: RequestInit) => { - return Promise.resolve(handler(input, init)); - }; + return createLagoMockFetch("GET@/api/v1/mock", handler).fetch; } Deno.test("LagoRateLimitError contains rate limit information", () => { diff --git a/tests/subscription.test.ts b/tests/subscription.test.ts index 4f8a586..2eb8c60 100644 --- a/tests/subscription.test.ts +++ b/tests/subscription.test.ts @@ -13,8 +13,9 @@ const subscriptionInput = { "external_id": "54321", "billing_time": "calendar", "subscription_at": "2022-08-08T00:00:00Z", + "purchase_order_number": "PO-123", }, -} satisfies SubscriptionCreateInput; +} as const satisfies SubscriptionCreateInput; const subscriptionResponse = { "subscription": { @@ -34,6 +35,7 @@ const subscriptionResponse = { "previous_plan_code": "previous_code", "next_plan_code": "next_code", "downgrade_plan_date": "2022-09-14T16:35:31Z", + "purchase_order_number": "PO-123", }, } satisfies Subscription; @@ -50,6 +52,7 @@ Deno.test("Successfully sent subscription responds with 2xx", async (t) => { inputParams: [subscriptionInput], responseObject: subscriptionResponse, status: 200, + expectedBody: subscriptionInput, }); }); @@ -76,6 +79,7 @@ Deno.test( inputParams: ["id", subscriptionInput], responseObject: subscriptionResponse, status: 200, + expectedBody: subscriptionInput, }); }, ); diff --git a/tests/utils.ts b/tests/utils.ts index 3e1b8d8..6138a13 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -66,6 +66,7 @@ export async function lagoTest< status, testType, urlParams, + expectedBody, }: { testType: "error" | "200"; t: Deno.TestContext; @@ -77,6 +78,7 @@ export async function lagoTest< >; status: number; urlParams?: Record; + expectedBody?: unknown; }, ) { const { fetch, getRequest, expectedMethod, expectedPath } = createMockFetch( @@ -90,7 +92,7 @@ export async function lagoTest< ); const client = Client("api_key", { customFetch: fetch }); - const assertRequest = () => { + const assertRequest = async () => { const request = getRequest(); if (!request) throw new Error("Expected a request to be sent"); @@ -104,6 +106,10 @@ export async function lagoTest< assertEquals(urlSearchParams.get(key), value); }); } + + if (expectedBody !== undefined) { + assertEquals(await request.json(), expectedBody); + } }; switch (testType) { @@ -125,7 +131,7 @@ export async function lagoTest< (lagoError as ApiErrorUnprocessableEntity).error, errorMessage, ); - assertRequest(); + await assertRequest(); } }); break; @@ -138,7 +144,7 @@ export async function lagoTest< ) as Response; assertEquals(response.status, 200); - assertRequest(); + await assertRequest(); }); break; diff --git a/tests/wallet.test.ts b/tests/wallet.test.ts index 4e0b86d..8ab4254 100644 --- a/tests/wallet.test.ts +++ b/tests/wallet.test.ts @@ -1,7 +1,7 @@ import type { Wallet, - WalletInput, - Wallets, + WalletCreateInput, + WalletsPaginated, WalletUpdateInput, } from "../mod.ts"; import { lagoTest, unprocessableErrorResponse } from "./utils.ts"; @@ -11,12 +11,23 @@ const walletInput = { "name": "Wallet name", "rate_amount": 2, "currency": "EUR", - "paid_credits": 500, - "granted_credits": 10, + "paid_credits": "500", + "granted_credits": "10", "external_customer_id": "12345", "expiration_at": "2022-09-14T23:59:59Z", + "purchase_order_number": "PO-123", + "recurring_transaction_rules": [ + { + "trigger": "interval", + "interval": "monthly", + "method": "fixed", + "paid_credits": "100", + "granted_credits": "10", + "purchase_order_number": "PO-RULE-123", + }, + ], }, -} as const satisfies WalletInput; +} satisfies WalletCreateInput; const walletResponse = { "wallet": { @@ -35,19 +46,43 @@ const walletResponse = { "last_balance_sync_at": "2022-09-14T16:35:31Z", "last_consumed_credit_at": "2022-09-14T16:35:31Z", "terminated_at": "2022-09-14T16:35:31Z", + "purchase_order_number": "PO-123", + "recurring_transaction_rules": [ + { + "lago_id": "483da83c-c007-4fbb-afcd-b00c07c41ffe", + "trigger": "interval", + "interval": "monthly", + "method": "fixed", + "status": "active", + "threshold_credits": "10", + "paid_credits": "100", + "granted_credits": "10", + "grants_target_top_up": null, + "started_at": "2022-08-08T00:00:00Z", + "target_ongoing_balance": null, + "created_at": "2022-09-14T16:35:31Z", + "expiration_at": null, + "invoice_requires_successful_payment": false, + "transaction_name": null, + "ignore_paid_top_up_limits": false, + "transaction_metadata": [], + "purchase_order_number": "PO-RULE-123", + }, + ], }, -} as const satisfies Wallet; +} satisfies Wallet; const walletUpdateInput = { "wallet": { "name": "Wallet name", "expiration_at": "2022-09-14T23:59:59Z", + "purchase_order_number": "PO-123", }, -} as const satisfies WalletUpdateInput; +} satisfies WalletUpdateInput; const walletsResponse = { - wallets: [walletInput.wallet], -} satisfies Wallets; + wallets: [walletResponse.wallet], +} satisfies WalletsPaginated; Deno.test("Successfully sent wallet responds with 2xx", async (t) => { await lagoTest({ @@ -58,6 +93,7 @@ Deno.test("Successfully sent wallet responds with 2xx", async (t) => { inputParams: [walletInput], responseObject: walletResponse, status: 200, + expectedBody: walletInput, }); }); @@ -82,6 +118,7 @@ Deno.test("Successfully sent wallet update request responds with 2xx", async (t) inputParams: ["id", walletUpdateInput], responseObject: walletResponse, status: 200, + expectedBody: walletUpdateInput, }); }); diff --git a/tests/wallet_transaction.test.ts b/tests/wallet_transaction.test.ts index b73c4f0..127298f 100644 --- a/tests/wallet_transaction.test.ts +++ b/tests/wallet_transaction.test.ts @@ -1,13 +1,14 @@ -import type { WalletTransaction, WalletTransactionInput } from "../mod.ts"; +import type { WalletTransactionCreateInput } from "../mod.ts"; import { lagoTest, unprocessableErrorResponse } from "./utils.ts"; const walletTransactionInput = { "wallet_transaction": { "wallet_id": "985da83c-c007-4fbb-afcd-b00c07c41ffe", - "paid_credits": 100, - "granted_credits": 10, + "paid_credits": "100", + "granted_credits": "10", + "purchase_order_number": "PO-123", }, -} as const satisfies WalletTransactionInput; +} satisfies WalletTransactionCreateInput; Deno.test( "Successfully sent wallet transaction responds with 2xx", @@ -25,14 +26,16 @@ Deno.test( lago_wallet_id: "", status: "settled", transaction_type: "inbound", - amount: 500, - credit_amount: 500, + amount: "500", + credit_amount: "500", + purchase_order_number: "PO-123", settled_at: "2022-09-14T16:35:31Z", created_at: "2022-09-14T16:35:31Z", }, ], }, status: 200, + expectedBody: walletTransactionInput, }); }, );