diff --git a/crates/analysis/src/js_source.rs b/crates/analysis/src/js_source.rs index 2e6e184ce..25fbdd6c2 100644 --- a/crates/analysis/src/js_source.rs +++ b/crates/analysis/src/js_source.rs @@ -335,6 +335,10 @@ struct UncertainSource<'a> { /// An incomplete source-map inventory (truncated, an unread external section, or a map that /// couldn't be read) cannot establish that an otherwise unmatched source is absent. Such sources /// are reported separately in `uncertain_sources`; positive matches remain available. +/// +/// At most `max_rows` source scripts are examined and at most `max_rows` rows of each list are +/// kept: `summary.scripts_examined` and `rows_capped` say when the counts cover only that window, +/// while bundle units without an observed source are counted in full. pub fn diff_source_and_bundle(src: &SourceGraph, app: &AppGraph, max_rows: usize) -> Value { let mut units: Vec> = Vec::new(); for m in app.modules.iter().filter(|m| m.kind == "script") { @@ -384,7 +388,10 @@ pub fn diff_source_and_bundle(src: &SourceGraph, app: &AppGraph, max_rows: usize let mut uncertain = Vec::new(); let mut claimed: BTreeSet = BTreeSet::new(); let mut by_quality: BTreeMap<&str, usize> = BTreeMap::new(); + let source_scripts = src.files.iter().filter(|f| is_script(&f.path)).count(); + let mut scripts_examined = 0usize; for f in src.files.iter().filter(|f| is_script(&f.path)).take(max_rows) { + scripts_examined += 1; let norm = norm_path(&f.path); let fs: BTreeSet<&str> = f.strings.iter().map(String::as_str).filter(|s| s.len() >= 4).collect(); let ff: BTreeSet<&str> = f.functions.iter().map(String::as_str).collect(); @@ -456,20 +463,28 @@ pub fn diff_source_and_bundle(src: &SourceGraph, app: &AppGraph, max_rows: usize }, } } - let unclaimed: Vec = units - .iter() - .enumerate() - .filter(|(i, u)| !claimed.contains(i) && !u.id.contains("node_modules")) - .map(|(_, u)| json!({"unit": u.id, "file": u.file, "kind": u.kind})) - .take(max_rows) - .collect(); + // A unit without a source is counted in full even when only some rows are listed: the list is + // for reading, the count is evidence. + let mut bundle_units_without_source = 0usize; + let mut unclaimed: Vec = Vec::new(); + for (i, u) in units.iter().enumerate() { + if claimed.contains(&i) || u.id.contains("node_modules") { + continue; + } + bundle_units_without_source += 1; + if unclaimed.len() < max_rows { + unclaimed.push(json!({"unit": u.id, "file": u.file, "kind": u.kind})); + } + } + let rows_capped = scripts_examined < source_scripts || bundle_units_without_source > unclaimed.len(); json!({ "source_graph_id": src.source_graph_id, "graph_id": app.graph_id, "source_map_inventory_truncated": source_map_inventory_truncated, "source_map_external_sections": source_map_external, "source_map_unreadable": source_map_unreadable, - "summary": {"source_scripts": src.files.iter().filter(|f| is_script(&f.path)).count(), "bundle_units": units.len(), "matched": matches.len(), "unmatched_sources": unmatched.len(), "uncertain_sources": uncertain.len(), "bundle_units_without_source": unclaimed.len(), "by_quality": by_quality}, + "rows_capped": rows_capped, + "summary": {"source_scripts": source_scripts, "scripts_examined": scripts_examined, "bundle_units": units.len(), "matched": matches.len(), "unmatched_sources": unmatched.len(), "uncertain_sources": uncertain.len(), "bundle_units_without_source": bundle_units_without_source, "by_quality": by_quality}, "matches": matches, "unmatched_sources": unmatched, "uncertain_sources": uncertain, diff --git a/crates/analysis/tests/source_bundle_compare_rows.rs b/crates/analysis/tests/source_bundle_compare_rows.rs new file mode 100644 index 000000000..1a8975246 --- /dev/null +++ b/crates/analysis/tests/source_bundle_compare_rows.rs @@ -0,0 +1,50 @@ +//! A source-to-bundle comparison that examines only `max_rows` scripts says how many it examined, +//! and the unclaimed-unit count stays complete even when only some rows are listed. + +use craft_anything_analysis::js_app::{AppFile, AppGraph, AppInput, AppOptions, analyze_application}; +use craft_anything_analysis::js_source::{SourceGraph, build_source_graph, diff_source_and_bundle}; +use serde_json::json; + +fn app_graph(files: &[(&str, &str)]) -> AppGraph { + let files: Vec = files.iter().map(|(path, src)| AppFile { path: (*path).into(), bytes: src.as_bytes().to_vec() }).collect(); + let listing = files.iter().map(|f| (f.path.clone(), f.bytes.len() as u64)).collect(); + analyze_application(&AppInput { files, listing, ..Default::default() }, &AppOptions::default(), &|| false).unwrap() +} + +fn source_graph(paths: &[&str]) -> SourceGraph { + let files: Vec<_> = paths.iter().map(|path| ((*path).to_string(), Vec::new())).collect(); + build_source_graph("reference", &files, 0, false, &Default::default(), &|| false).unwrap() +} + +/// A capped comparison examines only the first script but reports that beside the total, and the +/// unclaimed-unit count stays complete even though only one row is listed. +#[test] +fn examined_scripts_and_unclaimed_units_are_counted_in_full() { + let graph = app_graph(&[("a.js", "const a = 1;\n"), ("b.js", "const b = 2;\n"), ("c.js", "const c = 3;\n")]); + let src = source_graph(&["a.js", "b.js"]); + let diff = diff_source_and_bundle(&src, &graph, 1); + let summary = &diff["summary"]; + assert_eq!(summary["scripts_examined"], json!(1), "{diff}"); + assert_eq!(summary["source_scripts"], json!(2), "{diff}"); + assert_eq!(summary["matched"], json!(1), "{diff}"); + assert_eq!(summary["unmatched_sources"], json!(0), "{diff}"); + assert_eq!(summary["uncertain_sources"], json!(0), "{diff}"); + assert_eq!(diff["rows_capped"], json!(true), "{diff}"); + // b.js and c.js are unclaimed: the count is complete even though only one row is listed. + assert_eq!(summary["bundle_units_without_source"], json!(2), "{diff}"); + assert_eq!(diff["bundle_units_without_source"].as_array().map(Vec::len), Some(1), "{diff}"); +} + +/// With room for every script, the comparison examines all of them and does not claim a cap. +#[test] +fn an_uncapped_comparison_reports_every_script() { + let graph = app_graph(&[("a.js", "const a = 1;\n"), ("b.js", "const b = 2;\n")]); + let src = source_graph(&["a.js", "b.js"]); + let diff = diff_source_and_bundle(&src, &graph, 10); + let summary = &diff["summary"]; + assert_eq!(summary["scripts_examined"], json!(2), "{diff}"); + assert_eq!(summary["source_scripts"], json!(2), "{diff}"); + assert_eq!(summary["matched"], json!(2), "{diff}"); + assert_eq!(summary["bundle_units_without_source"], json!(0), "{diff}"); + assert_eq!(diff["rows_capped"], json!(false), "{diff}"); +} diff --git a/crates/engine/src/tools/application_tools.rs b/crates/engine/src/tools/application_tools.rs index 5552a1416..66349f6f3 100644 --- a/crates/engine/src/tools/application_tools.rs +++ b/crates/engine/src/tools/application_tools.rs @@ -488,7 +488,7 @@ fn default_rows() -> usize { } /// Compare a reference source tree with a shipped bundle, carrying a source map's unread external -/// sections into the evidence as uncertainty rather than absent sources. +/// sections and the row cap into the evidence as uncertainty rather than absent sources. fn compare_source_to_bundle_tool(ctx: &mut Ctx<'_>, params: Value) -> Result { let p: SourceBundleParams = parse_params(params)?; let (src, sid) = if let Some(path) = is_path_input(&p.source) { @@ -508,12 +508,14 @@ fn compare_source_to_bundle_tool(ctx: &mut Ctx<'_>, params: Value) -> Result, params: Value) -> Result