import { describe, expect, it } from "vitest"; import { readdirSync, readFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { GLOBAL_REFERENCED_MODELS, PLATFORM_CATALOG_GLOBALS, TENANT_SCOPED_MODELS, globalSnapshotModelNames, isGlobalSnapshotModel, isTenantScopedModel, tenantScopedModelNames, } from "./models.js"; const here = dirname(fileURLToPath(import.meta.url)); const repoRoot = join(here, "..", "..", "..", "..", ".."); const schemaDir = join(repoRoot, "prisma", "schema"); interface SchemaModel { readonly name: string; readonly schema: string; readonly hasTenantId: boolean; } function listSchemaModels(): SchemaModel[] { const files = readdirSync(schemaDir).filter((f) => f.endsWith(".prisma")); const models: SchemaModel[] = []; for (const file of files) { const text = readFileSync(join(schemaDir, file), "utf8"); const re = /model\s+(\w+)\s*\{([\s\S]*?)\n\}/g; let m: RegExpExecArray | null; while ((m = re.exec(text)) !== null) { const body = m[2]; const hasTenantId = /^\s*tenantId\s+String/m.test(body); models.push({ name: m[1], schema: file, hasTenantId }); } } return models; } describe("TENANT_SCOPED_MODELS", () => { const schemaModels = listSchemaModels(); const schemaTenantModels = schemaModels.filter((m) => m.hasTenantId); it("covers every prisma model that has a tenantId String column", () => { const listed = new Set(TENANT_SCOPED_MODELS.map((m) => m.model)); const missing = schemaTenantModels.filter((m) => !listed.has(m.name)); expect(missing, `missing models in TENANT_SCOPED_MODELS: ${missing.map((x) => x.name).join(", ")}`).toHaveLength(0); }); it("does not list a model that is not actually tenant-scoped", () => { const schemaByName = new Map(schemaModels.map((m) => [m.name, m])); const bogus = TENANT_SCOPED_MODELS.filter((m) => { const s = schemaByName.get(m.model); return !s || !s.hasTenantId; }); expect(bogus, `tenant-scoped list contains non-tenant models: ${bogus.map((x) => x.model).join(", ")}`).toHaveLength(0); }); it("declares unique model names", () => { const names = TENANT_SCOPED_MODELS.map((m) => m.model); expect(new Set(names).size).toBe(names.length); }); it("isTenantScopedModel returns true for every known model and false otherwise", () => { for (const m of TENANT_SCOPED_MODELS) { expect(isTenantScopedModel(m.model)).toBe(true); } expect(isTenantScopedModel("DefinitelyNotARealModel")).toBe(false); }); it("tenantScopedModelNames returns the list in declared order", () => { expect(tenantScopedModelNames()).toEqual(TENANT_SCOPED_MODELS.map((m) => m.model)); }); }); interface RelationRef { readonly sourceModel: string; readonly targetModel: string; readonly fkColumn: string; } /** * Parse every prisma schema file and return one entry per FK relation * declared on a tenant-scoped model (i.e. lines with both `@relation` and * `fields: [...]`). Used by the globals-coverage tests below to prove * that every global referenced from the tenant-scoped slice is either * listed in GLOBAL_REFERENCED_MODELS, on PLATFORM_CATALOG_GLOBALS, or is * itself a tenant-scoped model (i.e. already inside the snapshot). */ function collectTenantScopedFkRelations(): RelationRef[] { const files = readdirSync(schemaDir).filter((f) => f.endsWith(".prisma")); const all: RelationRef[] = []; const tenantScopedNames = new Set(TENANT_SCOPED_MODELS.map((m) => m.model)); for (const file of files) { const text = readFileSync(join(schemaDir, file), "utf8"); const modelBlock = /model\s+(\w+)\s*\{([\s\S]*?)\n\}/g; let m: RegExpExecArray | null; while ((m = modelBlock.exec(text)) !== null) { const sourceModel = m[1]; const body = m[2]; if (!tenantScopedNames.has(sourceModel)) continue; for (const line of body.split("\n")) { const s = line.trim(); if (!s.includes("@relation")) continue; if (!s.includes("fields:")) continue; const fieldDecl = /^(\w+)\s+(\w+)(\??)/u.exec(s); if (!fieldDecl) continue; const targetModel = fieldDecl[2]; const fieldsMatch = /fields:\s*\[([^\]]+)\]/u.exec(s); if (!fieldsMatch) continue; const cols = fieldsMatch[1].split(",").map((x) => x.trim()).filter(Boolean); for (const fk of cols) { all.push({ sourceModel, targetModel, fkColumn: fk }); } } } } return all; } describe("GLOBAL_REFERENCED_MODELS + PLATFORM_CATALOG_GLOBALS coverage", () => { const schemaModels = listSchemaModels(); const tenantScopedNames = new Set(TENANT_SCOPED_MODELS.map((m) => m.model)); const globalNames = new Set(GLOBAL_REFERENCED_MODELS.map((m) => m.model)); const platformNames = new Set(PLATFORM_CATALOG_GLOBALS); it("every tenant-scoped FK relation targets a tenant-scoped model, a snapshot-global, or a platform-catalog global", () => { const relations = collectTenantScopedFkRelations(); const uncategorized: RelationRef[] = []; for (const rel of relations) { if (tenantScopedNames.has(rel.targetModel)) continue; if (globalNames.has(rel.targetModel)) continue; if (platformNames.has(rel.targetModel)) continue; // `Tenant` itself is the snapshot root — restore creates the target // tenant row before replay, so it is never copied via the globals // pathway. if (rel.targetModel === "Tenant") continue; uncategorized.push(rel); } expect( uncategorized, `Uncategorized globals: ${uncategorized.map((r) => `${r.sourceModel}.${r.fkColumn} → ${r.targetModel}`).join(", ")}. Add the target model to GLOBAL_REFERENCED_MODELS (snapshot it) or PLATFORM_CATALOG_GLOBALS (treat as deployment baseline).`, ).toHaveLength(0); }); it("every entry in GLOBAL_REFERENCED_MODELS corresponds to an actual prisma model with no tenantId column", () => { const byName = new Map(schemaModels.map((m) => [m.name, m])); const bogus = GLOBAL_REFERENCED_MODELS.filter((g) => { const s = byName.get(g.model); return !s || s.hasTenantId; }); expect(bogus, `bogus global entries: ${bogus.map((x) => x.model).join(", ")}`).toHaveLength(0); }); it("every entry in PLATFORM_CATALOG_GLOBALS corresponds to an actual prisma model with no tenantId column", () => { const byName = new Map(schemaModels.map((m) => [m.name, m])); const bogus = PLATFORM_CATALOG_GLOBALS.filter((name) => { const s = byName.get(name); return !s || s.hasTenantId; }); expect(bogus, `bogus platform-catalog entries: ${bogus.join(", ")}`).toHaveLength(0); }); it("every declared referencedBy entry resolves to a tenant-scoped model with the named FK column", () => { const tenantScopedSet = new Set(TENANT_SCOPED_MODELS.map((m) => m.model)); const violations: string[] = []; for (const g of GLOBAL_REFERENCED_MODELS) { for (const ref of g.referencedBy) { if (!tenantScopedSet.has(ref.model)) { violations.push(`${g.model}.referencedBy: ${ref.model} is not tenant-scoped`); } } } expect(violations).toEqual([]); }); it("isGlobalSnapshotModel + globalSnapshotModelNames agree with GLOBAL_REFERENCED_MODELS", () => { for (const g of GLOBAL_REFERENCED_MODELS) { expect(isGlobalSnapshotModel(g.model)).toBe(true); } expect(isGlobalSnapshotModel("DefinitelyNotARealGlobal")).toBe(false); expect(globalSnapshotModelNames()).toEqual(GLOBAL_REFERENCED_MODELS.map((g) => g.model)); }); it("snapshot-globals and platform-catalog-globals are disjoint", () => { const overlap = GLOBAL_REFERENCED_MODELS.map((g) => g.model).filter((n) => platformNames.has(n)); expect(overlap).toEqual([]); }); });