Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,93 @@
|
||||
// @vitest-environment node
|
||||
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
vi.mock("@toolctrl/db/subscriptions.server", () => ({
|
||||
getTenantSubscription: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@toolctrl/queue", () => ({
|
||||
setTenantPlanLookup: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("./subscriptionCache.server", () => ({
|
||||
getSubscriptionCache: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/logger.server", () => ({
|
||||
createScopedLogger: () => ({
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
import { getTenantSubscription } from "@toolctrl/db/subscriptions.server";
|
||||
import { setTenantPlanLookup } from "@toolctrl/queue";
|
||||
import { getSubscriptionCache } from "./subscriptionCache.server";
|
||||
import {
|
||||
__resetTenantPlanLookupForTests,
|
||||
getCachedTenantPlan,
|
||||
setSubscriptionToPlanTier,
|
||||
wireTenantPlanLookup,
|
||||
} from "./tenantPlanLookup.server";
|
||||
|
||||
const getTenantSubscriptionMock = vi.mocked(getTenantSubscription);
|
||||
const setTenantPlanLookupMock = vi.mocked(setTenantPlanLookup);
|
||||
const getSubscriptionCacheMock = vi.mocked(getSubscriptionCache);
|
||||
const cacheGet = vi.fn<[string], Promise<unknown>>();
|
||||
const cacheSet = vi.fn<[string, unknown], Promise<void>>();
|
||||
|
||||
describe("tenant plan lookup", () => {
|
||||
beforeEach(() => {
|
||||
getTenantSubscriptionMock.mockReset();
|
||||
cacheGet.mockReset();
|
||||
cacheSet.mockReset().mockResolvedValue(undefined);
|
||||
setTenantPlanLookupMock.mockReset();
|
||||
getSubscriptionCacheMock.mockReset().mockReturnValue({ get: cacheGet, set: cacheSet } as any);
|
||||
__resetTenantPlanLookupForTests();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
__resetTenantPlanLookupForTests();
|
||||
});
|
||||
|
||||
test("returns null when no classifier is registered", async () => {
|
||||
cacheGet.mockResolvedValue({ id: "sub_1" });
|
||||
await expect(getCachedTenantPlan("tenant_1")).resolves.toBeNull();
|
||||
// Cache hit — DB never queried
|
||||
expect(getTenantSubscriptionMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("fetches subscription from DB on cache miss and primes L2", async () => {
|
||||
cacheGet.mockResolvedValue(null);
|
||||
getTenantSubscriptionMock.mockResolvedValue({ id: "sub_1" });
|
||||
setSubscriptionToPlanTier((sub) => (sub ? "pro" : null));
|
||||
|
||||
await expect(getCachedTenantPlan("tenant_1")).resolves.toBe("pro");
|
||||
expect(getTenantSubscriptionMock).toHaveBeenCalledWith("tenant_1");
|
||||
expect(cacheSet).toHaveBeenCalledWith("tenant_1", { id: "sub_1" });
|
||||
});
|
||||
|
||||
test("returns null and logs when DB fetch fails", async () => {
|
||||
cacheGet.mockResolvedValue(null);
|
||||
getTenantSubscriptionMock.mockRejectedValue(new Error("db down"));
|
||||
setSubscriptionToPlanTier(() => "enterprise");
|
||||
await expect(getCachedTenantPlan("tenant_2")).resolves.toBeNull();
|
||||
});
|
||||
|
||||
test("classifier maps subscription to a plan tier", async () => {
|
||||
cacheGet.mockResolvedValue({ tier: "enterprise" });
|
||||
setSubscriptionToPlanTier((sub) => {
|
||||
const tier = (sub as { tier?: string } | null)?.tier;
|
||||
return tier === "enterprise" ? "enterprise" : tier === "pro" ? "pro" : "free";
|
||||
});
|
||||
await expect(getCachedTenantPlan("tenant_ent")).resolves.toBe("enterprise");
|
||||
});
|
||||
|
||||
test("wireTenantPlanLookup registers exactly once", () => {
|
||||
setTenantPlanLookupMock.mockClear();
|
||||
wireTenantPlanLookup();
|
||||
wireTenantPlanLookup();
|
||||
expect(setTenantPlanLookupMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user