Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions tests/rate_limit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response>,
handler: (request: Request) => Response | Promise<Response>,
): 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", () => {
Expand Down
6 changes: 5 additions & 1 deletion tests/subscription.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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;

Expand All @@ -50,6 +52,7 @@ Deno.test("Successfully sent subscription responds with 2xx", async (t) => {
inputParams: [subscriptionInput],
responseObject: subscriptionResponse,
status: 200,
expectedBody: subscriptionInput,
});
});

Expand All @@ -76,6 +79,7 @@ Deno.test(
inputParams: ["id", subscriptionInput],
responseObject: subscriptionResponse,
status: 200,
expectedBody: subscriptionInput,
});
},
);
Expand Down
12 changes: 9 additions & 3 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export async function lagoTest<
status,
testType,
urlParams,
expectedBody,
}: {
testType: "error" | "200";
t: Deno.TestContext;
Expand All @@ -77,6 +78,7 @@ export async function lagoTest<
>;
status: number;
urlParams?: Record<string, string>;
expectedBody?: unknown;
},
) {
const { fetch, getRequest, expectedMethod, expectedPath } = createMockFetch(
Expand All @@ -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");

Expand All @@ -104,6 +106,10 @@ export async function lagoTest<
assertEquals(urlSearchParams.get(key), value);
});
}

if (expectedBody !== undefined) {
assertEquals(await request.json(), expectedBody);
}
};

switch (testType) {
Expand All @@ -125,7 +131,7 @@ export async function lagoTest<
(lagoError as ApiErrorUnprocessableEntity).error,
errorMessage,
);
assertRequest();
await assertRequest();
}
});
break;
Expand All @@ -138,7 +144,7 @@ export async function lagoTest<
) as Response;

assertEquals(response.status, 200);
assertRequest();
await assertRequest();
});
break;

Expand Down
55 changes: 46 additions & 9 deletions tests/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
Wallet,
WalletInput,
Wallets,
WalletCreateInput,
WalletsPaginated,
WalletUpdateInput,
} from "../mod.ts";
import { lagoTest, unprocessableErrorResponse } from "./utils.ts";
Expand All @@ -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": {
Expand All @@ -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({
Expand All @@ -58,6 +93,7 @@ Deno.test("Successfully sent wallet responds with 2xx", async (t) => {
inputParams: [walletInput],
responseObject: walletResponse,
status: 200,
expectedBody: walletInput,
});
});

Expand All @@ -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,
});
});

Expand Down
15 changes: 9 additions & 6 deletions tests/wallet_transaction.test.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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,
});
},
);
Expand Down
Loading