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/refactoring-target",
176        name: "Refactoring Target",
177        short: "File identified as a high-priority refactoring candidate",
178        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.",
179        docs_path: "explanations/health#refactoring-targets",
180    },
181    RuleDef {
182        id: "fallow/untested-file",
183        name: "Untested File",
184        short: "Runtime-reachable file has no test dependency path",
185        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.",
186        docs_path: "explanations/health#coverage-gaps",
187    },
188    RuleDef {
189        id: "fallow/untested-export",
190        name: "Untested Export",
191        short: "Runtime-reachable export has no test dependency path",
192        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.",
193        docs_path: "explanations/health#coverage-gaps",
194    },
195    RuleDef {
196        id: "fallow/production-safe-to-delete",
197        name: "Production Safe To Delete",
198        short: "Statically unused AND never invoked in production with V8 tracking",
199        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.",
200        docs_path: "explanations/health#production-coverage",
201    },
202    RuleDef {
203        id: "fallow/production-review-required",
204        name: "Production Review Required",
205        short: "Statically used but never invoked in production",
206        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.",
207        docs_path: "explanations/health#production-coverage",
208    },
209    RuleDef {
210        id: "fallow/production-low-traffic",
211        name: "Production Low Traffic",
212        short: "Function was invoked below the low-traffic threshold",
213        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.",
214        docs_path: "explanations/health#production-coverage",
215    },
216    RuleDef {
217        id: "fallow/production-coverage-unavailable",
218        name: "Production Coverage Unavailable",
219        short: "Production coverage could not be resolved for this function",
220        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.",
221        docs_path: "explanations/health#production-coverage",
222    },
223    RuleDef {
224        id: "fallow/production-coverage",
225        name: "Production Coverage",
226        short: "Production coverage finding",
227        full: "Generic production-coverage finding for verdicts not covered by a more specific rule. Includes `active` entries surfaced for completeness when the sidecar emits them and the forward-compat `unknown` sentinel.",
228        docs_path: "explanations/health#production-coverage",
229    },
230];
231
232pub const DUPES_RULES: &[RuleDef] = &[RuleDef {
233    id: "fallow/code-duplication",
234    name: "Code Duplication",
235    short: "Duplicated code block",
236    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.",
237    docs_path: "explanations/duplication#clone-groups",
238}];
239
240// ── JSON _meta builders ─────────────────────────────────────────
241
242/// Build the `_meta` object for `fallow dead-code --format json --explain`.
243#[must_use]
244pub fn check_meta() -> Value {
245    let rules: Value = CHECK_RULES
246        .iter()
247        .map(|r| {
248            (
249                r.id.replace("fallow/", ""),
250                json!({
251                    "name": r.name,
252                    "description": r.full,
253                    "docs": rule_docs_url(r)
254                }),
255            )
256        })
257        .collect::<serde_json::Map<String, Value>>()
258        .into();
259
260    json!({
261        "docs": CHECK_DOCS,
262        "rules": rules
263    })
264}
265
266/// Build the `_meta` object for `fallow health --format json --explain`.
267#[must_use]
268#[expect(
269    clippy::too_many_lines,
270    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"
271)]
272pub fn health_meta() -> Value {
273    json!({
274        "docs": HEALTH_DOCS,
275        "metrics": {
276            "cyclomatic": {
277                "name": "Cyclomatic Complexity",
278                "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.",
279                "range": "[1, \u{221e})",
280                "interpretation": "lower is better; default threshold: 20"
281            },
282            "cognitive": {
283                "name": "Cognitive Complexity",
284                "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.",
285                "range": "[0, \u{221e})",
286                "interpretation": "lower is better; default threshold: 15"
287            },
288            "line_count": {
289                "name": "Function Line Count",
290                "description": "Number of lines in the function body.",
291                "range": "[1, \u{221e})",
292                "interpretation": "context-dependent; long functions may need splitting"
293            },
294            "lines": {
295                "name": "File Line Count",
296                "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.",
297                "range": "[1, \u{221e})",
298                "interpretation": "context-dependent; large files may benefit from splitting even if individual functions are small"
299            },
300            "maintainability_index": {
301                "name": "Maintainability Index",
302                "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.",
303                "range": "[0, 100]",
304                "interpretation": "higher is better; <40 poor, 40\u{2013}70 moderate, >70 good"
305            },
306            "complexity_density": {
307                "name": "Complexity Density",
308                "description": "Total cyclomatic complexity divided by lines of code. Measures how densely complex the code is per line.",
309                "range": "[0, \u{221e})",
310                "interpretation": "lower is better; >1.0 indicates very dense complexity"
311            },
312            "dead_code_ratio": {
313                "name": "Dead Code Ratio",
314                "description": "Fraction of value exports (excluding type-only exports like interfaces and type aliases) with zero references across the project.",
315                "range": "[0, 1]",
316                "interpretation": "lower is better; 0 = all exports are used"
317            },
318            "fan_in": {
319                "name": "Fan-in (Importers)",
320                "description": "Number of files that import this file. High fan-in means high blast radius \u{2014} changes to this file affect many dependents.",
321                "range": "[0, \u{221e})",
322                "interpretation": "context-dependent; high fan-in files need careful review before changes"
323            },
324            "fan_out": {
325                "name": "Fan-out (Imports)",
326                "description": "Number of files this file directly imports. High fan-out indicates high coupling and change propagation risk.",
327                "range": "[0, \u{221e})",
328                "interpretation": "lower is better; MI penalty caps at ~40 imports"
329            },
330            "score": {
331                "name": "Hotspot Score",
332                "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.",
333                "range": "[0, 100]",
334                "interpretation": "higher = riskier; prioritize refactoring high-score files"
335            },
336            "weighted_commits": {
337                "name": "Weighted Commits",
338                "description": "Recency-weighted commit count using exponential decay with 90-day half-life. Recent commits contribute more than older ones.",
339                "range": "[0, \u{221e})",
340                "interpretation": "higher = more recent churn activity"
341            },
342            "trend": {
343                "name": "Churn Trend",
344                "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.",
345                "values": ["accelerating", "stable", "cooling"],
346                "interpretation": "accelerating files need attention; cooling files are stabilizing"
347            },
348            "priority": {
349                "name": "Refactoring Priority",
350                "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.",
351                "range": "[0, 100]",
352                "interpretation": "higher = more urgent to refactor"
353            },
354            "efficiency": {
355                "name": "Efficiency Score",
356                "description": "priority / effort_numeric (Low=1, Medium=2, High=3). Surfaces quick wins: high-priority, low-effort targets rank first. Default sort order.",
357                "range": "[0, 100] \u{2014} effective max depends on effort: Low=100, Medium=50, High\u{2248}33",
358                "interpretation": "higher = better quick-win value; targets are sorted by efficiency descending"
359            },
360            "effort": {
361                "name": "Effort Estimate",
362                "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.",
363                "values": ["low", "medium", "high"],
364                "interpretation": "low = quick win, high = needs planning and coordination"
365            },
366            "confidence": {
367                "name": "Confidence Level",
368                "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).",
369                "values": ["high", "medium", "low"],
370                "interpretation": "high = act on it, medium = verify context, low = treat as a signal, not a directive"
371            },
372            "health_score": {
373                "name": "Health Score",
374                "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.",
375                "range": "[0, 100]",
376                "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)"
377            },
378            "crap_max": {
379                "name": "Untested Complexity Risk (CRAP)",
380                "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.",
381                "range": "[1, \u{221e})",
382                "interpretation": "lower is better; >=30 is high-risk (CC >= 5 without test path)"
383            },
384            "bus_factor": {
385                "name": "Bus Factor",
386                "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.",
387                "range": "[1, \u{221e})",
388                "interpretation": "lower is higher knowledge-loss risk; 1 means a single contributor covers most of the recent history"
389            },
390            "contributor_count": {
391                "name": "Contributor Count",
392                "description": "Number of distinct authors who touched this file in the analysis window after bot-pattern filtering.",
393                "range": "[0, \u{221e})",
394                "interpretation": "higher generally indicates broader knowledge spread; pair with bus_factor for context"
395            },
396            "share": {
397                "name": "Contributor Share",
398                "description": "Recency-weighted share of total weighted commits attributed to a single contributor. Rounded to three decimals.",
399                "range": "[0, 1]",
400                "interpretation": "share close to 1.0 indicates dominance and pairs with low bus_factor"
401            },
402            "stale_days": {
403                "name": "Stale Days",
404                "description": "Days since this contributor last touched the file. Computed at analysis time.",
405                "range": "[0, \u{221e})",
406                "interpretation": "high stale_days on the top contributor often correlates with ownership drift"
407            },
408            "drift": {
409                "name": "Ownership Drift",
410                "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%.",
411                "values": [true, false],
412                "interpretation": "true means the original author is no longer maintaining; route reviews to the current top contributor"
413            },
414            "unowned": {
415                "name": "Unowned (Tristate)",
416                "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).",
417                "values": [true, false, null],
418                "interpretation": "true on a hotspot is a review-bottleneck risk; null means the signal is unavailable, not absent"
419            },
420            "production_coverage_verdict": {
421                "name": "Production Coverage Verdict",
422                "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).",
423                "values": ["clean", "hot-path-changes-needed", "cold-code-detected", "license-expired-grace", "unknown"],
424                "interpretation": "`cold-code-detected` is the primary actionable signal; `hot-path-changes-needed` elevates code-review attention for touched hot paths"
425            },
426            "production_coverage_state": {
427                "name": "Production Coverage State",
428                "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.",
429                "values": ["called", "never-called", "coverage-unavailable", "unknown"],
430                "interpretation": "`never-called` in combination with static `unused` is the highest-confidence delete signal"
431            },
432            "production_coverage_confidence": {
433                "name": "Production Coverage Confidence",
434                "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.",
435                "values": ["high", "medium", "low", "unknown"],
436                "interpretation": "high = act on it; medium = verify context; low = treat as a signal only"
437            },
438            "production_invocations": {
439                "name": "Production Invocations",
440                "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.",
441                "range": "[0, \u{221e})",
442                "interpretation": "0 + tracked = cold path; 0 + untracked = unknown; high + never-called cannot occur by definition"
443            },
444            "percent_dead_in_production": {
445                "name": "Percent Dead in Production",
446                "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.",
447                "range": "[0, 100]",
448                "interpretation": "lower is better; values above ~10% on a long-running service indicate a large cleanup opportunity"
449            }
450        }
451    })
452}
453
454/// Build the `_meta` object for `fallow dupes --format json --explain`.
455#[must_use]
456pub fn dupes_meta() -> Value {
457    json!({
458        "docs": DUPES_DOCS,
459        "metrics": {
460            "duplication_percentage": {
461                "name": "Duplication Percentage",
462                "description": "Fraction of total source tokens that appear in at least one clone group. Computed over the full analyzed file set.",
463                "range": "[0, 100]",
464                "interpretation": "lower is better"
465            },
466            "token_count": {
467                "name": "Token Count",
468                "description": "Number of normalized source tokens in the clone group. Tokens are language-aware (keywords, identifiers, operators, punctuation). Higher token count = larger duplicate.",
469                "range": "[1, \u{221e})",
470                "interpretation": "larger clones have higher refactoring value"
471            },
472            "line_count": {
473                "name": "Line Count",
474                "description": "Number of source lines spanned by the clone instance. Approximation of clone size for human readability.",
475                "range": "[1, \u{221e})",
476                "interpretation": "larger clones are more impactful to deduplicate"
477            },
478            "clone_groups": {
479                "name": "Clone Groups",
480                "description": "A set of code fragments with identical or near-identical normalized token sequences. Each group has 2+ instances across different locations.",
481                "interpretation": "each group is a single refactoring opportunity"
482            },
483            "clone_families": {
484                "name": "Clone Families",
485                "description": "Groups of clone groups that share the same set of files. Indicates systematic duplication patterns (e.g., mirrored directory structures).",
486                "interpretation": "families suggest extract-module refactoring opportunities"
487            }
488        }
489    })
490}
491
492#[cfg(test)]
493mod tests {
494    use super::*;
495
496    // ── rule_by_id ───────────────────────────────────────────────────
497
498    #[test]
499    fn rule_by_id_finds_check_rule() {
500        let rule = rule_by_id("fallow/unused-file").unwrap();
501        assert_eq!(rule.name, "Unused Files");
502    }
503
504    #[test]
505    fn rule_by_id_finds_health_rule() {
506        let rule = rule_by_id("fallow/high-cyclomatic-complexity").unwrap();
507        assert_eq!(rule.name, "High Cyclomatic Complexity");
508    }
509
510    #[test]
511    fn rule_by_id_finds_dupes_rule() {
512        let rule = rule_by_id("fallow/code-duplication").unwrap();
513        assert_eq!(rule.name, "Code Duplication");
514    }
515
516    #[test]
517    fn rule_by_id_returns_none_for_unknown() {
518        assert!(rule_by_id("fallow/nonexistent").is_none());
519        assert!(rule_by_id("").is_none());
520    }
521
522    // ── rule_docs_url ────────────────────────────────────────────────
523
524    #[test]
525    fn rule_docs_url_format() {
526        let rule = rule_by_id("fallow/unused-export").unwrap();
527        let url = rule_docs_url(rule);
528        assert!(url.starts_with("https://docs.fallow.tools/"));
529        assert!(url.contains("unused-exports"));
530    }
531
532    // ── CHECK_RULES completeness ─────────────────────────────────────
533
534    #[test]
535    fn check_rules_all_have_fallow_prefix() {
536        for rule in CHECK_RULES {
537            assert!(
538                rule.id.starts_with("fallow/"),
539                "rule {} should start with fallow/",
540                rule.id
541            );
542        }
543    }
544
545    #[test]
546    fn check_rules_all_have_docs_path() {
547        for rule in CHECK_RULES {
548            assert!(
549                !rule.docs_path.is_empty(),
550                "rule {} should have a docs_path",
551                rule.id
552            );
553        }
554    }
555
556    #[test]
557    fn check_rules_no_duplicate_ids() {
558        let mut seen = rustc_hash::FxHashSet::default();
559        for rule in CHECK_RULES.iter().chain(HEALTH_RULES).chain(DUPES_RULES) {
560            assert!(seen.insert(rule.id), "duplicate rule id: {}", rule.id);
561        }
562    }
563
564    // ── check_meta ───────────────────────────────────────────────────
565
566    #[test]
567    fn check_meta_has_docs_and_rules() {
568        let meta = check_meta();
569        assert!(meta.get("docs").is_some());
570        assert!(meta.get("rules").is_some());
571        let rules = meta["rules"].as_object().unwrap();
572        // Verify all 13 rule categories are present (stripped fallow/ prefix)
573        assert_eq!(rules.len(), CHECK_RULES.len());
574        assert!(rules.contains_key("unused-file"));
575        assert!(rules.contains_key("unused-export"));
576        assert!(rules.contains_key("unused-type"));
577        assert!(rules.contains_key("unused-dependency"));
578        assert!(rules.contains_key("unused-dev-dependency"));
579        assert!(rules.contains_key("unused-optional-dependency"));
580        assert!(rules.contains_key("unused-enum-member"));
581        assert!(rules.contains_key("unused-class-member"));
582        assert!(rules.contains_key("unresolved-import"));
583        assert!(rules.contains_key("unlisted-dependency"));
584        assert!(rules.contains_key("duplicate-export"));
585        assert!(rules.contains_key("type-only-dependency"));
586        assert!(rules.contains_key("circular-dependency"));
587    }
588
589    #[test]
590    fn check_meta_rule_has_required_fields() {
591        let meta = check_meta();
592        let rules = meta["rules"].as_object().unwrap();
593        for (key, value) in rules {
594            assert!(value.get("name").is_some(), "rule {key} missing 'name'");
595            assert!(
596                value.get("description").is_some(),
597                "rule {key} missing 'description'"
598            );
599            assert!(value.get("docs").is_some(), "rule {key} missing 'docs'");
600        }
601    }
602
603    // ── health_meta ──────────────────────────────────────────────────
604
605    #[test]
606    fn health_meta_has_metrics() {
607        let meta = health_meta();
608        assert!(meta.get("docs").is_some());
609        let metrics = meta["metrics"].as_object().unwrap();
610        assert!(metrics.contains_key("cyclomatic"));
611        assert!(metrics.contains_key("cognitive"));
612        assert!(metrics.contains_key("maintainability_index"));
613        assert!(metrics.contains_key("complexity_density"));
614        assert!(metrics.contains_key("fan_in"));
615        assert!(metrics.contains_key("fan_out"));
616    }
617
618    // ── dupes_meta ───────────────────────────────────────────────────
619
620    #[test]
621    fn dupes_meta_has_metrics() {
622        let meta = dupes_meta();
623        assert!(meta.get("docs").is_some());
624        let metrics = meta["metrics"].as_object().unwrap();
625        assert!(metrics.contains_key("duplication_percentage"));
626        assert!(metrics.contains_key("token_count"));
627        assert!(metrics.contains_key("clone_groups"));
628        assert!(metrics.contains_key("clone_families"));
629    }
630
631    // ── HEALTH_RULES completeness ──────────────────────────────────
632
633    #[test]
634    fn health_rules_all_have_fallow_prefix() {
635        for rule in HEALTH_RULES {
636            assert!(
637                rule.id.starts_with("fallow/"),
638                "health rule {} should start with fallow/",
639                rule.id
640            );
641        }
642    }
643
644    #[test]
645    fn health_rules_all_have_docs_path() {
646        for rule in HEALTH_RULES {
647            assert!(
648                !rule.docs_path.is_empty(),
649                "health rule {} should have a docs_path",
650                rule.id
651            );
652        }
653    }
654
655    #[test]
656    fn health_rules_all_have_non_empty_fields() {
657        for rule in HEALTH_RULES {
658            assert!(
659                !rule.name.is_empty(),
660                "health rule {} missing name",
661                rule.id
662            );
663            assert!(
664                !rule.short.is_empty(),
665                "health rule {} missing short description",
666                rule.id
667            );
668            assert!(
669                !rule.full.is_empty(),
670                "health rule {} missing full description",
671                rule.id
672            );
673        }
674    }
675
676    // ── DUPES_RULES completeness ───────────────────────────────────
677
678    #[test]
679    fn dupes_rules_all_have_fallow_prefix() {
680        for rule in DUPES_RULES {
681            assert!(
682                rule.id.starts_with("fallow/"),
683                "dupes rule {} should start with fallow/",
684                rule.id
685            );
686        }
687    }
688
689    #[test]
690    fn dupes_rules_all_have_docs_path() {
691        for rule in DUPES_RULES {
692            assert!(
693                !rule.docs_path.is_empty(),
694                "dupes rule {} should have a docs_path",
695                rule.id
696            );
697        }
698    }
699
700    #[test]
701    fn dupes_rules_all_have_non_empty_fields() {
702        for rule in DUPES_RULES {
703            assert!(!rule.name.is_empty(), "dupes rule {} missing name", rule.id);
704            assert!(
705                !rule.short.is_empty(),
706                "dupes rule {} missing short description",
707                rule.id
708            );
709            assert!(
710                !rule.full.is_empty(),
711                "dupes rule {} missing full description",
712                rule.id
713            );
714        }
715    }
716
717    // ── CHECK_RULES field completeness ─────────────────────────────
718
719    #[test]
720    fn check_rules_all_have_non_empty_fields() {
721        for rule in CHECK_RULES {
722            assert!(!rule.name.is_empty(), "check rule {} missing name", rule.id);
723            assert!(
724                !rule.short.is_empty(),
725                "check rule {} missing short description",
726                rule.id
727            );
728            assert!(
729                !rule.full.is_empty(),
730                "check rule {} missing full description",
731                rule.id
732            );
733        }
734    }
735
736    // ── rule_docs_url with health/dupes rules ──────────────────────
737
738    #[test]
739    fn rule_docs_url_health_rule() {
740        let rule = rule_by_id("fallow/high-cyclomatic-complexity").unwrap();
741        let url = rule_docs_url(rule);
742        assert!(url.starts_with("https://docs.fallow.tools/"));
743        assert!(url.contains("health"));
744    }
745
746    #[test]
747    fn rule_docs_url_dupes_rule() {
748        let rule = rule_by_id("fallow/code-duplication").unwrap();
749        let url = rule_docs_url(rule);
750        assert!(url.starts_with("https://docs.fallow.tools/"));
751        assert!(url.contains("duplication"));
752    }
753
754    // ── health_meta metric structure ───────────────────────────────
755
756    #[test]
757    fn health_meta_all_metrics_have_name_and_description() {
758        let meta = health_meta();
759        let metrics = meta["metrics"].as_object().unwrap();
760        for (key, value) in metrics {
761            assert!(
762                value.get("name").is_some(),
763                "health metric {key} missing 'name'"
764            );
765            assert!(
766                value.get("description").is_some(),
767                "health metric {key} missing 'description'"
768            );
769            assert!(
770                value.get("interpretation").is_some(),
771                "health metric {key} missing 'interpretation'"
772            );
773        }
774    }
775
776    #[test]
777    fn health_meta_has_all_expected_metrics() {
778        let meta = health_meta();
779        let metrics = meta["metrics"].as_object().unwrap();
780        let expected = [
781            "cyclomatic",
782            "cognitive",
783            "line_count",
784            "lines",
785            "maintainability_index",
786            "complexity_density",
787            "dead_code_ratio",
788            "fan_in",
789            "fan_out",
790            "score",
791            "weighted_commits",
792            "trend",
793            "priority",
794            "efficiency",
795            "effort",
796            "confidence",
797            "bus_factor",
798            "contributor_count",
799            "share",
800            "stale_days",
801            "drift",
802            "unowned",
803            "production_coverage_verdict",
804            "production_coverage_state",
805            "production_coverage_confidence",
806            "production_invocations",
807            "percent_dead_in_production",
808        ];
809        for key in &expected {
810            assert!(
811                metrics.contains_key(*key),
812                "health_meta missing expected metric: {key}"
813            );
814        }
815    }
816
817    // ── dupes_meta metric structure ────────────────────────────────
818
819    #[test]
820    fn dupes_meta_all_metrics_have_name_and_description() {
821        let meta = dupes_meta();
822        let metrics = meta["metrics"].as_object().unwrap();
823        for (key, value) in metrics {
824            assert!(
825                value.get("name").is_some(),
826                "dupes metric {key} missing 'name'"
827            );
828            assert!(
829                value.get("description").is_some(),
830                "dupes metric {key} missing 'description'"
831            );
832        }
833    }
834
835    #[test]
836    fn dupes_meta_has_line_count() {
837        let meta = dupes_meta();
838        let metrics = meta["metrics"].as_object().unwrap();
839        assert!(metrics.contains_key("line_count"));
840    }
841
842    // ── docs URLs ─────────────────────────────────────────────────
843
844    #[test]
845    fn check_docs_url_valid() {
846        assert!(CHECK_DOCS.starts_with("https://"));
847        assert!(CHECK_DOCS.contains("dead-code"));
848    }
849
850    #[test]
851    fn health_docs_url_valid() {
852        assert!(HEALTH_DOCS.starts_with("https://"));
853        assert!(HEALTH_DOCS.contains("health"));
854    }
855
856    #[test]
857    fn dupes_docs_url_valid() {
858        assert!(DUPES_DOCS.starts_with("https://"));
859        assert!(DUPES_DOCS.contains("dupes"));
860    }
861
862    // ── check_meta docs URL matches constant ──────────────────────
863
864    #[test]
865    fn check_meta_docs_url_matches_constant() {
866        let meta = check_meta();
867        assert_eq!(meta["docs"].as_str().unwrap(), CHECK_DOCS);
868    }
869
870    #[test]
871    fn health_meta_docs_url_matches_constant() {
872        let meta = health_meta();
873        assert_eq!(meta["docs"].as_str().unwrap(), HEALTH_DOCS);
874    }
875
876    #[test]
877    fn dupes_meta_docs_url_matches_constant() {
878        let meta = dupes_meta();
879        assert_eq!(meta["docs"].as_str().unwrap(), DUPES_DOCS);
880    }
881
882    // ── rule_by_id finds all check rules ──────────────────────────
883
884    #[test]
885    fn rule_by_id_finds_all_check_rules() {
886        for rule in CHECK_RULES {
887            assert!(
888                rule_by_id(rule.id).is_some(),
889                "rule_by_id should find check rule {}",
890                rule.id
891            );
892        }
893    }
894
895    #[test]
896    fn rule_by_id_finds_all_health_rules() {
897        for rule in HEALTH_RULES {
898            assert!(
899                rule_by_id(rule.id).is_some(),
900                "rule_by_id should find health rule {}",
901                rule.id
902            );
903        }
904    }
905
906    #[test]
907    fn rule_by_id_finds_all_dupes_rules() {
908        for rule in DUPES_RULES {
909            assert!(
910                rule_by_id(rule.id).is_some(),
911                "rule_by_id should find dupes rule {}",
912                rule.id
913            );
914        }
915    }
916
917    // ── Rule count verification ───────────────────────────────────
918
919    #[test]
920    fn check_rules_count() {
921        assert_eq!(CHECK_RULES.len(), 14);
922    }
923
924    #[test]
925    fn health_rules_count() {
926        assert_eq!(HEALTH_RULES.len(), 11);
927    }
928
929    #[test]
930    fn dupes_rules_count() {
931        assert_eq!(DUPES_RULES.len(), 1);
932    }
933}