mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 07:12:12 +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).
199 lines
7.6 KiB
TypeScript
199 lines
7.6 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import {
|
|
collectDbModules,
|
|
extractReexportedModules,
|
|
findMissingReexports,
|
|
hasLogic,
|
|
extractStringLiterals,
|
|
findRawSql,
|
|
collectSqlScanFiles,
|
|
} from "../../scripts/check/check-db-rules.mjs";
|
|
|
|
const REPO_ROOT = path.resolve(fileURLToPath(import.meta.url), "../../..");
|
|
const LOCAL_DB = path.join(REPO_ROOT, "src/lib/localDb.ts");
|
|
|
|
// ---------- (a) re-export completeness ----------
|
|
|
|
test("findMissingReexports: flags a NEW db module that is not re-exported", () => {
|
|
const dbModules = ["providers", "brandNewModule"];
|
|
const reexported = new Set(["providers"]) as Set<string>;
|
|
const allowlist = new Set<string>();
|
|
const missing = findMissingReexports(dbModules, reexported, allowlist) as string[];
|
|
assert.deepEqual(missing, ["brandNewModule"]);
|
|
});
|
|
|
|
test("findMissingReexports: a re-exported module passes", () => {
|
|
const dbModules = ["providers"];
|
|
const reexported = new Set(["providers"]) as Set<string>;
|
|
const missing = findMissingReexports(dbModules, reexported, new Set<string>()) as string[];
|
|
assert.deepEqual(missing, []);
|
|
});
|
|
|
|
test("findMissingReexports: an allowlisted (frozen) module passes even if not re-exported", () => {
|
|
const dbModules = ["notion"];
|
|
const reexported = new Set<string>();
|
|
const allowlist = new Set(["notion"]) as Set<string>;
|
|
const missing = findMissingReexports(dbModules, reexported, allowlist) as string[];
|
|
assert.deepEqual(missing, []);
|
|
});
|
|
|
|
test("extractReexportedModules: parses ./db/X from export forms", () => {
|
|
const src = [
|
|
'export { getCombos } from "./db/combos";',
|
|
'export * from "./db/featureFlags";',
|
|
'export type { Webhook } from "./db/webhooks";',
|
|
'export { sumUsageTokensThisMonth } from "./db/usageSummary";',
|
|
// not a db module — must be ignored
|
|
'export { initPricingSync } from "./pricingSync";',
|
|
].join("\n");
|
|
const mods = extractReexportedModules(src) as Set<string>;
|
|
assert.equal(mods.has("combos"), true);
|
|
assert.equal(mods.has("featureFlags"), true);
|
|
assert.equal(mods.has("webhooks"), true);
|
|
assert.equal(mods.has("usageSummary"), true);
|
|
assert.equal(mods.has("pricingSync"), false);
|
|
});
|
|
|
|
test("collectDbModules: returns real modules and excludes core/localDb/index", () => {
|
|
const mods = collectDbModules() as string[];
|
|
assert.ok(mods.includes("providers"), "expected providers module");
|
|
assert.ok(mods.includes("combos"), "expected combos module");
|
|
assert.equal(mods.includes("core"), false, "core must be excluded");
|
|
assert.equal(mods.includes("localDb"), false, "localDb must be excluded");
|
|
assert.equal(mods.includes("index"), false, "index must be excluded");
|
|
});
|
|
|
|
// FREEZE GUARD: the live repo state must be green under the shipped allowlist.
|
|
test("live repo: no NEW unexported db modules beyond the frozen allowlist", async () => {
|
|
// Re-import the gate's frozen allowlist indirectly by running its default behavior:
|
|
// findMissingReexports with the gate default allowlist must be empty for the repo.
|
|
const dbModules = collectDbModules() as string[];
|
|
const reexported = extractReexportedModules(fs.readFileSync(LOCAL_DB, "utf8")) as Set<string>;
|
|
// Default allowlist (KNOWN_UNEXPORTED) is applied inside findMissingReexports.
|
|
const missing = findMissingReexports(dbModules, reexported) as string[];
|
|
assert.deepEqual(
|
|
missing,
|
|
[],
|
|
`Unexported db module(s) not in KNOWN_UNEXPORTED: ${missing.join(", ")}`
|
|
);
|
|
});
|
|
|
|
// ---------- (b) localDb has no logic ----------
|
|
|
|
test("hasLogic: false for a pure re-export layer", () => {
|
|
const src = [
|
|
"// re-export layer",
|
|
'export { a, b } from "./db/foo";',
|
|
'export * from "./db/bar";',
|
|
'export type { T } from "./db/baz";',
|
|
].join("\n");
|
|
assert.equal(hasLogic(src) as boolean, false);
|
|
});
|
|
|
|
test("hasLogic: true for a function declaration", () => {
|
|
const src = 'export { a } from "./db/foo";\nfunction doThing() { return 1; }';
|
|
assert.equal(hasLogic(src) as boolean, true);
|
|
});
|
|
|
|
test("hasLogic: true for an arrow-function const", () => {
|
|
const src = 'export { a } from "./db/foo";\nconst helper = (x) => x + 1;';
|
|
assert.equal(hasLogic(src) as boolean, true);
|
|
});
|
|
|
|
test("hasLogic: true for a class declaration", () => {
|
|
const src = 'export { a } from "./db/foo";\nclass Thing {}';
|
|
assert.equal(hasLogic(src) as boolean, true);
|
|
});
|
|
|
|
test("hasLogic: SQL/logic-looking text inside comments or strings does not trip", () => {
|
|
const src = [
|
|
"/* function notReal() {} */",
|
|
"// const fake = () => 1;",
|
|
'export const SOURCE = "./db/foo";', // string only, no function on rhs
|
|
'export { a } from "./db/foo";',
|
|
].join("\n");
|
|
// export const X = "string" is a value (not logic): the rhs is a string literal,
|
|
// so the arrow/call pattern must NOT match.
|
|
assert.equal(hasLogic(src) as boolean, false);
|
|
});
|
|
|
|
test("live repo: src/lib/localDb.ts contains no logic", () => {
|
|
const src = fs.readFileSync(LOCAL_DB, "utf8");
|
|
assert.equal(hasLogic(src) as boolean, false);
|
|
});
|
|
|
|
// ---------- (c) no raw SQL outside db/ ----------
|
|
|
|
test("extractStringLiterals: returns only string bodies, ignoring code", () => {
|
|
const code = 'import { x } from "y";\nconst q = `SELECT * FROM t`;\nobj.set(1);';
|
|
const literals = extractStringLiterals(code) as string;
|
|
assert.ok(literals.includes("SELECT * FROM t"), "captures the template body");
|
|
assert.ok(literals.includes("y"), "captures the import path string");
|
|
assert.equal(literals.includes("set"), false, "JS .set() call is not a string body");
|
|
});
|
|
|
|
test("findRawSql: flags a NEW route with raw SQL in a string literal", () => {
|
|
const tmp = path.join(REPO_ROOT, ".tmp-check-db-rules-raw-sql.route.ts");
|
|
fs.writeFileSync(
|
|
tmp,
|
|
'const rows = db.prepare(`SELECT id FROM users WHERE x = ?`).all();\n',
|
|
"utf8"
|
|
);
|
|
try {
|
|
const offenders = findRawSql([tmp], new Set<string>()) as string[];
|
|
assert.equal(offenders.length, 1, "raw SELECT...FROM should be flagged");
|
|
} finally {
|
|
fs.rmSync(tmp, { force: true });
|
|
}
|
|
});
|
|
|
|
test("findRawSql: does NOT flag SQL that only appears in a comment", () => {
|
|
const tmp = path.join(REPO_ROOT, ".tmp-check-db-rules-comment.route.ts");
|
|
fs.writeFileSync(tmp, "// SELECT id FROM users -- documentation only\nexport const x = 1;\n", "utf8");
|
|
try {
|
|
const offenders = findRawSql([tmp], new Set<string>()) as string[];
|
|
assert.deepEqual(offenders, []);
|
|
} finally {
|
|
fs.rmSync(tmp, { force: true });
|
|
}
|
|
});
|
|
|
|
test("findRawSql: does NOT flag JS .set()/import-from/new Set() false positives", () => {
|
|
const tmp = path.join(REPO_ROOT, ".tmp-check-db-rules-falsepos.route.ts");
|
|
fs.writeFileSync(
|
|
tmp,
|
|
[
|
|
'import { NextResponse } from "next/server";',
|
|
"const seen = new Set();",
|
|
"headers.set(key, value);",
|
|
"delete obj.field;",
|
|
].join("\n"),
|
|
"utf8"
|
|
);
|
|
try {
|
|
const offenders = findRawSql([tmp], new Set<string>()) as string[];
|
|
assert.deepEqual(offenders, []);
|
|
} finally {
|
|
fs.rmSync(tmp, { force: true });
|
|
}
|
|
});
|
|
|
|
test("findRawSql: an allowlisted (frozen) offender passes", () => {
|
|
const rel = "src/app/api/skills/[id]/route.ts";
|
|
const abs = path.join(REPO_ROOT, rel);
|
|
const allowlist = new Set([rel]) as Set<string>;
|
|
const offenders = findRawSql([abs], allowlist) as string[];
|
|
assert.deepEqual(offenders, []);
|
|
});
|
|
|
|
test("live repo: no NEW raw-SQL offenders beyond the frozen allowlist", () => {
|
|
// findRawSql uses the gate default allowlist (KNOWN_RAW_SQL) when none is passed.
|
|
const files = collectSqlScanFiles() as string[];
|
|
const offenders = findRawSql(files) as string[];
|
|
assert.deepEqual(offenders, [], `New raw-SQL offender(s): ${offenders.join(", ")}`);
|
|
});
|