115 lines
4.4 KiB
TypeScript
115 lines
4.4 KiB
TypeScript
import { readFile, readdir } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
/**
|
|
* Spec §17 / §24 / §50. Observability boundary gates:
|
|
* - feature (and every non-adapter) code never imports a vendor telemetry SDK,
|
|
* - no global union of every feature error or telemetry payload exists,
|
|
* - the dependency rule that enforces the first point stays configured.
|
|
*/
|
|
|
|
const ROOT = path.resolve(".");
|
|
|
|
async function productionSources(directory = path.join(ROOT, "src")): Promise<string[]> {
|
|
const files: string[] = [];
|
|
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
|
const target = path.join(directory, entry.name);
|
|
if (entry.isDirectory()) files.push(...(await productionSources(target)));
|
|
else if (/\.tsx?$/u.test(entry.name) && !/\.d\.ts$/u.test(entry.name)) {
|
|
files.push(target);
|
|
}
|
|
}
|
|
return files;
|
|
}
|
|
|
|
async function readProductionSources(): Promise<string> {
|
|
const files = await productionSources();
|
|
const sources = await Promise.all(files.map((file) => readFile(file, "utf8")));
|
|
return sources.join("\n");
|
|
}
|
|
|
|
type DependencyRule = Readonly<{
|
|
name: string;
|
|
from: Readonly<{ path?: string; pathNot?: string }>;
|
|
to: Readonly<{ path?: string }>;
|
|
}>;
|
|
|
|
async function dependencyRules(): Promise<readonly DependencyRule[]> {
|
|
const config = JSON.parse(
|
|
await readFile(path.join(ROOT, ".dependency-cruiser.json"), "utf8"),
|
|
) as { forbidden: DependencyRule[] };
|
|
return config.forbidden;
|
|
}
|
|
|
|
const VENDOR_SDKS = [
|
|
"@sentry/browser",
|
|
"@sentry/react",
|
|
"@datadog/browser-rum",
|
|
"@datadog/browser-logs",
|
|
"@opentelemetry/api",
|
|
"@newrelic/browser-agent",
|
|
"logrocket",
|
|
"posthog-js",
|
|
];
|
|
|
|
describe("observability boundaries", () => {
|
|
it("configures a rule that rejects vendor SDK imports outside adapters", async () => {
|
|
const rule = (await dependencyRules()).find(
|
|
({ name }) => name === "observability-vendors-live-behind-adapters",
|
|
);
|
|
expect(rule).toBeDefined();
|
|
const from = new RegExp(rule?.from.path ?? "$^", "u");
|
|
const fromNot = new RegExp(rule?.from.pathNot ?? "$^", "u");
|
|
const to = new RegExp(rule?.to.path ?? "$^", "u");
|
|
|
|
const applies = (source: string, target: string) =>
|
|
from.test(source) && !fromNot.test(source) && to.test(target);
|
|
|
|
for (const vendor of VENDOR_SDKS) {
|
|
expect(applies("src/features/catalog/application/x.ts", vendor), vendor).toBe(true);
|
|
expect(applies("src/platform/telemetry/x.ts", vendor), vendor).toBe(true);
|
|
expect(applies("src/adapters/telemetry/vendor-transport.ts", vendor), vendor).toBe(false);
|
|
}
|
|
expect(applies("src/features/a/x.ts", "react")).toBe(false);
|
|
expect(applies("src/features/a/x.ts", "@sentry-like/other")).toBe(false);
|
|
});
|
|
|
|
it("finds no vendor telemetry SDK import in production sources outside adapters", async () => {
|
|
const offenders: string[] = [];
|
|
const vendorImport =
|
|
/from\s+["'](@sentry|@datadog|@opentelemetry|@newrelic|newrelic|logrocket|posthog-js|mixpanel-browser|@amplitude|@bugsnag)(\/[^"']*)?["']/u;
|
|
for (const file of await productionSources()) {
|
|
const relative = path.relative(ROOT, file).split(path.sep).join("/");
|
|
if (relative.startsWith("src/adapters/")) continue;
|
|
if (vendorImport.test(await readFile(file, "utf8"))) offenders.push(relative);
|
|
}
|
|
expect(offenders).toEqual([]);
|
|
});
|
|
|
|
it("does not define installed/global unions of all feature errors or telemetry payloads", async () => {
|
|
const source = await readProductionSources();
|
|
expect(source).not.toMatch(
|
|
/AllFeatureErrors|GlobalFeatureErrors|GlobalApplicationError|InstalledFeatureErrors|AllTelemetryEvents|AllFeatureTelemetry/u,
|
|
);
|
|
});
|
|
|
|
it("keeps the platform kernel independent of concrete product modules", async () => {
|
|
const rule = (await dependencyRules()).find(
|
|
({ name }) => name === "platform-kernel-is-product-neutral",
|
|
);
|
|
expect(rule).toBeDefined();
|
|
const offenders: string[] = [];
|
|
for (const file of await productionSources(path.join(ROOT, "src/platform"))) {
|
|
const relative = path.relative(ROOT, file).split(path.sep).join("/");
|
|
if (relative.startsWith("src/platform/routing/")) continue;
|
|
const source = await readFile(file, "utf8");
|
|
if (/from\s+["']\.\.\/\.\.\/(features|contexts|apps|adapters|bootstrap|presentation|application)\//u.test(source)) {
|
|
offenders.push(relative);
|
|
}
|
|
}
|
|
expect(offenders).toEqual([]);
|
|
});
|
|
});
|