72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
"use strict";
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const {
|
|
createPolarAdapter,
|
|
normalizeTopUpPayload,
|
|
hasStandardWebhookHeaders,
|
|
} = require("../src/integrations/polar");
|
|
|
|
test("detects standard polar webhook headers", () => {
|
|
const ok = hasStandardWebhookHeaders({
|
|
"webhook-id": "a",
|
|
"webhook-timestamp": "b",
|
|
"webhook-signature": "c",
|
|
});
|
|
|
|
assert.equal(ok, true);
|
|
});
|
|
|
|
test("normalizeTopUpPayload supports legacy simple webhook shape", () => {
|
|
const parsed = normalizeTopUpPayload({ userId: "u1", credits: "12", eventId: "evt1" });
|
|
assert.equal(parsed.userId, "u1");
|
|
assert.equal(parsed.credits, 12);
|
|
assert.equal(parsed.eventId, "evt1");
|
|
});
|
|
|
|
test("normalizeTopUpPayload supports metadata-based order event shape", () => {
|
|
const parsed = normalizeTopUpPayload({
|
|
type: "order.paid",
|
|
data: {
|
|
id: "ord_1",
|
|
metadata: {
|
|
xartaudio_user_id: "u2",
|
|
xartaudio_credits: "20",
|
|
},
|
|
},
|
|
});
|
|
|
|
assert.equal(parsed.userId, "u2");
|
|
assert.equal(parsed.credits, 20);
|
|
assert.equal(parsed.eventId, "ord_1");
|
|
});
|
|
|
|
test("createCheckoutSession calls polar sdk with configured products", async () => {
|
|
const calls = [];
|
|
const adapter = createPolarAdapter({
|
|
productIds: ["prod_1"],
|
|
sdk: {
|
|
checkouts: {
|
|
async create(payload) {
|
|
calls.push(payload);
|
|
return { id: "chk_1", url: "https://polar.sh/checkout/chk_1" };
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const checkout = await adapter.createCheckoutSession({
|
|
userId: "u1",
|
|
successUrl: "https://app/success",
|
|
returnUrl: "https://app/return",
|
|
metadata: { xartaudio_user_id: "u1", xartaudio_credits: "50" },
|
|
});
|
|
|
|
assert.equal(checkout.id, "chk_1");
|
|
assert.equal(checkout.url, "https://polar.sh/checkout/chk_1");
|
|
assert.equal(calls.length, 1);
|
|
assert.deepEqual(calls[0].products, ["prod_1"]);
|
|
assert.equal(calls[0].externalCustomerId, "u1");
|
|
});
|