diff --git a/Cargo.lock b/Cargo.lock index 56a44dd1..243f4bd7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1040,7 +1040,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -1308,9 +1308,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.4.15" +version = "0.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cb093c84e8bd9b188d4c4a8cb6579fc016968d14c99882163cd3ff402a4f155" +checksum = "a9f37a958b41b3b19ee2707c06439c0e9e547e847223eb791ecb0cb821c65e27" dependencies = [ "atomic-waker", "bytes", @@ -1955,7 +1955,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -2200,7 +2200,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -2552,7 +2552,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -2611,7 +2611,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -3032,7 +3032,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix 1.1.4", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -4079,7 +4079,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.48.0", ] [[package]] diff --git a/crates/codegraph-cli/src/main.rs b/crates/codegraph-cli/src/main.rs index 285fecc6..e1e04795 100644 --- a/crates/codegraph-cli/src/main.rs +++ b/crates/codegraph-cli/src/main.rs @@ -5831,17 +5831,13 @@ fn glob_match_bytes(pattern: &[u8], value: &[u8]) -> bool { } } +/// An explicit `--filter` glob fully REPLACES the heuristic — a user who names +/// their test files takes precedence over any pattern table. fn is_test_file(file: &str, filter: Option<&str>) -> bool { if let Some(filter) = filter { return glob_matches(filter, file); } - file.contains(".spec.") - || file.contains(".test.") - || file.contains("/__tests__/") - || file.contains("/test/") - || file.contains("/tests/") - || file.contains("/e2e/") - || file.contains("/spec/") + codegraph_core::file_class::is_test_file(file) } fn print_json(value: &serde_json::Value) -> Result<()> { @@ -6495,6 +6491,15 @@ mod pure_helper_tests { assert!(is_test_file("app/tests/mod.rs", None)); assert!(is_test_file("e2e/flow.spec.ts", None)); assert!(!is_test_file("src/main.rs", None)); + // Go's test convention is a `_test.` stem, not a `.test.` one (#1507). + assert!(is_test_file("pkg/math_test.go", None)); + assert!(is_test_file("math_test.go", None)); + // Directory patterns are path-SEGMENT anchored, so a repo-root test + // directory (no leading slash) classifies like a nested one. + assert!(is_test_file("e2e/flow.go", None)); + assert!(is_test_file("tests/mod.rs", None)); + assert!(!is_test_file("internal/latest.go", None)); + assert!(!is_test_file("src/route2e2e.ts", None)); } #[test] diff --git a/crates/codegraph-cli/tests/affected.rs b/crates/codegraph-cli/tests/affected.rs index 90caeb25..8cafa4a5 100644 --- a/crates/codegraph-cli/tests/affected.rs +++ b/crates/codegraph-cli/tests/affected.rs @@ -19,6 +19,10 @@ fn fixture() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/godot_audit") } +fn go_fixture() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/go_test_convention") +} + fn copy_tree(src: &Path, dst: &Path) { fs::create_dir_all(dst).unwrap(); for entry in fs::read_dir(src).unwrap() { @@ -75,9 +79,13 @@ fn cli(args: &[&str]) -> (String, String, bool) { } fn indexed_project(label: &str) -> (TestDir, PathBuf) { + indexed_fixture(label, "godot_audit", &fixture()) +} + +fn indexed_fixture(label: &str, name: &str, src: &Path) -> (TestDir, PathBuf) { let dir = TestDir::new(label); - let project = dir.path().join("godot_audit"); - copy_tree(&fixture(), &project); + let project = dir.path().join(name); + copy_tree(src, &project); let p = project.to_str().unwrap(); let (out, err, ok) = cli(&["init", p]); assert!(ok, "init failed: stdout={out} stderr={err}"); @@ -211,3 +219,72 @@ fn affected_files_superset_of_tests() { "affectedFiles must also list the non-test dependent main.tscn, got: {affected_files:?}" ); } + +/// Tier 1 item 1 (upstream #1507), end-to-end: Go names its tests `*_test.go`, +/// so a predicate keyed on `.test.` never classifies one. The dependent WAS +/// reachable — `affectedFiles` listed `math_test.go` — while `affectedTests` +/// came back `[]`, i.e. `affected` reported Go's entire test convention as "no +/// coverage". The same run also pins the repo-root `e2e/` directory, which the +/// original `contains("/e2e/")` shape missed for want of a leading slash. +#[test] +fn affected_classifies_go_test_convention_and_root_e2e_dir() { + // Given an indexed Go module where math_test.go calls Add/Double from + // math.go, and a repo-root e2e/flow.go calls Double as well. + let (_dir, project) = indexed_fixture("go-test-conv", "go_test_convention", &go_fixture()); + let p = project.to_str().unwrap(); + + // When affected runs on the implementation file, + let value = affected_json(p, "math.go", &["--depth", "5"]); + + // Then both dependents are reachable (this held before the fix too — the + // defect was classification, not traversal), + let affected_files = string_array(&value, "affectedFiles"); + for expected in ["math_test.go", "e2e/flow.go"] { + assert!( + affected_files.contains(&expected.to_string()), + "affectedFiles must list {expected}, got: {affected_files:?}" + ); + } + + // and both are reported as TESTS rather than as production dependents. + let affected_tests = string_array(&value, "affectedTests"); + assert!( + affected_tests.contains(&"math_test.go".to_string()), + "Go's `_test.go` convention must count as test coverage, got: {affected_tests:?}" + ); + assert!( + affected_tests.contains(&"e2e/flow.go".to_string()), + "a repo-root e2e/ directory must count as test coverage, got: {affected_tests:?}" + ); + + // and affectedFiles stays a superset of affectedTests. + for test_file in &affected_tests { + assert!( + affected_files.contains(test_file), + "affectedFiles must be a superset of affectedTests; missing {test_file:?} in {affected_files:?}" + ); + } +} + +/// The explicit `--filter` glob keeps overriding the heuristic: under a filter +/// that matches nothing in the project, `math_test.go` must NOT be classified +/// as a test even though the widened heuristic would now claim it. +#[test] +fn affected_filter_glob_still_overrides_the_heuristic() { + let (_dir, project) = indexed_fixture("go-filter", "go_test_convention", &go_fixture()); + let p = project.to_str().unwrap(); + + let value = affected_json(p, "math.go", &["--depth", "5", "--filter", "*_never.go"]); + + let affected_tests = string_array(&value, "affectedTests"); + assert!( + affected_tests.is_empty(), + "an explicit --filter that matches nothing must suppress the heuristic, got: {affected_tests:?}" + ); + // Traversal is unaffected: the dependents are still listed. + let affected_files = string_array(&value, "affectedFiles"); + assert!( + affected_files.contains(&"math_test.go".to_string()), + "a filtered-out test file must remain reachable, got: {affected_files:?}" + ); +} diff --git a/crates/codegraph-cli/tests/fixtures/go_test_convention/e2e/flow.go b/crates/codegraph-cli/tests/fixtures/go_test_convention/e2e/flow.go new file mode 100644 index 00000000..7079eb8c --- /dev/null +++ b/crates/codegraph-cli/tests/fixtures/go_test_convention/e2e/flow.go @@ -0,0 +1,7 @@ +package e2e + +import "example.com/mathutil" + +func RunCheckoutFlow() int { + return mathutil.Double(21) +} diff --git a/crates/codegraph-cli/tests/fixtures/go_test_convention/go.mod b/crates/codegraph-cli/tests/fixtures/go_test_convention/go.mod new file mode 100644 index 00000000..cea57769 --- /dev/null +++ b/crates/codegraph-cli/tests/fixtures/go_test_convention/go.mod @@ -0,0 +1,3 @@ +module example.com/mathutil + +go 1.22 diff --git a/crates/codegraph-cli/tests/fixtures/go_test_convention/math.go b/crates/codegraph-cli/tests/fixtures/go_test_convention/math.go new file mode 100644 index 00000000..151d8314 --- /dev/null +++ b/crates/codegraph-cli/tests/fixtures/go_test_convention/math.go @@ -0,0 +1,9 @@ +package mathutil + +func Add(a int, b int) int { + return a + b +} + +func Double(n int) int { + return Add(n, n) +} diff --git a/crates/codegraph-cli/tests/fixtures/go_test_convention/math_test.go b/crates/codegraph-cli/tests/fixtures/go_test_convention/math_test.go new file mode 100644 index 00000000..b6a8fb64 --- /dev/null +++ b/crates/codegraph-cli/tests/fixtures/go_test_convention/math_test.go @@ -0,0 +1,15 @@ +package mathutil + +import "testing" + +func TestAdd(t *testing.T) { + if Add(1, 2) != 3 { + t.Fatal("Add is wrong") + } +} + +func TestDouble(t *testing.T) { + if Double(2) != 4 { + t.Fatal("Double is wrong") + } +} diff --git a/crates/codegraph-core/src/file_class.rs b/crates/codegraph-core/src/file_class.rs new file mode 100644 index 00000000..caf559f9 --- /dev/null +++ b/crates/codegraph-core/src/file_class.rs @@ -0,0 +1,284 @@ +//! Shared file-classification predicates. +//! +//! Both the CLI (`affected`) and the MCP engine (blast radius, +//! `excludeLowValueFiles`, symbol disambiguation, `explore` ranking) need to ask +//! "is this a test file?" and "does this path look tool-generated?". They used +//! to answer with two hand-maintained pattern lists that drifted apart in BOTH +//! directions (upstream #1507), so a Go `_test.go` file read as production code +//! on the CLI path while an `e2e/` tree read as production code on the MCP path. +//! This module is the single source of truth for both questions. +//! +//! Every predicate here is a pure, allocation-free function of the path string. +//! Both are RELEVANCE HINTS, never filters: a test or generated file stays fully +//! present in the graph and reachable in results — it only ranks after a real +//! implementation of the same name, or is skipped when a query has better +//! signal to offer. +//! +//! `codegraph-graph`'s `query::scoring::is_test_file` deliberately stays +//! separate. It answers a BROADER question for FTS relevance damping (it also +//! treats `examples/`, `demos/`, `benchmarks/`, camelCase `FooTests/` dirs and +//! `TestCase`-suffixed stems as non-production), so folding it in here would +//! change search ranking rather than converge a duplicate. + +/// Path segments that mark a test tree. Matched as whole `/`-delimited +/// segments, so a repo-root `e2e/` counts and a `route2e2e.ts` filename +/// does not. +const TEST_DIR_SEGMENTS: &[&str] = &["test", "tests", "__tests__", "spec", "e2e"]; + +/// Filename markers that mark a test file. `_test.` is Go's convention +/// (`math_test.go`); `.test.` / `.spec.` are the JS/TS ones. +const TEST_FILE_MARKERS: &[&str] = &[".test.", ".spec.", "_test."]; + +/// Whether `path` names a test file, by directory segment or filename marker. +/// +/// The union of the two predicates this replaces. Callers that also honor an +/// explicit user-supplied glob (the CLI's `affected --filter`) must apply that +/// glob FIRST and skip this heuristic entirely. +pub fn is_test_file(path: &str) -> bool { + TEST_FILE_MARKERS.iter().any(|m| path.contains(m)) + || TEST_DIR_SEGMENTS + .iter() + .any(|seg| contains_path_segment(path, seg)) +} + +/// Whether `segment` appears in `path` as a complete `/`-delimited segment. +/// +/// Scans forward over every occurrence rather than allocating `"/{segment}/"`, +/// because this runs per-file inside sort comparators. Offsets are tracked +/// against the ORIGINAL path so a rejected occurrence cannot make the next one +/// look segment-initial (`xe2e/a.ts` must not match `e2e`). +fn contains_path_segment(path: &str, segment: &str) -> bool { + let bytes = path.as_bytes(); + let mut from = 0usize; + while let Some(rel) = path[from..].find(segment) { + let start = from + rel; + let end = start + segment.len(); + let starts_segment = start == 0 || bytes[start - 1] == b'/'; + let ends_segment = bytes.get(end) == Some(&b'/'); + if starts_segment && ends_segment { + return true; + } + // `segment` is ASCII, so `start` indexes an ASCII byte and `start + 1` + // is always a char boundary. + from = start + 1; + } + false +} + +/// Filename suffixes that mark tool-generated output, ported from upstream's +/// `GENERATED_PATTERNS` (`src/extraction/generated-detection.ts`). Upstream +/// writes them as end-anchored regexes containing no `/`, so a suffix test over +/// the whole path is equivalent — and case-sensitive, matching upstream's lack +/// of an `i` flag (`Grpc.cs`, `OuterClass.java`). +/// +/// Mockgen's DEFAULT `mock_.go` output is NOT here: upstream anchors it to +/// the basename (`/^mock_[^/]+\.go$/`), so it needs [`is_mockgen_default_output`]. +const GENERATED_SUFFIXES: &[&str] = &[ + // Go — protobuf / gRPC / pulsar + ".pb.go", + ".pulsar.go", + "_grpc.pb.go", + // Go — mockgen output renamed by the project (cosmos-sdk uses + // `expected_*_mocks.go`); both spellings are accepted upstream. + "_mock.go", + "_mocks.go", + // TypeScript / JavaScript — Apollo/GraphQL codegen, Prisma, Hasura, + // ts-proto, gRPC-web, swagger-codegen. Upstream's `[jt]sx?` character + // class expands to these four spellings per stem. + ".generated.ts", + ".generated.tsx", + ".generated.js", + ".generated.jsx", + ".gen.ts", + ".gen.tsx", + ".gen.js", + ".gen.jsx", + ".pb.ts", + ".pb.js", + "_pb.ts", + "_pb.js", + "_grpc_pb.ts", + "_grpc_pb.js", + // Minified bundles vendored into a repo: single-letter symbols make + // name-based edges pure noise. + ".min.js", + ".min.mjs", + // Python — protobuf / gRPC + "_pb2.py", + "_pb2_grpc.py", + "_pb2.pyi", + // C++ — protobuf + ".pb.cc", + ".pb.h", + // C# — protobuf / gRPC + ".g.cs", + "Grpc.cs", + // Java — protoc-gen-java / protoc-gen-grpc-java + "OuterClass.java", + "Grpc.java", + // Swift — protobuf + ".pb.swift", + // Dart — build_runner / freezed / json_serializable / protobuf / chopper + ".g.dart", + ".freezed.dart", + ".pb.dart", + ".pbgrpc.dart", + ".chopper.dart", + // Rust — in-tree codegen convention + ".generated.rs", +]; + +/// Whether `path` looks like tool-generated source, judged from the filename +/// alone. Never reads file content. +/// +/// A relevance hint for disambiguation and ranking, not a hard claim and not a +/// filter: generated nodes stay in the graph and stay reachable. Content-banner +/// detection (Go's `// Code generated ... DO NOT EDIT.` convention) is a +/// separate, index-time signal and deliberately not part of this path check. +pub fn is_generated_file(path: &str) -> bool { + GENERATED_SUFFIXES.iter().any(|s| path.ends_with(s)) || is_mockgen_default_output(path) +} + +/// Mockgen's default output, `mock_.go`, anchored to the final path +/// segment. Upstream's `/^mock_[^/]+\.go$/` is basename-scoped, so a nested +/// `src/mock_helpers.go` matches while a `mock_store/` DIRECTORY does not, and +/// `[^/]+` requires a non-empty stem between the prefix and the extension. +fn is_mockgen_default_output(path: &str) -> bool { + basename(path) + .strip_prefix("mock_") + .and_then(|rest| rest.strip_suffix(".go")) + .is_some_and(|stem| !stem.is_empty()) +} + +fn basename(path: &str) -> &str { + match path.rfind(['/', '\\']) { + Some(idx) => &path[idx + 1..], + None => path, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_dir_segments_match_whole_segments_only() { + // Nested and repo-root forms both classify. + assert!(is_test_file("apps/web/e2e/login.ts")); + assert!(is_test_file("e2e/login.ts")); + assert!(is_test_file("tests/mod.rs")); + assert!(is_test_file("app/tests/mod.rs")); + assert!(is_test_file("pkg/__tests__/a.js")); + assert!(is_test_file("app/spec/mod.rb")); + assert!(is_test_file("spec/mod.rb")); + // A segment name embedded in a longer segment does not. + assert!(!is_test_file("src/route2e2e.ts")); + assert!(!is_test_file("src/latest/mod.rs")); + assert!(!is_test_file("src/contest/mod.rs")); + assert!(!is_test_file("src/spectrum/mod.rs")); + // A trailing segment with no following `/` is a filename, not a dir. + assert!(!is_test_file("src/e2e")); + } + + #[test] + fn test_file_markers_cover_go_and_js_conventions() { + assert!(is_test_file("pkg/math_test.go")); + assert!(is_test_file("math_test.go")); + assert!(is_test_file("src/foo.test.ts")); + assert!(is_test_file("src/foo.spec.ts")); + assert!(!is_test_file("src/lib.rs")); + assert!(!is_test_file("internal/latest.go")); + assert!(!is_test_file("src/testify.go")); + } + + #[test] + fn contains_path_segment_rejects_a_shifted_false_start() { + // Regression for the offset bug: rejecting the `xe2e` occurrence must + // not leave the scan believing the next byte is segment-initial. + assert!(!contains_path_segment("xe2e/a.ts", "e2e")); + assert!(contains_path_segment("x/e2e/a.ts", "e2e")); + assert!(contains_path_segment("e2e/e2e/a.ts", "e2e")); + } + + #[test] + fn contains_path_segment_tolerates_multibyte_neighbors() { + assert!(!contains_path_segment("src/日e2e/a.ts", "e2e")); + assert!(contains_path_segment("src/日本/e2e/a.ts", "e2e")); + } + + #[test] + fn generated_suffixes_cover_every_ported_family() { + for path in [ + "api.pb.go", + "api.pulsar.go", + "api_grpc.pb.go", + "store_mock.go", + "expected_keepers_mocks.go", + "schema.generated.ts", + "schema.generated.tsx", + "schema.generated.js", + "schema.generated.jsx", + "api.gen.ts", + "api.gen.tsx", + "api.gen.js", + "api.gen.jsx", + "svc.pb.ts", + "svc.pb.js", + "svc_pb.ts", + "svc_pb.js", + "svc_grpc_pb.ts", + "svc_grpc_pb.js", + "vendor/chart.min.js", + "vendor/chart.min.mjs", + "svc_pb2.py", + "svc_pb2_grpc.py", + "svc_pb2.pyi", + "svc.pb.cc", + "svc.pb.h", + "Model.g.cs", + "GreeterGrpc.cs", + "GreeterOuterClass.java", + "GreeterGrpc.java", + "Model.pb.swift", + "model.g.dart", + "model.freezed.dart", + "model.pb.dart", + "model.pbgrpc.dart", + "api.chopper.dart", + "src/proto.generated.rs", + ] { + assert!(is_generated_file(path), "must classify: {path}"); + } + } + + #[test] + fn generated_suffixes_leave_hand_written_files_alone() { + for path in [ + "main.rs", + "src/generator.ts", + "src/mocks.go", + "src/genesis.go", + "src/pb.go", + "src/table.js", + "docs/min.css", + "src/grpc.cs", + "src/outerclass.java", + ] { + assert!(!is_generated_file(path), "must not classify: {path}"); + } + } + + #[test] + fn mockgen_default_output_is_basename_anchored() { + assert!(is_generated_file("mock_store.go")); + assert!(is_generated_file("src/mock_helpers.go")); + assert!(is_generated_file("a/b/c/mock_x.go")); + // A `mock_` DIRECTORY is not a generated file. + assert!(!is_generated_file("mock_store/registry.go")); + // `[^/]+` requires a non-empty stem. + assert!(!is_generated_file("mock_.go")); + assert!(!is_generated_file("src/mock_.go")); + // The prefix needs its underscore separator. + assert!(!is_generated_file("src/mockery_notgen.go")); + } +} diff --git a/crates/codegraph-core/src/lib.rs b/crates/codegraph-core/src/lib.rs index 4773e784..e52b442a 100644 --- a/crates/codegraph-core/src/lib.rs +++ b/crates/codegraph-core/src/lib.rs @@ -2,6 +2,7 @@ pub mod config; pub mod errors; +pub mod file_class; pub mod index_paths; pub mod logger; pub mod node_id; diff --git a/crates/codegraph-mcp/src/engine.rs b/crates/codegraph-mcp/src/engine.rs index daf7b7c7..9a14efed 100644 --- a/crates/codegraph-mcp/src/engine.rs +++ b/crates/codegraph-mcp/src/engine.rs @@ -14,6 +14,7 @@ use std::sync::Arc; use std::time::{Duration, Instant, UNIX_EPOCH}; use codegraph_core::config::Config; +use codegraph_core::file_class::{is_generated_file, is_test_file}; use codegraph_core::node_id::hash_content; use codegraph_core::types::{EdgeKind, FileRecord, Node, NodeKind}; use codegraph_graph::graph::{GodotReach, GraphTraverser, NodeEdge}; @@ -2304,7 +2305,7 @@ impl ExploreSubgraph { neighbor_files.insert(n.file_path.as_str()); } } - let scores: HashMap = self + let scores: HashMap = self .file_order .iter() .map(|fp| { @@ -2321,12 +2322,20 @@ impl ExploreSubgraph { } else { 0 }; + // Generated-path demotion sits INSIDE the tier, so it only ever + // reorders files of equal query relevance: a protobuf stub loses + // to the implementation it shadows, never falls behind an + // unrelated incidental file, and still ranks first in its tier + // when it is the only match. The sort is descending, so real + // code must carry the HIGHER value. `find_symbol_matches` and + // `find_all_symbols` apply the same signal. + let not_generated = u8::from(!is_generated_file(fp)); let sym_count = self .nodes .iter() .filter(|n| &n.file_path == fp && n.kind != NodeKind::File) .count(); - (fp.clone(), (tier, sym_count)) + (fp.clone(), (tier, not_generated, sym_count)) }) .collect(); self.file_order @@ -2838,26 +2847,6 @@ fn is_handler_method_name(name: &str) -> bool { ) } -fn is_generated_file(path: &str) -> bool { - let p = path; - p.ends_with(".pb.go") - || p.ends_with(".pulsar.go") - || p.ends_with("_grpc.pb.go") - || p.ends_with(".g.dart") - || p.ends_with(".freezed.dart") -} - -fn is_test_file(path: &str) -> bool { - let p = path; - p.contains("/test/") - || p.contains("/tests/") - || p.contains("/__tests__/") - || p.contains("/spec/") - || p.contains(".test.") - || p.contains(".spec.") - || p.contains("_test.") -} - fn is_container(kind: NodeKind) -> bool { matches!( kind, @@ -4753,6 +4742,339 @@ mod tests { assert!(!is_low_value_file("src/lib.rs")); } + /// Tier 1 item 1 (upstream #1507) — the MCP-side half of the bidirectional + /// disagreement: the CLI's `is_test_file` had `/e2e/` and this one did not, + /// so a Playwright/Cypress `e2e/` tree read as production code on every MCP + /// path (blast radius, `excludeLowValueFiles`, the indirect-test note). + /// After convergence both predicates are the UNION of the two pattern sets. + #[test] + fn ext_is_test_file_recognizes_an_e2e_directory() { + // The MCP-side gap: an `/e2e/` segment, with no `.test.`/`.spec.` help. + assert!( + is_test_file("e2e/checkout.ts"), + "a top-level e2e/ tree is a test tree" + ); + assert!( + is_test_file("apps/web/e2e/login.ts"), + "a nested e2e/ tree is a test tree" + ); + // And it flows through to `excludeLowValueFiles`, which is what keeps an + // e2e tree from crowding out implementations in explore output. + assert!(is_low_value_file("e2e/checkout.ts")); + // Negative: `e2e` must be a whole path SEGMENT, never a substring. + assert!( + !is_test_file("src/route2e2e.ts"), + "an `e2e` substring inside a filename is not a test tree" + ); + } + + /// Tier 1 item 1, CLI-side half kept in force here too: Go's `_test.go` + /// convention (already recognized by this predicate) plus the union's other + /// members all classify, so the two predicates agree in BOTH directions. + #[test] + fn ext_is_test_file_covers_the_full_union() { + for path in [ + "pkg/math_test.go", + "src/foo.test.ts", + "src/foo.spec.ts", + "pkg/__tests__/a.js", + "app/test/mod.rs", + "app/tests/mod.rs", + "app/spec/mod.rb", + "e2e/flow.ts", + ] { + assert!(is_test_file(path), "union member must classify: {path}"); + } + for path in ["src/lib.rs", "src/main.rs", "internal/latest.go"] { + assert!( + !is_test_file(path), + "production file must not classify: {path}" + ); + } + } + + /// Tier 1 item 1 through a real MCP-path CONSUMER, not just the predicate: + /// `codegraph_explore`'s blast radius splits a symbol's caller files into + /// "used by" (production) and "tested via callers" (tests). Pre-fix an + /// `e2e/` caller landed in the production list, so the MCP surface claimed + /// the symbol had no test coverage. + #[test] + fn ext_blast_radius_lists_an_e2e_caller_as_a_test() { + let mut engine = test_engine(); + let target = node_lang( + "checkout", + "checkout", + "src/checkout.ts", + 1, + 3, + NodeKind::Function, + Language::TypeScript, + ); + let e2e_caller = node_lang( + "checkoutFlow", + "checkoutFlow", + "e2e/checkout.ts", + 1, + 5, + NodeKind::Function, + Language::TypeScript, + ); + put_indexed_source( + &engine, + "src/checkout.ts", + "export function checkout() {\n return 1;\n}\n", + Language::TypeScript, + 1, + ); + put_indexed_source( + &engine, + "e2e/checkout.ts", + "import { checkout } from '../src/checkout';\nexport function checkoutFlow() {\n checkout();\n}\n", + Language::TypeScript, + 1, + ); + put_nodes(&mut engine, &[target.clone(), e2e_caller.clone()]); + put_edges( + &mut engine, + &[mk_edge( + &e2e_caller.id, + &target.id, + codegraph_core::types::EdgeKind::Calls, + )], + ); + + let text = text_of(&engine.execute( + "codegraph_explore", + &serde_json::json!({"query": "checkout"}), + )); + let radius = text + .lines() + .find(|l| l.contains("checkout") && l.contains("e2e/checkout.ts")) + .unwrap_or_else(|| panic!("blast radius must mention the e2e caller: {text}")); + assert!( + radius.contains("tests: `e2e/checkout.ts`"), + "an e2e/ caller must be credited as test coverage; got: {radius}" + ); + assert!( + !radius.contains("no tests found"), + "an e2e/ caller must not leave a no-coverage claim standing; got: {radius}" + ); + assert!( + !radius.contains("in `e2e/checkout.ts`"), + "an e2e/ caller must not also be listed as a production user; got: {radius}" + ); + } + + /// Tier 1 item 3 (upstream `generated-detection.ts` `GENERATED_PATTERNS`) — + /// one assertion group per language family in the ported table. Pre-fix only + /// the five Go/Dart protobuf suffixes were recognized, so mockgen output, + /// every TS/JS codegen convention, vendored minified bundles, Python + /// `_pb2*`, C++/C#/Java protobuf, Swift and in-tree Rust codegen all read as + /// hand-written implementations and outranked the real code. + #[test] + fn ext_is_generated_file_covers_upstream_language_families() { + // Go — protobuf / gRPC / pulsar (pre-existing). + for p in ["api.pb.go", "api.pulsar.go", "api_grpc.pb.go"] { + assert!(is_generated_file(p), "go protobuf: {p}"); + } + // Go — mockgen. Upstream accepts BOTH suffix spellings because projects + // rename mockgen output (cosmos-sdk uses `expected_*_mocks.go`). + for p in ["store_mock.go", "expected_keepers_mocks.go"] { + assert!(is_generated_file(p), "go mockgen: {p}"); + } + // TypeScript / JavaScript codegen (Apollo/GraphQL, Prisma, ts-proto, + // gRPC-web, swagger-codegen) — all four `[jt]sx?` spellings. + for p in [ + "schema.generated.ts", + "schema.generated.tsx", + "schema.generated.js", + "schema.generated.jsx", + "api.gen.ts", + "api.gen.tsx", + "api.gen.js", + "api.gen.jsx", + "svc.pb.ts", + "svc.pb.js", + "svc_pb.ts", + "svc_pb.js", + "svc_grpc_pb.ts", + "svc_grpc_pb.js", + ] { + assert!(is_generated_file(p), "ts/js codegen: {p}"); + } + // Vendored minified bundles — single-letter symbols make name edges noise. + for p in ["vendor/chart.min.js", "vendor/chart.min.mjs"] { + assert!(is_generated_file(p), "minified bundle: {p}"); + } + // Python — protobuf / gRPC. + for p in ["svc_pb2.py", "svc_pb2_grpc.py", "svc_pb2.pyi"] { + assert!(is_generated_file(p), "python protobuf: {p}"); + } + // C++ — protobuf. + for p in ["svc.pb.cc", "svc.pb.h"] { + assert!(is_generated_file(p), "c++ protobuf: {p}"); + } + // C# — protobuf / gRPC. + for p in ["Model.g.cs", "GreeterGrpc.cs"] { + assert!(is_generated_file(p), "c# protobuf: {p}"); + } + // Java — protoc-gen-java / protoc-gen-grpc-java. + for p in ["GreeterOuterClass.java", "GreeterGrpc.java"] { + assert!(is_generated_file(p), "java protobuf: {p}"); + } + // Swift — protobuf. + assert!(is_generated_file("Model.pb.swift"), "swift protobuf"); + // Dart — build_runner / freezed / protobuf / chopper. + for p in [ + "model.g.dart", + "model.freezed.dart", + "model.pb.dart", + "model.pbgrpc.dart", + "api.chopper.dart", + ] { + assert!(is_generated_file(p), "dart codegen: {p}"); + } + // Rust — in-tree codegen convention. + assert!(is_generated_file("src/proto.generated.rs"), "rust codegen"); + + // Negatives: hand-written files that merely share a stem. + for p in [ + "main.rs", + "src/generator.ts", + "src/mocks.go", + "src/genesis.go", + "src/pb.go", + "src/table.js", + "docs/min.css", + ] { + assert!( + !is_generated_file(p), + "hand-written file must not match: {p}" + ); + } + } + + /// Tier 1 item 3, shape detail: upstream anchors mockgen's DEFAULT output as + /// `/^mock_[^/]+\.go$/` — a BASENAME rule. A naive `starts_with` on the full + /// path would miss every nested occurrence, which is where mocks actually + /// live, so the final path segment is what must be tested. + #[test] + fn ext_is_generated_file_mock_prefix_is_basename_anchored() { + assert!( + is_generated_file("src/mock_helpers.go"), + "nested mockgen default output must match on its basename" + ); + assert!( + is_generated_file("mock_store.go"), + "repo-root mockgen default output must match" + ); + assert!( + !is_generated_file("src/mockery_notgen.go"), + "`mock` without the underscore separator is a hand-written file" + ); + assert!( + !is_generated_file("mock_store/registry.go"), + "a `mock_` DIRECTORY is not a generated file" + ); + } + + /// Tier 1 item 2 — generated-file demotion on the `explore` path. Two + /// same-named `ComputePay` definitions, one in a `.pb.go` stub and one in a + /// real file whose path sorts LAST alphabetically. `codegraph_node` already + /// demoted the stub (`find_symbol_matches`); `explore` did not consult the + /// signal at all, so within the same relevance tier the alphabetical + /// tie-break put the generated stub's section first. + #[test] + fn ext_explore_ranks_a_real_implementation_above_a_generated_stub() { + let mut engine = test_engine(); + let generated = node_lang( + "ComputePay", + "ComputePay", + "payroll/a_gen.pb.go", + 1, + 3, + NodeKind::Function, + Language::Go, + ); + let real = node_lang( + "ComputePay", + "ComputePay", + "payroll/zz_real.go", + 1, + 3, + NodeKind::Function, + Language::Go, + ); + put_indexed_source( + &engine, + "payroll/a_gen.pb.go", + "func ComputePay() int {\n\treturn 0\n}\n", + Language::Go, + 1, + ); + put_indexed_source( + &engine, + "payroll/zz_real.go", + "func ComputePay() int {\n\treturn 42\n}\n", + Language::Go, + 1, + ); + put_nodes(&mut engine, &[generated.clone(), real.clone()]); + + let text = text_of(&engine.execute( + "codegraph_explore", + &serde_json::json!({"query": "ComputePay"}), + )); + let order: Vec<&str> = text + .lines() + .filter(|l| l.starts_with("#### ")) + .filter_map(|l| { + ["payroll/zz_real.go", "payroll/a_gen.pb.go"] + .into_iter() + .find(|f| l.contains(f)) + }) + .collect(); + assert_eq!( + order, + vec!["payroll/zz_real.go", "payroll/a_gen.pb.go"], + "the real implementation's section must precede the generated stub's; got {order:?} in:\n{text}" + ); + } + + /// Tier 1 item 2 NEGATIVE — the demotion is a RANKING hint, never a filter: + /// a generated file must stay reachable, and when it is the ONLY definition + /// site it still renders. + #[test] + fn ext_explore_still_renders_a_generated_file_when_it_is_the_only_match() { + let mut engine = test_engine(); + let only = node_lang( + "MarshalOnly", + "MarshalOnly", + "payroll/only_gen.pb.go", + 1, + 3, + NodeKind::Function, + Language::Go, + ); + put_indexed_source( + &engine, + "payroll/only_gen.pb.go", + "func MarshalOnly() int {\n\treturn 0\n}\n", + Language::Go, + 1, + ); + put_nodes(&mut engine, std::slice::from_ref(&only)); + + let text = text_of(&engine.execute( + "codegraph_explore", + &serde_json::json!({"query": "MarshalOnly"}), + )); + assert!( + text.contains("payroll/only_gen.pb.go"), + "a generated file must remain reachable when it is the only match: {text}" + ); + } + #[test] fn ext_query_mentions_tests_detector() { assert!(query_mentions_tests("how does the test suite work"));