From 32d215f3d6996ec24f3efb0bc6163edc17b0f1ab Mon Sep 17 00:00:00 2001 From: Edward Langley Date: Thu, 9 Apr 2026 01:25:26 -0700 Subject: [PATCH] fix: AddFormula now adds target item to category When adding a formula interactively, the target (e.g. "Margin") was registered as a formula but never added as an item to the target category. The grid layout never created cells for it, making the formula invisible. Now AddFormula::apply calls add_item before registering the formula. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/ui/effect.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/ui/effect.rs b/src/ui/effect.rs index 2e6213c..8b3d201 100644 --- a/src/ui/effect.rs +++ b/src/ui/effect.rs @@ -82,6 +82,11 @@ impl Effect for AddFormula { fn apply(&self, app: &mut App) { match crate::formula::parse_formula(&self.raw, &self.target_category) { Ok(formula) => { + // Ensure the formula target exists as an item in the target category + // so the grid layout includes cells for it. + if let Some(cat) = app.model.category_mut(&formula.target_category) { + cat.add_item(&formula.target); + } app.model.add_formula(formula); } Err(e) => { @@ -1021,6 +1026,38 @@ mod tests { assert!(!app.model.formulas().is_empty()); } + /// Regression: AddFormula must add the target item to the target category + /// so the grid layout includes cells for it. Without this, a formula like + /// `Margin = Profit / Revenue` would be registered but invisible in the grid. + #[test] + fn add_formula_adds_target_item_to_category() { + let mut app = test_app(); + // "Margin" does not exist as an item in "Type" before adding the formula + assert!(!app + .model + .category("Type") + .unwrap() + .ordered_item_names() + .contains(&"Margin")); + AddFormula { + raw: "Margin = Food * 2".to_string(), + target_category: "Type".to_string(), + } + .apply(&mut app); + let items: Vec<&str> = app + .model + .category("Type") + .unwrap() + .ordered_item_names() + .into_iter() + .collect(); + assert!( + items.contains(&"Margin"), + "formula target 'Margin' should be added as item to 'Type' category, got: {:?}", + items + ); + } + #[test] fn add_formula_invalid_sets_error_status() { let mut app = test_app();