64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
/**
|
|
* Enforce that production source files do not use disallowed globals
|
|
* (Math.random, Date.now, crypto.randomUUID) that would make the library
|
|
* non-deterministic or platform-dependent.
|
|
*/
|
|
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
|
|
const SRC_ROOT = join(import.meta.dirname, "../src");
|
|
|
|
const DISALLOWED = [
|
|
/\bMath\.random\b/,
|
|
/\bDate\.now\b/,
|
|
/\bcrypto\.randomUUID\b/,
|
|
];
|
|
|
|
// The tokenize strategy uses a module-level counter (no randomness).
|
|
// No files need to be approved for random usage.
|
|
const APPROVED_FILES: string[] = [];
|
|
|
|
function walkTs(dir: string): string[] {
|
|
const result: string[] = [];
|
|
for (const entry of readdirSync(dir)) {
|
|
const full = join(dir, entry);
|
|
if (statSync(full).isDirectory()) {
|
|
result.push(...walkTs(full));
|
|
} else if (entry.endsWith(".ts")) {
|
|
result.push(full);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function getSourceFiles(): string[] {
|
|
return walkTs(SRC_ROOT).filter(
|
|
(f) => !f.includes(".test.") && !f.includes(".bench.")
|
|
);
|
|
}
|
|
|
|
describe("no-disallowed globals", () => {
|
|
const sourceFiles = getSourceFiles();
|
|
|
|
it("found at least one source file to check", { timeout: 5000 }, () => {
|
|
expect(sourceFiles.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
for (const pattern of DISALLOWED) {
|
|
it(`no source file uses ${pattern.source}`, { timeout: 5000 }, () => {
|
|
const violations: string[] = [];
|
|
for (const file of sourceFiles) {
|
|
const relative = file.replace(`${SRC_ROOT}/`, "");
|
|
if (APPROVED_FILES.some((ap) => file.endsWith(ap))) continue;
|
|
|
|
const content = readFileSync(file, "utf-8");
|
|
if (pattern.test(content)) {
|
|
violations.push(relative);
|
|
}
|
|
}
|
|
expect(violations).toEqual([]);
|
|
});
|
|
}
|
|
});
|