import { beforeEach, describe, expect, it, vi } from "vitest"; const { clearAllSubscriptionsCache, createSubscriptionFeature, deleteSubscriptionFeatures, getSubscriptionProductsInIds, } = vi.hoisted(() => ({ clearAllSubscriptionsCache: vi.fn(), createSubscriptionFeature: vi.fn(), deleteSubscriptionFeatures: vi.fn(), getSubscriptionProductsInIds: vi.fn(), })); vi.mock("@hivegrid/db/subscriptions.server", () => ({ createSubscriptionFeature, deleteSubscriptionFeatures, getAllSubscriptionProductsWithTenants: vi.fn(), getSubscriptionProductsInIds, })); vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn(async () => ({ t: (key: string) => key })), })); vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: vi.fn(), })); vi.mock("~/utils/session.server", () => ({ validateCSRFToken: vi.fn(), })); vi.mock("~/utils/services/.server/subscriptionService", () => ({ clearAllSubscriptionsCache, })); import { action } from "./pricing.features"; function updateFeaturesRequest(planIds: string[]): Request { return new Request("http://admin.localhost:3000/admin/settings/pricing/features", { method: "POST", body: new URLSearchParams({ action: "update-features", planIds: JSON.stringify(planIds), features: "[]", }), }); } describe("admin pricing feature mutations", () => { beforeEach(() => { vi.clearAllMocks(); }); it("rejects feature changes for canonical base plans before deleting features", async () => { getSubscriptionProductsInIds.mockResolvedValue([ { id: "starter", kind: "base_plan", code: "starter" }, ]); const response = await action({ request: updateFeaturesRequest(["starter"]), params: {}, context: {}, } as never); expect(response.status).toBe(400); await expect(response.json()).resolves.toEqual({ error: "admin.pricing.errors.managedCatalogProduct", }); expect(deleteSubscriptionFeatures).not.toHaveBeenCalled(); expect(createSubscriptionFeature).not.toHaveBeenCalled(); expect(clearAllSubscriptionsCache).not.toHaveBeenCalled(); }); it("continues to update explicitly editable add-ons", async () => { getSubscriptionProductsInIds.mockResolvedValue([ { id: "portal-monthly", kind: "recurring_add_on", code: "customer_portal_monthly" }, ]); const response = await action({ request: updateFeaturesRequest(["portal-monthly"]), params: {}, context: {}, } as never); expect(response.status).toBe(200); await expect(response.json()).resolves.toEqual({ success: "admin.pricing.features.actions.updated", }); expect(deleteSubscriptionFeatures).toHaveBeenCalledWith("portal-monthly"); expect(createSubscriptionFeature).not.toHaveBeenCalled(); expect(clearAllSubscriptionsCache).toHaveBeenCalledOnce(); }); });