import { readFileSync } from "node:fs"; import { resolve } from "node:path"; import { describe, expect, it } from "vitest"; const repoRoot = resolve(__dirname, "../../.."); function readRepoFile(path: string) { return readFileSync(resolve(repoRoot, path), "utf8"); } const migratedWrappers = [ "apps/web/app/utils/db/subscriptionProducts.db.server.ts", "apps/web/app/utils/db/tenantSubscriptions.db.server.ts", "apps/web/app/utils/db/subscriptions/tenantSubscriptionProducts.db.server.ts", ]; const directBillingPrismaModels = [ "subscriptionProduct", "subscriptionPrice", "subscriptionFeature", "subscriptionUsageBasedPrice", "subscriptionUsageBasedTier", "tenantSubscription", "tenantSubscriptionProduct", "tenantSubscriptionProductPrice", "tenantSubscriptionUsageRecord", "checkoutSessionStatus", ]; describe("subscription app boundary", () => { it.each(migratedWrappers)("keeps %s behind @hivegrid/db/subscriptions.server", (path) => { const source = readRepoFile(path); expect(source).toContain("@hivegrid/db/subscriptions.server"); expect(source).not.toContain("~/utils/db.server"); expect(source).not.toContain("../../db.server"); expect(source).not.toContain("../db.server"); expect(source).not.toMatch(/\bdb\.[a-zA-Z]/); }); it("keeps subscriptionService subscription billing persistence behind the package boundary", () => { const source = readRepoFile("apps/web/app/utils/services/.server/subscriptionService.ts"); expect(source).toContain("@hivegrid/db/subscriptions.server"); expect(source).toContain("../../db.server"); expect(source).toContain("clearCacheKey"); expect(source).toContain("cancelTenantSubscription"); expect(source).toContain("cancelRecurringProductAcquisition"); expect(source).not.toContain("EventsService"); expect(source).not.toContain("cancelTenantSubscriptionProduct"); for (const model of directBillingPrismaModels) { expect(source).not.toMatch(new RegExp(`\\bdb\\.${model}\\b`)); } }); it("keeps tenant subscription settings writes behind cache-clearing web wrappers", () => { const source = readRepoFile("apps/web/app/routes/app.$tenant/settings/billing/index.tsx"); const directPackageImport = source.match(/import\s+\{([^}]+)\}\s+from\s+["']@hivegrid\/db\/subscriptions\.server["']/s); const importedNames = directPackageImport?.[1] ?? ""; expect(importedNames).not.toMatch(/\bcreateTenantSubscription\b/); expect(importedNames).not.toMatch(/\bupdateTenantSubscriptionCustomerId\b/); expect(source).toMatch(/~\/utils\/db\/tenantSubscriptions\.db\.server|~\/services\/billing\/self-service-subscription\.server/u); }); it("keeps the legacy subscribe route free of in-app checkout writes and plan UI", () => { const source = readRepoFile("apps/web/app/routes/subscribe.$tenant.tsx"); expect(source).not.toContain("@hivegrid/db/subscriptions.server"); expect(source).not.toContain("~/utils/db/tenantSubscriptions.db.server"); expect(source).not.toContain("createCheckoutSession"); expect(source).not.toContain("createBillingCustomer"); expect(source).not.toContain("PlansGrouped"); }); });