Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/entitlements/src/boundary.test.ts
T

75 lines
2.7 KiB
TypeScript

import { readdir, readFile } from "node:fs/promises";
import { dirname, extname, join, relative, sep } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
const sourceRoot = dirname(fileURLToPath(import.meta.url));
const forbiddenImportPattern =
/(?:from\s+["']|import\s*\(\s*["'])(~\/|app\/|@hivegrid\/(?:db|config)(?:\/|$)|react(?:\/|$)|react-router(?:\/|$)|@prisma\/client(?:\/|$)|\.prisma\/client(?:\/|$)|prisma(?:\/|$))/;
const diagnosticTemplatePattern = /(?:message\s*[:=]|new\s+\w*Error\s*\()[^;\n]*/g;
const unsafeDiagnosticPattern = /tenant|customer|user|api[-_\s]?key|subscription[-_\s]?id|secret|sk_[a-z0-9]/i;
async function collectSourceFiles(directory: string): Promise<readonly string[]> {
const entries = await readdir(directory, { withFileTypes: true });
const files = await Promise.all(
entries.map(async (entry) => {
const entryPath = join(directory, entry.name);
if (entry.isDirectory()) {
return collectSourceFiles(entryPath);
}
const isSourceFile = extname(entryPath) === ".ts" && !entryPath.endsWith(".test.ts");
return isSourceFile ? [entryPath] : [];
})
);
return files.flat();
}
function toPackageRelativePath(absolutePath: string): string {
return relative(sourceRoot, absolutePath).split(sep).join("/");
}
describe("entitlements package boundary", () => {
it("keeps runtime source files free of app and framework imports", async () => {
const sourceFiles = await collectSourceFiles(sourceRoot);
expect(sourceFiles.map(toPackageRelativePath).sort()).toEqual([
"blueprints.ts",
"commercial-catalog.ts",
"customer-portal-commercial.ts",
"email-marketing-commercial.ts",
"evaluate.ts",
"features.ts",
"index.ts",
"plans.ts",
"product-access.ts",
"registration-plan-advisor.ts",
"seat-packages.ts",
"seat-usage.ts",
"seats.ts",
"storage-capacity.ts",
]);
for (const sourceFile of sourceFiles) {
const source = await readFile(sourceFile, "utf8");
expect(source, toPackageRelativePath(sourceFile)).not.toMatch(forbiddenImportPattern);
}
});
it("keeps exported diagnostic templates free of identifiers and secrets", async () => {
const exportedSourceFiles = [join(sourceRoot, "features.ts"), join(sourceRoot, "evaluate.ts")];
for (const sourceFile of exportedSourceFiles) {
const source = await readFile(sourceFile, "utf8");
const diagnosticTemplates = source.match(diagnosticTemplatePattern) ?? [];
expect(diagnosticTemplates.join("\n"), toPackageRelativePath(sourceFile)).not.toMatch(unsafeDiagnosticPattern);
}
});
});