Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/app_roots.rs

3.3 KiB, 9 runs

created by r1870400018:76082, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Steel's three roots (SRV-LAYOUT U3, D-20261030-22): with no `state_root` or `secret_root`
2//! a config resolves exactly as before, and with them Steel writes under the state root and
3//! reads its wallet from the secret root.
4//!
5//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
6//! Anthropic Claude
7
8use oxedyne_fe2o3_core::prelude::*;
9use oxedyne_fe2o3_jdat::{
10 prelude::*,
11 cfg::Config,
12};
13use oxedyne_fe2o3_steel::{
14 app::cfg::AppConfig,
15 srv::cfg::ServerConfig,
16};
17
18use std::path::{
19 Path,
20 PathBuf,
21};
22
23/// A fresh directory under the test target's own scratch area.
24fn scratch(name: &str) -> Outcome<PathBuf> {
25 let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("app_roots").join(name);
26 if dir.exists() {
27 res!(std::fs::remove_dir_all(&dir));
28 }
29 res!(std::fs::create_dir_all(&dir));
30 Ok(dir)
31}
32
33#[test]
34fn a_config_without_the_new_keys_resolves_as_before() -> Outcome<()> {
35 let app = res!(scratch("before"));
36 let mut map = try_extract_dat!(AppConfig::to_datmap(res!(AppConfig::new())), Map);
37 map.remove(&dat!("state_root"));
38 map.remove(&dat!("secret_root"));
39 map.insert(dat!("app_root"), dat!(app.to_string_lossy().to_string()));
40 let mut cfg = res!(AppConfig::from_datmap(map));
41 res!(cfg.check_and_fix());
42
43 let roots = res!(cfg.roots());
44 assert_eq!(roots.app_root(), app.as_path());
45 assert_eq!(roots.state_root(), app.as_path());
46 assert_eq!(roots.secret_root(), app.as_path());
47 let cfg_dir = Path::new("/somewhere/else");
48 assert_eq!(res!(cfg.wallet_dir(cfg_dir)), cfg_dir);
49
50 let server_cfg = ServerConfig::default();
51 assert_eq!(res!(roots.state(&server_cfg.log_dir())), app.join("www").join("logs"));
52 Ok(())
53}
54
55#[test]
56fn named_roots_place_state_and_the_wallet_apart() -> Outcome<()> {
57 let base = res!(scratch("apart"));
58 let (app, state, secret) = (base.join("srv"), base.join("var"), base.join("etc"));
59 res!(std::fs::create_dir_all(&secret));
60 let mut cfg = res!(AppConfig::new());
61 cfg.app_root = app.to_string_lossy().to_string();
62 cfg.state_root = state.to_string_lossy().to_string();
63 cfg.secret_root = secret.to_string_lossy().to_string();
64 res!(cfg.check_and_fix());
65
66 assert!(state.is_dir(), "check_and_fix creates the state root");
67 let roots = res!(cfg.roots());
68 assert_eq!(roots.state_root(), state.as_path());
69 assert_eq!(roots.secret_root(), secret.as_path());
70 assert_eq!(res!(cfg.wallet_dir(&app)), secret);
71
72 let mut server_cfg = ServerConfig::default();
73 assert_eq!(res!(roots.state(&server_cfg.log_dir())), state.join("www").join("logs"));
74 server_cfg.log_dir_rel = fmt!("./www-logs");
75 assert_eq!(res!(roots.state(&server_cfg.log_dir())), state.join("www-logs"));
76 // A config written with the key empty, as Steel once wrote it, takes the default.
77 server_cfg.log_dir_rel = String::new();
78 assert_eq!(res!(roots.state(&server_cfg.log_dir())), state.join("www").join("logs"));
79 Ok(())
80}
81
82#[test]
83fn a_relative_state_root_is_refused() -> Outcome<()> {
84 let app = res!(scratch("relative"));
85 let mut cfg = res!(AppConfig::new());
86 cfg.app_root = app.to_string_lossy().to_string();
87 cfg.state_root = fmt!("var/lib/steel");
88 match cfg.check_and_fix() {
89 Ok(()) => Err(err!("A relative state root was accepted."; Test, Unexpected)),
90 Err(_) => Ok(()),
91 }
92}