use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use sapphire_framework_settings::{
Apply, FieldError, Kind, Process, Registry, Scope, SettingDef, SettingsOwner, Staged,
StartState, Value,
};
use sapphire_workspace::AppContext;
use tokio::sync::watch;
use crate::config::{MCP_MOVED, SERVER_CONFIG_FILE};
use crate::error::{Error, Result};
use crate::selection::SelectionFile;
const OLD_CONFIG_FILE: &str = "server.toml";
const OLD_CONFIG_BACKUP: &str = "server.toml.v1";
const NO_WORKSPACE: &str = "no workspace is selected";
const FRAMEWORK_NAMES: [&str; 5] = ["http", "update", "sync", "retrieve", "workspace"];
const CHECKED_NAMES: [&str; 2] = ["http", "update"];
pub(crate) const BRIDGE_NAMES: [&str; 3] = ["net", "embed", "gui"];
pub trait AppSettings: Send + Sync + 'static {
fn defs(&self) -> Vec<SettingDef>;
fn validate(
&self,
_files: &AppFiles,
_staged: &Staged,
) -> std::result::Result<(), Vec<FieldError>> {
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AppFiles {
device: PathBuf,
workspace: Option<PathBuf>,
}
impl AppFiles {
pub fn device(&self) -> &Path {
&self.device
}
pub fn workspace(&self) -> Option<&Path> {
self.workspace.as_deref()
}
}
impl AppSettings for () {
fn defs(&self) -> Vec<SettingDef> {
Vec::new()
}
}
#[derive(Clone, Debug)]
pub(crate) struct SettingsFiles {
config_dir: PathBuf,
app_name: &'static str,
selection: SelectionFile,
}
impl SettingsFiles {
#[cfg(test)]
pub(crate) fn for_app(ctx: &AppContext) -> Result<SettingsFiles> {
SettingsFiles::with_selection(ctx, SelectionFile::for_app(ctx))
}
pub(crate) fn with_selection(
ctx: &AppContext,
selection: SelectionFile,
) -> Result<SettingsFiles> {
let config_dir = ctx.try_config_dir().ok_or_else(|| {
Error::Config(format!(
"{} has no config directory, so no settings",
ctx.app_name
))
})?;
Ok(SettingsFiles {
config_dir: config_dir.to_path_buf(),
app_name: ctx.app_name,
selection,
})
}
pub(crate) fn device(&self) -> PathBuf {
self.config_dir.join(SERVER_CONFIG_FILE)
}
pub(crate) fn workspace(&self) -> Result<PathBuf> {
match self.selection.load(self.app_name)? {
Some(selection) => Ok(selection
.root
.join(format!(".{}", self.app_name))
.join("config.toml")),
None => Err(Error::Config(NO_WORKSPACE.to_owned())),
}
}
fn app_files(&self) -> AppFiles {
AppFiles {
device: self.device(),
workspace: self.workspace().ok(),
}
}
pub(crate) fn secret(&self, key: &str) -> PathBuf {
self.config_dir.join("secrets").join(key)
}
fn scope_file(&self, scope: Scope) -> sapphire_framework_settings::Result<PathBuf> {
match scope {
Scope::Device => Ok(self.device()),
Scope::Workspace => self
.workspace()
.map_err(|e| std::io::Error::other(e.to_string()).into()),
Scope::Workgroup => Err(std::io::Error::other(format!(
"the {} server has no workgroup scope",
self.app_name
))
.into()),
}
}
}
fn framework_defs() -> Vec<SettingDef> {
let def = |key: &str, kind: Kind, default: Option<Value>, doc: &str| SettingDef {
key: key.into(),
kind,
scopes: vec![Scope::Device],
overridable: false,
apply: Apply::Restart,
default,
section: "Server".into(),
doc: doc.into(),
process: Process::App,
};
vec![
def(
"http.listen",
Kind::SocketAddr,
None,
"Where the HTTP listener binds; unset = the application's default address",
),
def(
"http.allowed_hosts",
Kind::List {
item: Box::new(Kind::String { non_empty: true }),
},
None,
"The Host names clients reach the server by, beyond loopback",
),
def(
"http.mcp.enabled",
Kind::Bool,
Some(Value::Bool(false)),
"Serve the application's MCP tools at /mcp, behind an external device's token",
),
def(
"http.metrics.enabled",
Kind::Bool,
Some(Value::Bool(false)),
"Serve Prometheus metrics at /metrics, without a token",
),
def(
"update.check",
Kind::Bool,
Some(Value::Bool(true)),
"Look for newer releases of the application, at start and once a day",
),
]
}
struct FrameworkOwner {
files: SettingsFiles,
}
impl SettingsOwner for FrameworkOwner {
fn defs(&self) -> Vec<SettingDef> {
framework_defs()
}
fn file(&self, scope: Scope) -> sapphire_framework_settings::Result<PathBuf> {
self.files.scope_file(scope)
}
fn secret_file(&self, key: &str) -> sapphire_framework_settings::Result<PathBuf> {
Ok(self.files.secret(key))
}
fn validate(&self, staged: &Staged) -> std::result::Result<(), Vec<FieldError>> {
check_device_file(&self.files, staged)
}
}
struct AppOwner {
files: SettingsFiles,
app: Arc<dyn AppSettings>,
}
impl SettingsOwner for AppOwner {
fn defs(&self) -> Vec<SettingDef> {
self.app
.defs()
.into_iter()
.map(|def| SettingDef {
process: Process::App,
..def
})
.collect()
}
fn file(&self, scope: Scope) -> sapphire_framework_settings::Result<PathBuf> {
self.files.scope_file(scope)
}
fn secret_file(&self, key: &str) -> sapphire_framework_settings::Result<PathBuf> {
Ok(self.files.secret(key))
}
fn validate(&self, staged: &Staged) -> std::result::Result<(), Vec<FieldError>> {
self.app.validate(&self.files.app_files(), staged)
}
}
pub(crate) fn registry(
ctx: &AppContext,
files: &SettingsFiles,
app: Arc<dyn AppSettings>,
) -> sapphire_framework_settings::Result<Registry> {
let prefix = format!("{}.", ctx.app_name);
for def in app.defs() {
let invalid = |message: String| sapphire_framework_settings::Error::InvalidDef {
key: def.key.clone(),
message,
};
if !def.key.starts_with(&prefix) {
return Err(invalid(format!(
"an application's key starts with `{prefix}`"
)));
}
if def.scopes.contains(&Scope::Workgroup) {
return Err(invalid(
"an application's key lives on the device or in the workspace, not in the workgroup"
.to_owned(),
));
}
}
const NO_OWNER: usize = usize::MAX;
let mut reserved: Vec<(&str, usize)> = FRAMEWORK_NAMES.iter().map(|n| (*n, 0)).collect();
reserved.push((ctx.app_name, 1));
reserved.extend(BRIDGE_NAMES.iter().map(|n| (*n, NO_OWNER)));
Registry::new(
vec![
Arc::new(FrameworkOwner {
files: files.clone(),
}),
Arc::new(AppOwner {
files: files.clone(),
app,
}),
],
&reserved,
)
}
fn check_device_file(
files: &SettingsFiles,
staged: &Staged,
) -> std::result::Result<(), Vec<FieldError>> {
let path = files.device();
let failure = |key: Option<String>, message: String| FieldError {
key,
file: Some(path.clone()),
message,
};
let text = match staged.read(&path) {
Ok(Some(text)) => text,
Ok(None) => return Ok(()),
Err(e) => return Err(vec![failure(None, e.to_string())]),
};
let table = match text.parse::<toml::Table>() {
Ok(table) => table,
Err(e) => {
return Err(vec![failure(
None,
sapphire_framework_settings::describe_toml_error(&text, e.span(), e.message()),
)]);
}
};
let defs = framework_defs();
let mut errors = Vec::new();
for (name, item) in &table {
if name == "mcp" {
errors.push(failure(None, MCP_MOVED.to_owned()));
} else if CHECKED_NAMES.contains(&name.as_str()) {
let mut leaves = Vec::new();
leaves_of(name.clone(), item, &mut leaves);
for (key, value) in leaves {
let in_file = format!("in {}", path.display());
match defs.iter().find(|d| d.key == key) {
None => errors.push(failure(Some(key), format!("unknown setting {in_file}"))),
Some(def) => {
if !accepts(&def.kind, value) {
errors.push(failure(
Some(key),
format!("the value {in_file} is not a valid one"),
));
}
}
}
}
}
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
fn leaves_of<'a>(path: String, item: &'a toml::Value, out: &mut Vec<(String, &'a toml::Value)>) {
match item {
toml::Value::Table(table) => {
for (name, child) in table {
leaves_of(format!("{path}.{name}"), child, out);
}
}
other => out.push((path, other)),
}
}
fn accepts(kind: &Kind, value: &toml::Value) -> bool {
to_value(value).is_some_and(|v| kind.check(&v).is_ok())
}
fn to_value(value: &toml::Value) -> Option<Value> {
match value {
toml::Value::String(s) => Some(Value::String(s.clone())),
toml::Value::Integer(i) => Some(Value::Int(*i)),
toml::Value::Boolean(b) => Some(Value::Bool(*b)),
toml::Value::Array(items) => items
.iter()
.map(to_value)
.collect::<Option<_>>()
.map(Value::List),
_ => None,
}
}
pub(crate) fn migrate(config_dir: &Path) -> Result<()> {
let new = config_dir.join(SERVER_CONFIG_FILE);
let old = config_dir.join(OLD_CONFIG_FILE);
let text = match std::fs::read_to_string(&old) {
Ok(text) => text,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(e) => return Err(Error::Io(e)),
};
let merged = match std::fs::read_to_string(&new) {
Ok(existing) => {
let mut doc = parse_doc(&new, &existing)?;
let from = parse_doc(&old, &text)?;
let mut kept = Vec::new();
merge_tables(doc.as_table_mut(), from.as_table(), "", &mut kept);
for key in kept {
tracing::warn!(
"{key} is in both {} and {}; the value in {} is kept",
old.display(),
new.display(),
new.display()
);
}
doc.to_string()
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => text,
Err(e) => return Err(Error::Io(e)),
};
sapphire_framework_settings::write_atomic(&new, &merged)?;
let backup = config_dir.join(OLD_CONFIG_BACKUP);
match std::fs::rename(&old, &backup) {
Ok(()) => tracing::info!(
"{} moved to {}; the old file is kept as {OLD_CONFIG_BACKUP}",
old.display(),
new.display()
),
Err(e) => tracing::warn!(
"{} moved to {}, but it could not be renamed to {OLD_CONFIG_BACKUP}: {e}",
old.display(),
new.display()
),
}
Ok(())
}
fn parse_doc(path: &Path, text: &str) -> Result<toml_edit::DocumentMut> {
text.parse::<toml_edit::DocumentMut>().map_err(|e| {
sapphire_framework_settings::Error::Unparsable {
file: path.to_path_buf(),
message: sapphire_framework_settings::describe_toml_error(text, e.span(), e.message()),
}
.into()
})
}
fn merge_tables(
into: &mut dyn toml_edit::TableLike,
from: &dyn toml_edit::TableLike,
prefix: &str,
kept: &mut Vec<String>,
) {
for (name, item) in from.iter() {
let key = if prefix.is_empty() {
name.to_owned()
} else {
format!("{prefix}.{name}")
};
match into.get_mut(name) {
None => {
let mut item = item.clone();
unplace(&mut item);
into.insert(name, item);
}
Some(existing) => match (existing.as_table_like_mut(), item.as_table_like()) {
(Some(existing), Some(item)) => merge_tables(existing, item, &key, kept),
_ => kept.push(key),
},
}
}
}
fn unplace(item: &mut toml_edit::Item) {
fn table(table: &mut toml_edit::Table) {
table.set_position(None);
for (_, child) in table.iter_mut() {
unplace(child);
}
}
match item {
toml_edit::Item::Table(t) => table(t),
toml_edit::Item::ArrayOfTables(array) => array.iter_mut().for_each(table),
_ => {}
}
}
pub trait FromValue: Sized {
fn from_value(value: Value) -> Option<Self>;
}
impl FromValue for bool {
fn from_value(value: Value) -> Option<Self> {
match value {
Value::Bool(b) => Some(b),
_ => None,
}
}
}
impl FromValue for i64 {
fn from_value(value: Value) -> Option<Self> {
match value {
Value::Int(i) => Some(i),
_ => None,
}
}
}
impl FromValue for String {
fn from_value(value: Value) -> Option<Self> {
match value {
Value::String(s) => Some(s),
_ => None,
}
}
}
impl FromValue for Vec<String> {
fn from_value(value: Value) -> Option<Self> {
match value {
Value::List(items) => items.into_iter().map(String::from_value).collect(),
_ => None,
}
}
}
#[derive(Clone)]
pub struct Settings {
inner: Arc<Inner>,
}
struct Inner {
registry: Registry,
start: StartState,
values: RwLock<BTreeMap<String, Value>>,
changed: watch::Sender<u64>,
}
impl std::fmt::Debug for Settings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Settings").finish_non_exhaustive()
}
}
impl Settings {
pub(crate) fn load(ctx: &AppContext, app: Arc<dyn AppSettings>) -> Result<Settings> {
Settings::load_with(ctx, app, SelectionFile::for_app(ctx))
}
pub(crate) fn load_with(
ctx: &AppContext,
app: Arc<dyn AppSettings>,
selection: SelectionFile,
) -> Result<Settings> {
let files = SettingsFiles::with_selection(ctx, selection)?;
migrate(&files.config_dir)?;
let registry = registry(ctx, &files, Arc::clone(&app))?;
startup_check(&files, &*app)?;
let values = read(®istry)?;
let start = StartState::capture(®istry)?;
Ok(Settings {
inner: Arc::new(Inner {
registry,
start,
values: RwLock::new(values),
changed: watch::channel(0).0,
}),
})
}
pub(crate) fn defaults(ctx: &AppContext, app: Arc<dyn AppSettings>) -> Result<Settings> {
let config_dir = ctx
.try_config_dir()
.map(Path::to_path_buf)
.unwrap_or_default();
let files = SettingsFiles {
selection: SelectionFile::at(config_dir.join(crate::selection::SELECTION_FILE)),
config_dir,
app_name: ctx.app_name,
};
let registry = registry(ctx, &files, app)?;
let values = registry
.schema()
.into_iter()
.filter(|def| def.kind != Kind::Secret)
.filter_map(|def| def.default.map(|value| (def.key, value)))
.collect();
Ok(Settings {
inner: Arc::new(Inner {
registry,
start: StartState::default(),
values: RwLock::new(values),
changed: watch::channel(0).0,
}),
})
}
pub(crate) fn device_file(ctx: &AppContext) -> Option<PathBuf> {
ctx.try_config_dir().map(|dir| dir.join(SERVER_CONFIG_FILE))
}
pub fn get(&self, key: &str) -> Option<Value> {
self.inner
.values
.read()
.unwrap_or_else(|e| e.into_inner())
.get(key)
.cloned()
}
pub fn get_as<T: FromValue>(&self, key: &str) -> Option<T> {
self.get(key).and_then(T::from_value)
}
pub fn changed(&self) -> watch::Receiver<u64> {
self.inner.changed.subscribe()
}
pub(crate) fn refresh(&self) -> Result<()> {
let new = read(&self.inner.registry)?;
let mut values = self.inner.values.write().unwrap_or_else(|e| e.into_inner());
let mut any = false;
for def in self.inner.registry.schema() {
if def.apply != Apply::Live || values.get(&def.key) == new.get(&def.key) {
continue;
}
any = true;
match new.get(&def.key) {
Some(value) => values.insert(def.key, value.clone()),
None => values.remove(&def.key),
};
}
drop(values);
if any {
self.inner.changed.send_modify(|n| *n = n.wrapping_add(1));
}
Ok(())
}
pub(crate) fn registry(&self) -> &Registry {
&self.inner.registry
}
pub(crate) fn start(&self) -> &StartState {
&self.inner.start
}
}
fn startup_check(files: &SettingsFiles, app: &dyn AppSettings) -> Result<()> {
let mut errors = Vec::new();
for result in [
check_device_file(files, &Staged::default()),
app.validate(&files.app_files(), &Staged::default()),
] {
if let Err(e) = result {
errors.extend(e);
}
}
if errors.is_empty() {
Ok(())
} else {
Err(sapphire_framework_settings::Error::Rejected(errors).into())
}
}
pub(crate) fn open_unchecked(
ctx: &AppContext,
app: Arc<dyn AppSettings>,
) -> Result<(Registry, Result<()>)> {
let files = SettingsFiles::with_selection(ctx, SelectionFile::for_app(ctx))?;
migrate(&files.config_dir)?;
let registry = registry(ctx, &files, Arc::clone(&app))?;
let checked = startup_check(&files, &*app);
Ok((registry, checked))
}
fn read(registry: &Registry) -> Result<BTreeMap<String, Value>> {
Ok(sapphire_framework_settings::list(registry, None, None)?
.into_iter()
.filter_map(|entry| match entry.value {
sapphire_framework_settings::EntryValue::Value(value) => Some((entry.key, value)),
_ => None,
})
.collect())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::ServerConfig;
use crate::selection::Selection;
use sapphire_framework_settings::{Op, apply};
use sapphire_workspace::AppContext;
use std::path::Path;
use std::sync::Arc;
const APP: &str = "settingstest";
fn context(tmp: &Path) -> AppContext {
let ctx = AppContext::new(APP);
let dir = tmp.join("config");
std::fs::create_dir_all(&dir).unwrap();
ctx.set_config_dir(dir);
ctx
}
struct TestApp(Vec<SettingDef>);
impl AppSettings for TestApp {
fn defs(&self) -> Vec<SettingDef> {
self.0.clone()
}
}
fn app_def(key: &str, kind: Kind, scopes: Vec<Scope>, apply: Apply) -> SettingDef {
SettingDef {
key: key.into(),
kind,
scopes,
overridable: false,
apply,
default: None,
section: "Test".into(),
doc: "test".into(),
process: Process::Bridge,
}
}
fn one(def: SettingDef) -> Arc<dyn AppSettings> {
Arc::new(TestApp(vec![def]))
}
fn set(key: &str, value: Value) -> Op {
Op::Set {
key: key.into(),
value,
}
}
fn select(ctx: &AppContext, root: &Path) {
std::fs::create_dir_all(root).unwrap();
SelectionFile::for_app(ctx)
.save(&Selection {
root: root.to_path_buf(),
sync: false,
})
.unwrap();
}
#[test]
fn server_toml_becomes_config_toml() {
let tmp = tempfile::tempdir().unwrap();
let text = "# mine\n[update]\ncheck = false\n";
std::fs::write(tmp.path().join("server.toml"), text).unwrap();
migrate(tmp.path()).unwrap();
assert_eq!(
std::fs::read_to_string(tmp.path().join("config.toml")).unwrap(),
text
);
assert_eq!(
std::fs::read_to_string(tmp.path().join("server.toml.v1")).unwrap(),
text
);
assert!(!tmp.path().join("server.toml").exists());
migrate(tmp.path()).unwrap();
assert!(!tmp.path().join("server.toml").exists());
}
fn logged(f: impl FnOnce()) -> String {
use std::sync::Mutex;
#[derive(Clone, Default)]
struct Captured(Arc<Mutex<Vec<u8>>>);
impl std::io::Write for Captured {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.lock().unwrap().extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let captured = Captured::default();
let writer = captured.clone();
let subscriber = tracing_subscriber::fmt()
.with_writer(move || writer.clone())
.with_ansi(false)
.finish();
tracing::subscriber::with_default(subscriber, f);
String::from_utf8(captured.0.lock().unwrap().clone()).unwrap()
}
#[test]
fn server_toml_merges_into_an_existing_config_toml() {
let tmp = tempfile::tempdir().unwrap();
let old = "# old\n[update]\ncheck = false\n\n[http.mcp]\n# mine\nenabled = true\n";
std::fs::write(tmp.path().join("server.toml"), old).unwrap();
let app =
"# the app's own\nsync_interval_minutes = 5\n\n[anthropic]\napi_key = \"x\" # keep\n";
std::fs::write(tmp.path().join("config.toml"), app).unwrap();
let log = logged(|| migrate(tmp.path()).unwrap());
let merged = std::fs::read_to_string(tmp.path().join("config.toml")).unwrap();
assert!(merged.starts_with(app), "{merged}");
let table: toml::Table = merged.parse().unwrap();
assert_eq!(table["update"]["check"].as_bool(), Some(false));
assert_eq!(table["http"]["mcp"]["enabled"].as_bool(), Some(true));
assert_eq!(table["anthropic"]["api_key"].as_str(), Some("x"));
assert!(merged.contains("# mine"), "{merged}");
assert_eq!(
std::fs::read_to_string(tmp.path().join("server.toml.v1")).unwrap(),
old
);
assert!(!tmp.path().join("server.toml").exists());
assert!(!log.contains("WARN"), "{log}");
migrate(tmp.path()).unwrap();
assert_eq!(
std::fs::read_to_string(tmp.path().join("config.toml")).unwrap(),
merged
);
}
#[test]
fn a_key_config_toml_already_has_wins_with_a_warning() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("server.toml"),
"[update]\ncheck = false\n[http]\nlisten = \"10.9.8.7:1\"\n",
)
.unwrap();
std::fs::write(tmp.path().join("config.toml"), "[update]\ncheck = true\n").unwrap();
let log = logged(|| migrate(tmp.path()).unwrap());
let table: toml::Table = std::fs::read_to_string(tmp.path().join("config.toml"))
.unwrap()
.parse()
.unwrap();
assert_eq!(table["update"]["check"].as_bool(), Some(true));
assert_eq!(table["http"]["listen"].as_str(), Some("10.9.8.7:1"));
assert!(
log.contains("WARN") && log.contains("update.check"),
"{log}"
);
assert!(!log.contains("false"), "{log}");
assert!(tmp.path().join("server.toml.v1").exists());
assert!(!tmp.path().join("server.toml").exists());
}
#[cfg(unix)]
#[test]
fn a_merge_keeps_config_tomls_permissions() {
use std::os::unix::fs::PermissionsExt;
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("server.toml"), "[update]\ncheck = false\n").unwrap();
let path = tmp.path().join("config.toml");
std::fs::write(&path, "[anthropic]\napi_key = \"x\"\n").unwrap();
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
migrate(tmp.path()).unwrap();
let mode = std::fs::metadata(&path).unwrap().permissions().mode();
assert_eq!(mode & 0o777, 0o600);
}
#[test]
fn the_apps_own_tables_are_left_alone() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
std::fs::write(
ctx.config_dir().join("config.toml"),
"sync_interval_minutes = 5\n\n[anthropic]\napi_key = \"x\"\n\n\
[workspace]\ndefault = \"a\"\n\n[sync]\nwhatever = true\n\n\
[retrieve]\nk = 1\n\n[cache]\ndir = \"/c\"\n",
)
.unwrap();
let settings = Settings::load(&ctx, Arc::new(())).unwrap();
assert_eq!(settings.get_as::<bool>("update.check"), Some(true));
}
#[test]
fn a_framework_write_keeps_the_apps_own_tables_byte_for_byte() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let path = ctx.config_dir().join("config.toml");
let before = "# sapphire-agent\nsync_interval_minutes = 5 # minutes\n\n\
[anthropic]\n# the key\napi_key = \"x\"\n\n\
[http]\n# where\nlisten = \"127.0.0.1:1\"\n\n\
[matrix]\nhomeserver = \"https://m\" # mine\n";
std::fs::write(&path, before).unwrap();
let settings = Settings::load(&ctx, Arc::new(())).unwrap();
apply(
settings.registry(),
None,
vec![set("http.listen", Value::String("127.0.0.1:2".into()))],
)
.unwrap();
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
before.replace("127.0.0.1:1", "127.0.0.1:2")
);
}
#[test]
fn an_unknown_key_under_http_or_update_is_still_an_error() {
for text in ["[http]\nlisn = 1\n", "[update]\nchekc = true\n"] {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
std::fs::write(ctx.config_dir().join("config.toml"), text).unwrap();
assert!(Settings::load(&ctx, Arc::new(())).is_err(), "{text}");
}
}
#[test]
fn a_missing_server_toml_migrates_nothing() {
let tmp = tempfile::tempdir().unwrap();
migrate(tmp.path()).unwrap();
assert!(!tmp.path().join("config.toml").exists());
}
#[test]
fn the_old_mcp_table_still_says_where_it_went() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
std::fs::write(ctx.config_dir().join("server.toml"), "[mcp]\nhttp = true\n").unwrap();
let err = Settings::load(&ctx, Arc::new(()))
.err()
.unwrap()
.to_string();
assert!(
err.contains("[mcp] http moved to [http.mcp] enabled"),
"{err}"
);
assert!(
err.contains(&ctx.config_dir().join("config.toml").display().to_string()),
"{err}"
);
}
#[test]
fn server_config_comes_from_the_settings() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
std::fs::write(
ctx.config_dir().join("config.toml"),
"[http]\nlisten = \"127.0.0.1:9000\"\nallowed_hosts = [\"box.example\"]\n\n\
[http.mcp]\nenabled = true\n\n[update]\ncheck = false\n",
)
.unwrap();
let settings = Settings::load(&ctx, Arc::new(())).unwrap();
let config = ServerConfig::from_settings(&settings).unwrap();
assert_eq!(config.http.listen, Some("127.0.0.1:9000".parse().unwrap()));
assert_eq!(config.http.allowed_hosts, vec!["box.example".to_owned()]);
assert!(config.http.mcp.enabled);
assert!(!config.http.metrics.enabled);
assert!(!config.update.check);
}
#[test]
fn a_misspelt_framework_setting_is_an_error_not_a_default() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
std::fs::write(
ctx.config_dir().join("config.toml"),
"[http.mcp]\nenabeld = true\n",
)
.unwrap();
let err = Settings::load(&ctx, Arc::new(()))
.err()
.unwrap()
.to_string();
assert!(err.contains("http.mcp.enabeld"), "{err}");
}
#[test]
fn a_framework_value_of_the_wrong_kind_is_an_error() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
std::fs::write(
ctx.config_dir().join("config.toml"),
"[http]\nlisten = \"nowhere\"\n",
)
.unwrap();
let err = Settings::load(&ctx, Arc::new(()))
.err()
.unwrap()
.to_string();
assert!(err.contains("http.listen"), "{err}");
}
#[test]
fn an_app_key_outside_its_name_is_refused() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let files = SettingsFiles::for_app(&ctx).unwrap();
let def = app_def("other.x", Kind::Bool, vec![Scope::Device], Apply::Live);
let err = registry(&ctx, &files, one(def)).err().unwrap();
assert!(
matches!(&err, sapphire_framework_settings::Error::InvalidDef { key, .. } if key == "other.x"),
"{err}"
);
}
#[test]
fn an_app_key_in_the_workgroup_is_refused() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let files = SettingsFiles::for_app(&ctx).unwrap();
for scopes in [
vec![Scope::Workgroup],
vec![Scope::Workspace, Scope::Workgroup],
] {
let def = app_def("settingstest.x", Kind::Bool, scopes, Apply::Live);
let err = registry(&ctx, &files, one(def)).err().unwrap();
assert!(
matches!(&err, sapphire_framework_settings::Error::InvalidDef { key, .. } if key == "settingstest.x"),
"{err}"
);
}
}
#[test]
fn an_app_key_named_http_is_refused() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let files = SettingsFiles::for_app(&ctx).unwrap();
let def = app_def("http.port", Kind::Bool, vec![Scope::Device], Apply::Live);
assert!(registry(&ctx, &files, one(def)).is_err());
}
#[test]
fn every_def_is_the_app_servers() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let files = SettingsFiles::for_app(&ctx).unwrap();
let def = app_def(
"settingstest.x",
Kind::Bool,
vec![Scope::Device],
Apply::Live,
);
let reg = registry(&ctx, &files, one(def)).unwrap();
let schema = reg.schema();
assert_eq!(
schema.iter().map(|d| d.key.as_str()).collect::<Vec<_>>(),
vec![
"http.listen",
"http.allowed_hosts",
"http.mcp.enabled",
"http.metrics.enabled",
"update.check",
"settingstest.x",
]
);
assert!(schema.iter().all(|d| d.process == Process::App));
}
#[test]
fn the_workspace_scope_follows_the_selection() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let def = app_def(
"settingstest.note",
Kind::String { non_empty: false },
vec![Scope::Workspace],
Apply::Live,
);
let settings = Settings::load(&ctx, one(def)).unwrap();
let (a, b) = (tmp.path().join("a"), tmp.path().join("b"));
let file = |root: &Path| root.join(format!(".{APP}")).join("config.toml");
select(&ctx, &a);
apply(
settings.registry(),
None,
vec![set("settingstest.note", Value::String("first".into()))],
)
.unwrap();
select(&ctx, &b);
apply(
settings.registry(),
None,
vec![set("settingstest.note", Value::String("second".into()))],
)
.unwrap();
let a_text = std::fs::read_to_string(file(&a)).unwrap();
let b_text = std::fs::read_to_string(file(&b)).unwrap();
assert!(
a_text.contains("first") && !a_text.contains("second"),
"{a_text}"
);
assert!(b_text.contains("second"), "{b_text}");
}
#[test]
fn no_selection_means_no_workspace_scope() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let files = SettingsFiles::for_app(&ctx).unwrap();
let err = files.workspace().unwrap_err().to_string();
assert_eq!(err, "no workspace is selected");
let def = app_def(
"settingstest.note",
Kind::String { non_empty: false },
vec![Scope::Workspace],
Apply::Live,
);
let settings = Settings::load(&ctx, one(def)).unwrap();
assert_eq!(settings.get("settingstest.note"), None);
let err = apply(
settings.registry(),
None,
vec![set("settingstest.note", Value::String("x".into()))],
)
.unwrap_err()
.to_string();
assert!(err.contains("no workspace is selected"), "{err}");
}
#[test]
fn a_live_change_fires_changed_and_a_restart_change_does_not() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let app = TestApp(vec![
app_def(
"settingstest.live",
Kind::Int { min: 0, max: 10 },
vec![Scope::Device],
Apply::Live,
),
app_def(
"settingstest.slow",
Kind::Int { min: 0, max: 10 },
vec![Scope::Device],
Apply::Restart,
),
]);
let settings = Settings::load(&ctx, Arc::new(app)).unwrap();
let mut changed = settings.changed();
apply(
settings.registry(),
None,
vec![set("settingstest.slow", Value::Int(3))],
)
.unwrap();
settings.refresh().unwrap();
assert!(!changed.has_changed().unwrap());
assert_eq!(
settings.get("settingstest.slow"),
None,
"kept until restart"
);
apply(
settings.registry(),
None,
vec![set("settingstest.live", Value::Int(4))],
)
.unwrap();
settings.refresh().unwrap();
assert!(changed.has_changed().unwrap());
changed.mark_unchanged();
assert_eq!(settings.get_as::<i64>("settingstest.live"), Some(4));
assert_eq!(
settings.clone().get("settingstest.live"),
Some(Value::Int(4))
);
settings.refresh().unwrap();
assert!(!changed.has_changed().unwrap());
}
struct Paired;
impl AppSettings for Paired {
fn defs(&self) -> Vec<SettingDef> {
["settingstest.a", "settingstest.b"]
.map(|key| app_def(key, Kind::Bool, vec![Scope::Device], Apply::Live))
.into()
}
fn validate(
&self,
files: &AppFiles,
staged: &Staged,
) -> std::result::Result<(), Vec<FieldError>> {
assert_eq!(files.workspace(), None, "no workspace is selected");
let text = staged.read(files.device()).unwrap().unwrap_or_default();
let table: toml::Table = text.parse().unwrap();
let has = |k: &str| table.get("settingstest").and_then(|t| t.get(k)).is_some();
if has("a") && !has("b") {
return Err(vec![FieldError {
key: Some("settingstest.a".into()),
file: Some(files.device().to_path_buf()),
message: "needs settingstest.b".into(),
}]);
}
Ok(())
}
}
#[test]
fn an_apps_validate_sees_the_files_whole() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let settings = Settings::load(&ctx, Arc::new(Paired)).unwrap();
let reg = settings.registry();
let err = apply(reg, None, vec![set("settingstest.a", Value::Bool(true))])
.unwrap_err()
.to_string();
assert!(err.contains("needs settingstest.b"), "{err}");
assert!(!ctx.config_dir().join("config.toml").exists());
apply(reg, None, vec![set("settingstest.b", Value::Bool(true))]).unwrap();
apply(reg, None, vec![set("settingstest.a", Value::Bool(true))]).unwrap();
let err = apply(
reg,
None,
vec![Op::Unset {
key: "settingstest.b".into(),
}],
)
.unwrap_err()
.to_string();
assert!(err.contains("needs settingstest.b"), "{err}");
}
#[test]
fn an_unparsable_config_names_the_file() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let path = ctx.config_dir().join("config.toml");
std::fs::write(&path, "this is = = not toml").unwrap();
let err = Settings::load(&ctx, Arc::new(()))
.err()
.unwrap()
.to_string();
assert!(err.contains(&path.display().to_string()), "{err}");
}
#[test]
fn an_unparsable_config_is_never_quoted() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
std::fs::write(
ctx.config_dir().join("config.toml"),
"[anthropic]\napi_key = \"hunter2\" x\n",
)
.unwrap();
let err = Settings::load(&ctx, Arc::new(()))
.err()
.unwrap()
.to_string();
assert!(err.contains("line 2"), "{err}");
assert!(!err.contains("hunter2"), "{err}");
std::fs::write(
ctx.config_dir().join("server.toml"),
"[update]\ncheck = false\n",
)
.unwrap();
let err = migrate(ctx.config_dir()).unwrap_err().to_string();
assert!(err.contains("line 2") && !err.contains("hunter2"), "{err}");
}
#[test]
fn a_secret_is_never_read_back_and_lives_under_secrets() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
let def = app_def(
"settingstest.token",
Kind::Secret,
vec![Scope::Device],
Apply::Live,
);
let settings = Settings::load(&ctx, one(def)).unwrap();
apply(
settings.registry(),
None,
vec![set("settingstest.token", Value::String("hunter2".into()))],
)
.unwrap();
let path = ctx.config_dir().join("secrets").join("settingstest.token");
assert_eq!(std::fs::read_to_string(&path).unwrap(), "hunter2");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = std::fs::metadata(&path).unwrap().permissions().mode();
assert_eq!(mode & 0o777, 0o600);
}
settings.refresh().unwrap();
assert_eq!(settings.get("settingstest.token"), None);
}
#[test]
fn values_convert_to_their_rust_types() {
let tmp = tempfile::tempdir().unwrap();
let ctx = context(tmp.path());
std::fs::write(
ctx.config_dir().join("config.toml"),
"[http]\nallowed_hosts = [\"a\", \"b\"]\n",
)
.unwrap();
let settings = Settings::load(&ctx, Arc::new(())).unwrap();
assert_eq!(settings.get_as::<bool>("update.check"), Some(true));
assert_eq!(
settings.get_as::<Vec<String>>("http.allowed_hosts"),
Some(vec!["a".to_owned(), "b".to_owned()])
);
assert_eq!(
settings.get_as::<String>("update.check"),
None,
"wrong type"
);
assert_eq!(settings.get_as::<i64>("http.listen"), None, "unset");
assert_eq!(settings.get("no.such.key"), None);
}
}