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) <noreply@anthropic.com>
This commit is contained in:
Edward Langley
2026-04-09 01:25:26 -07:00
parent a61d7aa229
commit 32d215f3d6

View File

@ -82,6 +82,11 @@ impl Effect for AddFormula {
fn apply(&self, app: &mut App) { fn apply(&self, app: &mut App) {
match crate::formula::parse_formula(&self.raw, &self.target_category) { match crate::formula::parse_formula(&self.raw, &self.target_category) {
Ok(formula) => { 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); app.model.add_formula(formula);
} }
Err(e) => { Err(e) => {
@ -1021,6 +1026,38 @@ mod tests {
assert!(!app.model.formulas().is_empty()); 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] #[test]
fn add_formula_invalid_sets_error_status() { fn add_formula_invalid_sets_error_status() {
let mut app = test_app(); let mut app = test_app();