refactor: colocate cmd tests with their modules
Move tests from the monolithic tests.rs into #[cfg(test)] mod tests blocks in each command module. Shared test helpers live in mod.rs::test_helpers. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Executed-By: fido
This commit is contained in:
@ -5,6 +5,133 @@ use crate::ui::effect::{self, Effect};
|
||||
use super::core::{Cmd, CmdContext};
|
||||
use super::navigation::{viewport_effects, CursorState, EnterAdvance};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::*;
|
||||
use crate::command::cmd::test_helpers::*;
|
||||
use crate::model::Model;
|
||||
|
||||
#[test]
|
||||
fn commit_formula_with_categories_adds_formula() {
|
||||
let m = two_cat_model();
|
||||
let layout = make_layout(&m);
|
||||
let reg = make_registry();
|
||||
let mut bufs = HashMap::new();
|
||||
bufs.insert("formula".to_string(), "Profit = Revenue - Cost".to_string());
|
||||
let mut ctx = make_ctx(&m, &layout, ®);
|
||||
ctx.buffers = &bufs;
|
||||
let effects = CommitFormula.execute(&ctx);
|
||||
let dbg = effects_debug(&effects);
|
||||
assert!(
|
||||
dbg.contains("AddFormula"),
|
||||
"Expected AddFormula, got: {dbg}"
|
||||
);
|
||||
assert!(
|
||||
dbg.contains("FormulaPanel"),
|
||||
"Expected return to FormulaPanel, got: {dbg}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commit_formula_without_regular_categories_shows_status() {
|
||||
let m = Model::new("Empty");
|
||||
let layout = make_layout(&m);
|
||||
let reg = make_registry();
|
||||
let mut bufs = HashMap::new();
|
||||
bufs.insert("formula".to_string(), "X = Y + Z".to_string());
|
||||
let mut ctx = make_ctx(&m, &layout, ®);
|
||||
ctx.buffers = &bufs;
|
||||
let effects = CommitFormula.execute(&ctx);
|
||||
let dbg = effects_debug(&effects);
|
||||
assert!(
|
||||
!dbg.contains("AddFormula"),
|
||||
"Should not add formula when only virtual categories exist, got: {dbg}"
|
||||
);
|
||||
assert!(
|
||||
dbg.contains("Add at least one category first"),
|
||||
"Expected status message, got: {dbg}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commit_category_add_with_name_produces_add_effect() {
|
||||
let m = two_cat_model();
|
||||
let layout = make_layout(&m);
|
||||
let reg = make_registry();
|
||||
let mut bufs = HashMap::new();
|
||||
bufs.insert("category".to_string(), "Region".to_string());
|
||||
let mut ctx = make_ctx(&m, &layout, ®);
|
||||
ctx.buffers = &bufs;
|
||||
let effects = CommitCategoryAdd.execute(&ctx);
|
||||
let dbg = effects_debug(&effects);
|
||||
assert!(
|
||||
dbg.contains("AddCategory"),
|
||||
"Expected AddCategory, got: {dbg}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commit_category_add_with_empty_buffer_returns_to_panel() {
|
||||
let m = two_cat_model();
|
||||
let layout = make_layout(&m);
|
||||
let reg = make_registry();
|
||||
let mut bufs = HashMap::new();
|
||||
bufs.insert("category".to_string(), "".to_string());
|
||||
let mut ctx = make_ctx(&m, &layout, ®);
|
||||
ctx.buffers = &bufs;
|
||||
let effects = CommitCategoryAdd.execute(&ctx);
|
||||
let dbg = effects_debug(&effects);
|
||||
assert!(
|
||||
dbg.contains("CategoryPanel"),
|
||||
"Expected return to CategoryPanel, got: {dbg}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commit_item_add_with_name_produces_add_item() {
|
||||
let m = two_cat_model();
|
||||
let layout = make_layout(&m);
|
||||
let reg = make_registry();
|
||||
let mut bufs = HashMap::new();
|
||||
bufs.insert("item".to_string(), "March".to_string());
|
||||
let mut ctx = make_ctx(&m, &layout, ®);
|
||||
let item_add_mode = AppMode::item_add("Month".to_string());
|
||||
ctx.mode = &item_add_mode;
|
||||
ctx.buffers = &bufs;
|
||||
let effects = CommitItemAdd.execute(&ctx);
|
||||
let dbg = effects_debug(&effects);
|
||||
assert!(dbg.contains("AddItem"), "Expected AddItem, got: {dbg}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commit_item_add_outside_item_add_mode_returns_empty() {
|
||||
let m = two_cat_model();
|
||||
let layout = make_layout(&m);
|
||||
let reg = make_registry();
|
||||
let ctx = make_ctx(&m, &layout, ®);
|
||||
let effects = CommitItemAdd.execute(&ctx);
|
||||
assert!(effects.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commit_export_produces_export_and_normal_mode() {
|
||||
let m = two_cat_model();
|
||||
let layout = make_layout(&m);
|
||||
let reg = make_registry();
|
||||
let mut bufs = HashMap::new();
|
||||
bufs.insert("export".to_string(), "/tmp/test.csv".to_string());
|
||||
let mut ctx = make_ctx(&m, &layout, ®);
|
||||
ctx.buffers = &bufs;
|
||||
let effects = CommitExport.execute(&ctx);
|
||||
assert_eq!(effects.len(), 2);
|
||||
let dbg = effects_debug(&effects);
|
||||
assert!(dbg.contains("ExportCsv"), "Expected ExportCsv, got: {dbg}");
|
||||
assert!(dbg.contains("Normal"), "Expected Normal mode, got: {dbg}");
|
||||
}
|
||||
}
|
||||
|
||||
// ── Commit commands (mode-specific buffer consumers) ────────────────────────
|
||||
|
||||
/// Commit a cell value: for synthetic records keys, stage in drill pending edits
|
||||
|
||||
Reference in New Issue
Block a user