mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-08 00:02:20 +03:00
* feat(quality): generic ratchet comparator (multi-metric, regression-only)
* chore(ci): Fase 0 quality-gate fixes — reconcile coverage gate (40->60), tier npm audit, wire orphaned contract gates, re-enable cheap husky pre-commit
* feat(quality): ratchet engine (collector + frozen baseline + CI job) and provider-consistency gate
- collect-metrics.mjs: emits quality-metrics.json (ESLint warnings + coverage when present)
- quality-baseline.json: frozen baseline (eslintWarnings=3482, regression-only)
- ci.yml: quality-gate job (ratchet + step summary + artifact) and check:provider-consistency in lint job
- check-provider-consistency.ts: every REGISTRY id must be a canonical provider (found krutrim half-registered → allowlisted as known pre-existing, blocks any NEW orphan)
- TDD: 9 tests (5 ratchet + 4 provider-consistency)
* feat(quality): Fase 2 anti-hallucination gates — fetch-targets, openapi-routes, deps allowlist
- check-fetch-targets: every dashboard fetch(/api/...) resolves to a real route.ts; found 7 pre-existing dashboard->route mismatches frozen as KNOWN_MISSING for triage
- check-openapi-routes: every openapi.yaml path resolves to a real route; found 1 stale spec entry (agent-bridge agents/{id}/state) frozen as KNOWN_STALE_SPEC
- check-deps: anti-slopsquatting allowlist (105 deps); new deps need explicit human-reviewed entry
- all wired into CI lint/docs jobs; TDD +12 tests (21 total across 5 gates)
* docs(quality): add quality-gates report + implementation plan to repo root
* feat(quality): Fase 3a — file-size ratchet (freeze 91 files >800 LOC, cap 800 for new)
- check-file-size.mjs: frozen files can only shrink; new files must be <= cap (kills the next 12k-line god-component)
- file-size-baseline.json: 91 files frozen at current LOC (largest 12883)
- wired into CI lint job; TDD 5 tests; --update ratchets the baseline down on shrink
* feat(quality): Fase 3b — duplication ratchet (jscpd@4, baseline 5.72%)
- check-duplication.mjs: runs jscpd@4 (pinned; v5 is an incompatible Rust rewrite) over src+open-sse, fails if duplication % rises vs frozen baseline (5.72%, measured: 1358 clones / 22967 dup lines). Targets the executor copy-paste (48/50 override execute() wholesale)
- wired into the parallel quality-gate CI job (off the lint critical path); TDD 4 tests; --update ratchets down
- snapshot now complete: coverage ~82.6%, eslint 3482 (98.5% no-explicit-any), duplication 5.72%, 91 files >800 LOC
* feat(quality): Fase 4a — anti test-masking gate
- check-test-masking.mjs: for each MODIFIED test file in a PR, flags net assert removal + new assert.ok(true) tautologies (base...HEAD diff). Directly enforces CLAUDE.md 'never weaken asserts to go green'
- wired into pr-test-policy CI job (reuses base fetch); no-op outside PR; TDD 5 tests
* feat(quality): Fase 4b — coverage ratchet (conservative floors, CI consumes merged coverage)
- quality-baseline.json: coverage.{statements,lines,functions,branches} floors (80/80/82/73, real ~82.58/82.58/84.23/75.22 with margin; tighten via --update after a green main run)
- check-quality-ratchet.mjs: --allow-missing (local quality:gate skips coverage.* without a coverage run; CI runs strict)
- ci.yml quality-gate job: needs test-coverage + downloads merged coverage-report so the ratchet enforces 'coverage cannot drop'
- TDD +1 test (6 total)
* feat(quality): Fase 6 — 8 new gates (Rule #11/#12, migrations, known-symbols, route-guard, complexity, docs-symbols, db-rules)
Deterministic gates, each freezing pre-existing violations in a documented allowlist (ratchet) so they pass now and block only NEW regressions:
- check-error-helper (Rule #12): 7 executors/handlers forwarding raw err.message frozen
- check-public-creds (Rule #11): 5 literal client_ids (Claude/Codex/Qwen/Kimi/Copilot) frozen
- check-migration-numbering: gaps 026/055 + dup 041 frozen (prevents the git-rm-deleted-migration incident)
- check-known-symbols: 93 executors conformance + 15 combo strategies + 18 translator pairs
- check-route-guard-membership (#15/#17): all 25 spawn-capable routes verified local-only (0 gaps)
- check-complexity: cyclomatic>15 / fn-length>80 ratchet (baseline 1739)
- check-docs-symbols: 30 stale doc /api refs frozen (docs hallucination)
- check-db-rules (#2/#5): 25 unexported db modules + 15 raw-SQL routes frozen
Wired into CI (lint / docs-sync-strict / quality-gate jobs). 115 TDD tests, all green. ESLint ratchet held at 3482.
* docs(quality): Phase 7 plan (security/dead-code/mutation/community tooling) — GATED to 2026-06-16
Stored, not active. 7 suggested gates + all discussed OSS/Community tools (SonarQube Community + osv-scanner + CodeQL + knip + sonarjs + type-coverage + lockfile-lint + Stryker + size-limit + axe-core + semcheck + agent-lsp + Qlty). Activation gate: do not start before 2026-06-16 (use Phases 0-6 in production for 1 week, validate in practice, then evolve).
180 lines
6.9 KiB
TypeScript
180 lines
6.9 KiB
TypeScript
// Tests for the Rule #12 error-sanitization gate (scripts/check/check-error-helper.mjs).
|
|
// Exercises the pure findErrorHelperViolations() against synthetic file shapes so the
|
|
// conservative heuristic (flag direct + indirect raw-error leaks, never internal sinks
|
|
// or helper-importing files) is locked down as a regression guard.
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
// @ts-expect-error — .mjs gate module has no type declarations; runtime shape is known.
|
|
import { findErrorHelperViolations, KNOWN_MISSING_ERROR_HELPER } from "../../scripts/check/check-error-helper.mjs";
|
|
|
|
type FileEntry = { path: string; source: string };
|
|
type FindFn = (files: FileEntry[], allowlist: Set<string>) => string[];
|
|
const find = findErrorHelperViolations as FindFn;
|
|
const allowlist = KNOWN_MISSING_ERROR_HELPER as Set<string>;
|
|
|
|
const EMPTY = new Set<string>();
|
|
|
|
function run(source: string, path = "open-sse/executors/x.ts"): string[] {
|
|
return find([{ path, source } as FileEntry], EMPTY);
|
|
}
|
|
|
|
test("flags raw err.message assigned directly to an error: field", () => {
|
|
const src = `export function exec() {
|
|
try { doThing(); } catch (err) {
|
|
return { success: false, status: 502, error: err.message };
|
|
}
|
|
}`;
|
|
assert.deepEqual(run(src), ["open-sse/executors/x.ts"]);
|
|
});
|
|
|
|
test("flags raw err.message interpolated into a message: field", () => {
|
|
const src = `function build(err: Error) {
|
|
return new Response(JSON.stringify({ error: { message: \`boom: \${err.message}\` } }));
|
|
}`;
|
|
assert.deepEqual(run(src), ["open-sse/executors/x.ts"]);
|
|
});
|
|
|
|
test("flags err.stack placed into a message: field", () => {
|
|
const src = `function build(err: Error) {
|
|
return { error: { message: err.stack } };
|
|
}`;
|
|
assert.deepEqual(run(src), ["open-sse/executors/x.ts"]);
|
|
});
|
|
|
|
test("flags multi-line OpenAI error envelope inside new Response()", () => {
|
|
const src = `function build(err: unknown) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: {
|
|
message: isTls
|
|
? \`tls failed: \${(err as Error).message}\`
|
|
: \`conn failed: \${err instanceof Error ? err.message : String(err)}\`,
|
|
type: "upstream_error",
|
|
},
|
|
}),
|
|
{ status: 502 }
|
|
);
|
|
}`;
|
|
assert.deepEqual(run(src), ["open-sse/executors/x.ts"]);
|
|
});
|
|
|
|
test("flags a tainted local variable passed into a response-builder call", () => {
|
|
const src = `function makeErrorResponse(s: number, m: string) { return new Response(m); }
|
|
function exec(err: unknown) {
|
|
const msg = err instanceof Error ? err.message : String(err);
|
|
return { response: makeErrorResponse(401, \`auth failed: \${msg}\`) };
|
|
}`;
|
|
assert.deepEqual(run(src), ["open-sse/executors/x.ts"]);
|
|
});
|
|
|
|
test("flags errResp(msg) where msg is tainted", () => {
|
|
const src = `function errResp(message: string) { return new Response(JSON.stringify({ error: { message } })); }
|
|
function exec(err: unknown) {
|
|
const msg = err instanceof Error ? err.message : "Failed to get nonce";
|
|
return { response: errResp(msg) };
|
|
}`;
|
|
assert.deepEqual(run(src), ["open-sse/executors/x.ts"]);
|
|
});
|
|
|
|
test("flags forwarded upstream body.error.message without sanitize", () => {
|
|
const src = `function build(body: { error: { message: string } }) {
|
|
return { success: false, error: body.error.message };
|
|
}`;
|
|
assert.deepEqual(run(src), ["open-sse/executors/x.ts"]);
|
|
});
|
|
|
|
// --- Negative cases: the gate must NOT flag these (conservative, no false positives) ---
|
|
|
|
test("does NOT flag a file that imports utils/error (relative)", () => {
|
|
const src = `import { sanitizeErrorMessage } from "../utils/error.ts";
|
|
function build(err: Error) { return { error: { message: err.message } }; }`;
|
|
assert.deepEqual(run(src), []);
|
|
});
|
|
|
|
test("does NOT flag a file that imports utils/error (workspace alias)", () => {
|
|
const src = `import { buildErrorBody } from "@omniroute/open-sse/utils/error";
|
|
function build(err: Error) { return new Response(JSON.stringify({ error: { message: \`x \${err.message}\` } })); }`;
|
|
assert.deepEqual(run(src), []);
|
|
});
|
|
|
|
test("does NOT flag raw err.message inside a saveCallLog audit row", () => {
|
|
const src = `function exec(err: Error) {
|
|
saveCallLog({
|
|
method: "POST",
|
|
status: 502,
|
|
error: err.message,
|
|
requestBody: rb,
|
|
}).catch(() => {});
|
|
return ok;
|
|
}`;
|
|
assert.deepEqual(run(src), []);
|
|
});
|
|
|
|
test("does NOT flag raw err.message inside a log call", () => {
|
|
const src = `function exec(err: Error) {
|
|
log?.error?.("X", \`refresh error: \${err.message}\`);
|
|
return ok;
|
|
}`;
|
|
assert.deepEqual(run(src), []);
|
|
});
|
|
|
|
test("does NOT flag err.message inside a thrown Error", () => {
|
|
const src = `function exec(err: Error) {
|
|
throw new Error(\`SPA send failed: \${err instanceof Error ? err.message : String(err)}\`);
|
|
}`;
|
|
assert.deepEqual(run(src), []);
|
|
});
|
|
|
|
test("does NOT flag err.message inside reject()", () => {
|
|
const src = `new Promise((_, reject) => {
|
|
onErr((err: Error) => reject(new Error(\`failed: \${err.message}\`)));
|
|
});`;
|
|
assert.deepEqual(run(src), []);
|
|
});
|
|
|
|
test("does NOT flag upstream-event read event.error.message", () => {
|
|
const src = `function parse(event: { error: { message: string } }) {
|
|
const content = typeof event.error === "string" ? event.error : event.error.message;
|
|
return { choices: [{ message: { content } }] };
|
|
}`;
|
|
assert.deepEqual(run(src), []);
|
|
});
|
|
|
|
test("does NOT flag a sanitized body.error.message line", () => {
|
|
const src = `function build(body: { error: { message: string } }) {
|
|
return { error: sanitizeErrorMessage(body.error.message) };
|
|
}`;
|
|
assert.deepEqual(run(src), []);
|
|
});
|
|
|
|
// --- Allowlist behavior ---
|
|
|
|
test("an allowlisted path is suppressed even when it would otherwise flag", () => {
|
|
const src = `function build(err: Error) { return { error: { message: err.message } }; }`;
|
|
const path = "open-sse/executors/legacy.ts";
|
|
assert.deepEqual(find([{ path, source: src } as FileEntry], EMPTY), [path]);
|
|
assert.deepEqual(find([{ path, source: src } as FileEntry], new Set([path])), []);
|
|
});
|
|
|
|
test("the shipped allowlist freezes exactly the known current violators", () => {
|
|
const frozen = [...allowlist].sort();
|
|
assert.deepEqual(frozen, [
|
|
"open-sse/executors/adapta-web.ts",
|
|
"open-sse/executors/deepseek-web.ts",
|
|
"open-sse/executors/perplexity-web.ts",
|
|
"open-sse/executors/qoder.ts",
|
|
"open-sse/executors/veoaifree-web.ts",
|
|
"open-sse/handlers/embeddings.ts",
|
|
"open-sse/handlers/search.ts",
|
|
]);
|
|
});
|
|
|
|
test("returns multiple violating paths and preserves input order", () => {
|
|
const files: FileEntry[] = [
|
|
{ path: "open-sse/executors/a.ts", source: `return { error: { message: err.message } };` },
|
|
{ path: "open-sse/executors/b.ts", source: `import { x } from "../utils/error.ts"; return { error: err.message };` },
|
|
{ path: "open-sse/executors/c.ts", source: `return { error: e.stack };` },
|
|
];
|
|
assert.deepEqual(find(files, EMPTY), ["open-sse/executors/a.ts", "open-sse/executors/c.ts"]);
|
|
});
|