settlekit/packages/usage/test/charges.test.ts
caelum0x 3fe0c88942 feat: implement all 77 SettleKit packages with real code
Core engine: entitlements (§14), delivery action runner (§21).
Payment rails: arc (viem USDC verify), circle (REST), x402 middleware, payments, webhooks (HMAC), notifications (Resend).
Integrations: github (Octokit App), discord (REST v10).
Commerce: saas billing, bundles, license-keys, api-keys, file-delivery (signed URLs + SigV4), usage/credits, agent-services, escrow, marketplace-core, risk.
Persistence: database (Drizzle/Postgres, 48 tables §25).
Plus supporting domain packages.

561 tests passing across 117 files; tsc -b green for every package.
2026-06-15 03:24:19 +03:00

51 lines
1.7 KiB
TypeScript

import { money } from "@settlekit/common";
import { describe, expect, it } from "vitest";
import { computeMeteredCharge, computeUsageCharge, createMeter, recordUsage } from "../src/index.js";
const start = new Date("2026-01-01T00:00:00.000Z");
const base = {
organizationId: "org_1",
customerId: "cus_1",
productId: "prod_1",
metric: "api_calls",
} as const;
function meterWith(value: number) {
return recordUsage(createMeter({ ...base, periodStart: start, period: "monthly" }), "api_calls", value);
}
describe("computeUsageCharge", () => {
it("multiplies unit price by recorded units", () => {
const charge = computeUsageCharge(meterWith(1000), money("0.001"));
expect(charge).toEqual(money("1.000000"));
});
it("returns zero for a meter with no usage", () => {
const charge = computeUsageCharge(meterWith(0), money("0.05"));
expect(charge.amount).toBe("0");
expect(charge.currency).toBe("USDC");
});
it("handles a single unit price exactly", () => {
const charge = computeUsageCharge(meterWith(3), money("2.50"));
expect(charge).toEqual(money("7.500000"));
});
});
describe("computeMeteredCharge", () => {
it("only bills units in excess of the included allowance", () => {
const charge = computeMeteredCharge(meterWith(1200), money("0.01"), 1000);
expect(charge).toEqual(money("2.000000"));
});
it("bills nothing when usage is within the allowance", () => {
const charge = computeMeteredCharge(meterWith(500), money("0.01"), 1000);
expect(charge.amount).toBe("0");
});
it("rejects a negative allowance", () => {
expect(() => computeMeteredCharge(meterWith(10), money("0.01"), -5)).toThrow();
});
});