Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+177
@@ -0,0 +1,177 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
getCurrentSubscriptionPeriod,
|
||||
getTenantApiUsage,
|
||||
mergeApiSubscriptionFeatures,
|
||||
SUBSCRIPTION_FEATURE_LIMIT,
|
||||
} from "./usage.server.js";
|
||||
import type { TenantSubscriptionWithDetails } from "./subscriptions.server.js";
|
||||
import type { LegacyApiKeyLogWriterClient } from "./api-keys.server.js";
|
||||
|
||||
type ProductOverrides = {
|
||||
quantity?: number | null;
|
||||
currentPeriodStart?: Date | null;
|
||||
currentPeriodEnd?: Date | null;
|
||||
hasApiFeature?: boolean;
|
||||
featureType?: number;
|
||||
featureValue?: number;
|
||||
};
|
||||
|
||||
function buildSubscription(...products: ProductOverrides[]): TenantSubscriptionWithDetails {
|
||||
return {
|
||||
id: "subscription_1",
|
||||
tenantId: "tenant_1",
|
||||
products: products.map((p, index) => ({
|
||||
id: `tenant_product_${index}`,
|
||||
quantity: p.quantity ?? 1,
|
||||
currentPeriodStart: p.currentPeriodStart ?? null,
|
||||
currentPeriodEnd: p.currentPeriodEnd ?? null,
|
||||
subscriptionProduct: {
|
||||
features:
|
||||
p.hasApiFeature === false
|
||||
? [{ name: "storage", title: "Storage", type: SUBSCRIPTION_FEATURE_LIMIT.included, value: 5, accumulate: false }]
|
||||
: [
|
||||
{
|
||||
name: "api",
|
||||
title: "{0} API calls",
|
||||
type: p.featureType ?? SUBSCRIPTION_FEATURE_LIMIT.monthly,
|
||||
value: p.featureValue ?? 10,
|
||||
accumulate: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
})),
|
||||
} as unknown as TenantSubscriptionWithDetails;
|
||||
}
|
||||
|
||||
function createUsageClient(count: number): LegacyApiKeyLogWriterClient {
|
||||
return { apiKeyLog: { count: vi.fn().mockResolvedValue(count) } } as unknown as LegacyApiKeyLogWriterClient;
|
||||
}
|
||||
|
||||
describe("mergeApiSubscriptionFeatures", () => {
|
||||
it("returns undefined for an empty feature list", () => {
|
||||
expect(mergeApiSubscriptionFeatures([])).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps the highest limit type but only the first value when features are not accumulating", () => {
|
||||
const merged = mergeApiSubscriptionFeatures([
|
||||
{ title: "Plan", type: SUBSCRIPTION_FEATURE_LIMIT.included, value: 3, accumulate: false } as never,
|
||||
{ title: "Plan", type: SUBSCRIPTION_FEATURE_LIMIT.monthly, value: 9, accumulate: false } as never,
|
||||
]);
|
||||
|
||||
expect(merged).toMatchObject({ type: SUBSCRIPTION_FEATURE_LIMIT.monthly, value: 3 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("getTenantApiUsage", () => {
|
||||
it("returns undefined when there is no subscription at all", async () => {
|
||||
await expect(getTenantApiUsage("tenant_1", null)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined when no product carries an api feature", async () => {
|
||||
const client = createUsageClient(0);
|
||||
await expect(getTenantApiUsage("tenant_1", buildSubscription({ hasApiFeature: false }), client)).resolves.toBeUndefined();
|
||||
expect(client.apiKeyLog.count).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reports unlimited plans without counting usage", async () => {
|
||||
const client = createUsageClient(0);
|
||||
|
||||
const usage = await getTenantApiUsage("tenant_1", buildSubscription({ featureType: SUBSCRIPTION_FEATURE_LIMIT.unlimited, featureValue: 0 }), client);
|
||||
|
||||
expect(usage).toMatchObject({ enabled: true, remaining: "unlimited" });
|
||||
expect(client.apiKeyLog.count).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("marks not-included plans disabled with the explanatory message", async () => {
|
||||
const client = createUsageClient(0);
|
||||
|
||||
const usage = await getTenantApiUsage("tenant_1", buildSubscription({ featureType: SUBSCRIPTION_FEATURE_LIMIT.notIncluded, featureValue: 0 }), client);
|
||||
|
||||
expect(usage).toMatchObject({ enabled: false, remaining: 0, message: "API usage is not included in this plan." });
|
||||
expect(client.apiKeyLog.count).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("leaves an included (non-metered) plan enabled without counting usage", async () => {
|
||||
const client = createUsageClient(0);
|
||||
|
||||
const usage = await getTenantApiUsage("tenant_1", buildSubscription({ featureType: SUBSCRIPTION_FEATURE_LIMIT.included, featureValue: 5 }), client);
|
||||
|
||||
expect(usage).toMatchObject({ enabled: true, remaining: 0 });
|
||||
expect(client.apiKeyLog.count).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("falls back to a single unit when product quantity is zero or missing", async () => {
|
||||
const client = createUsageClient(2);
|
||||
|
||||
const usage = await getTenantApiUsage(
|
||||
"tenant_1",
|
||||
buildSubscription({ quantity: 0, featureType: SUBSCRIPTION_FEATURE_LIMIT.monthly, featureValue: 10 }),
|
||||
client,
|
||||
);
|
||||
|
||||
// quantity 0 collapses to one feature instance, so the limit stays at the single feature value.
|
||||
expect(usage).toMatchObject({ value: 10, used: 2, remaining: 8 });
|
||||
});
|
||||
|
||||
it("uses the monthly message when a metered monthly plan is exhausted", async () => {
|
||||
const usage = await getTenantApiUsage(
|
||||
"tenant_1",
|
||||
buildSubscription({ featureType: SUBSCRIPTION_FEATURE_LIMIT.monthly, featureValue: 5 }),
|
||||
createUsageClient(5),
|
||||
);
|
||||
|
||||
expect(usage).toMatchObject({ enabled: false, remaining: 0, message: "You have reached the API usage limit this month (5/5)." });
|
||||
});
|
||||
|
||||
it("uses the max (lifetime) message when a capped plan is exhausted", async () => {
|
||||
const usage = await getTenantApiUsage(
|
||||
"tenant_1",
|
||||
buildSubscription({ featureType: SUBSCRIPTION_FEATURE_LIMIT.max, featureValue: 4 }),
|
||||
createUsageClient(4),
|
||||
);
|
||||
|
||||
expect(usage).toMatchObject({ enabled: false, message: "You have reached the API usage limit (4/4)." });
|
||||
});
|
||||
});
|
||||
|
||||
describe("getCurrentSubscriptionPeriod", () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("handles the December year-boundary branch", () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-12-15T12:00:00.000Z"));
|
||||
|
||||
const period = getCurrentSubscriptionPeriod(null);
|
||||
|
||||
expect(period.start.getFullYear()).toBe(2026);
|
||||
expect(period.start.getMonth()).toBe(11);
|
||||
// December clamps the period end to the last day of the year.
|
||||
expect(period.end.getFullYear()).toBe(2026);
|
||||
expect(period.end.getMonth()).toBe(11);
|
||||
expect(period.end.getDate()).toBe(31);
|
||||
});
|
||||
|
||||
it("prefers an explicit product billing period over the calendar month", () => {
|
||||
const start = new Date("2026-04-10T00:00:00.000Z");
|
||||
const end = new Date("2026-05-10T00:00:00.000Z");
|
||||
|
||||
const period = getCurrentSubscriptionPeriod(buildSubscription({ currentPeriodStart: start, currentPeriodEnd: end }));
|
||||
|
||||
expect(period).toEqual({ start, end });
|
||||
});
|
||||
|
||||
it("ignores products that lack a complete billing period", () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-06-15T12:00:00.000Z"));
|
||||
|
||||
const period = getCurrentSubscriptionPeriod(buildSubscription({ currentPeriodStart: null, currentPeriodEnd: null }));
|
||||
|
||||
// No override applied — falls back to the calendar month.
|
||||
expect(period.start.getMonth()).toBe(5);
|
||||
expect(period.end.getMonth()).toBe(5);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user