diff --git a/crates/ui-egui/src/panels/assets_panel.rs b/crates/ui-egui/src/panels/assets_panel.rs index 9d58a81c5..9ee6c0dfe 100644 --- a/crates/ui-egui/src/panels/assets_panel.rs +++ b/crates/ui-egui/src/panels/assets_panel.rs @@ -1008,10 +1008,16 @@ fn usage(app: &mut FigApp, ui: &mut Ui, w: f32, link: &str) { }); app.run_background(&ui.ctx().clone(), USAGE, json!({"days": period.unwrap_or(days)}), done); } - let Some(v) = shown else { - ui.add_space(8.0); - return; - }; + match shown { + Some(v) => usage_answer(ui, w, usage_id, &v), + None => ui.add_space(8.0), + } +} + +/// Show usage's answer `v`: what uses the library, the period's insertions and detachments and +/// the rows of the list chosen (the choice kept with `id`). +pub(crate) fn usage_answer(ui: &mut Ui, w: f32, id: Id, v: &Value) { + let days = v["days"].as_u64().unwrap_or(USAGE_PERIODS[0].0); let files = v["files"].as_array().map_or(0, Vec::len); let instances = v["instances"].as_u64().unwrap_or(0); let known = v["detachesKnown"].as_bool().unwrap_or(false); @@ -1020,11 +1026,11 @@ fn usage(app: &mut FigApp, ui: &mut Ui, w: f32, link: &str) { if !known { hint(ui, tr("This server doesn't count detached instances")); } - let Some(kind) = usage_kind(ui, w, usage_id, &v) else { + let Some(kind) = usage_kind(ui, w, id, v) else { ui.add_space(8.0); return; }; - if let Some((i, d)) = period_totals(&v, kind) { + if let Some((i, d)) = period_totals(v, kind) { let label = USAGE_PERIODS.iter().find(|p| p.0 == days).map_or(USAGE_PERIODS[0].2, |p| p.2); let counts = trf("{insertions} · {detachments}", &[("insertions", &inserted_n(i)), ("detachments", &detached_n(d))]); hint(ui, &trf("{period}: {counts}", &[("period", &tr(label)), ("counts", &counts)])); @@ -1035,9 +1041,9 @@ fn usage(app: &mut FigApp, ui: &mut Ui, w: f32, link: &str) { hint(ui, tr("Nothing counted yet")); } if kind == "components" { - component_rows(ui, w, &v, known); + component_rows(ui, w, v, known); } else { - asset_rows(ui, w, &v, kind); + asset_rows(ui, w, v, kind); } ui.add_space(8.0); } diff --git a/crates/ui-egui/src/usage_summary_tests.rs b/crates/ui-egui/src/usage_summary_tests.rs index 4a512cc85..e9ce53fb9 100644 --- a/crates/ui-egui/src/usage_summary_tests.rs +++ b/crates/ui-egui/src/usage_summary_tests.rs @@ -1,9 +1,11 @@ //! Show usage's period line sums the list the Type choice shows, so it never reads 0 above a list -//! with counts, and a server that doesn't count a kind says so for that kind alone. +//! with counts, and a server that doesn't count a kind says so for that kind alone. The answer draws +//! its totals, its period and the rows of the list chosen. use serde_json::json; -use crate::panels::assets_panel::period_totals; +use crate::live_tests::{flat, has_text}; +use crate::panels::assets_panel::{period_totals, usage_answer}; #[test] fn the_period_line_sums_the_list_shown() { @@ -28,3 +30,60 @@ fn a_server_that_doesnt_count_a_kind_has_no_totals_for_it() { assert_eq!(period_totals(&v, "styles"), None, "a server from before them"); assert_eq!(period_totals(&json!({}), "components"), None); } + +/// The texts Show usage draws for the answer `v` with the Type choice at `kind` (0 components, +/// 1 styles, 2 variables). +fn drawn(v: &serde_json::Value, kind: usize) -> Vec { + let ctx = egui::Context::default(); + let id = egui::Id::new("usage"); + ctx.data_mut(|d| d.insert_temp(id.with("kind"), kind)); + let mut out = ctx.run_ui(egui::RawInput::default(), |ui| usage_answer(ui, 600.0, id, v)); + out.textures_delta.clear(); + flat(&out.shapes) +} + +#[test] +fn show_usage_draws_the_totals_the_period_and_the_list_chosen() { + let v = json!({ + "days": 90, + "files": [{}, {}], + "instances": 5, + "detachesKnown": true, + "detached": 3, + "actionsKnown": true, + "assetActionsKnown": true, + "assetsKnown": true, + "components": [ + {"name": "Button", "instances": 4, "files": 2, "detached": 2, "insertions": 6, "detachments": 2}, + {"name": "Gone", "instances": 0, "files": 0, "detached": 3}, + ], + "styles": [{"name": "Brand", "layers": 7, "files": 3, "insertions": 2, "detachments": 0}], + "variables": [], + }); + let components = drawn(&v, 0); + for text in [ + "Used in 2 files · 5 instances · 3 detached", + "Past 90 days: 6 inserted · 2 detached", + "4 instances in 2 files · 2 detached · 6 inserted", + "3 detached", + ] { + assert!(has_text(&components, text), "{text}"); + } + let styles = drawn(&v, 1); + assert!(has_text(&styles, "Past 90 days: 2 inserted · 0 detached")); + assert!(has_text(&styles, "7 layers in 3 files · 2 inserted")); + assert!(has_text(&drawn(&v, 2), "Nothing counted yet"), "an empty list says so"); +} + +#[test] +fn show_usage_from_an_older_server_says_what_it_doesnt_count() { + let shapes = drawn(&json!({"files": [{}, {}], "instances": 5, "components": []}), 0); + for text in [ + "Used in 2 files · 5 instances", + "This server doesn't count detached instances", + "This server doesn't count styles and variables", + "This server doesn't count insertions", + ] { + assert!(has_text(&shapes, text), "{text}"); + } +}