use std::path::PathBuf;
use std::time::Instant;
use shep_core::protocol::{DogSource, ProcessInfo};
use shep_core::status::ProcStatus;
use crate::commands::settings::{DogView, ScalarView, SettingField, SettingsSnapshot};
use crate::lookout::app::{App, Control, KeyPress, Msg, SettingsRow};
use crate::style::StyleSource;
use super::flock::{app_with, flock_of};
use super::palette::plain;
pub fn settings_snapshot() -> SettingsSnapshot {
let config = |value: &str| ScalarView {
value: value.to_string(),
source: StyleSource::Config,
};
SettingsSnapshot {
log_level: config("warn"),
log_json: config("false"),
socket: config("/home/ada/.shep/run/shep.sock"),
max_cron_sleep: config("30s"),
allow_control: config("false"),
style_level: config("full"),
style_level_in_file: Some("full".to_string()),
dogs: vec![
DogView {
name: "bark".to_string(),
enabled: false,
adopted_path: None,
},
DogView {
name: "metrics".to_string(),
enabled: true,
adopted_path: None,
},
],
}
}
pub fn app_in_settings() -> App {
let mut app = app_with(flock_of(3, 0), plain());
app.update(Msg::Key(KeyPress::Settings));
app.update(Msg::Settings {
result: Ok(settings_snapshot()),
});
app
}
pub fn app_in_settings_on(field: SettingField) -> App {
let mut app = app_in_settings_with_control();
let target = app
.settings()
.unwrap()
.rows()
.iter()
.position(|row| *row == SettingsRow::Scalar(field))
.expect("field is one of the six scalar rows Settings::rows always carries");
for _ in 0..target {
app.update(Msg::Key(KeyPress::SelectDown));
}
app
}
pub fn app_in_settings_at() -> (App, Instant) {
let t0 = Instant::now();
let mut app = App::new(plain(), Control::Allowed, "/home/ada/.shep".to_string(), t0);
app.update(Msg::Key(KeyPress::Settings));
app.update(Msg::Settings {
result: Ok(settings_snapshot()),
});
(app, t0)
}
pub fn app_in_settings_with_shadowed_style(source: StyleSource) -> App {
let mut app = app_in_settings_on(SettingField::StyleLevel);
let mut snapshot = settings_snapshot();
snapshot.style_level = ScalarView {
value: "bare".to_string(),
source,
};
snapshot.style_level_in_file = Some("full".to_string());
app.update(Msg::Settings {
result: Ok(snapshot),
});
app
}
pub fn app_in_settings_with_control() -> App {
let mut app = app_in_settings();
app.set_control_for_tests(Control::Allowed);
app
}
fn settings_snapshot_with_dogs(dogs: Vec<DogView>) -> SettingsSnapshot {
SettingsSnapshot {
dogs,
..settings_snapshot()
}
}
pub fn app_in_settings_with_dog_drift() -> App {
let flock = vec![
ProcessInfo::builder(90, "otel", ProcStatus::Online)
.pid(Some(90_000))
.dog(Some(DogSource::BuiltIn { grants: None }))
.build(),
];
let mut app = app_with(flock, plain());
app.update(Msg::Key(KeyPress::Settings));
app.update(Msg::Settings {
result: Ok(settings_snapshot_with_dogs(vec![
DogView {
name: "otel".to_string(),
enabled: false,
adopted_path: Some(PathBuf::from("/usr/local/bin/shep-otel")),
},
DogView {
name: "ledger".to_string(),
enabled: true,
adopted_path: Some(PathBuf::from("/opt/ledger/bin/dog")),
},
])),
});
app
}
pub fn app_in_settings_with_silent_dog() -> App {
let flock = vec![
ProcessInfo::builder(91, "bark", ProcStatus::Online)
.pid(Some(91_000))
.dog(Some(DogSource::BuiltIn { grants: None }))
.handshook(Some(false))
.build(),
];
let mut app = app_with(flock, plain());
app.update(Msg::Key(KeyPress::Settings));
app.update(Msg::Settings {
result: Ok(settings_snapshot_with_dogs(vec![DogView {
name: "bark".to_string(),
enabled: true,
adopted_path: None,
}])),
});
app
}
fn settings_snapshot_for_toggle_tests() -> SettingsSnapshot {
settings_snapshot_with_dogs(vec![
DogView {
name: "metrics".to_string(),
enabled: false,
adopted_path: None,
},
DogView {
name: "otel".to_string(),
enabled: true,
adopted_path: Some(PathBuf::from("/usr/local/bin/shep-otel")),
},
])
}
pub fn app_in_settings_on_dog(name: &str) -> App {
let mut app = app_with(flock_of(3, 0), plain());
app.set_control_for_tests(Control::Allowed);
app.update(Msg::Key(KeyPress::Settings));
app.update(Msg::Settings {
result: Ok(settings_snapshot_for_toggle_tests()),
});
let dog_index = settings_snapshot_for_toggle_tests()
.dogs
.iter()
.position(|dog| dog.name == name)
.expect("name is one of settings_snapshot_for_toggle_tests's own dogs");
for _ in 0..(6 + dog_index) {
app.update(Msg::Key(KeyPress::SelectDown));
}
app
}
pub fn app_in_settings_on_enabled_dog(name: &str) -> App {
app_in_settings_on_dog(name)
}