From 7b200dbec664fa5af7645c0721ba7993b85143ca Mon Sep 17 00:00:00 2001 From: stephenleo Date: Fri, 27 Mar 2026 20:10:58 +0800 Subject: [PATCH 1/3] refactor(config): remove #[cfg(test)] type aliases CostSubfieldConfig and ContextWindowSubfieldConfig Replace all test usages with SubfieldConfig directly so test code honestly reflects the real type and future type divergence is not masked by silent-compiling aliases. Co-Authored-By: Claude Sonnet 4.6 --- src/config.rs | 6 ------ src/modules/context_window.rs | 36 +++++++++++++++++------------------ src/modules/cost.rs | 20 +++++++++---------- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/config.rs b/src/config.rs index 85bc3dc..b45e314 100644 --- a/src/config.rs +++ b/src/config.rs @@ -81,12 +81,6 @@ pub struct SubfieldConfig { pub invert_threshold: Option, } -/// Backwards-compatible type aliases (used by test code). -#[cfg(test)] -pub type CostSubfieldConfig = SubfieldConfig; -#[cfg(test)] -pub type ContextWindowSubfieldConfig = SubfieldConfig; - /// Trait for uniform access to style/threshold fields shared by config types. /// Used by `render_styled_value()` to resolve sub-field → parent fallback. /// diff --git a/src/modules/context_window.rs b/src/modules/context_window.rs index c8f3511..074a523 100644 --- a/src/modules/context_window.rs +++ b/src/modules/context_window.rs @@ -51,7 +51,7 @@ pub fn render_used_percentage(ctx: &Context, cfg: &CshipConfig) -> Option Context { @@ -589,7 +589,7 @@ mod tests { }; let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - used_percentage: Some(ContextWindowSubfieldConfig { + used_percentage: Some(SubfieldConfig { warn_threshold: Some(80.0), warn_style: Some("yellow".to_string()), critical_threshold: Some(95.0), @@ -624,7 +624,7 @@ mod tests { }; let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - used_percentage: Some(ContextWindowSubfieldConfig { + used_percentage: Some(SubfieldConfig { warn_threshold: Some(80.0), warn_style: Some("yellow".to_string()), critical_threshold: Some(95.0), @@ -659,7 +659,7 @@ mod tests { }; let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - used_percentage: Some(ContextWindowSubfieldConfig { + used_percentage: Some(SubfieldConfig { warn_threshold: Some(80.0), warn_style: Some("yellow".to_string()), ..Default::default() @@ -708,7 +708,7 @@ mod tests { let ctx = ctx_full(); // used_percentage=35, remaining_percentage=65 let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - used_percentage: Some(ContextWindowSubfieldConfig { + used_percentage: Some(SubfieldConfig { disabled: Some(true), ..Default::default() }), @@ -737,7 +737,7 @@ mod tests { }; let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - used_percentage: Some(ContextWindowSubfieldConfig { + used_percentage: Some(SubfieldConfig { format: Some("[$value%]($style)".to_string()), warn_threshold: Some(80.0), warn_style: Some("yellow".to_string()), @@ -772,7 +772,7 @@ mod tests { }; let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - size: Some(ContextWindowSubfieldConfig { + size: Some(SubfieldConfig { warn_threshold: Some(150000.0), warn_style: Some("yellow".to_string()), ..Default::default() @@ -801,7 +801,7 @@ mod tests { }; let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - total_input_tokens: Some(ContextWindowSubfieldConfig { + total_input_tokens: Some(SubfieldConfig { warn_threshold: Some(150000.0), warn_style: Some("yellow".to_string()), ..Default::default() @@ -828,7 +828,7 @@ mod tests { let cfg = CshipConfig { context_window: Some(ContextWindowConfig { style: Some("green".to_string()), - used_percentage: Some(ContextWindowSubfieldConfig { + used_percentage: Some(SubfieldConfig { style: Some("blue".to_string()), ..Default::default() }), @@ -860,7 +860,7 @@ mod tests { }; let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - remaining_percentage: Some(ContextWindowSubfieldConfig { + remaining_percentage: Some(SubfieldConfig { invert_threshold: Some(true), warn_threshold: Some(20.0), warn_style: Some("yellow".to_string()), @@ -892,7 +892,7 @@ mod tests { }; let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - remaining_percentage: Some(ContextWindowSubfieldConfig { + remaining_percentage: Some(SubfieldConfig { invert_threshold: Some(true), warn_threshold: Some(20.0), warn_style: Some("yellow".to_string()), @@ -921,7 +921,7 @@ mod tests { }; let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - remaining_percentage: Some(ContextWindowSubfieldConfig { + remaining_percentage: Some(SubfieldConfig { invert_threshold: Some(true), warn_threshold: Some(20.0), warn_style: Some("yellow".to_string()), @@ -955,7 +955,7 @@ mod tests { context_window: Some(ContextWindowConfig { warn_threshold: Some(80.0), // parent: warn when 80% USED warn_style: Some("yellow".to_string()), - remaining_percentage: Some(ContextWindowSubfieldConfig { + remaining_percentage: Some(SubfieldConfig { invert_threshold: Some(true), // no subfield thresholds set → nothing fires ..Default::default() @@ -978,7 +978,7 @@ mod tests { let result_default = render_used_percentage(&ctx, &CshipConfig::default()); let cfg_no_thresh = CshipConfig { context_window: Some(ContextWindowConfig { - used_percentage: Some(ContextWindowSubfieldConfig { + used_percentage: Some(SubfieldConfig { ..Default::default() // all None }), ..Default::default() @@ -1017,7 +1017,7 @@ mod tests { let ctx = ctx_used_tokens(85.0); let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - used_tokens: Some(ContextWindowSubfieldConfig { + used_tokens: Some(SubfieldConfig { warn_threshold: Some(80.0), warn_style: Some("yellow".to_string()), critical_threshold: Some(95.0), @@ -1049,7 +1049,7 @@ mod tests { let ctx = ctx_used_tokens(97.0); let cfg = CshipConfig { context_window: Some(ContextWindowConfig { - used_tokens: Some(ContextWindowSubfieldConfig { + used_tokens: Some(SubfieldConfig { warn_threshold: Some(80.0), warn_style: Some("yellow".to_string()), critical_threshold: Some(95.0), @@ -1082,7 +1082,7 @@ mod tests { let result_default = render_used_tokens(&ctx, &CshipConfig::default()).unwrap(); let cfg_no_thresh = CshipConfig { context_window: Some(ContextWindowConfig { - used_tokens: Some(ContextWindowSubfieldConfig { + used_tokens: Some(SubfieldConfig { ..Default::default() // all None }), ..Default::default() diff --git a/src/modules/cost.rs b/src/modules/cost.rs index 22a64bd..a16f10a 100644 --- a/src/modules/cost.rs +++ b/src/modules/cost.rs @@ -1,5 +1,5 @@ #[cfg(test)] -use crate::config::CostSubfieldConfig; +use crate::config::SubfieldConfig; /// Render the `[cship.cost]` family of modules. /// /// `$cship.cost` — convenience alias: formats total_cost_usd as "$X.XX" with threshold styling. @@ -450,7 +450,7 @@ mod tests { let ctx = ctx_with_cost(5.0); // total_cost_usd = 5.0 let cfg = CshipConfig { cost: Some(CostConfig { - total_cost_usd: Some(CostSubfieldConfig { + total_cost_usd: Some(SubfieldConfig { warn_threshold: Some(3.0), warn_style: Some("yellow".to_string()), critical_threshold: Some(10.0), @@ -475,7 +475,7 @@ mod tests { let ctx = ctx_with_cost(12.0); // total_cost_usd = 12.0 let cfg = CshipConfig { cost: Some(CostConfig { - total_cost_usd: Some(CostSubfieldConfig { + total_cost_usd: Some(SubfieldConfig { warn_threshold: Some(3.0), warn_style: Some("yellow".to_string()), critical_threshold: Some(10.0), @@ -500,7 +500,7 @@ mod tests { let ctx = ctx_with_cost(1.0); // below warn_threshold of 3.0 let cfg = CshipConfig { cost: Some(CostConfig { - total_cost_usd: Some(CostSubfieldConfig { + total_cost_usd: Some(SubfieldConfig { warn_threshold: Some(3.0), warn_style: Some("yellow".to_string()), ..Default::default() @@ -523,7 +523,7 @@ mod tests { let ctx = ctx_with_cost(0.01); // ctx_with_cost sets total_duration_ms = 45000 let cfg = CshipConfig { cost: Some(CostConfig { - total_duration_ms: Some(CostSubfieldConfig { + total_duration_ms: Some(SubfieldConfig { warn_threshold: Some(30000.0), warn_style: Some("yellow".to_string()), critical_threshold: Some(60000.0), @@ -545,7 +545,7 @@ mod tests { let ctx = ctx_with_cost(0.01); // total_duration_ms = 45000 > 30000 let cfg = CshipConfig { cost: Some(CostConfig { - total_duration_ms: Some(CostSubfieldConfig { + total_duration_ms: Some(SubfieldConfig { format: Some("[$value ms]($style)".to_string()), warn_threshold: Some(30000.0), warn_style: Some("yellow".to_string()), @@ -574,7 +574,7 @@ mod tests { let ctx = ctx_with_cost(0.01); // ctx_with_cost sets total_api_duration_ms = 2300 let cfg = CshipConfig { cost: Some(CostConfig { - total_api_duration_ms: Some(CostSubfieldConfig { + total_api_duration_ms: Some(SubfieldConfig { warn_threshold: Some(2000.0), warn_style: Some("yellow".to_string()), ..Default::default() @@ -594,7 +594,7 @@ mod tests { let ctx = ctx_with_cost(0.01); // ctx_with_cost sets total_lines_added = 156 let cfg = CshipConfig { cost: Some(CostConfig { - total_lines_added: Some(CostSubfieldConfig { + total_lines_added: Some(SubfieldConfig { warn_threshold: Some(100.0), warn_style: Some("yellow".to_string()), ..Default::default() @@ -614,7 +614,7 @@ mod tests { let ctx = ctx_with_cost(0.01); // ctx_with_cost sets total_lines_removed = 23 let cfg = CshipConfig { cost: Some(CostConfig { - total_lines_removed: Some(CostSubfieldConfig { + total_lines_removed: Some(SubfieldConfig { warn_threshold: Some(10.0), warn_style: Some("yellow".to_string()), ..Default::default() @@ -635,7 +635,7 @@ mod tests { let result_default = render_total_cost_usd(&ctx, &CshipConfig::default()); let cfg_no_thresh = CshipConfig { cost: Some(CostConfig { - total_cost_usd: Some(CostSubfieldConfig { + total_cost_usd: Some(SubfieldConfig { ..Default::default() // all None }), ..Default::default() From 31dff3704d8e7a568d5697016aee3e3796474f74 Mon Sep 17 00:00:00 2001 From: stephenleo Date: Fri, 27 Mar 2026 20:17:31 +0800 Subject: [PATCH 2/3] fix: remove unused impl_has_threshold_style macro and move test import Remove the dead `impl_has_threshold_style!` macro (defined but never invoked) from config.rs and relocate the `#[cfg(test)] use SubfieldConfig` import in cost.rs from file-top to inside the test module where it belongs. Co-Authored-By: Claude Sonnet 4.6 --- src/config.rs | 23 ----------------------- src/modules/cost.rs | 4 +--- 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/src/config.rs b/src/config.rs index b45e314..df83410 100644 --- a/src/config.rs +++ b/src/config.rs @@ -101,29 +101,6 @@ pub trait HasThresholdStyle { } } -#[allow(unused_macros)] -macro_rules! impl_has_threshold_style { - ($t:ty) => { - impl HasThresholdStyle for $t { - fn style(&self) -> Option<&str> { - self.style.as_deref() - } - fn warn_threshold(&self) -> Option { - self.warn_threshold - } - fn warn_style(&self) -> Option<&str> { - self.warn_style.as_deref() - } - fn critical_threshold(&self) -> Option { - self.critical_threshold - } - fn critical_style(&self) -> Option<&str> { - self.critical_style.as_deref() - } - } - }; -} - /// Configuration for `[cship.context_bar]` — visual progress bar with thresholds. /// Implemented in Story 2.2. Defined here so all Epic 2 config is available. #[derive(Debug, Deserialize, Default)] diff --git a/src/modules/cost.rs b/src/modules/cost.rs index a16f10a..0b81022 100644 --- a/src/modules/cost.rs +++ b/src/modules/cost.rs @@ -1,5 +1,3 @@ -#[cfg(test)] -use crate::config::SubfieldConfig; /// Render the `[cship.cost]` family of modules. /// /// `$cship.cost` — convenience alias: formats total_cost_usd as "$X.XX" with threshold styling. @@ -172,7 +170,7 @@ fn is_subfield_disabled( #[cfg(test)] mod tests { use super::*; - use crate::config::{CostConfig, CshipConfig}; + use crate::config::{CostConfig, CshipConfig, SubfieldConfig}; use crate::context::{Context, Cost}; fn ctx_with_cost(usd: f64) -> Context { From 871ba5ffec4016c96cd04a811c917cc96a4e90fd Mon Sep 17 00:00:00 2001 From: stephenleo Date: Fri, 27 Mar 2026 20:25:23 +0800 Subject: [PATCH 3/3] style: apply rustfmt to story 3-4 import ordering Co-Authored-By: Claude Sonnet 4.6 --- src/modules/context_window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/context_window.rs b/src/modules/context_window.rs index 074a523..f0b24f0 100644 --- a/src/modules/context_window.rs +++ b/src/modules/context_window.rs @@ -404,7 +404,7 @@ pub fn render_current_usage_cache_read_input_tokens( #[cfg(test)] mod tests { use super::*; - use crate::config::{ContextWindowConfig, SubfieldConfig, CshipConfig}; + use crate::config::{ContextWindowConfig, CshipConfig, SubfieldConfig}; use crate::context::{Context, ContextWindow, CurrentUsage}; fn ctx_full() -> Context {