Files
suaveplan/packages/testkit/testing/testing-e2e/tests/no-disallowed.test.ts
2026-05-27 23:47:53 +01:00

77 lines
3.0 KiB
TypeScript

import { readdir, readFile } from "node:fs/promises";
import { dirname, join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
const here = dirname(fileURLToPath(import.meta.url));
const srcRoot = resolve(here, "..", "src");
const PATTERNS: Array<{ name: string; regex: RegExp }> = [
{ name: "any", regex: /(?<![a-zA-Z0-9_])any(?![a-zA-Z0-9_])/ },
{ name: "ts-ignore", regex: /@ts-ignore/ },
{ name: "ts-expect-error", regex: /@ts-expect-error/ },
{ name: "TODO", regex: /\bTODO\b/ },
{ name: "FIXME", regex: /\bFIXME\b/ },
{ name: "@deprecated", regex: /@deprecated/ },
{ name: ".skip(", regex: /\.skip\s*\(/ },
{ name: ".only(", regex: /\.only\s*\(/ },
// waitForTimeout / wait_for_timeout emit CDP protocol traffic that
// reCAPTCHA v3 / Enterprise and other behavioural scorers
// detect — use a native sleep instead. Pattern matches both the
// Playwright JS form and any incidental Python form that might
// leak into TS via codegen.
{ name: "waitForTimeout", regex: /\bwaitForTimeout\s*\(/ },
{ name: "wait_for_timeout", regex: /\bwait_for_timeout\s*\(/ },
];
const EXCLUDED_FILE_SUFFIXES = [".test.ts", ".bench.ts"];
async function walk(dir: string, out: string[]): Promise<void> {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const full = join(dir, entry.name);
if (entry.isDirectory()) {
await walk(full, out);
} else if (entry.isFile() && entry.name.endsWith(".ts")) {
out.push(full);
}
}
}
describe("no-disallowed source guard", () => {
it("src/ contains no disallowed constructs", {
timeout: 10000,
}, async () => {
const all: string[] = [];
await walk(srcRoot, all);
const files = all.filter((file) => {
const rel = relative(srcRoot, file).replace(/\\/g, "/");
return !EXCLUDED_FILE_SUFFIXES.some((s) => rel.endsWith(s));
});
const violations: string[] = [];
for (const file of files) {
const text = await readFile(file, "utf8");
const lines = text.split("\n");
const rel = relative(srcRoot, file).replace(/\\/g, "/");
for (const { name, regex } of PATTERNS) {
for (let i = 0; i < lines.length; i++) {
const line = lines[i] ?? "";
const trimmed = line.trim();
if (
trimmed.startsWith("//") ||
trimmed.startsWith("*") ||
trimmed.startsWith("/*")
)
continue;
if (regex.test(line)) {
violations.push(
`${rel}:${i + 1} contains "${name}": ${trimmed}`
);
}
}
}
}
expect(violations).toEqual([]);
});
});