128 lines
4.5 KiB
TypeScript
128 lines
4.5 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
formatTenantApiUsagePlanTitle,
|
|
getTenantApiUsage,
|
|
mergeApiSubscriptionFeatures,
|
|
SUBSCRIPTION_FEATURE_LIMIT,
|
|
} from "./usage.server.js";
|
|
import type { TenantSubscriptionWithDetails } from "./subscriptions.server.js";
|
|
import type { LegacyApiKeyLogWriterClient } from "./api-keys.server.js";
|
|
|
|
const API_USAGE_TITLE_TEMPLATE = "{0} API calls";
|
|
|
|
function createSubscription(
|
|
feature: { title?: string; type: number; value: number; accumulate?: boolean },
|
|
overrides: Partial<TenantSubscriptionWithDetails["products"][number]> = {},
|
|
): TenantSubscriptionWithDetails {
|
|
return {
|
|
id: "subscription_1",
|
|
tenantId: "tenant_1",
|
|
active: true,
|
|
billingProviderCustomerId: null,
|
|
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
products: [
|
|
{
|
|
id: "tenant_product_1",
|
|
tenantSubscriptionId: "subscription_1",
|
|
subscriptionProductId: "product_1",
|
|
quantity: 1,
|
|
cancelledAt: null,
|
|
currentPeriodStart: new Date("2026-02-01T00:00:00.000Z"),
|
|
currentPeriodEnd: new Date("2026-03-01T00:00:00.000Z"),
|
|
endsAt: null,
|
|
billingProviderSubscriptionId: null,
|
|
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
subscriptionProduct: {
|
|
id: "product_1",
|
|
order: 0,
|
|
title: "Pro",
|
|
model: 0,
|
|
public: true,
|
|
groupTitle: null,
|
|
groupDescription: null,
|
|
description: null,
|
|
badge: null,
|
|
billingAddressCollection: null,
|
|
hasQuantity: false,
|
|
canBuyAgain: false,
|
|
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
features: [
|
|
{
|
|
id: "feature_1",
|
|
subscriptionProductId: "product_1",
|
|
order: 0,
|
|
name: "api",
|
|
title: feature.title ?? API_USAGE_TITLE_TEMPLATE,
|
|
type: feature.type,
|
|
value: feature.value,
|
|
href: null,
|
|
badge: null,
|
|
accumulate: feature.accumulate ?? false,
|
|
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
updatedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
},
|
|
],
|
|
},
|
|
prices: [],
|
|
...overrides,
|
|
},
|
|
],
|
|
} as unknown as TenantSubscriptionWithDetails;
|
|
}
|
|
|
|
function createUsageClient(count: number): LegacyApiKeyLogWriterClient {
|
|
return {
|
|
apiKeyLog: {
|
|
count: vi.fn().mockResolvedValue(count),
|
|
},
|
|
} as unknown as LegacyApiKeyLogWriterClient;
|
|
}
|
|
|
|
describe("tenant API usage boundary", () => {
|
|
it("resolves monthly usage through the tenant-scoped API key log boundary", async () => {
|
|
const subscription = createSubscription({ type: SUBSCRIPTION_FEATURE_LIMIT.monthly, value: 10 });
|
|
const client = createUsageClient(7);
|
|
|
|
const usage = await getTenantApiUsage("tenant_1", subscription, client);
|
|
|
|
expect(usage).toMatchObject({ enabled: true, used: 7, remaining: 3 });
|
|
expect(client.apiKeyLog.count).toHaveBeenCalledWith({
|
|
where: {
|
|
tenantId: "tenant_1",
|
|
status: { in: [200, 201, 202, 203, 204, 205, 206, 207, 208, 226] },
|
|
endpoint: { notIn: ["/v1/usage"] },
|
|
createdAt: {
|
|
gte: new Date("2026-02-01T00:00:00.000Z"),
|
|
lt: new Date("2026-03-01T00:00:00.000Z"),
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("marks usage disabled when a capped plan is exhausted", async () => {
|
|
const usage = await getTenantApiUsage("tenant_1", createSubscription({ type: SUBSCRIPTION_FEATURE_LIMIT.max, value: 2 }), createUsageClient(3));
|
|
|
|
expect(usage).toMatchObject({
|
|
enabled: false,
|
|
remaining: -1,
|
|
message: "You have reached the API usage limit (3/2).",
|
|
});
|
|
});
|
|
|
|
it("formats plan titles and merges accumulated feature quantities", () => {
|
|
const first = createSubscription({ type: SUBSCRIPTION_FEATURE_LIMIT.monthly, value: 5, accumulate: true }).products[0].subscriptionProduct.features[0];
|
|
const second = { ...first, id: "feature_2", value: 7 };
|
|
|
|
const merged = mergeApiSubscriptionFeatures([first, second]);
|
|
|
|
expect(merged).toMatchObject({ type: SUBSCRIPTION_FEATURE_LIMIT.monthly, value: 12 });
|
|
expect(formatTenantApiUsagePlanTitle({ title: API_USAGE_TITLE_TEMPLATE, type: 2, value: 12, used: 0, remaining: 12, enabled: true, message: "" })).toBe(
|
|
"12 API calls",
|
|
);
|
|
});
|
|
});
|