Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/scripts/verify-cross-cutting-models-complete.mjs
2026-07-08 16:15:37 +02:00

106 lines
4.1 KiB
JavaScript

#!/usr/bin/env node
/**
* Footgun guard (R-P6C, AGENTS DoD #16/§Querschnitt): every Prisma model that
* carries a `tenantId String` scalar MUST be listed in TENANT_SCOPED_MODELS
* (packages/cli/src/commands/tenant/models.ts). A forgotten entry silently leaks
* the model out of tenant snapshot/restore (and the withTenantScope contract)
* and previously only surfaced via the cryptic packages/cli `models.test.ts`
* failure that broke the R-P6 CI run.
*
* This dedicated check fails loudly and names the offending model + schema so
* the fix is one-line obvious, and runs project-wide in CI (.gitea/workflows/
* ci.yml) instead of only inside the cli package test bucket.
*
* The schema-parsing here is kept intentionally in sync with
* packages/cli/src/commands/tenant/models.test.ts (same model-block + `tenantId
* String` regexes) so both gates agree on what "tenant-scoped" means.
*
* NOTE on the optional search-registry check: a "models with a packages/search
* schema must be in indexed-entities" assertion was deliberately NOT added —
* search index schemas (rows, users, entities, hr-directory, …) do not 1:1 map
* to a Prisma model and most are intentionally absent from the registry, so
* such a check would produce false positives. Out of scope, left to a future CR.
*/
import { readFileSync, readdirSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const here = dirname(fileURLToPath(import.meta.url));
const root = join(here, "..");
const schemaDir = join(root, "prisma", "schema");
const modelsFile = join(root, "packages", "cli", "src", "commands", "tenant", "models.ts");
/**
* Parse every prisma schema file and return the models that declare a
* `tenantId String` scalar (nullable `String?` included, matching the
* `^\s*tenantId\s+String` regex used by models.test.ts).
*/
function listTenantScopedSchemaModels() {
const files = readdirSync(schemaDir).filter((f) => f.endsWith(".prisma"));
const result = [];
for (const file of files) {
const text = readFileSync(join(schemaDir, file), "utf8");
const modelBlock = /model\s+(\w+)\s*\{([\s\S]*?)\n\}/g;
let m;
while ((m = modelBlock.exec(text)) !== null) {
const name = m[1];
const body = m[2];
if (/^\s*tenantId\s+String/m.test(body)) {
result.push({ model: name, schema: file });
}
}
}
return result;
}
/**
* Extract the model names registered in the TENANT_SCOPED_MODELS array of
* models.ts. Only the TENANT_SCOPED_MODELS block is scanned so that the
* GLOBAL_REFERENCED_MODELS / referencedBy `model:` keys further down the file
* are not picked up.
*/
function listRegisteredModels() {
const text = readFileSync(modelsFile, "utf8");
const start = text.indexOf("export const TENANT_SCOPED_MODELS");
if (start === -1) {
throw new Error(`could not find TENANT_SCOPED_MODELS in ${modelsFile}`);
}
const end = text.indexOf("];", start);
if (end === -1) {
throw new Error(`could not find end of TENANT_SCOPED_MODELS array in ${modelsFile}`);
}
const block = text.slice(start, end);
const names = new Set();
const entry = /model:\s*"([^"]+)"/g;
let m;
while ((m = entry.exec(block)) !== null) {
names.add(m[1]);
}
return names;
}
const schemaModels = listTenantScopedSchemaModels();
const registered = listRegisteredModels();
const missing = schemaModels.filter((m) => !registered.has(m.model));
if (missing.length > 0) {
const lines = missing.map(
(m) =>
`- Modell ${m.model} (schema ${m.schema}) hat tenantId, fehlt aber in ` +
`TENANT_SCOPED_MODELS — eintragen in packages/cli/src/commands/tenant/models.ts`,
);
process.stderr.write(
[
"FAIL cross-cutting model registry completeness",
...lines,
`Summary: ${schemaModels.length - missing.length}/${schemaModels.length} tenantId models registered, ${missing.length} missing`,
].join("\n") + "\n",
);
process.exit(1);
}
process.stdout.write(
`PASS cross-cutting model registry completeness\n` +
`Summary: ${schemaModels.length}/${schemaModels.length} tenantId models registered, 0 missing\n`,
);