Initial Commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* The approved-files set is empty because pagination is pure computation
|
||||
* with no randomness or timestamp requirements.
|
||||
*/
|
||||
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/,
|
||||
];
|
||||
|
||||
// No files are in the approved set — pagination has no randomness.
|
||||
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([]);
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user