test(guards): strip comments through the shared scanner

This commit is contained in:
Marc-André Bombeck committed 2026-10-20 09:51:12 +02:00
1 parent 5d1d6aa176
commit 5fdddaec0a
5 files changed
+20 -45

No files matched your search

@@ -21,7 +21,7 @@ import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { walkSourceFiles } from "./helpers/source-files";
import { stripComments, walkSourceFiles } from "./helpers/source-files";
const SRC = join(process.cwd(), "src");
@@ -34,10 +34,6 @@ const PEARSON_DEFINITION =
/(?:function\s+|const\s+)\w*(?:pearson|correlationCoefficient)\w*\s*(?:=|\()/i;
const DELETED = /(?<![A-Za-z])pearsonCorrelation\b/;
function stripComments(text: string): string {
return text.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "");
}
function sourceFiles(): string[] {
return walkSourceFiles(SRC, { floor: 3000 }).filter(
(p) =>
@@ -16,7 +16,7 @@ import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { walkSourceFiles } from "./helpers/source-files";
import { stripComments, walkSourceFiles } from "./helpers/source-files";
const SRC = join(process.cwd(), "src");
@@ -31,10 +31,6 @@ const CALLERS: Record<string, string> = {
const CALL = /\breconstructDoseHistory\s*\(/;
function stripComments(text: string): string {
return text.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "");
}
describe("one compliance ledger", () => {
it("the dose rows are built only where this file says", () => {
const callers = walkSourceFiles(SRC, { floor: 3000 })
@@ -19,7 +19,7 @@ import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { walkSourceFiles } from "./helpers/source-files";
import { stripComments, walkSourceFiles } from "./helpers/source-files";
const SRC = join(process.cwd(), "src");
@@ -37,24 +37,15 @@ const ALLOWED: Readonly<Record<string, string>> = {
"fills that input field from openInsightsCachedText",
};
/** The legacy name, not its sealed sibling, outside comment lines. */
/** The legacy name, not its sealed sibling, outside comments. */
const LEGACY_NAME = /\binsightsCachedText\b(?!Encrypted)/;
function codeLines(source: string): string[] {
return source.split("\n").filter((line) => {
const t = line.trim();
return !(t.startsWith("*") || t.startsWith("//") || t.startsWith("/*"));
});
}
function filesSpellingTheLegacyName(): string[] {
return walkSourceFiles(SRC, { floor: 2000 })
.filter((f) => !f.startsWith("generated/"))
.filter((f) => !f.includes("__tests__/") && !/\.test\.tsx?$/.test(f))
.filter((f) =>
codeLines(readFileSync(join(SRC, f), "utf8")).some((l) =>
LEGACY_NAME.test(l),
),
LEGACY_NAME.test(stripComments(readFileSync(join(SRC, f), "utf8"))),
);
}
@@ -21,17 +21,15 @@ import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { walkSourceFiles } from "./helpers/source-files";
import { stripComments, walkSourceFiles } from "./helpers/source-files";
/** The shared comment strip, plus SQL line comments inside template strings. */
function stripCode(text: string): string {
return stripComments(text).replace(/^\s*--.*$/gm, "");
}
const SRC = join(process.cwd(), "src");
function stripComments(text: string): string {
return text
.replace(/\/\*[\s\S]*?\*\//g, "")
.replace(/^\s*\/\/.*$/gm, "")
.replace(/^\s*--.*$/gm, "");
}
/** `DISTINCT ON (type)` or `DISTINCT ON (m."type")`, the type alone. */
const LATEST_PER_TYPE = /DISTINCT\s+ON\s*\(\s*(?:\w+\s*\.\s*)?"?type"?\s*\)/i;
@@ -64,18 +62,14 @@ describe("latest-per-type reads", () => {
it("no production read keeps one row per type with DISTINCT ON", () => {
const offenders = productionFiles().filter((file) =>
LATEST_PER_TYPE.test(
stripComments(readFileSync(join(SRC, file), "utf8")),
),
LATEST_PER_TYPE.test(stripCode(readFileSync(join(SRC, file), "utf8"))),
);
expect(offenders).toEqual([]);
});
it("the summaries slice is built only behind its cache cell", () => {
const callers = productionFiles().filter((file) =>
SLICE_BUILDER_CALL.test(
stripComments(readFileSync(join(SRC, file), "utf8")),
),
SLICE_BUILDER_CALL.test(stripCode(readFileSync(join(SRC, file), "utf8"))),
);
expect(callers).toEqual(["lib/analytics/summaries-slice.ts"]);
});
@@ -21,7 +21,12 @@ import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { walkSourceFiles } from "./helpers/source-files";
import { stripComments, walkSourceFiles } from "./helpers/source-files";
/** The shared comment strip, plus SQL line comments inside template strings. */
function stripCode(text: string): string {
return stripComments(text).replace(/^\s*--.*$/gm, "");
}
const SRC = join(process.cwd(), "src");
@@ -31,13 +36,6 @@ const SINGLE_SHIFT =
const DOUBLE_SHIFT =
/AT\s+TIME\s+ZONE\s+'UTC'\s*\)?\s*AT\s+TIME\s+ZONE\s+(?:\$\{|\$\d)/i;
function stripComments(text: string): string {
return text
.replace(/\/\*[\s\S]*?\*\//g, "")
.replace(/^\s*\/\/.*$/gm, "")
.replace(/^\s*--.*$/gm, "");
}
describe("naive timestamp zone shifts", () => {
it("the matcher fires on the single shift and spares the double one", () => {
expect(
@@ -62,7 +60,7 @@ describe("naive timestamp zone shifts", () => {
!/\.test\.tsx?$/.test(file),
);
const code = (file: string) =>
stripComments(readFileSync(join(SRC, file), "utf8"));
stripCode(readFileSync(join(SRC, file), "utf8"));
// The reads that shift correctly, so a sweep that reads nothing fails.
const doubleShifts = files.filter((file) => DOUBLE_SHIFT.test(code(file)));
expect(doubleShifts.length).toBeGreaterThanOrEqual(5);