src/mimir_lens/room_feed/reuse_tests.rs
//! **`reimplements_shared`** — a copy of a library's function in a crate that
//! already depends on that library. The fixture's `parse_header` is written
//! three times (alpha, beta, gamma); with gamma depending on alpha, gamma's
//! copy is the re-implementation and alpha's is the one to call. beta does not
//! depend on alpha, so its copy is no finding (a twin, not a re-implementation),
//! and alpha never "re-implements" gamma (the dependency points one way).
use serde_json::{Value, json};
use super::fixture::{self, vectors as vx};
fn state(v: &Value) -> &str {
v["lens_meta"]["state"].as_str().unwrap_or_else(|| panic!("no lens_meta.state: {v}"))
}
fn worst(v: &Value) -> &Vec<Value> {
v["worst"].as_array().unwrap_or_else(|| panic!("no worst: {v}"))
}
fn stable(m: &str) -> String {
mimir_schema::moniker::stable(m).into_owned()
}
#[test]
fn a_copy_in_a_crate_that_imports_the_library_is_found_and_points_at_the_library() {
let _g = fixture::serial();
crate::mimir_lens::clone_inc::forget();
let f = vx::build_using(&[vx::alpha("crate/")]);
f.shapes(vx::MONO);
f.shapes(vx::OTHER);
let v = f.ws.ask("reimplements_shared", json!({ "limit": 50 }));
assert_eq!(state(&v), "complete", "{v}");
assert_eq!(v["kind"], "reimplements_shared", "{v}");
let w = worst(&v);
assert_eq!(w.len(), 1, "exactly gamma's copy of alpha's parse_header: {v}");
assert_eq!(v["functions"], 1, "the headline counts functions to replace: {v}");
// the gated pair decides; similar_code's cluster only adds evidence to it
assert!(hit_lenses(&w[0]).contains(&"semantic_twins".to_string()), "{}", w[0]);
let hit = &w[0];
assert_eq!(hit["replace"]["id"], stable(&vx::gamma("parse_header().")), "{hit}");
assert_eq!(hit["use_instead"]["id"], stable(&vx::alpha("parse_header().")), "{hit}");
assert_eq!(hit["tier"], "already_imports", "gamma's own manifest names alpha: {hit}");
assert_eq!(hit["fit"], "drop_in", "the same parameters: {hit}");
assert!(hit["evidence"].as_array().is_some_and(|e| !e.is_empty()), "every finding says which lens and index found it: {hit}");
assert!(hit["gain"]["lines"].as_u64().is_some_and(|l| l >= 1), "{hit}");
assert!(hit["gain"]["callers"].is_u64(), "{hit}");
// beta's copy: no dependency between beta and alpha / gamma — not a re-implementation
assert!(!v["worst"].to_string().contains(&stable(&vx::beta("http/parse_header()."))), "{v}");
}
fn hit_lenses(f: &Value) -> Vec<String> {
f["evidence"].as_array().into_iter().flatten().filter_map(|e| e["lens"].as_str()).map(str::to_string).collect()
}
/// A pair only `similar_code`'s cluster names (no semantic_twins pair: its
/// gates did not pass it) is no finding: a cluster's every-two-members are not
/// each a re-implementation. Without shapes semantic_twins has no copy here.
#[test]
fn a_cluster_pair_without_a_gated_twin_pair_is_no_finding() {
let _g = fixture::serial();
crate::mimir_lens::clone_inc::forget();
let f = vx::build_using(&[vx::alpha("crate/")]);
let v = f.ws.ask("reimplements_shared", json!({}));
assert!(worst(&v).is_empty(), "{v}");
assert!(v["left_out"]["only similar_code's cluster (no gated twin pair)"].as_u64().is_some_and(|n| n >= 1), "{v}");
}
#[test]
fn without_a_dependency_the_copies_are_twins_not_reimplementations() {
let _g = fixture::serial();
crate::mimir_lens::clone_inc::forget();
let f = vx::build();
let v = f.ws.ask("reimplements_shared", json!({}));
assert!(matches!(state(&v), "complete" | "empty_fact"), "{v}");
assert!(worst(&v).is_empty(), "no crate depends on another: nothing to call instead: {v}");
assert!(v["pairs_read"].as_u64().is_some_and(|n| n >= 1), "the copy/twin pairs were read: {v}");
}
#[test]
fn the_fit_is_drop_in_only_for_the_same_parameter_and_return_types() {
use super::reuse::{Signature, fit};
let a = Signature { params: vec!["&str".into()], ret: Some("Option<(String, String)>".into()) };
let b = a.clone();
assert_eq!(fit(&a, &b), "drop_in");
let c = Signature { params: vec!["&[u8]".into()], ret: a.ret.clone() };
assert_eq!(fit(&a, &c), "adapter");
let d = Signature { params: a.params.clone(), ret: None };
assert_eq!(fit(&a, &d), "adapter");
}
#[test]
fn only_a_direct_normal_dependency_is_already_imported() {
use super::reuse::direct_deps;
let m = r#"
[package]
name = "a"
[dependencies]
draupnir = { version = "0.2", optional = true }
korp_installer = { package = "korp-installer", version = "0.2" }
serde = "1"
[dev-dependencies]
nornir = "0.6"
[target.'cfg(unix)'.dependencies]
libc = "0.2"
"#;
let d = direct_deps(m);
assert!(d.contains("korp-installer") && d.contains("serde") && d.contains("libc"), "{d:?}");
assert!(!d.contains("draupnir"), "optional: add a dependency, not already imported");
assert!(!d.contains("nornir"), "a dev-dependency is not the library's caller");
}
#[test]
fn two_bodies_over_their_own_static_are_a_partial_fit() {
use super::reuse::own_statics;
let korp = "pub fn is_registered(code: &str) -> bool {\n REGISTRY.iter().any(|e| e.code == code)\n}";
let facett = "pub fn is_registered(code: &str) -> bool {\n REGISTRY.iter().any(|e| e.code == code)\n}";
assert_eq!(own_statics(korp, facett), vec!["REGISTRY".to_string()]);
// a path-qualified or called name is not an own static
assert!(own_statics("x::TABLE.get(1); MAX(2)", "x::TABLE.get(1); MAX(2)").is_empty());
}