import { readFileSync } from "node:fs"; import path from "node:path"; import { describe, expect, it } from "vitest"; import { getRepoRoot, readRepoFile, repoPathExists } from "~/utils/testing/repoPaths"; const repoRoot = getRepoRoot(); const packagesDbIndexPath = path.join(repoRoot, "packages/db/src/index.ts"); const temporaryModuleDbFiles = [ "apps/web/app/modules/portals/db/portals.db.server.ts", "apps/web/app/modules/portals/db/portalPages.db.server.ts", "apps/web/app/modules/portals/db/portalUsers.db.server.ts", "apps/web/app/modules/sites/db/sites.db.server.ts", ]; function getExportLines(source: string): string[] { return source .split(/\r?\n/) .map((line) => line.trim()) .filter((line) => line.startsWith("export ")); } describe("Portals/Sites DB ownership boundary", () => { it("keeps portal/site persistence out of packages/db until the Feature Decision decision", () => { const exportLines = getExportLines(readFileSync(packagesDbIndexPath, "utf8")); const forbiddenExportPattern = /\b(?:portal|portals|site|sites)\b/i; const forbiddenLines = exportLines.filter((line) => forbiddenExportPattern.test(line)); expect( forbiddenLines, [ "D006 defers Portals/Sites DB ownership until Feature Decision.", "Remove portal/site persistence exports from packages/db/src/index.ts or update the Feature Decision ownership decision first.", `Forbidden export lines: ${forbiddenLines.join(" | ") || "none"}`, ].join(" ") ).toEqual([]); }); it("documents the accepted temporary module-owned DB files", () => { for (const relativePath of temporaryModuleDbFiles) { expect( repoPathExists(relativePath), `D006/Feature Decision boundary drift: ${relativePath} must remain as the temporary module-owned persistence file until Feature Decision changes the ownership decision.` ).toBe(true); expect( readRepoFile(relativePath), `D006/Feature Decision boundary drift: ${relativePath} must continue importing ~/utils/db.server while Portals/Sites persistence is deferred from packages/db.` ).toContain('~/utils/db.server'); } }); it("keeps tenant-owned portal and site mutations scoped at the DB boundary", () => { const portalsSource = readRepoFile("apps/web/app/modules/portals/db/portals.db.server.ts"); const sitesSource = readRepoFile("apps/web/app/modules/sites/db/sites.db.server.ts"); expect(portalsSource).toContain("where: { id: before.id, tenantId: before.tenantId }"); expect(portalsSource).toContain("export async function deletePortal({ id, tenantId }: { id: string; tenantId: string | null })"); expect(sitesSource).toContain("export async function getSite({ id, tenantId }: { id: string; tenantId: string })"); expect(sitesSource).toContain("export async function updateSite("); expect(sitesSource).toContain("scope: { id: string; tenantId: string }"); expect(sitesSource).toContain("export async function deleteSite(scope: { id: string; tenantId: string })"); }); it("keeps tenant-owned portal child mutations scoped at the DB boundary", () => { const portalUsersSource = readRepoFile("apps/web/app/modules/portals/db/portalUsers.db.server.ts"); const portalPagesSource = readRepoFile("apps/web/app/modules/portals/db/portalPages.db.server.ts"); expect(portalUsersSource).toContain("export async function updatePortalUser("); expect(portalUsersSource).toContain("portalId: string,"); expect(portalUsersSource).toContain("db.portalUser.updateMany"); expect(portalUsersSource).toContain("where: { portalId, id }"); expect(portalUsersSource).toContain("export async function updatePortalUserPassword(portalId: string, id: string"); expect(portalPagesSource).toContain("export async function updatePortalPage(portalId: string, id: string"); expect(portalPagesSource).toContain("db.portalPage.updateMany"); expect(portalPagesSource).toContain("export async function deletePortalPage(portalId: string, id: string)"); expect(portalPagesSource).toContain("db.portalPage.deleteMany"); }); });