Skip to main content

fallow_cli/
explain.rs

1//! Metric and rule definitions for explainable CLI output.
2//!
3//! Provides structured metadata that describes what each metric, threshold,
4//! and rule means — consumed by the `_meta` object in JSON output and by
5//! SARIF `fullDescription` / `helpUri` fields.
6
7use serde_json::{Value, json};
8
9// ── Docs base URL ────────────────────────────────────────────────
10
11const DOCS_BASE: &str = "https://docs.fallow.tools";
12
13/// Docs URL for the dead-code (check) command.
14pub const CHECK_DOCS: &str = "https://docs.fallow.tools/cli/dead-code";
15
16/// Docs URL for the health command.
17pub const HEALTH_DOCS: &str = "https://docs.fallow.tools/cli/health";
18
19/// Docs URL for the dupes command.
20pub const DUPES_DOCS: &str = "https://docs.fallow.tools/cli/dupes";
21
22// ── Check rules ─────────────────────────────────────────────────
23
24/// Rule definition for SARIF `fullDescription` and JSON `_meta`.
25pub struct RuleDef {
26    pub id: &'static str,
27    pub name: &'static str,
28    pub short: &'static str,
29    pub full: &'static str,
30    pub docs_path: &'static str,
31}
32
33pub const CHECK_RULES: &[RuleDef] = &[
34    RuleDef {
35        id: "fallow/unused-file",
36        name: "Unused Files",
37        short: "File is not reachable from any entry point",
38        full: "Source files that are not imported by any other module and are not entry points (scripts, tests, configs). These files can safely be deleted. Detection uses graph reachability from configured entry points.",
39        docs_path: "explanations/dead-code#unused-files",
40    },
41    RuleDef {
42        id: "fallow/unused-export",
43        name: "Unused Exports",
44        short: "Export is never imported",
45        full: "Named exports that are never imported by any other module in the project. Includes both direct exports and re-exports through barrel files. The export may still be used locally within the same file.",
46        docs_path: "explanations/dead-code#unused-exports",
47    },
48    RuleDef {
49        id: "fallow/unused-type",
50        name: "Unused Type Exports",
51        short: "Type export is never imported",
52        full: "Type-only exports (interfaces, type aliases, enums used only as types) that are never imported. These do not generate runtime code but add maintenance burden.",
53        docs_path: "explanations/dead-code#unused-types",
54    },
55    RuleDef {
56        id: "fallow/unused-dependency",
57        name: "Unused Dependencies",
58        short: "Dependency listed but never imported",
59        full: "Packages listed in dependencies that are never imported or required by any source file. Framework plugins and CLI tools may be false positives — use the ignore_dependencies config to suppress.",
60        docs_path: "explanations/dead-code#unused-dependencies",
61    },
62    RuleDef {
63        id: "fallow/unused-dev-dependency",
64        name: "Unused Dev Dependencies",
65        short: "Dev dependency listed but never imported",
66        full: "Packages listed in devDependencies that are never imported by test files, config files, or scripts. Build tools and jest presets that are referenced only in config may appear as false positives.",
67        docs_path: "explanations/dead-code#unused-devdependencies",
68    },
69    RuleDef {
70        id: "fallow/unused-optional-dependency",
71        name: "Unused Optional Dependencies",
72        short: "Optional dependency listed but never imported",
73        full: "Packages listed in optionalDependencies that are never imported. Optional dependencies are typically platform-specific — verify they are not needed on any supported platform before removing.",
74        docs_path: "explanations/dead-code#unused-optionaldependencies",
75    },
76    RuleDef {
77        id: "fallow/type-only-dependency",
78        name: "Type-only Dependencies",
79        short: "Production dependency only used via type-only imports",
80        full: "Production dependencies that are only imported via `import type` statements. These can be moved to devDependencies since they generate no runtime code and are stripped during compilation.",
81        docs_path: "explanations/dead-code#type-only-dependencies",
82    },
83    RuleDef {
84        id: "fallow/unused-enum-member",
85        name: "Unused Enum Members",
86        short: "Enum member is never referenced",
87        full: "Enum members that are never referenced in the codebase. Uses scope-aware binding analysis to track all references including computed access patterns.",
88        docs_path: "explanations/dead-code#unused-enum-members",
89    },
90    RuleDef {
91        id: "fallow/unused-class-member",
92        name: "Unused Class Members",
93        short: "Class member is never referenced",
94        full: "Class methods and properties that are never referenced outside the class. Private members are checked within the class scope; public members are checked project-wide.",
95        docs_path: "explanations/dead-code#unused-class-members",
96    },
97    RuleDef {
98        id: "fallow/unresolved-import",
99        name: "Unresolved Imports",
100        short: "Import could not be resolved",
101        full: "Import specifiers that could not be resolved to a file on disk. Common causes: deleted files, typos in paths, missing path aliases in tsconfig, or uninstalled packages.",
102        docs_path: "explanations/dead-code#unresolved-imports",
103    },
104    RuleDef {
105        id: "fallow/unlisted-dependency",
106        name: "Unlisted Dependencies",
107        short: "Dependency used but not in package.json",
108        full: "Packages that are imported in source code but not listed in package.json. These work by accident (hoisted from another workspace package or transitive dep) and will break in strict package managers.",
109        docs_path: "explanations/dead-code#unlisted-dependencies",
110    },
111    RuleDef {
112        id: "fallow/duplicate-export",
113        name: "Duplicate Exports",
114        short: "Export name appears in multiple modules",
115        full: "The same export name is defined in multiple modules. Consumers may import from the wrong module, leading to subtle bugs. Consider renaming or consolidating.",
116        docs_path: "explanations/dead-code#duplicate-exports",
117    },
118    RuleDef {
119        id: "fallow/circular-dependency",
120        name: "Circular Dependencies",
121        short: "Circular dependency chain detected",
122        full: "A cycle in the module import graph. Circular dependencies cause undefined behavior with CommonJS (partial modules) and initialization ordering issues with ESM. Break cycles by extracting shared code.",
123        docs_path: "explanations/dead-code#circular-dependencies",
124    },
125    RuleDef {
126        id: "fallow/stale-suppression",
127        name: "Stale Suppressions",
128        short: "Suppression comment or tag no longer matches any issue",
129        full: "A fallow-ignore-next-line, fallow-ignore-file, or @expected-unused suppression that no longer matches any active issue. The underlying problem was fixed but the suppression was left behind. Remove it to keep the codebase clean.",
130        docs_path: "explanations/dead-code#stale-suppressions",
131    },
132];
133
134/// Look up a rule definition by its SARIF rule ID across all rule sets.
135#[must_use]
136pub fn rule_by_id(id: &str) -> Option<&'static RuleDef> {
137    CHECK_RULES
138        .iter()
139        .chain(HEALTH_RULES.iter())
140        .chain(DUPES_RULES.iter())
141        .find(|r| r.id == id)
142}
143
144/// Build the docs URL for a rule.
145#[must_use]
146pub fn rule_docs_url(rule: &RuleDef) -> String {
147    format!("{DOCS_BASE}/{}", rule.docs_path)
148}
149
150// ── Health SARIF rules ──────────────────────────────────────────
151
152pub const HEALTH_RULES: &[RuleDef] = &[
153    RuleDef {
154        id: "fallow/high-cyclomatic-complexity",
155        name: "High Cyclomatic Complexity",
156        short: "Function has high cyclomatic complexity",
157        full: "McCabe cyclomatic complexity exceeds the configured threshold. Cyclomatic complexity counts the number of independent paths through a function (1 + decision points: if/else, switch cases, loops, ternary, logical operators). High values indicate functions that are hard to test exhaustively.",
158        docs_path: "explanations/health#cyclomatic-complexity",
159    },
160    RuleDef {
161        id: "fallow/high-cognitive-complexity",
162        name: "High Cognitive Complexity",
163        short: "Function has high cognitive complexity",
164        full: "SonarSource cognitive complexity exceeds the configured threshold. Unlike cyclomatic complexity, cognitive complexity penalizes nesting depth and non-linear control flow (breaks, continues, early returns). It measures how hard a function is to understand when reading sequentially.",
165        docs_path: "explanations/health#cognitive-complexity",
166    },
167    RuleDef {
168        id: "fallow/high-complexity",
169        name: "High Complexity (Both)",
170        short: "Function exceeds both complexity thresholds",
171        full: "Function exceeds both cyclomatic and cognitive complexity thresholds. This is the strongest signal that a function needs refactoring, it has many paths AND is hard to understand.",
172        docs_path: "explanations/health#complexity-metrics",
173    },
174    RuleDef {
175        id: "fallow/high-crap-score",
176        name: "High CRAP Score",
177        short: "Function has a high CRAP score (complexity combined with low coverage)",
178        full: "The function's CRAP (Change Risk Anti-Patterns) score meets or exceeds the configured threshold. CRAP combines cyclomatic complexity with test coverage using the Savoia and Evans (2007) formula: `CC^2 * (1 - coverage/100)^3 + CC`. High CRAP indicates changes to this function carry high risk because it is complex AND poorly tested. Pair with `--coverage` for accurate per-function scoring; without it fallow estimates coverage from the module graph.",
179        docs_path: "explanations/health#crap-score",
180    },
181    RuleDef {
182        id: "fallow/refactoring-target",
183        name: "Refactoring Target",
184        short: "File identified as a high-priority refactoring candidate",
185        full: "File identified as a refactoring candidate based on a weighted combination of complexity density, churn velocity, dead code ratio, fan-in (blast radius), and fan-out (coupling). Categories: urgent churn+complexity, break circular dependency, split high-impact file, remove dead code, extract complex functions, reduce coupling.",
186        docs_path: "explanations/health#refactoring-targets",
187    },
188    RuleDef {
189        id: "fallow/untested-file",
190        name: "Untested File",
191        short: "Runtime-reachable file has no test dependency path",
192        full: "A file is reachable from runtime entry points but not from any discovered test entry point. This indicates production code that no test imports, directly or transitively, according to the static module graph.",
193        docs_path: "explanations/health#coverage-gaps",
194    },
195    RuleDef {
196        id: "fallow/untested-export",
197        name: "Untested Export",
198        short: "Runtime-reachable export has no test dependency path",
199        full: "A value export is reachable from runtime entry points but no test-reachable module references it. This is a static test dependency gap rather than line coverage, and highlights exports exercised only through production entry paths.",
200        docs_path: "explanations/health#coverage-gaps",
201    },
202    RuleDef {
203        id: "fallow/production-safe-to-delete",
204        name: "Production Safe To Delete",
205        short: "Statically unused AND never invoked in production with V8 tracking",
206        full: "The function is both statically unreachable in the module graph and was never invoked during the observed production coverage window. This is the highest-confidence delete signal fallow emits.",
207        docs_path: "explanations/health#production-coverage",
208    },
209    RuleDef {
210        id: "fallow/production-review-required",
211        name: "Production Review Required",
212        short: "Statically used but never invoked in production",
213        full: "The function is reachable in the module graph (or exercised by tests / untracked call sites) but was not invoked during the observed production coverage window. Needs a human look — may be seasonal, error-path only, or legitimately unused.",
214        docs_path: "explanations/health#production-coverage",
215    },
216    RuleDef {
217        id: "fallow/production-low-traffic",
218        name: "Production Low Traffic",
219        short: "Function was invoked below the low-traffic threshold",
220        full: "The function was invoked in production but below the configured `--low-traffic-threshold` fraction of total trace count (spec default 0.1%). Effectively dead for the current period.",
221        docs_path: "explanations/health#production-coverage",
222    },
223    RuleDef {
224        id: "fallow/production-coverage-unavailable",
225        name: "Production Coverage Unavailable",
226        short: "Production coverage could not be resolved for this function",
227        full: "The function could not be matched to a V8-tracked coverage entry. Common causes: the function lives in a worker thread (separate V8 isolate), it is lazy-parsed and never reached the JIT tier, or its source map did not resolve to the expected source path. This is advisory, not a dead-code signal.",
228        docs_path: "explanations/health#production-coverage",
229    },
230    RuleDef {
231        id: "fallow/production-coverage",
232        name: "Production Coverage",
233        short: "Production coverage finding",
234        full: "Generic production-coverage finding for verdicts not covered by a more specific rule. Covers the forward-compat `unknown` sentinel; the CLI filters `active` entries out of `production_coverage.findings` so the surfaced list stays actionable.",
235        docs_path: "explanations/health#production-coverage",
236    },
237];
238
239pub const DUPES_RULES: &[RuleDef] = &[RuleDef {
240    id: "fallow/code-duplication",
241    name: "Code Duplication",
242    short: "Duplicated code block",
243    full: "A block of code that appears in multiple locations with identical or near-identical token sequences. Clone detection uses normalized token comparison — identifier names and literals are abstracted away in non-strict modes.",
244    docs_path: "explanations/duplication#clone-groups",
245}];
246
247// ── JSON _meta builders ─────────────────────────────────────────
248
249/// Build the `_meta` object for `fallow dead-code --format json --explain`.
250#[must_use]
251pub fn check_meta() -> Value {
252    let rules: Value = CHECK_RULES
253        .iter()
254        .map(|r| {
255            (
256                r.id.replace("fallow/", ""),
257                json!({
258                    "name": r.name,
259                    "description": r.full,
260                    "docs": rule_docs_url(r)
261                }),
262            )
263        })
264        .collect::<serde_json::Map<String, Value>>()
265        .into();
266
267    json!({
268        "docs": CHECK_DOCS,
269        "rules": rules
270    })
271}
272
273/// Build the `_meta` object for `fallow health --format json --explain`.
274#[must_use]
275#[expect(
276    clippy::too_many_lines,
277    reason = "flat metric table: every entry is 3-4 short lines of metadata and keeping them in one map is clearer than splitting into per-metric helpers"
278)]
279pub fn health_meta() -> Value {
280    json!({
281        "docs": HEALTH_DOCS,
282        "metrics": {
283            "cyclomatic": {
284                "name": "Cyclomatic Complexity",
285                "description": "McCabe cyclomatic complexity: 1 + number of decision points (if/else, switch cases, loops, ternary, logical operators). Measures the number of independent paths through a function.",
286                "range": "[1, \u{221e})",
287                "interpretation": "lower is better; default threshold: 20"
288            },
289            "cognitive": {
290                "name": "Cognitive Complexity",
291                "description": "SonarSource cognitive complexity: penalizes nesting depth and non-linear control flow (breaks, continues, early returns). Measures how hard a function is to understand when reading top-to-bottom.",
292                "range": "[0, \u{221e})",
293                "interpretation": "lower is better; default threshold: 15"
294            },
295            "line_count": {
296                "name": "Function Line Count",
297                "description": "Number of lines in the function body.",
298                "range": "[1, \u{221e})",
299                "interpretation": "context-dependent; long functions may need splitting"
300            },
301            "lines": {
302                "name": "File Line Count",
303                "description": "Total lines of code in the file (from line offsets). Provides scale context for other metrics: a file with 0.4 complexity density at 80 LOC is different from 0.4 density at 800 LOC.",
304                "range": "[1, \u{221e})",
305                "interpretation": "context-dependent; large files may benefit from splitting even if individual functions are small"
306            },
307            "maintainability_index": {
308                "name": "Maintainability Index",
309                "description": "Composite score: 100 - (complexity_density \u{00d7} 30 \u{00d7} dampening) - (dead_code_ratio \u{00d7} 20) - min(ln(fan_out+1) \u{00d7} 4, 15), where dampening = min(lines/50, 1.0). Clamped to [0, 100]. Higher is better.",
310                "range": "[0, 100]",
311                "interpretation": "higher is better; <40 poor, 40\u{2013}70 moderate, >70 good"
312            },
313            "complexity_density": {
314                "name": "Complexity Density",
315                "description": "Total cyclomatic complexity divided by lines of code. Measures how densely complex the code is per line.",
316                "range": "[0, \u{221e})",
317                "interpretation": "lower is better; >1.0 indicates very dense complexity"
318            },
319            "dead_code_ratio": {
320                "name": "Dead Code Ratio",
321                "description": "Fraction of value exports (excluding type-only exports like interfaces and type aliases) with zero references across the project.",
322                "range": "[0, 1]",
323                "interpretation": "lower is better; 0 = all exports are used"
324            },
325            "fan_in": {
326                "name": "Fan-in (Importers)",
327                "description": "Number of files that import this file. High fan-in means high blast radius \u{2014} changes to this file affect many dependents.",
328                "range": "[0, \u{221e})",
329                "interpretation": "context-dependent; high fan-in files need careful review before changes"
330            },
331            "fan_out": {
332                "name": "Fan-out (Imports)",
333                "description": "Number of files this file directly imports. High fan-out indicates high coupling and change propagation risk.",
334                "range": "[0, \u{221e})",
335                "interpretation": "lower is better; MI penalty caps at ~40 imports"
336            },
337            "score": {
338                "name": "Hotspot Score",
339                "description": "normalized_churn \u{00d7} normalized_complexity \u{00d7} 100, where normalization is against the project maximum. Identifies files that are both complex AND frequently changing.",
340                "range": "[0, 100]",
341                "interpretation": "higher = riskier; prioritize refactoring high-score files"
342            },
343            "weighted_commits": {
344                "name": "Weighted Commits",
345                "description": "Recency-weighted commit count using exponential decay with 90-day half-life. Recent commits contribute more than older ones.",
346                "range": "[0, \u{221e})",
347                "interpretation": "higher = more recent churn activity"
348            },
349            "trend": {
350                "name": "Churn Trend",
351                "description": "Compares recent vs older commit frequency within the analysis window. accelerating = recent > 1.5\u{00d7} older, cooling = recent < 0.67\u{00d7} older, stable = in between.",
352                "values": ["accelerating", "stable", "cooling"],
353                "interpretation": "accelerating files need attention; cooling files are stabilizing"
354            },
355            "priority": {
356                "name": "Refactoring Priority",
357                "description": "Weighted score: complexity density (30%), hotspot boost (25%), dead code ratio (20%), fan-in (15%), fan-out (10%). Fan-in and fan-out normalization uses adaptive percentile-based thresholds (p95 of the project distribution). Does not use the maintainability index to avoid double-counting.",
358                "range": "[0, 100]",
359                "interpretation": "higher = more urgent to refactor"
360            },
361            "efficiency": {
362                "name": "Efficiency Score",
363                "description": "priority / effort_numeric (Low=1, Medium=2, High=3). Surfaces quick wins: high-priority, low-effort targets rank first. Default sort order.",
364                "range": "[0, 100] \u{2014} effective max depends on effort: Low=100, Medium=50, High\u{2248}33",
365                "interpretation": "higher = better quick-win value; targets are sorted by efficiency descending"
366            },
367            "effort": {
368                "name": "Effort Estimate",
369                "description": "Heuristic effort estimate based on file size, function count, and fan-in. Thresholds adapt to the project\u{2019}s distribution (percentile-based). Low: small file, few functions, low fan-in. High: large file, high fan-in, or many functions with high density. Medium: everything else.",
370                "values": ["low", "medium", "high"],
371                "interpretation": "low = quick win, high = needs planning and coordination"
372            },
373            "confidence": {
374                "name": "Confidence Level",
375                "description": "Reliability of the recommendation based on data source. High: deterministic graph/AST analysis (dead code, circular deps, complexity). Medium: heuristic thresholds (fan-in/fan-out coupling). Low: depends on git history quality (churn-based recommendations).",
376                "values": ["high", "medium", "low"],
377                "interpretation": "high = act on it, medium = verify context, low = treat as a signal, not a directive"
378            },
379            "health_score": {
380                "name": "Health Score",
381                "description": "Project-level aggregate score computed from vital signs: dead code, complexity, maintainability, hotspots, unused dependencies, and circular dependencies. Penalties subtracted from 100. Missing metrics (from pipelines that didn't run) don't penalize. Use --score to force full pipeline for maximum accuracy.",
382                "range": "[0, 100]",
383                "interpretation": "higher is better; A (85\u{2013}100), B (70\u{2013}84), C (55\u{2013}69), D (40\u{2013}54), F (0\u{2013}39)"
384            },
385            "crap_max": {
386                "name": "Untested Complexity Risk (CRAP)",
387                "description": "Change Risk Anti-Patterns score (Savoia & Evans, 2007). Formula: CC\u{00b2} \u{00d7} (1 - cov/100)\u{00b3} + CC. Default model (static_estimated): estimates per-function coverage from export references \u{2014} directly test-referenced exports get 85%, indirectly test-reachable functions get 40%, untested files get 0%. Provide --coverage <path> with Istanbul-format coverage-final.json (from Jest, Vitest, c8, nyc) for exact per-function CRAP scores.",
388                "range": "[1, \u{221e})",
389                "interpretation": "lower is better; >=30 is high-risk (CC >= 5 without test path)"
390            },
391            "bus_factor": {
392                "name": "Bus Factor",
393                "description": "Avelino truck factor: the minimum number of distinct contributors who together account for at least 50% of recency-weighted commits to this file in the analysis window. Bot authors are excluded.",
394                "range": "[1, \u{221e})",
395                "interpretation": "lower is higher knowledge-loss risk; 1 means a single contributor covers most of the recent history"
396            },
397            "contributor_count": {
398                "name": "Contributor Count",
399                "description": "Number of distinct authors who touched this file in the analysis window after bot-pattern filtering.",
400                "range": "[0, \u{221e})",
401                "interpretation": "higher generally indicates broader knowledge spread; pair with bus_factor for context"
402            },
403            "share": {
404                "name": "Contributor Share",
405                "description": "Recency-weighted share of total weighted commits attributed to a single contributor. Rounded to three decimals.",
406                "range": "[0, 1]",
407                "interpretation": "share close to 1.0 indicates dominance and pairs with low bus_factor"
408            },
409            "stale_days": {
410                "name": "Stale Days",
411                "description": "Days since this contributor last touched the file. Computed at analysis time.",
412                "range": "[0, \u{221e})",
413                "interpretation": "high stale_days on the top contributor often correlates with ownership drift"
414            },
415            "drift": {
416                "name": "Ownership Drift",
417                "description": "True when the file's original author (earliest first commit in the window) differs from the current top contributor, the file is at least 30 days old, and the original author's recency-weighted share is below 10%.",
418                "values": [true, false],
419                "interpretation": "true means the original author is no longer maintaining; route reviews to the current top contributor"
420            },
421            "unowned": {
422                "name": "Unowned (Tristate)",
423                "description": "true = a CODEOWNERS file exists but no rule matches this file; false = a rule matches; null = no CODEOWNERS file was discovered for the repository (cannot determine).",
424                "values": [true, false, null],
425                "interpretation": "true on a hotspot is a review-bottleneck risk; null means the signal is unavailable, not absent"
426            },
427            "production_coverage_verdict": {
428                "name": "Production Coverage Verdict",
429                "description": "Overall verdict across all production-coverage findings. `clean` = nothing cold; `cold-code-detected` = one or more tracked functions had zero invocations; `hot-path-changes-needed` = a function modified in the current change set is on the hot path; `license-expired-grace` = analysis ran but the license is in its post-expiry grace window; `unknown` = verdict could not be computed (degenerate input).",
430                "values": ["clean", "hot-path-changes-needed", "cold-code-detected", "license-expired-grace", "unknown"],
431                "interpretation": "`cold-code-detected` is the primary actionable signal; `hot-path-changes-needed` elevates code-review attention for touched hot paths"
432            },
433            "production_coverage_state": {
434                "name": "Production Coverage State",
435                "description": "Per-function observation: `called` = V8 saw at least one invocation; `never-called` = V8 tracked the function but it never ran; `coverage-unavailable` = the function was not in the V8 tracking set (e.g., lazy-parsed, worker thread, dynamic code); `unknown` = forward-compat sentinel for newer sidecar states.",
436                "values": ["called", "never-called", "coverage-unavailable", "unknown"],
437                "interpretation": "`never-called` in combination with static `unused` is the highest-confidence delete signal"
438            },
439            "production_coverage_confidence": {
440                "name": "Production Coverage Confidence",
441                "description": "Confidence in a production-coverage finding. `high` = tracked by V8 with a statistically meaningful observation volume; `medium` = either low observation volume or indirect evidence; `low` = minimal data; `unknown` = insufficient information to classify.",
442                "values": ["high", "medium", "low", "unknown"],
443                "interpretation": "high = act on it; medium = verify context; low = treat as a signal only"
444            },
445            "production_invocations": {
446                "name": "Production Invocations",
447                "description": "Observed invocation count for the function over the collected coverage window. For `coverage-unavailable` findings this is `0` and semantically means `null` (not tracked). Absolute counts are not directly comparable across services without normalizing by trace_count.",
448                "range": "[0, \u{221e})",
449                "interpretation": "0 + tracked = cold path; 0 + untracked = unknown; high + never-called cannot occur by definition"
450            },
451            "percent_dead_in_production": {
452                "name": "Percent Dead in Production",
453                "description": "Fraction of tracked functions with zero observed invocations, multiplied by 100. Computed before any `--top` truncation so the summary total is stable regardless of display limits.",
454                "range": "[0, 100]",
455                "interpretation": "lower is better; values above ~10% on a long-running service indicate a large cleanup opportunity"
456            }
457        }
458    })
459}
460
461/// Build the `_meta` object for `fallow dupes --format json --explain`.
462#[must_use]
463pub fn dupes_meta() -> Value {
464    json!({
465        "docs": DUPES_DOCS,
466        "metrics": {
467            "duplication_percentage": {
468                "name": "Duplication Percentage",
469                "description": "Fraction of total source tokens that appear in at least one clone group. Computed over the full analyzed file set.",
470                "range": "[0, 100]",
471                "interpretation": "lower is better"
472            },
473            "token_count": {
474                "name": "Token Count",
475                "description": "Number of normalized source tokens in the clone group. Tokens are language-aware (keywords, identifiers, operators, punctuation). Higher token count = larger duplicate.",
476                "range": "[1, \u{221e})",
477                "interpretation": "larger clones have higher refactoring value"
478            },
479            "line_count": {
480                "name": "Line Count",
481                "description": "Number of source lines spanned by the clone instance. Approximation of clone size for human readability.",
482                "range": "[1, \u{221e})",
483                "interpretation": "larger clones are more impactful to deduplicate"
484            },
485            "clone_groups": {
486                "name": "Clone Groups",
487                "description": "A set of code fragments with identical or near-identical normalized token sequences. Each group has 2+ instances across different locations.",
488                "interpretation": "each group is a single refactoring opportunity"
489            },
490            "clone_families": {
491                "name": "Clone Families",
492                "description": "Groups of clone groups that share the same set of files. Indicates systematic duplication patterns (e.g., mirrored directory structures).",
493                "interpretation": "families suggest extract-module refactoring opportunities"
494            }
495        }
496    })
497}
498
499#[cfg(test)]
500mod tests {
501    use super::*;
502
503    // ── rule_by_id ───────────────────────────────────────────────────
504
505    #[test]
506    fn rule_by_id_finds_check_rule() {
507        let rule = rule_by_id("fallow/unused-file").unwrap();
508        assert_eq!(rule.name, "Unused Files");
509    }
510
511    #[test]
512    fn rule_by_id_finds_health_rule() {
513        let rule = rule_by_id("fallow/high-cyclomatic-complexity").unwrap();
514        assert_eq!(rule.name, "High Cyclomatic Complexity");
515    }
516
517    #[test]
518    fn rule_by_id_finds_dupes_rule() {
519        let rule = rule_by_id("fallow/code-duplication").unwrap();
520        assert_eq!(rule.name, "Code Duplication");
521    }
522
523    #[test]
524    fn rule_by_id_returns_none_for_unknown() {
525        assert!(rule_by_id("fallow/nonexistent").is_none());
526        assert!(rule_by_id("").is_none());
527    }
528
529    // ── rule_docs_url ────────────────────────────────────────────────
530
531    #[test]
532    fn rule_docs_url_format() {
533        let rule = rule_by_id("fallow/unused-export").unwrap();
534        let url = rule_docs_url(rule);
535        assert!(url.starts_with("https://docs.fallow.tools/"));
536        assert!(url.contains("unused-exports"));
537    }
538
539    // ── CHECK_RULES completeness ─────────────────────────────────────
540
541    #[test]
542    fn check_rules_all_have_fallow_prefix() {
543        for rule in CHECK_RULES {
544            assert!(
545                rule.id.starts_with("fallow/"),
546                "rule {} should start with fallow/",
547                rule.id
548            );
549        }
550    }
551
552    #[test]
553    fn check_rules_all_have_docs_path() {
554        for rule in CHECK_RULES {
555            assert!(
556                !rule.docs_path.is_empty(),
557                "rule {} should have a docs_path",
558                rule.id
559            );
560        }
561    }
562
563    #[test]
564    fn check_rules_no_duplicate_ids() {
565        let mut seen = rustc_hash::FxHashSet::default();
566        for rule in CHECK_RULES.iter().chain(HEALTH_RULES).chain(DUPES_RULES) {
567            assert!(seen.insert(rule.id), "duplicate rule id: {}", rule.id);
568        }
569    }
570
571    // ── check_meta ───────────────────────────────────────────────────
572
573    #[test]
574    fn check_meta_has_docs_and_rules() {
575        let meta = check_meta();
576        assert!(meta.get("docs").is_some());
577        assert!(meta.get("rules").is_some());
578        let rules = meta["rules"].as_object().unwrap();
579        // Verify all 13 rule categories are present (stripped fallow/ prefix)
580        assert_eq!(rules.len(), CHECK_RULES.len());
581        assert!(rules.contains_key("unused-file"));
582        assert!(rules.contains_key("unused-export"));
583        assert!(rules.contains_key("unused-type"));
584        assert!(rules.contains_key("unused-dependency"));
585        assert!(rules.contains_key("unused-dev-dependency"));
586        assert!(rules.contains_key("unused-optional-dependency"));
587        assert!(rules.contains_key("unused-enum-member"));
588        assert!(rules.contains_key("unused-class-member"));
589        assert!(rules.contains_key("unresolved-import"));
590        assert!(rules.contains_key("unlisted-dependency"));
591        assert!(rules.contains_key("duplicate-export"));
592        assert!(rules.contains_key("type-only-dependency"));
593        assert!(rules.contains_key("circular-dependency"));
594    }
595
596    #[test]
597    fn check_meta_rule_has_required_fields() {
598        let meta = check_meta();
599        let rules = meta["rules"].as_object().unwrap();
600        for (key, value) in rules {
601            assert!(value.get("name").is_some(), "rule {key} missing 'name'");
602            assert!(
603                value.get("description").is_some(),
604                "rule {key} missing 'description'"
605            );
606            assert!(value.get("docs").is_some(), "rule {key} missing 'docs'");
607        }
608    }
609
610    // ── health_meta ──────────────────────────────────────────────────
611
612    #[test]
613    fn health_meta_has_metrics() {
614        let meta = health_meta();
615        assert!(meta.get("docs").is_some());
616        let metrics = meta["metrics"].as_object().unwrap();
617        assert!(metrics.contains_key("cyclomatic"));
618        assert!(metrics.contains_key("cognitive"));
619        assert!(metrics.contains_key("maintainability_index"));
620        assert!(metrics.contains_key("complexity_density"));
621        assert!(metrics.contains_key("fan_in"));
622        assert!(metrics.contains_key("fan_out"));
623    }
624
625    // ── dupes_meta ───────────────────────────────────────────────────
626
627    #[test]
628    fn dupes_meta_has_metrics() {
629        let meta = dupes_meta();
630        assert!(meta.get("docs").is_some());
631        let metrics = meta["metrics"].as_object().unwrap();
632        assert!(metrics.contains_key("duplication_percentage"));
633        assert!(metrics.contains_key("token_count"));
634        assert!(metrics.contains_key("clone_groups"));
635        assert!(metrics.contains_key("clone_families"));
636    }
637
638    // ── HEALTH_RULES completeness ──────────────────────────────────
639
640    #[test]
641    fn health_rules_all_have_fallow_prefix() {
642        for rule in HEALTH_RULES {
643            assert!(
644                rule.id.starts_with("fallow/"),
645                "health rule {} should start with fallow/",
646                rule.id
647            );
648        }
649    }
650
651    #[test]
652    fn health_rules_all_have_docs_path() {
653        for rule in HEALTH_RULES {
654            assert!(
655                !rule.docs_path.is_empty(),
656                "health rule {} should have a docs_path",
657                rule.id
658            );
659        }
660    }
661
662    #[test]
663    fn health_rules_all_have_non_empty_fields() {
664        for rule in HEALTH_RULES {
665            assert!(
666                !rule.name.is_empty(),
667                "health rule {} missing name",
668                rule.id
669            );
670            assert!(
671                !rule.short.is_empty(),
672                "health rule {} missing short description",
673                rule.id
674            );
675            assert!(
676                !rule.full.is_empty(),
677                "health rule {} missing full description",
678                rule.id
679            );
680        }
681    }
682
683    // ── DUPES_RULES completeness ───────────────────────────────────
684
685    #[test]
686    fn dupes_rules_all_have_fallow_prefix() {
687        for rule in DUPES_RULES {
688            assert!(
689                rule.id.starts_with("fallow/"),
690                "dupes rule {} should start with fallow/",
691                rule.id
692            );
693        }
694    }
695
696    #[test]
697    fn dupes_rules_all_have_docs_path() {
698        for rule in DUPES_RULES {
699            assert!(
700                !rule.docs_path.is_empty(),
701                "dupes rule {} should have a docs_path",
702                rule.id
703            );
704        }
705    }
706
707    #[test]
708    fn dupes_rules_all_have_non_empty_fields() {
709        for rule in DUPES_RULES {
710            assert!(!rule.name.is_empty(), "dupes rule {} missing name", rule.id);
711            assert!(
712                !rule.short.is_empty(),
713                "dupes rule {} missing short description",
714                rule.id
715            );
716            assert!(
717                !rule.full.is_empty(),
718                "dupes rule {} missing full description",
719                rule.id
720            );
721        }
722    }
723
724    // ── CHECK_RULES field completeness ─────────────────────────────
725
726    #[test]
727    fn check_rules_all_have_non_empty_fields() {
728        for rule in CHECK_RULES {
729            assert!(!rule.name.is_empty(), "check rule {} missing name", rule.id);
730            assert!(
731                !rule.short.is_empty(),
732                "check rule {} missing short description",
733                rule.id
734            );
735            assert!(
736                !rule.full.is_empty(),
737                "check rule {} missing full description",
738                rule.id
739            );
740        }
741    }
742
743    // ── rule_docs_url with health/dupes rules ──────────────────────
744
745    #[test]
746    fn rule_docs_url_health_rule() {
747        let rule = rule_by_id("fallow/high-cyclomatic-complexity").unwrap();
748        let url = rule_docs_url(rule);
749        assert!(url.starts_with("https://docs.fallow.tools/"));
750        assert!(url.contains("health"));
751    }
752
753    #[test]
754    fn rule_docs_url_dupes_rule() {
755        let rule = rule_by_id("fallow/code-duplication").unwrap();
756        let url = rule_docs_url(rule);
757        assert!(url.starts_with("https://docs.fallow.tools/"));
758        assert!(url.contains("duplication"));
759    }
760
761    // ── health_meta metric structure ───────────────────────────────
762
763    #[test]
764    fn health_meta_all_metrics_have_name_and_description() {
765        let meta = health_meta();
766        let metrics = meta["metrics"].as_object().unwrap();
767        for (key, value) in metrics {
768            assert!(
769                value.get("name").is_some(),
770                "health metric {key} missing 'name'"
771            );
772            assert!(
773                value.get("description").is_some(),
774                "health metric {key} missing 'description'"
775            );
776            assert!(
777                value.get("interpretation").is_some(),
778                "health metric {key} missing 'interpretation'"
779            );
780        }
781    }
782
783    #[test]
784    fn health_meta_has_all_expected_metrics() {
785        let meta = health_meta();
786        let metrics = meta["metrics"].as_object().unwrap();
787        let expected = [
788            "cyclomatic",
789            "cognitive",
790            "line_count",
791            "lines",
792            "maintainability_index",
793            "complexity_density",
794            "dead_code_ratio",
795            "fan_in",
796            "fan_out",
797            "score",
798            "weighted_commits",
799            "trend",
800            "priority",
801            "efficiency",
802            "effort",
803            "confidence",
804            "bus_factor",
805            "contributor_count",
806            "share",
807            "stale_days",
808            "drift",
809            "unowned",
810            "production_coverage_verdict",
811            "production_coverage_state",
812            "production_coverage_confidence",
813            "production_invocations",
814            "percent_dead_in_production",
815        ];
816        for key in &expected {
817            assert!(
818                metrics.contains_key(*key),
819                "health_meta missing expected metric: {key}"
820            );
821        }
822    }
823
824    // ── dupes_meta metric structure ────────────────────────────────
825
826    #[test]
827    fn dupes_meta_all_metrics_have_name_and_description() {
828        let meta = dupes_meta();
829        let metrics = meta["metrics"].as_object().unwrap();
830        for (key, value) in metrics {
831            assert!(
832                value.get("name").is_some(),
833                "dupes metric {key} missing 'name'"
834            );
835            assert!(
836                value.get("description").is_some(),
837                "dupes metric {key} missing 'description'"
838            );
839        }
840    }
841
842    #[test]
843    fn dupes_meta_has_line_count() {
844        let meta = dupes_meta();
845        let metrics = meta["metrics"].as_object().unwrap();
846        assert!(metrics.contains_key("line_count"));
847    }
848
849    // ── docs URLs ─────────────────────────────────────────────────
850
851    #[test]
852    fn check_docs_url_valid() {
853        assert!(CHECK_DOCS.starts_with("https://"));
854        assert!(CHECK_DOCS.contains("dead-code"));
855    }
856
857    #[test]
858    fn health_docs_url_valid() {
859        assert!(HEALTH_DOCS.starts_with("https://"));
860        assert!(HEALTH_DOCS.contains("health"));
861    }
862
863    #[test]
864    fn dupes_docs_url_valid() {
865        assert!(DUPES_DOCS.starts_with("https://"));
866        assert!(DUPES_DOCS.contains("dupes"));
867    }
868
869    // ── check_meta docs URL matches constant ──────────────────────
870
871    #[test]
872    fn check_meta_docs_url_matches_constant() {
873        let meta = check_meta();
874        assert_eq!(meta["docs"].as_str().unwrap(), CHECK_DOCS);
875    }
876
877    #[test]
878    fn health_meta_docs_url_matches_constant() {
879        let meta = health_meta();
880        assert_eq!(meta["docs"].as_str().unwrap(), HEALTH_DOCS);
881    }
882
883    #[test]
884    fn dupes_meta_docs_url_matches_constant() {
885        let meta = dupes_meta();
886        assert_eq!(meta["docs"].as_str().unwrap(), DUPES_DOCS);
887    }
888
889    // ── rule_by_id finds all check rules ──────────────────────────
890
891    #[test]
892    fn rule_by_id_finds_all_check_rules() {
893        for rule in CHECK_RULES {
894            assert!(
895                rule_by_id(rule.id).is_some(),
896                "rule_by_id should find check rule {}",
897                rule.id
898            );
899        }
900    }
901
902    #[test]
903    fn rule_by_id_finds_all_health_rules() {
904        for rule in HEALTH_RULES {
905            assert!(
906                rule_by_id(rule.id).is_some(),
907                "rule_by_id should find health rule {}",
908                rule.id
909            );
910        }
911    }
912
913    #[test]
914    fn rule_by_id_finds_all_dupes_rules() {
915        for rule in DUPES_RULES {
916            assert!(
917                rule_by_id(rule.id).is_some(),
918                "rule_by_id should find dupes rule {}",
919                rule.id
920            );
921        }
922    }
923
924    // ── Rule count verification ───────────────────────────────────
925
926    #[test]
927    fn check_rules_count() {
928        assert_eq!(CHECK_RULES.len(), 14);
929    }
930
931    #[test]
932    fn health_rules_count() {
933        assert_eq!(HEALTH_RULES.len(), 12);
934    }
935
936    #[test]
937    fn dupes_rules_count() {
938        assert_eq!(DUPES_RULES.len(), 1);
939    }
940}