use std::collections::BTreeMap;
use pretty_assertions::assert_eq;
use rho_protocol::{
Applies, ClaudeCliModel, ConfigCategory, ConfigChangedParams, ConfigError, ConfigNamespace,
ConfigRevision, ConfigSchema, IntegerUnit, ModelChoice, ModelList, ModelRef, ModelRuntime,
ModelSelection, SettingChoice, SettingEntry, SettingKey, SettingKind, SettingSpec,
SettingValue, ValueSource,
};
use super::{Applied, DaemonSettings, ListedModels, NamespaceValues, SettingsNamespace};
use crate::tui::config_row::ConfigRow;
use crate::tui::{picker::PickerDetail, PickerItem};
fn spec(key: &str, label: &str, kind: SettingKind, applies: Applies) -> SettingSpec {
SettingSpec {
key: SettingKey::new(key),
kind,
default: None,
category: "agent".into(),
section: Some("Advisor".into()),
label: label.into(),
help: format!("{label} help."),
applies,
overridable_per_project: false,
per_machine: false,
read_only: false,
}
}
fn entry(value: Option<SettingValue>, source: ValueSource) -> SettingEntry {
SettingEntry { value, source }
}
fn values(
revision: &str,
entries: Vec<(&str, SettingEntry)>,
errors: Vec<ConfigError>,
) -> NamespaceValues {
NamespaceValues {
revision: ConfigRevision::new(revision),
values: entries
.into_iter()
.map(|(key, entry)| (SettingKey::new(key), entry))
.collect(),
errors,
}
}
fn models() -> ModelList {
ModelList {
models: vec![ModelChoice {
model: ModelRef {
provider: "openai".into(),
model: "gpt-5.5".into(),
},
label: "GPT-5.5".into(),
}],
claude_cli: vec![ClaudeCliModel {
model: None,
label: "claude-code/default".into(),
detail: "Set no model and use whichever model Claude Code selects.".into(),
}],
}
}
pub(in crate::tui) fn settings() -> DaemonSettings {
let mode = spec(
"behavior.advisor_mode",
"Advisor mode",
SettingKind::Bool,
Applies::NextTurn,
);
let model = spec(
"internal_agents.advisor",
"Advisor model",
SettingKind::Model {
runtimes: vec![ModelRuntime::Rho, ModelRuntime::ClaudeCli],
},
Applies::NextTurn,
);
let shell = SettingSpec {
default: Some(SettingValue::Text("bash".into())),
..spec(
"behavior.inline_shell",
"Inline shell",
SettingKind::Choice {
choices: vec![
SettingChoice {
value: "bash".into(),
label: "Bash".into(),
help: None,
},
SettingChoice {
value: "zsh".into(),
label: "Zsh".into(),
help: None,
},
],
},
Applies::Immediately,
)
};
let threshold = SettingSpec {
section: None,
category: "context".into(),
..spec(
"compaction.compact_threshold_percent",
"Compact threshold",
SettingKind::Integer {
min: 1,
max: Some(100),
unit: Some(IntegerUnit::Percent),
},
Applies::NextTurn,
)
};
let zen = SettingSpec {
category: "display".into(),
section: None,
..spec(
"zen_mode",
"Zen mode",
SettingKind::Bool,
Applies::Immediately,
)
};
let store = SettingSpec {
category: "providers".into(),
section: None,
..spec(
"behavior.credential_store",
"Credential store",
SettingKind::Text,
Applies::Restart,
)
};
let locked = SettingSpec {
category: "providers".into(),
section: None,
read_only: true,
..spec(
"providers.locked",
"Locked",
SettingKind::Bool,
Applies::Restart,
)
};
let schema = ConfigSchema {
categories: vec![
ConfigCategory {
id: "agent".into(),
label: "Agent".into(),
},
ConfigCategory {
id: "context".into(),
label: "Context".into(),
},
ConfigCategory {
id: "tools".into(),
label: "Tools".into(),
},
ConfigCategory {
id: "providers".into(),
label: "Providers".into(),
},
],
settings: vec![mode, model, shell, threshold, store, locked],
file_only: Vec::new(),
};
let agent = values(
"a1",
vec![
(
"behavior.advisor_mode",
entry(Some(SettingValue::Bool(true)), ValueSource::Global),
),
(
"internal_agents.advisor",
entry(
Some(SettingValue::Model(ModelSelection::Rho {
provider: "openai".into(),
model: "gpt-5.5".into(),
})),
ValueSource::Global,
),
),
(
"behavior.inline_shell",
entry(
Some(SettingValue::Text("bash".into())),
ValueSource::Default,
),
),
(
"compaction.compact_threshold_percent",
entry(Some(SettingValue::Integer(80)), ValueSource::Global),
),
],
vec![ConfigError {
key: Some(SettingKey::new("compaction.compact_threshold_percent")),
message: "must be at most 100, not 140".into(),
}],
);
let tui = values(
"t1",
vec![
(
"zen_mode",
entry(Some(SettingValue::Bool(false)), ValueSource::Default),
),
(
"output_streaming",
entry(
Some(SettingValue::Text("live".into())),
ValueSource::Default,
),
),
],
Vec::new(),
);
DaemonSettings::new(
schema,
vec![zen],
agent,
tui,
ListedModels::Listed(models()),
)
}
#[derive(Debug, PartialEq)]
struct Row {
section: Option<String>,
label: String,
badge: Option<String>,
detail: Option<String>,
does: Option<ConfigRow>,
}
fn rows(items: Vec<PickerItem>) -> Vec<Row> {
items
.into_iter()
.map(|item| Row {
section: item.section,
label: item.label,
badge: item.badge.map(|badge| badge.text),
detail: match item.detail {
Some(PickerDetail::Text(text)) => Some(text),
_ => None,
},
does: ConfigRow::parse(&item.value),
})
.collect()
}
fn setting(namespace: SettingsNamespace, key: &str) -> Option<ConfigRow> {
Some(ConfigRow::Setting {
namespace,
key: SettingKey::new(key),
})
}
#[test]
fn root_rows_list_every_category_with_settings() {
let category = |namespace, id: &str| {
Some(ConfigRow::Category {
namespace,
id: id.into(),
})
};
assert_eq!(
rows(settings().category_rows()),
vec![
Row {
section: Some("Settings".into()),
label: "Agent".into(),
badge: None,
detail: Some("Advisor mode, Advisor model, Inline shell.".into()),
does: category(SettingsNamespace::Agent, "agent"),
},
Row {
section: Some("Settings".into()),
label: "Context".into(),
badge: None,
detail: Some("Compact threshold.".into()),
does: category(SettingsNamespace::Agent, "context"),
},
Row {
section: Some("Settings".into()),
label: "Display".into(),
badge: None,
detail: Some("Zen mode.".into()),
does: category(SettingsNamespace::Tui, "display"),
},
]
);
}
#[test]
fn setting_rows_show_values_applies_and_problems() {
let settings = settings();
let agent = SettingsNamespace::Agent;
assert_eq!(
rows(settings.setting_rows(agent, "agent")),
vec![
Row {
section: Some("Advisor".into()),
label: "Advisor mode".into(),
badge: Some("on".into()),
detail: Some("Advisor mode help. Applies from the next turn.".into()),
does: setting(agent, "behavior.advisor_mode"),
},
Row {
section: Some("Advisor".into()),
label: "Advisor model".into(),
badge: Some("GPT-5.5".into()),
detail: Some("Advisor model help. Applies from the next turn.".into()),
does: setting(agent, "internal_agents.advisor"),
},
Row {
section: Some("Advisor".into()),
label: "Inline shell".into(),
badge: Some("Bash".into()),
detail: Some("Inline shell help.".into()),
does: setting(agent, "behavior.inline_shell"),
},
]
);
assert_eq!(
rows(settings.setting_rows(agent, "context")),
vec![Row {
section: None,
label: "Compact threshold".into(),
badge: Some("80%".into()),
detail: Some(
"Compact threshold help. Applies from the next turn.\n\nThe file's value is not used: must be at most 100, not 140"
.into()
),
does: setting(agent, "compaction.compact_threshold_percent"),
}]
);
}
#[test]
fn value_rows_offer_each_value_and_a_reset_once_set() {
let settings = settings();
let agent = SettingsNamespace::Agent;
let set = |key: &str, value| {
Some(ConfigRow::Set {
namespace: agent,
key: SettingKey::new(key),
value,
})
};
let model_key = SettingKey::new("internal_agents.advisor");
assert_eq!(
rows(settings.value_rows(agent, &model_key).unwrap()),
vec![
Row {
section: None,
label: "GPT-5.5".into(),
badge: Some("current".into()),
detail: Some("openai/gpt-5.5".into()),
does: set(
"internal_agents.advisor",
SettingValue::Model(ModelSelection::Rho {
provider: "openai".into(),
model: "gpt-5.5".into(),
}),
),
},
Row {
section: None,
label: "claude-code/default".into(),
badge: None,
detail: Some("Set no model and use whichever model Claude Code selects.".into()),
does: set(
"internal_agents.advisor",
SettingValue::Model(ModelSelection::ClaudeCli { model: None }),
),
},
Row {
section: None,
label: "Default (not set)".into(),
badge: None,
detail: Some("Remove the setting from the file, so its default applies.".into()),
does: Some(ConfigRow::Reset {
namespace: agent,
key: model_key.clone(),
}),
},
]
);
let shell = SettingKey::new("behavior.inline_shell");
assert_eq!(
rows(settings.value_rows(agent, &shell).unwrap())
.into_iter()
.map(|row| (row.label, row.badge))
.collect::<Vec<_>>(),
vec![
("Bash".to_owned(), Some("current".to_owned())),
("Zsh".to_owned(), None),
]
);
let mode = SettingKey::new("behavior.advisor_mode");
assert!(settings.value_rows(agent, &mode).is_none());
}
#[test]
fn changes_merge_and_report_new_problems_once() {
let mut settings = settings();
let problem = ConfigError {
key: Some(SettingKey::new("zen_mode")),
message: "expected a boolean".into(),
};
let changed = |revision: &str, value: bool, errors: Vec<ConfigError>| ConfigChangedParams {
namespace: SettingsNamespace::Tui.wire(),
revision: ConfigRevision::new(revision),
values: BTreeMap::from([(
SettingKey::new("zen_mode"),
entry(Some(SettingValue::Bool(value)), ValueSource::Global),
)]),
errors,
};
let first = settings.apply(changed("t2", true, vec![problem.clone()]));
let second = settings.apply(changed("t3", false, vec![problem.clone()]));
let fixed = settings.apply(changed("t4", true, Vec::new()));
let other = settings.apply(ConfigChangedParams {
namespace: ConfigNamespace::Client {
name: rho_protocol::ClientName::new("other").unwrap(),
},
..changed("o1", false, Vec::new())
});
assert_eq!(
(first, second, fixed, other),
(
Some(Applied {
namespace: SettingsNamespace::Tui,
changed: BTreeMap::from([(
SettingKey::new("zen_mode"),
entry(Some(SettingValue::Bool(true)), ValueSource::Global)
)]),
new_problems: vec![problem],
}),
Some(Applied {
namespace: SettingsNamespace::Tui,
changed: BTreeMap::from([(
SettingKey::new("zen_mode"),
entry(Some(SettingValue::Bool(false)), ValueSource::Global)
)]),
new_problems: Vec::new(),
}),
Some(Applied {
namespace: SettingsNamespace::Tui,
changed: BTreeMap::from([(
SettingKey::new("zen_mode"),
entry(Some(SettingValue::Bool(true)), ValueSource::Global)
)]),
new_problems: Vec::new(),
}),
None,
)
);
assert_eq!(
settings.values(SettingsNamespace::Tui),
&values(
"t4",
vec![
(
"zen_mode",
entry(Some(SettingValue::Bool(true)), ValueSource::Global)
),
(
"output_streaming",
entry(
Some(SettingValue::Text("live".into())),
ValueSource::Default
)
),
],
Vec::new(),
)
);
}