mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 15:22:12 +03:00
* test(quality): fail loudly when a source-scanning guard is negative-only A negative guard — assert.doesNotMatch(src, /x/) or src.includes(x) === false — passes against an empty string. Once the code it guards is extracted into another file the parent no longer contains the string, so the assertion keeps passing while protecting nothing. The regression coverage is deleted with no test turning red, which is exactly the failure mode the god-file decomposition campaign (#8617) is about to trigger 90-odd times. Adds tests/unit/source-scanner-guards.test.ts: a hard gate (no baseline, no allowlist) requiring every test variable bound to project source to carry at least one positive anchor. Classification runs on logical statements with strings, regexes and comments blanked out, so a guard wrapped across lines cannot slip past — that folding is what exposed 3 of the 7 violations. Fixes all 7 violations across 6 files with one stable top-level export anchor each. Two were security scope guards held only by multi-line negative assertions: the SSRF guards on /api/sync/initialize (#323) and the proxy-bypass guards on chatHelpers.ts and chatCore.ts (#3226) — the latter anchored on handleChatCore precisely because that file is a decomposition target. Adds tests/_helpers/readSrc.ts, a repo-root-relative reader that throws on a missing or empty file instead of returning "". Refs #8617 * docs(changelog): number the fragment for #8619 * chore(skills): sync cli-backup-sync SKILL.md with catalog Same tip fix as #8657 so Merge integrity is green without waiting for that PR to land. Regenerated via generate-agent-skills --apply.
58 lines
2.4 KiB
TypeScript
58 lines
2.4 KiB
TypeScript
// Shared reader for tests that assert against project source read as text.
|
|
//
|
|
// Why this throws instead of returning "" on a missing/empty file:
|
|
// static source scanners frequently carry NEGATIVE guards, e.g.
|
|
// assert.equal(src.includes(">Active Endpoints<"), false)
|
|
// A negative guard passes trivially against an empty string. So if the source is
|
|
// moved, renamed, or split into a new component file, a silently-empty read keeps
|
|
// the assertion green while it guards nothing at all — the regression coverage is
|
|
// deleted without a single test turning red. Failing loudly at read time makes
|
|
// that class of silent coverage loss impossible.
|
|
//
|
|
// The companion hard gate is tests/unit/source-scanner-guards.test.ts, which
|
|
// requires every source-bound variable to carry at least one positive anchor.
|
|
import { readFileSync } from "node:fs";
|
|
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
/**
|
|
* Repo root, derived from this file's location (tests/_helpers/) — never from cwd, so
|
|
* the reader behaves identically however the test runner was invoked.
|
|
*/
|
|
const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
|
|
|
/**
|
|
* Read a project source file as UTF-8 text, relative to the repository root.
|
|
*
|
|
* @param relPath Repo-relative path, e.g. "open-sse/executors/claude-web.ts".
|
|
* @throws If the file does not exist or its content is empty/whitespace-only.
|
|
*/
|
|
export function readSrc(relPath: string): string {
|
|
const absPath = isAbsolute(relPath) ? relPath : join(REPO_ROOT, relPath);
|
|
|
|
let content: string;
|
|
try {
|
|
content = readFileSync(absPath, "utf8");
|
|
} catch (err) {
|
|
const reason = err instanceof Error ? err.message : String(err);
|
|
throw new Error(
|
|
`readSrc("${relPath}"): cannot read source file at ${absPath}. ` +
|
|
`The file was probably moved, renamed, or split — update the test to point at ` +
|
|
`its new location instead of letting the assertions guard nothing. (${reason})`
|
|
);
|
|
}
|
|
|
|
if (!content.trim()) {
|
|
throw new Error(
|
|
`readSrc("${relPath}"): source file at ${absPath} is empty or whitespace-only. ` +
|
|
`Negative guards (assert.doesNotMatch / .includes(...) === false) pass trivially ` +
|
|
`against empty content, so an empty read would silently disable this test.`
|
|
);
|
|
}
|
|
|
|
return content;
|
|
}
|
|
|
|
/** The repository root this helper resolves against. Exposed for tests that need it. */
|
|
export { REPO_ROOT };
|