Files
HealthLog/src/__tests__/no-control-bytes-guard.test.ts
T
Marc-André Bombeck 4dc3bd46bf test(guards): read the source tree once per worker through a shared index
walkSourceFiles now lists each indexed folder (src, e2e, prisma) once
per worker and filters it by prefix, readSource and readSourceFolder read
through the same memoised file cache, and readSourceText is the cached
read for guards that read files themselves. The guards project shares a
module graph per worker, so a file is read once however many guards ask
for it. sourcesContaining adds the substring pre-filter for sweeps that
look for a few markers. Every guard over 1.5 s now reads through it;
anything outside the indexed folders is read from disk as before.
2026-10-22 10:30:50 +02:00

44 lines
1.6 KiB
TypeScript

import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { walkSourceFiles } from "@/__tests__/helpers/source-files";
import { readSourceText } from "./helpers/source-files";
/**
* No raw control byte in a source file, apart from tab, newline and carriage
* return.
*
* A control byte typed into a string literal is invisible in an editor, a
* diff and a review, renders as nothing or as a box, and survives copy and
* paste unseen; the same value written as an escape (`"\x01"`) is the same
* string and can be read. Three such bytes had crept in, one of them in a
* production join key.
*/
const CONTROL = /[\x00-\x08\x0b\x0c\x0e-\x1f]/;
describe("source files carry no raw control bytes", () => {
it("every file under src/ is free of them", () => {
const src = join(process.cwd(), "src");
const files = walkSourceFiles(src, {
floor: 3000,
extensions: [".ts", ".tsx", ".js", ".mjs", ".css", ".json", ".md"],
}).filter((file) => !file.startsWith("generated/"));
// A sweep that read nothing would agree with any tree.
expect(files.length).toBeGreaterThan(3000);
const offenders: string[] = [];
for (const file of files) {
const lines = readSourceText(join(src, file)).split("\n");
lines.forEach((line, i) => {
if (CONTROL.test(line)) offenders.push(`${file}:${i + 1}`);
});
}
expect(offenders).toEqual([]);
});
it("recognises a control byte when one is there", () => {
expect(CONTROL.test("a\u0001b")).toBe(true);
expect(CONTROL.test("tab\tnewline\ncr\r")).toBe(false);
});
});