mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
The linear-fit guard floored only its walk; it now also requires the implementation's call sites it expects. The per-type readings query aliased its driving relation t, which the i18n call-site scanner read as a translation call; it is mt now, as in readLatestEver.
78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
/**
|
||
* Structural guard: a least-squares line is fitted in `lib/stats/linear-fit`.
|
||
*
|
||
* Six readers fitted one by hand, four with the determinant form
|
||
* `n·Σxy − Σx·Σy` and two with centred sums, and two charts carried the same
|
||
* function twice. The matcher flags either form of the slope outside the
|
||
* implementation. Comments are stripped first; the planted cases prove the
|
||
* matcher fires.
|
||
*/
|
||
import { readFileSync } from "node:fs";
|
||
import { join } from "node:path";
|
||
import { describe, expect, it } from "vitest";
|
||
|
||
import { walkSourceFiles } from "./helpers/source-files";
|
||
|
||
const SRC = join(process.cwd(), "src");
|
||
|
||
const ALLOWED: Record<string, string> = {
|
||
"lib/stats/linear-fit.ts": "The implementation.",
|
||
};
|
||
|
||
const HAND_ROLLED_SLOPE =
|
||
/\bn\s*\*\s*sum_?xy\s*-\s*sum_?x\s*\*\s*sum_?y\b|\bsxy\s*\/\s*sxx\b|\bsumXy\s*-\s*\(\s*sumX\s*\*\s*sumY\s*\)\s*\/\s*n\b/i;
|
||
|
||
function stripComments(text: string): string {
|
||
return text.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "");
|
||
}
|
||
|
||
describe("one least-squares line", () => {
|
||
it("the matcher fires on a hand-rolled slope", () => {
|
||
expect(
|
||
HAND_ROLLED_SLOPE.test(
|
||
"const slope = (n * sumXY - sumX * sumY) / denominator;",
|
||
),
|
||
).toBe(true);
|
||
expect(HAND_ROLLED_SLOPE.test("const slope = sxy / sxx;")).toBe(true);
|
||
expect(
|
||
HAND_ROLLED_SLOPE.test("const sxy = sumXy - (sumX * sumY) / n;"),
|
||
).toBe(true);
|
||
expect(HAND_ROLLED_SLOPE.test("const fit = linearFit(xs, ys);")).toBe(
|
||
false,
|
||
);
|
||
});
|
||
|
||
it("no file outside the implementation fits its own", () => {
|
||
const files = walkSourceFiles(SRC, { floor: 3000 }).filter(
|
||
(file) =>
|
||
!file.startsWith("generated/") &&
|
||
!file.includes("__tests__/") &&
|
||
!/\.test\.tsx?$/.test(file),
|
||
);
|
||
const offenders = files.filter(
|
||
(file) =>
|
||
!(file in ALLOWED) &&
|
||
HAND_ROLLED_SLOPE.test(
|
||
stripComments(readFileSync(join(SRC, file), "utf8")),
|
||
),
|
||
);
|
||
expect(offenders).toEqual([]);
|
||
// The six readers now call the implementation: a sweep that finds fewer
|
||
// call sites read the wrong tree or lost one.
|
||
const callers = files.filter((file) =>
|
||
/\b(?:linearFit|fitFromSums|trendLineByTime)\s*\(/.test(
|
||
stripComments(readFileSync(join(SRC, file), "utf8")),
|
||
),
|
||
);
|
||
expect(callers.length).toBeGreaterThanOrEqual(7);
|
||
// The implementation itself matches, so the sweep read real files.
|
||
expect(
|
||
HAND_ROLLED_SLOPE.test(
|
||
stripComments(
|
||
readFileSync(join(SRC, "lib/stats/linear-fit.ts"), "utf8"),
|
||
),
|
||
),
|
||
).toBe(true);
|
||
});
|
||
});
|